def test_put_should_modify_whole_entry(self): # given entry = Entry('1234', name='Charlie', phone='5678') self.app.post_json(url='/phonebook/', params=objToDict(entry)) # when entry2 = Entry('1234', name='Bob', mobile='9876') response = self.app.put_json(url='/phonebook/1234', params=objToDict(entry2)) # then assert_that(response.status_int, is_(200)) assert_that(response.json, json_equal_to_entry(entry2)) assert_that( self.app.get(url='/phonebook/1234').json, json_equal_to_entry(entry2))
def test_obj_to_dict_with_builtin_types(self): assert_that(objToDict(1), is_(1)) assert_that(objToDict(1.2), is_(1.2)) assert_that(objToDict(True), is_(True)) assert_that(objToDict("asdf"), is_("asdf")) assert_that(objToDict(None), is_(None)) assert_that(objToDict([1, 2, 3]), is_([1, 2, 3])) assert_that(objToDict((1, 2, 3)), is_((1, 2, 3))) assert_that(objToDict({1, 2, 3}), is_({1, 2, 3})) # FIXME: set is not JSON serializable assert_that(objToDict({'a': 1, 'b': 2}), is_({'a': 1, 'b': 2}))
def test_obj_to_dict(self): # given e = Entry('1234', name='Alice') # when result = objToDict(e) # then assert_that(result, is_({'id': '1234', 'name': 'Alice'}))
def test_put_should_fail_when_id_in_url_and_body_mismatch(self): # given entry = Entry('1234', name='Charlie', phone='5678') self.app.post_json(url='/phonebook/', params=objToDict(entry)) # when entry2 = Entry('4567', name='Bob', mobile='9876') response = self.app.put_json(url='/phonebook/1234', params=objToDict(entry2), expect_errors=True) # then assert_that(response.status_int, is_(400)) assert_that( response.json, equal_to({ 'status': 400, 'detail': 'Entry ID in URL and body mismatch' }))
def test_post_same_id_twice_should_fail(self): # given entry1 = Entry('1234') entry2 = Entry('1234') # when self.app.post_json(url='/phonebook/', params=objToDict(entry1)) response = self.app.post_json(url='/phonebook/', params=objToDict(entry2), expect_errors=True) # then assert_that(response.status_int, is_(400)) assert_that( response.json, equal_to({ 'status': 400, 'detail': 'ID already exists: 1234' }))
def test_post_entry_with_trailing_slash(self): # given entry = Entry('1234') # when response = self.app.post_json(url='/phonebook/', params=objToDict(entry)) # then assert_that(response.status_int, is_(200)) assert_that(self.app.get(url='/phonebook/').json, has_length(1))
def test_delete_entry_with_trailing_slash(self): # given entry = Entry('1234', name='Charlie') self.app.post_json(url='/phonebook/', params=objToDict(entry)) # when response = self.app.delete(url='/phonebook/1234/') # then assert_that(response.status_int, is_(204)) assert_that(self.app.get('/phonebook/').json, empty())
def test_get_entry_by_id(self): # given entry = Entry('1234', name='Charlie') self.app.post_json(url='/phonebook/', params=objToDict(entry)) # when response = self.app.get('/phonebook/1234') # then assert_that(response.status_int, is_(200)) assert_that(response.json, json_equal_to_entry(entry))
def test_get_subresource_of_entry_should_fail(self): # given entry = Entry('1234', name='Charlie') self.app.post_json(url='/phonebook/', params=objToDict(entry)) # when response = self.app.get('/phonebook/1234/asdf', expect_errors=True) # then assert_that(response.status_int, is_(405)) assert_that(response.json, equal_to({ 'status': 405, 'detail': HTTP_405 }))
def test_put_with_trailing_slash(self): # given entry = Entry('1234', name='Charlie', phone='5678') # when response = self.app.put_json(url='/phonebook/1234/', params=objToDict(entry)) # then assert_that(response.status_int, is_(200)) assert_that(response.json, json_equal_to_entry(entry)) assert_that( self.app.get(url='/phonebook/1234').json, json_equal_to_entry(entry))
def test_put_to_subresource_should_fail(self): # given entry = Entry('1234', name='Charlie', phone='5678') # when response = self.app.put_json(url='/phonebook/1234/asdf', params=objToDict(entry), expect_errors=True) # then assert_that(response.status_int, is_(404)) assert_that(response.json, equal_to({ 'status': 404, 'detail': HTTP_404 }))
def test_put_new_entry_id_only_in_url(self): # given entry = Entry(name='Charlie', phone='5678') # when response = self.app.put_json(url='/phonebook/1234', params=objToDict(entry)) # then expected = Entry('1234', name='Charlie', phone='5678') assert_that(response.status_int, is_(200)) assert_that(response.json, json_equal_to_entry(expected)) assert_that( self.app.get(url='/phonebook/1234').json, json_equal_to_entry(expected))
def test_entry_should_reject_post(self): # given entry = Entry(name='Charlie', phone='5678') # when response = self.app.post_json(url='/phonebook/1234', params=objToDict(entry), expect_errors=True) # then assert_that(response.status_int, is_(405)) assert_that(response.json, equal_to({ 'status': 405, 'detail': HTTP_405 }))
def test_obj_to_dict_with_object_in_list(self): # given e1 = Entry('1234', name='Alice') e2 = Entry('456') # when result = objToDict([e1, e2]) # then assert_that(result, is_([{ 'id': '1234', 'name': 'Alice' }, { 'id': '456' }]))
def test_obj_to_dict_with_object_in_tuple(self): # given e1 = Entry('1234', name='Alice') e2 = Entry('456') # when result = objToDict((e1, e2)) # then assert_that(result, is_(({ 'id': '1234', 'name': 'Alice' }, { 'id': '456' })))
def test_obj_to_dict_with_object_in_object(self): # given e1 = Entry('1234', name='Alice') e2 = Entry('456', parent=e1) # when result = objToDict(e2) # then assert_that( result, is_({ 'id': '456', 'parent': { 'id': '1234', 'name': 'Alice' } }))
def patch(self) -> Entry: """ Modify a Phonebook Entry. The exising content of the Entry will patched with the JSON request body according to RFC7386 https://tools.ietf.org/html/rfc7386 :return: the modified Phonebook Entry """ actual = objToDict(self._db_adapter.get(self._entry_id)) if actual is None: abort(404, detail='Entry does not exists with ID: ' + self._entry_id) patch = request.json if 'id' in patch and patch['id'] != self._entry_id: abort(400, detail='Entry ID in URL and body mismatch') result = json_merge_patch.merge(actual, patch) result.pop('id') self._db_adapter.modify(Entry(self._entry_id, **result)) return self._db_adapter.get(self._entry_id)
def test_obj_to_dict_with_object_in_dict(self): # given e1 = Entry('1234', name='Alice') e2 = Entry('456') # when result = objToDict({'a': e1, 'b': e2}) # then assert_that( result, is_({ 'a': { 'id': '1234', 'name': 'Alice' }, 'b': { 'id': '456' } }))
def test_patch_with_trailing_slash(self): # given entry = Entry('1234', name='Charlie', phone='5678') self.app.post_json(url='/phonebook/', params=objToDict(entry)) # when response = self.app.patch_json(url='/phonebook/1234/', params={ 'name': 'David', 'phone': None, 'mobile': 5555 }) # then expected = Entry('1234', name='David', mobile=5555) assert_that(response.status_int, is_(200)) assert_that(response.json, json_equal_to_entry(expected)) assert_that( self.app.get(url='/phonebook/1234').json, json_equal_to_entry(expected))
def __init__(self, entry: Entry) -> None: super().__init__(objToDict(entry))
def jsonify_entry(entry: Entry): return objToDict(entry)