def test_db_field_name(self): # test if eve returns fields named like in db, not in python response = self.client.post( "/fieldsdoc/", data='{"longFieldName": "hello"}', content_type="application/json", ) json_data = response.get_json() self.assertEqual(response.status_code, 201) self.assertEqual(json_data[config.STATUS], "OK") response = self.client.post("/fieldsdoc/", data='{"n": "hello"}', content_type="application/json") json_data = response.get_json() self.assertEqual(response.status_code, 422) response = self.client.get("/fieldsdoc") json_data = response.get_json()[config.ITEMS][0] self.assertIn("longFieldName", json_data) self.assertEqual(json_data["longFieldName"], "hello") FieldsDoc.objects.delete() # the same, but through mongoengine FieldsDoc(n="hi").save() response = self.client.get("/fieldsdoc") json_data = response.get_json()[config.ITEMS][0] self.assertIn("longFieldName", json_data) self.assertEqual(json_data["longFieldName"], "hi")
def assertMediaStored(self, _id): _db = self.app.data.driver.db # retrieve media id coll = FieldsDoc._get_collection_name() media_id = _db[coll].find_one({ID_FIELD: ObjectId(_id)})['p'] # verify it's actually stored in the media storage system self.assertTrue(self.app.media.exists(media_id)) return media_id
def _fixture_template(self, data_ok, expected=None, data_fail=None, msg=None): d = FieldsDoc(**data_ok).save() if expected is None: expected = data_ok response = self.client.get('/fieldsdoc') json_data = response.get_json()[config.ITEMS][0] try: for key, value in iteritems(expected): self.assertEqual(json_data[key], value) finally: d.delete() if not data_fail: return # post response = self.client.post('/fieldsdoc/', data=json.dumps(data_fail), content_type='application/json') json_data = response.get_json() self.assertEqual(json_data[config.STATUS], "ERR") self.assertEqual(json_data[config.ISSUES], msg)
def test_patch_field_with_different_dbfield(self): # tests patching field whith has mongoengine's db_field specified # and different from python field name s = FieldsDoc(n="Hello").save() response = self.client.get('/fieldsdoc/%s' % s.id) etag = response.get_json()[config.ETAG] headers = [('If-Match', etag)] # patch document patch_data = {'longFieldName': 'Howdy'} patch_url = '/fieldsdoc/%s' % s.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")