def test_modify(self): # given entry1 = Entry(name='Alice', id='1234') self.adapter.add(entry1) entry2 = Entry(name='Bob', id='1234') # when self.adapter.modify(entry2) # then assert_that(self.adapter.get('1234'), equal_to(entry2))
def test_add_same_id_twice_should_fail(self): # given id = '1234' entry1 = Entry(id, name='Alice') entry2 = Entry(id, name='Bob') self.adapter.add(entry1) # then with self.assertRaises(PhonebookDbException): # when self.adapter.add(entry2) assert_that(self.adapter.size(), is_(1)) assert_that(self.adapter.get(id), equal_to(entry1))
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_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_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_modify_should_fail_without_id(self): # given entry = Entry(name='Alice') # then with self.assertRaises(PhonebookDbException): # when self.adapter.modify(entry)
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 test_add_entry(self): # given entry = Entry('1234') # when self.adapter.add(entry) # then assert_that(self.adapter.size(), is_(1))
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_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_add_entry_without_id_should_generate_random_id(self): # given entry = Entry(name='Alice') # when self.adapter.add(entry) # then assert_that(entry.id, not_none()) assert_that(entry.id, instance_of(str))
def test_add_should_deepcopy_the_entry(self): # given entry = Entry('1234', name='Alice') # when self.adapter.add(entry) # then entry.name = 'Bob' assert_that(self.adapter.get(entry.id).name, is_('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_modify_not_existing_entry_should_fail(self): # given entry = Entry(name='Alice', id='1234') assert_that( # when calling(self.adapter.modify).with_args(entry), # then raises(PhonebookDbException))
def test_get_entry(self): # given entry = Entry('1234', name='Alice') self.adapter.add(entry) # when result = self.adapter.get(entry.id) # then assert_that(result, equal_to(entry))
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_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 test_remove(self): # given entry = Entry(name='Alice') self.adapter.add(entry) assert_that(self.adapter.size(), is_(1)) # when self.adapter.remove(entry.id) # then assert_that(self.adapter.size(), is_(0))
def test_list_should_deepcopy_the_entries(self): # given entry = Entry('1234', name='Alice') self.adapter.add(entry) # when result = self.adapter.list()[0] # then result.name = 'Bob' assert_that(self.adapter.get(entry.id).name, is_('Alice'))
def test_modify_should_deepcopy_the_entry(self): # given entry = Entry('1234', name='Alice') self.adapter.add(entry) entry.name = 'Bob' # when self.adapter.modify(entry) # then entry.name = 'Charlie' assert_that(self.adapter.get(entry.id).name, is_('Bob'))
def test_init_with_extra_attrs(self): entry = Entry(2, attrBool=True, attrStr='attr', attrDict={ 'a': 1, 'b': 2 }) assert_that(entry.id, is_(2)) assert_that(entry.attrBool, is_(True)) assert_that(entry.attrStr, is_('attr')) assert_that(entry.attrDict, equal_to({'a': 1, 'b': 2}))
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 post(self) -> Entry: """ Create new Phonebook Entry. The content of the Entry will be set from the JSON request body. :return: the created Phonebook Entry """ entry = Entry(**request.json) try: self._db_adapter.add(entry) except PhonebookDbException as e: abort(400, e.args[0]) return 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_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 }))