def test_put_subresource(self): # create new resource and subresource s = SimpleDoc(a="Answer to everything", b=42).save() d = ComplexDoc(l=['a', 'b'], n=999, r=s).save() response = self.client.get('/simpledoc/%s/complexdoc/%s' % (s.id, d.id)) etag = response.get_json()[config.ETAG] headers = [('If-Match', etag)] # new putted document put_data = {'l': ['x', 'y', 'z'], 'r': str(s.id)} put_url = '/simpledoc/%s/complexdoc/%s' % (s.id, d.id) response = self.client.put(put_url, data=json.dumps(put_data), content_type='application/json', headers=headers) self.assertEqual(response.status_code, 200) resp_json = response.get_json() self.assertEqual(resp_json[config.STATUS], "OK") # check, if really edited response = self.client.get('/simpledoc/%s/complexdoc/%s' % (s.id, d.id)) json_data = response.get_json() self.assertListEqual(json_data['l'], ['x', 'y', 'z']) self.assertNotIn('n', json_data) s.delete() d.delete()
def test_embedded_document_field(self): i = Inner(a="hihi", b=123) d = ComplexDoc(i=i) d.save() response = self.client.get("/complexdoc") json_data = response.get_json()[config.ITEMS][0] self.assertDictEqual(json_data["i"], {"a": "hihi", "b": 123}) d.delete() # POST request response = self.client.post( "/complexdoc/", data='{"i": {"a": "xaxa", "b":-555}}', content_type="application/json", ) self.assertEqual(response.get_json()[config.STATUS], "OK") response = self.client.get("/complexdoc") json_data = response.get_json()[config.ITEMS][0] self.assertDictEqual(json_data["i"], {"a": "xaxa", "b": -555}) ComplexDoc.objects[0].delete() response = self.client.post( "/complexdoc/", data='{"i": {"a": "bar", "b": "baz"}}', content_type="application/json", ) json_data = response.get_json() self.assertEqual(json_data[config.STATUS], "ERR") self.assertIn("i", json_data[config.ISSUES]) self.assertIn("b", json_data[config.ISSUES]["i"]) self.assertEqual(json_data[config.ISSUES]["i"]["b"], "must be of integer type")
def test_embedded_document_field(self): i = Inner(a="hihi", b=123) d = ComplexDoc(i=i) d.save() response = self.client.get('/complexdoc') json_data = response.get_json()[config.ITEMS][0] self.assertDictEqual(json_data['i'], {'a':"hihi", 'b':123}) d.delete() # POST request response = self.client.post('/complexdoc/', data='{"i": {"a": "xaxa", "b":-555}}', content_type='application/json') self.assertEqual(response.get_json()[config.STATUS], "OK") response = self.client.get('/complexdoc') json_data = response.get_json()[config.ITEMS][0] self.assertDictEqual(json_data['i'], {'a':"xaxa", 'b':-555}) ComplexDoc.objects[0].delete() response = self.client.post('/complexdoc/', data='{"i": {"a": "bar", "b": "baz"}}', content_type='application/json') json_data = response.get_json() self.assertEqual(json_data[config.STATUS], "ERR") self.assertIn('i', json_data[config.ISSUES]) self.assertIn('b', json_data[config.ISSUES]['i']) self.assertEqual(json_data[config.ISSUES]['i']['b'], 'must be of integer type')
def test_put_subresource(self): # create new resource and subresource s = SimpleDoc(a="Answer to everything", b=42).save() d = ComplexDoc(l=["a", "b"], n=999, r=s).save() response = self.client.get("/simpledoc/%s/complexdoc/%s" % (s.id, d.id)) etag = response.get_json()[config.ETAG] headers = [("If-Match", etag)] # new putted document put_data = {"l": ["x", "y", "z"], "r": str(s.id)} put_url = "/simpledoc/%s/complexdoc/%s" % (s.id, d.id) response = self.client.put( put_url, data=json.dumps(put_data), content_type="application/json", headers=headers, ) self.assertEqual(response.status_code, 200) resp_json = response.get_json() self.assertEqual(resp_json[config.STATUS], "OK") # check, if really edited response = self.client.get("/simpledoc/%s/complexdoc/%s" % (s.id, d.id)) json_data = response.get_json() self.assertListEqual(json_data["l"], ["x", "y", "z"]) self.assertNotIn("n", json_data) s.delete() d.delete()
def test_embedded_document_field(self): i = Inner(a="hihi", b=123) d = ComplexDoc(i=i) d.save() response = self.client.get('/complexdoc') json_data = response.get_json()[config.ITEMS][0] self.assertDictEqual(json_data['i'], {'a': "hihi", 'b': 123}) d.delete() # POST request response = self.client.post('/complexdoc/', data='{"i": {"a": "xaxa", "b":-555}}', content_type='application/json') self.assertEqual(response.get_json()[config.STATUS], "OK") response = self.client.get('/complexdoc') json_data = response.get_json()[config.ITEMS][0] self.assertDictEqual(json_data['i'], {'a': "xaxa", 'b': -555}) ComplexDoc.objects[0].delete() response = self.client.post('/complexdoc/', data='{"i": {"a": "bar", "b": "baz"}}', content_type='application/json') json_data = response.get_json() self.assertEqual(json_data[config.STATUS], "ERR") self.assertIn('i', json_data[config.ISSUES]) self.assertIn('b', json_data[config.ISSUES]['i']) self.assertEqual(json_data[config.ISSUES]['i']['b'], 'must be of integer type')
def test_patch_subresource(self): # create new resource and subresource s = SimpleDoc(a="Answer to everything", b=42).save() d = ComplexDoc(l=['a', 'b'], n=999, r=s).save() response = self.client.get('/simpledoc/%s/complexdoc/%s' % (s.id, d.id)) etag = response.get_json()[config.ETAG] headers = [('If-Match', etag)] # patch document patch_data = {'l': ['x', 'y', 'z'], 'r': str(s.id)} patch_url = '/simpledoc/%s/complexdoc/%s' % (s.id, d.id) response = self.client.patch(patch_url, data=json.dumps(patch_data), content_type='application/json', headers=headers) self.assertEqual(response.status_code, 200) resp_json = response.get_json() self.assertEqual(resp_json[config.STATUS], "OK") # check, if really edited response = self.client.get('/simpledoc/%s/complexdoc/%s' % (s.id, d.id)) json_data = response.get_json() self.assertListEqual(json_data['l'], ['x', 'y', 'z']) self.assertEqual(json_data['n'], 999) # cleanup s.delete() d.delete()
def test_etag_in_item_and_resource(self): # etag of some entity has to be the same when fetching one item compared # to etag of part of feed (resource) d = ComplexDoc().save() feed = self.client.get('/complexdoc/').get_json() item = self.client.get('/complexdoc/%s' % d.id).get_json() try: self.assertEqual(feed[config.ITEMS][0][config.ETAG], item[config.ETAG]) finally: d.delete()
def test_dict_field(self): d = ComplexDoc(d={'g':'good', 'h':'hoorai'}) d.save() response = self.client.get('/complexdoc') try: json_data = response.get_json()[config.ITEMS][0] self.assertDictEqual(json_data['d'], {'g':'good', 'h':'hoorai'}) finally: # cleanup d.delete()
def test_dynamic_field(self): d = ComplexDoc(n=789) d.save() response = self.client.get('/complexdoc') try: json_data = response.get_json()[config.ITEMS][0] self.assertEqual(json_data['n'], 789) finally: # cleanup d.delete()
def test_dict_field(self): d = ComplexDoc(d={"g": "good", "h": "hoorai"}) d.save() response = self.client.get("/complexdoc") try: json_data = response.get_json()[config.ITEMS][0] self.assertDictEqual(json_data["d"], {"g": "good", "h": "hoorai"}) finally: # cleanup d.delete()
def test_dict_field(self): d = ComplexDoc(d={'g': 'good', 'h': 'hoorai'}) d.save() response = self.client.get('/complexdoc') try: json_data = response.get_json()[config.ITEMS][0] self.assertDictEqual(json_data['d'], {'g': 'good', 'h': 'hoorai'}) finally: # cleanup d.delete()
def test_embedded_in_list(self): # that's a tuff one i1 = Inner(a="foo", b=789) i2 = Inner(a="baz", b=456) d = ComplexDoc(o=[i1, i2]) d.save() response = self.client.get('/complexdoc') try: json_data = response.get_json()[config.ITEMS][0] self.assertListEqual(json_data['o'], [{'a':"foo", 'b':789},{'a':"baz", 'b':456}]) finally: d.delete()
def test_reference_field(self): s = SimpleDoc(a="samurai", b=911) s.save() d = ComplexDoc(r=s) d.save() response = self.client.get('/complexdoc') try: json_data = response.get_json()[config.ITEMS][0] self.assertEqual(json_data['r'], str(s.id)) finally: # cleanup d.delete() s.delete()
def test_embedded_resource_serialization(self): s = SimpleDoc(a="Answer to everything", b=42).save() d = ComplexDoc(r=s).save() response = self.client.get('/complexdoc?embedded={"r":1}') json_data = response.get_json() expected = {'a': "Answer to everything", 'b': 42} try: emb = json_data['_items'][0]['r'] self.assertEqual(emb['a'], expected['a']) self.assertEqual(emb['b'], expected['b']) self.assertIn(config.DATE_CREATED, emb) self.assertIn(config.LAST_UPDATED, emb) finally: d.delete() s.delete()
def test_embedded_in_list(self): # that's a tuff one i1 = Inner(a="foo", b=789) i2 = Inner(a="baz", b=456) d = ComplexDoc(o=[i1, i2]) d.save() response = self.client.get('/complexdoc') try: json_data = response.get_json()[config.ITEMS][0] self.assertListEqual(json_data['o'], [{ 'a': "foo", 'b': 789 }, { 'a': "baz", 'b': 456 }]) finally: d.delete()