def test_patch_overwrite_subset(self): # test what was really updated encoder = MongoengineJsonEncoder() raw = SimpleDoc._get_collection().find_one({"_id": ObjectId(self._id)}) time.sleep(1) response = self.do_patch(data='{"a": "greg"}') self.assert_correct_etag(response) expected = dict(raw) expected["a"] = "greg" for key in [config.ETAG, config.LAST_UPDATED]: expected[key] = response.json[key] real = SimpleDoc._get_collection().find_one( {"_id": ObjectId(self._id)}) self.assertEqual(encoder.encode(real), encoder.encode(expected)) # test if GET response returns corrent response response = self.client.get(self.url).get_json() self.assertIn("a", response) self.assertEqual(response["a"], "greg") self.assertIn("b", response) self.assertEqual(response["b"], 23) # cleanup headers = [("If-Match", response[config.ETAG])] response = self.client.delete("/simpledoc/%s" % self._id, headers=headers) assert response.status_code == 204 self.assertEqual(SimpleDoc.objects.count(), 0)
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_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_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_find_all_projection(self): d = SimpleDoc(a='Tom', b=223).save() response = self.client.get('/simpledoc?projection={"a": 1}') self.assertNotIn('b', response.get_json()['_items'][0]) response = self.client.get('/simpledoc?projection={"a": 1, "b": 1}') data = response.get_json()['_items'][0] self.assertIn('b', data) self.assertIn('a', data) d.delete()
def test_find_one(self): d = SimpleDoc(a='Tom', b=223).save() response = self.client.get('/simpledoc/%s' % d.id) # has to return one record json_data = response.get_json() self.assertIn(config.LAST_UPDATED, json_data) self.assertIn(config.DATE_CREATED, json_data) self.assertEqual(json_data['_id'], str(d.id)) self.assertEqual(json_data['a'], 'Tom') self.assertEqual(json_data['b'], 223) d.delete()
def wrapper(self): response = self.client.post('/simpledoc/', data='{"a": "jimmy", "b": 23}', content_type='application/json') json_data = response.get_json() self.url = '/simpledoc/%s' % json_data[config.ID_FIELD] response = self.client.get(self.url).get_json() self.etag = response[config.ETAG] self._id = response[config.ID_FIELD] self.updated = response[config.LAST_UPDATED] f(self) SimpleDoc.objects().delete()
def test_find_all_projection(self): d = SimpleDoc(a='Tom', b=223).save() response = self.client.get('/simpledoc?projection={"a": 1}') self.assertNotIn('b', response.get_json()['_items'][0]) response = self.client.get('/simpledoc?projection={"a": 1, "b": 1}') data = response.get_json()['_items'][0] self.assertIn('b', data) self.assertIn('a', data) response = self.client.get('/simpledoc?projection={"a": 0}') data = response.get_json()['_items'][0] self.assertIn('b', data) self.assertNotIn('a', data) d.delete()
def test_find_one_projection(self): # XXX: this it not eve's standard! self.skipTest('Projection on one document not supported') d = SimpleDoc(a='Tom', b=223).save() response = self.client.get('/simpledoc/%s?projection={"a":1}' % d.id) # has to return one record json_data = response.get_json() self.assertIn(config.LAST_UPDATED, json_data) self.assertIn(config.DATE_CREATED, json_data) self.assertNotIn('b', json_data) self.assertEqual(json_data['_id'], str(d.id)) self.assertEqual(json_data['a'], 'Tom') 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_patch_overwrite_subset(self): # test what was really updated raw = SimpleDoc._get_collection().find_one({"_id": ObjectId(self._id)}) response = self.do_patch(data='{"a": "greg"}') self.assert_correct_etag(response) expected = dict(raw) expected['a'] = 'greg' real = SimpleDoc._get_collection().find_one({"_id": ObjectId(self._id)}) self.assertDictEqual(real, expected) # test if GET response returns corrent response response = self.client.get(self.url).get_json() self.assertIn('a', response) self.assertEqual(response['a'], "greg") self.assertIn('b', response) self.assertEqual(response['b'], 23)
def test_find_all(self): _all = [] for data in ({'a': "Hello", 'b':1}, {'a': "Hi", 'b': 2}, {'a': "Seeya", 'b': 3}): d = SimpleDoc(**data).save() _all.append(d) response = self.client.get('/simpledoc') self.assertEqual(response.status_code, 200) data = response.get_json() s = set([item['a'] for item in data['_items']]) self.assertSetEqual(set(['Hello', 'Hi', 'Seeya']), s) # delete records for d in _all: d.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_uppercase_resource_names(self): # Sanity Check: the Default Setting is Uppercase Off response = self.client.get('/SimpleDoc') self.assertEqual(response.status_code, 404) # Create a new Eve App with settings to allow Uppercase Resource Names # in the URI. app = Eve(settings=SETTINGS) app.debug = True ext = EveMongoengine(app) ext.add_model(SimpleDoc, lowercase=False) client = app.test_client() d = SimpleDoc(a='Tom', b=223).save() # Sanity Check: Lowercase is Disabled response = client.get('/simpledoc/') self.assertEqual(response.status_code, 404) # Use the Uppercase Resource Name response = client.get('/SimpleDoc/') self.assertEqual(response.status_code, 200) json_data = response.get_json() expected_url = json_data[config.LINKS]['self']['href'] if ':' in expected_url: expected_url = '/' + '/'.join(expected_url.split('/')[1:]) self.assertTrue('SimpleDoc' in expected_url)
def test_delete_subresource(self): # more subresources -> delete them all s = SimpleDoc(a="James Bond", b=7).save() c1 = ComplexDoc(l=['p', 'q', 'r'], n=1, r=s).save() c2 = ComplexDoc(l=['s', 't', 'u'], n=2, r=s).save() # delete subresources del_url = '/simpledoc/%s/complexdoc' % s.id response = self.client.delete(del_url) self.assertEqual(response.status_code, 200) # check, if really deleted response = self.client.get('/simpledoc/%s/complexdoc' % s.id) json_data = response.get_json() self.assertEqual(json_data[config.ITEMS], []) # cleanup s.delete()
def test_datasource_projection(self): SETTINGS['DOMAIN'] = {'eve-mongoengine':{}} app = Eve(settings=SETTINGS) app.debug = True ext = EveMongoengine(app) ext.add_model(SimpleDoc, datasource={'projection': {'b': 0}}) client = app.test_client() d = SimpleDoc(a='Tom', b=223).save() response = client.get('/simpledoc/%s' % d.id) try: self.assertNotIn('b', response.get_json().keys()) # here it should return the field, but sadly, does not #response = client.get('/simpledoc/%s?projection={"b":1}' % d.id) #self.assertIn('b', response.get_json().keys()) finally: d.delete()
def test_patch_overwrite_subset(self): # test what was really updated raw = SimpleDoc._get_collection().find_one({"_id": ObjectId(self._id)}) response = self.do_patch(data='{"a": "greg"}') self.assert_correct_etag(response) expected = dict(raw) expected['a'] = 'greg' real = SimpleDoc._get_collection().find_one( {"_id": ObjectId(self._id)}) self.assertDictEqual(real, expected) # test if GET response returns corrent response response = self.client.get(self.url).get_json() self.assertIn('a', response) self.assertEqual(response['a'], "greg") self.assertIn('b', response) self.assertEqual(response['b'], 23)
def test_datasource_projection(self): SETTINGS['DOMAIN'] = {'eve-mongoengine': {}} app = Eve(settings=SETTINGS) app.debug = True ext = EveMongoengine(app) ext.add_model(SimpleDoc, datasource={'projection': {'b': 0}}) client = app.test_client() d = SimpleDoc(a='Tom', b=223).save() response = client.get('/simpledoc/%s' % d.id) try: self.assertNotIn('b', response.get_json().keys()) # here it should return the field, but sadly, does not #response = client.get('/simpledoc/%s?projection={"b":1}' % d.id) #self.assertIn('b', response.get_json().keys()) finally: d.delete()
def test_delete_subresource_item(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)] # delete subresource del_url = '/simpledoc/%s/complexdoc/%s' % (s.id, d.id) response = self.client.delete(del_url, headers=headers) self.assertEqual(response.status_code, 200) # check, if really deleted response = self.client.get('/simpledoc/%s/complexdoc/%s' % (s.id, d.id)) self.assertEqual(response.status_code, 404) s.delete()
def test_delete_subresource_item(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)] # delete subresource del_url = '/simpledoc/%s/complexdoc/%s' % (s.id, d.id) response = self.client.delete(del_url, headers=headers) self.assertEqual(response.status_code, 204) # check, if really deleted response = self.client.get('/simpledoc/%s/complexdoc/%s' % (s.id, d.id)) self.assertEqual(response.status_code, 404) s.delete()
def wrapper(self): response = self.client.post( "/simpledoc/", data='{"a": "jimmy", "b": 23}', content_type="application/json", ) json_data = response.get_json() self._id = json_data[config.ID_FIELD] self.url = "/simpledoc/%s" % self._id # json_data[config.ID_FIELD] # response = self.client.get(self.url).get_json() self.etag = json_data[config.ETAG] self.updated = json_data[config.LAST_UPDATED] try: f(self) finally: SimpleDoc.objects().delete()
def test_find_all_filtering(self): d = SimpleDoc(a='x', b=987).save() d2 = SimpleDoc(a='y', b=123).save() response = self.client.get('/simpledoc?where={"a": "y"}') json_data = response.get_json() try: self.assertEqual(len(json_data['_items']), 1) self.assertEqual(json_data['_items'][0]['b'], 123) finally: d.delete() d2.delete()
def test_get_subresource(self): s = SimpleDoc(a="Answer to everything", b=42).save() d = ComplexDoc(l=['a', 'b'], r=s).save() d2 = ComplexDoc(l=['c', 'd'], r=s).save() response = self.client.get('/simpledoc/%s/complexdoc' % s.id) self.assertEqual(response.status_code, 200) json_data = response.get_json() self.assertEqual(len(json_data['_items']), 2) real = [x['l'] for x in json_data['_items']] self.assertEqual(real, [['a', 'b'], ['c', 'd']])
def test_patch_subresource(self): # create new resource and subresource SimpleDoc.objects.delete() ComplexDoc.objects.delete() 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] self.assertNotIn("_cls", list(response.json.keys())) 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.assertNotIn("_cls", list(response.json.keys())) 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 headers = [("If-Match", json_data[config.ETAG])] response = self.client.delete("/complexdoc/%s" % d.id, headers=headers) assert response.status_code == 204 self.assertEqual(ComplexDoc.objects.count(), 0) s.delete()
def test_find_all_default_sort(self): s = self.app.config['DOMAIN']['simpledoc']['datasource'] d = SimpleDoc(a='abz', b=3).save() d2 = SimpleDoc(a='abc', b=-7).save() # set default sort to 'b', desc. if 'default_sort' in s: default = s['default_sort'] else: default = [] s['default_sort'] = [('b', -1)] self.app.set_defaults() response = self.client.get('/simpledoc') json_data = response.get_json() real = [x['b'] for x in json_data['_items']] expected = [3, -7] try: self.assertListEqual(real, expected) except Exception as e: # reset s['default_sort'] = default d.delete() d2.delete() raise # set default sort to 'b', asc. s['default_sort'] = [('b', 1)] self.app.set_defaults() response = self.client.get('/simpledoc') json_data = response.get_json() real = [x['b'] for x in json_data['_items']] expected = [-7, 3] try: self.assertListEqual(real, expected) finally: # reset s['default_sort'] = default d.delete() d2.delete()
def test_find_all(self): _all = [] for data in ({ 'a': "Hello", 'b': 1 }, { 'a': "Hi", 'b': 2 }, { 'a': "Seeya", 'b': 3 }): d = SimpleDoc(**data).save() _all.append(d) response = self.client.get('/simpledoc') self.assertEqual(response.status_code, 200) data = response.get_json() s = set([item['a'] for item in data['_items']]) self.assertSetEqual(set(['Hello', 'Hi', 'Seeya']), s) # delete records for d in _all: d.delete()
def test_find_all_sorting(self): d = SimpleDoc(a='abz', b=3).save() d2 = SimpleDoc(a='abc', b=-7).save() response = self.client.get('/simpledoc?sort={"a":1}') json_data = response.get_json() real = [x['a'] for x in json_data['_items']] expected = ['abc', 'abz'] try: self.assertListEqual(real, expected) except Exception as e: # reset d.delete() d2.delete() raise response = self.client.get('/simpledoc?sort={"b":-1}') json_data = response.get_json() real = [x['b'] for x in json_data['_items']] expected = [3, -7] try: self.assertListEqual(real, expected) finally: d.delete() d2.delete()
def test_post_subresource(self): s = SimpleDoc(a="Answer to everything", b=42).save() data = {"l": ["x", "y", "z"], "r": str(s.id)} post_url = "/simpledoc/%s/complexdoc" % s.id response = self.client.post( post_url, data=json.dumps(data), content_type="application/json" ) self.assertEqual(response.status_code, 201) resp_json = response.get_json() self.assertEqual(resp_json[config.STATUS], "OK") # verify saved data response = self.client.get("/simpledoc/%s/complexdoc" % s.id) self.assertEqual(response.status_code, 200) resp_json = response.get_json() self.assertEqual(len(resp_json[config.ITEMS]), 1) self.assertEqual(resp_json[config.ITEMS][0]["l"], ["x", "y", "z"])
def test_post_subresource(self): s = SimpleDoc(a="Answer to everything", b=42).save() data = {'l': ['x', 'y', 'z'], 'r': str(s.id)} post_url = '/simpledoc/%s/complexdoc' % s.id response = self.client.post(post_url, data=json.dumps(data), content_type='application/json') self.assertEqual(response.status_code, 201) resp_json = response.get_json() self.assertEqual(resp_json[config.STATUS], "OK") # verify saved data response = self.client.get('/simpledoc/%s/complexdoc' % s.id) self.assertEqual(response.status_code, 200) resp_json = response.get_json() self.assertEqual(len(resp_json[config.ITEMS]), 1) self.assertEqual(resp_json[config.ITEMS][0]['l'], ['x', 'y', 'z'])
def test_uppercase_resource_names(self): # test default lowercase response = self.client.get('/SimpleDoc') self.assertEqual(response.status_code, 404) # uppercase app = Eve(settings=SETTINGS) app.debug = True ext = EveMongoengine(app) ext.add_model(SimpleDoc, lowercase=False) client = app.test_client() d = SimpleDoc(a='Tom', b=223).save() response = client.get('/SimpleDoc/') self.assertEqual(response.status_code, 200) json_data = response.get_json() expected_url = json_data[config.LINKS]['self']['href'] if ':' in expected_url: expected_url = '/' + '/'.join(expected_url.split('/')[1:]) self.assertEqual(expected_url, '/SimpleDoc') # not lowercase when uppercase response = client.get('/simpledoc/') self.assertEqual(response.status_code, 404)
def test_update_date_consistency(self): # tests if _updated is really updated when PATCHing resource item = self.client.get(self.url).get_json() updated1 = item[config.LAST_UPDATED] time.sleep(1) s = SimpleDoc.objects.get() updated_before_db_patch = s.updated s.a = "bob" s.save() updated_after_db_patch = s.updated # mongoengine doesn't modify default document, timestamp will update only via RESTFUL # operation self.assertEqual(updated_before_db_patch, updated_after_db_patch) item2 = self.do_patch(data='{"a": "greg", "b": 300}').json updated2 = item2[config.LAST_UPDATED] self.assertNotEqual(updated1, updated2) self.assertNotEqual(item[config.ETAG], item2[config.ETAG]) doc = SimpleDoc(a="hello", created=updated_before_db_patch, updated=updated_before_db_patch).save(validate=False) self.assertEqual(doc.updated, updated_before_db_patch)
def test_delete_empty_resource(self): SimpleDoc.objects().delete() response = self.delete('/simpledoc') self.assertEqual(response.status_code, 200)
def tearDown(self): SimpleDoc.objects().delete()