Commits

hideki nara committed 1dddd8e

post fixtues and delete them

Comments (0)

Files changed (5)

src/mandb/__init__.py

                         "Content-Type" : content_type,
                         "charset" : charset,
                         }
-    print params,endpoint
+#    print params,endpoint
     if post_message == None:
         return requests.get(endpoint,**params )
     else:

src/mandb/fixtures/shop/fixture.shop_product.1.json

+[
+  {
+    "pk": 1, 
+    "model": "shop.product", 
+    "fields": {
+      "price": 150, 
+      "name": "sugar"
+    }
+  }
+]

src/mandb/fixtures/shop/fixture.shop_product.2.json

+[
+  {
+    "pk": 2, 
+    "model": "shop.product", 
+    "fields": {
+      "price": 100, 
+      "name": "coffee"
+    }
+  }
+]

src/mandb/tests.py

     """
 
     def setUp(self):
+        import os
         self.HOST= "http://localhost:8800"             #:mandb/app test server
+        self.FIXTURES= os.path.join( os.path.dirname(os.path.abspath(__file__)),
+                                     'fixtures')
 
     def endpoint(self,path):
         return self.HOST+ path
         res=mandb.webclient(endpoint=self.endpoint('/mandb/model/shop/Product/json/') )
         self.assertEqual(res.headers['content-type'].split(';')[0],'application/json')
         self.assertEqual(len(map(lambda x: x ,serializers.deserialize('json',res.text))),0 )
+
+    def test_webclient_post(self):
+        """
+        python manage.py test mandb.BasicTest.test_webclient_post
+
+        """
+
+        files = [   
+            'fixture.shop_product.1.json',  
+            'fixture.shop_product.2.json',
+        ]
+         
+        import os
+        import mandb
+        from django.core import serializers
+        for f in files:
+            res=mandb.webclient(
+                        endpoint=self.endpoint('/mandb/model/fixture'),
+                        post_message = open(os.path.join(self.FIXTURES,'shop/%s'%f ) ).read(),
+            )
+
+        res=mandb.webclient(endpoint=self.endpoint('/mandb/model/shop/Product/json/') )
+        self.assertEqual(res.headers['content-type'].split(';')[0],'application/json')
+        self.assertEqual(len(map(lambda x: x ,serializers.deserialize('json',res.text))),2 )
+        #
+        res=mandb.webclient(endpoint=self.endpoint('/mandb/model/shop/Product/delete/all') )
+        res=mandb.webclient(endpoint=self.endpoint('/mandb/model/shop/Product/json/') )
+        self.assertEqual(res.headers['content-type'].split(';')[0],'application/json')
+        self.assertEqual(len(map(lambda x: x ,serializers.deserialize('json',res.text))),0 )
+        

src/mandb/views.py

                             context_instance=template.RequestContext(request),)
 @csrf_exempt
 def page_fixture(request):
-    ret = None
-    if request.method == POST:
-        map( lambda x: x.save(), serializers.deserialize('json',request._stream ) )
-    return ret if ret else render_to_response( 'mandb/default.html', {} ,
-                            context_instance=template.RequestContext(request),)
+    try:
+        ret = None
+        if request.method == 'POST':
+            map( lambda x: x.save(), serializers.deserialize('json',request._stream ) )
+        return ret if ret else render_to_response( 'mandb/default.html', {} ,
+                                context_instance=template.RequestContext(request),)
+    except Exception,e:
+        print e