Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
unit tests for Resource#rename_fields
Browse files Browse the repository at this point in the history
  • Loading branch information
wojcikstefan committed May 5, 2016
1 parent 5a360b3 commit 201bbc6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
15 changes: 15 additions & 0 deletions example/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,21 @@ class DictDocView(ResourceView):
resource = DictDocResource
methods = [Fetch, List, Create, Update]

# Document, resource, and view for testing renamed fields
class ReqTitlePost(db.Document):
title_str = db.StringField(required=True)

class ReqTitlePostResource(Resource):
document = ReqTitlePost
rename_fields = {
'title_str': 'title',
}

@api.register(url='/title_post/')
class ReqTitlePostView(ResourceView):
resource = ReqTitlePostResource
methods = [Fetch, List, Create, Update]


if __name__ == "__main__":
port = int(os.environ.get('PORT', 8000))
Expand Down
55 changes: 55 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def setUp(self):
example.C.drop_collection()
example.MethodTestDoc.drop_collection()
example.DictDoc.drop_collection()
example.ReqTitlePost.drop_collection()

# create user 1
resp = self.app.post('/user/', data=json.dumps(self.user_1))
Expand Down Expand Up @@ -1176,6 +1177,60 @@ def test_send_bad_json(self):
# test list
self.assertRaises(ValueError, self.app.get, '/dict_doc/')

def test_rename_fields_create(self):
"""
Make sure we can create objects by posting a renamed field consistent
with Resource#rename_fields.
"""
resp = self.app.post('/title_post/', data=json.dumps({ 'title': 'title' }))
response_success(resp)
self.assertEqual(example.ReqTitlePost.objects.first().title_str, 'title')

def test_rename_fields_get(self):
"""
Make sure fetched objects contain a renamed field consistent with
Resource#rename_fields.
"""
resp = self.app.post('/title_post/', data=json.dumps({ 'title': 'title' }))
response_success(resp)
post = json.loads(resp.data)

# list objects
resp = self.app.get('/title_post/')
response_success(resp)
self.assertEqual(json.loads(resp.data)['data'][0]['title'], 'title')

# fetch a single object
resp = self.app.get('/title_post/%s/' % post['id'])
response_success(resp)
self.assertEqual(json.loads(resp.data)['title'], 'title')

def test_rename_fields_error(self):
"""
Make sure field errors for a renamed field are returned correctly.
"""
# post with a missing required field
resp = self.app.post('/title_post/', data=json.dumps({
'title': None
}))
response_error(resp, code=400)
self.assertEqual(json.loads(resp.data), {
'field-errors': { 'title': 'Field is required' }
})

# create a valid object
resp = self.app.post('/title_post/', data=json.dumps({ 'title': 'title' }))
response_success(resp)

# update with a missing required field
resp = self.app.put('/title_post/%s/' % json.loads(resp.data)['id'], data=json.dumps({
'title': None
}))
response_error(resp, code=400)
self.assertEqual(json.loads(resp.data), {
'field-errors': { 'title': 'Field is required' }
})


if __name__ == '__main__':
unittest.main()
Expand Down

0 comments on commit 201bbc6

Please sign in to comment.