def testDelegatesMatchingToNestedMatcher(self): self.assert_matches('should match', is_(equal_to(True)), True) self.assert_matches('should match', is_(equal_to(False)), False) self.assert_does_not_match('should not match', is_(equal_to(True)), False) self.assert_does_not_match('should not match', is_(equal_to(False)), True)
def test_are_network_applications_equal_in_flow(self): assert_that( IsEqualToFlowComparisonLogic. are_network_applications_equal_in_flow( ["app1", "app2"], {"networkApplications": [{ "name": "app1" }, { "name": "app2" }]}), is_(equal_to(True))) assert_that( IsEqualToFlowComparisonLogic. are_network_applications_equal_in_flow( ["app1", "app2", "app3"], {"networkApplications": [{ "name": "app1" }, { "name": "app2" }]}), is_(equal_to(False))) # Test the case where the network applications are set to ANY on the server assert_that( IsEqualToFlowComparisonLogic. are_network_applications_equal_in_flow( [], {"networkApplications": [ANY_NETWORK_APPLICATION]}), is_(equal_to(True))) assert_that( IsEqualToFlowComparisonLogic. are_network_applications_equal_in_flow( ["app1"], {"networkApplications": [ANY_NETWORK_APPLICATION]}), is_(equal_to(False)))
def test_are_network_users_equal_in_flow(self): assert_that( IsEqualToFlowComparisonLogic.are_network_users_equal_in_flow( ["user1", "user2"], {"networkUsers": [{ "name": "user1" }, { "name": "user2" }]}), is_(equal_to(True))) assert_that( IsEqualToFlowComparisonLogic.are_network_users_equal_in_flow( ["user1", "UnknownUser"], {"networkUsers": [{ "name": "user1" }, { "name": "user2" }]}), is_(equal_to(False))) # Test the case where the network users are set to ANY on the server assert_that( IsEqualToFlowComparisonLogic.are_network_users_equal_in_flow( ["user1"], {"networkUsers": [ANY_OBJECT]}), is_(equal_to(False))) assert_that( IsEqualToFlowComparisonLogic.are_network_users_equal_in_flow( [], {"networkUsers": [ANY_OBJECT]}), is_(equal_to(True)))
def test_search(self): assert_that(linear_search([1], 1), is_(0)) for i in range(3): assert_that(linear_search([1, 2, 3], i + 1), is_(i)) for i in range(4): assert_that(linear_search([1, 2, 3, 4], i + 1), is_(i)) for i in range(5): assert_that(linear_search([1, 2, 3, 4, 5], i + 1), is_(i))
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_delete_entry(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/1234/', expect_errors=True).status_int, is_(404)) assert_that(self.app.get('/phonebook/').json, empty())
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_db_user_fetching(user_chat_mock, close_db_connection): """Test for checking user data obtained from the DB.""" user_from_db = User.fetch_user(user_chat_mock) assert_that(user_from_db.user_id, is_(user_chat_mock.id), "Users ID is invalid from DB.") assert_that(user_from_db.username, is_(user_chat_mock.username), "Users username is invalid from DB.") assert_that(user_from_db.first_name, is_(user_chat_mock.first_name), "Users first name is invalid from DB.") assert_that(user_from_db.last_name, is_(user_chat_mock.last_name), "Users last name is invalid from DB.")
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_run_cheroot(self): # given self.addCleanup(main.stop) t = Thread(target=run) # when t.start() # then response = requests.get(url='http://localhost:8080/phonebook/') assert_that(response.status_code, is_(200)) assert_that(response.json(), is_([])) # when main.stop() # then with self.assertRaises(ConnectionError): requests.get(url='http://localhost:8080/phonebook/')
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_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__are_network_services_equal_in_flow(self): # TODO: Make sure that we have no issues with case sensitiveness of TCP/80 vs tcp/80 for any of the protocols assert_that( IsEqualToFlowComparisonLogic._are_network_services_equal_in_flow( ["service1", "service2"], [{ "name": "service2" }, { "name": "service1" }]), is_(equal_to(True))) assert_that( IsEqualToFlowComparisonLogic._are_network_services_equal_in_flow( ["service2"], [{ "name": "service1" }], ), is_(equal_to(False)))
def test_is_matcher(self): class Customer: pass customer = Customer() when(self.spy.one_arg_method).with_args( is_(customer)).then_return(1000) self.assertEqual(1000, self.spy.one_arg_method(customer))
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_should_fail_if_no_entry_found(self): # when response = self.app.get('/phonebook/asdf/', expect_errors=True) # then assert_that(response.status_int, is_(404)) assert_that(response.json, equal_to({ 'status': 404, 'detail': HTTP_404 }))
def test__are_destinations_equal_in_flow(self): assert_that( IsEqualToFlowComparisonLogic._are_destinations_equal_in_flow( ["objectName1", "objectName2"], [{ "name": "objectName1" }, { "name": "objectName2" }], ), is_(equal_to(True))) assert_that( IsEqualToFlowComparisonLogic._are_destinations_equal_in_flow( ["objectName1"], [{ "name": "UnknownObjectName" }], ), is_(equal_to(False)))
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_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_get_invalid_url_should_fail(self): # when response = self.app.get('/bogus/url', expect_errors=True) # then assert_that(response.status_int, is_(404)) assert_that(response.json, equal_to({ 'status': 404, 'detail': HTTP_404 }))
def test_base_url_should_reject_delete(self): # when response = self.app.delete(url='/phonebook/', expect_errors=True) # then assert_that(response.status_int, is_(405)) assert_that(response.json, equal_to({ 'status': 405, 'detail': HTTP_405 }))
def test_basic_sort(self): assert_that(self.sort([1]), is_([1])) assert_that(self.sort([1, 2]), is_([1, 2])) assert_that(self.sort([2, 1]), is_([1, 2])) assert_that(self.sort([1, 2, 3, 4, 5]), is_([1, 2, 3, 4, 5])) assert_that(self.sort([5, 4, 3, 2, 1]), is_([1, 2, 3, 4, 5])) assert_that(self.sort([1, 2, 5, 4, 3]), is_([1, 2, 3, 4, 5])) assert_that(self.sort(list(reversed(range(100)))), is_(list(range(100)))) assert_that(self.sort(list(reversed(range(101)))), is_(list(range(101))))
def test_plot_graph_image_generation(): """Test of generation graph image.""" test_graph_image_path = os.path.join(TEST_ARCHIVE_FOLDER, TEST_GRAPH_IMAGE_PNG_NAME) fetch_plot_graph_image([datetime.now()], [random.randrange(10, 1000)], test_graph_image_path, "test_label") assert_that(os.path.exists(test_graph_image_path), is_(equal_to(True)), f"Graph image '{test_graph_image_path}' does not generate.")
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_delete_nonexisting_entry_should_fail(self): # when response = self.app.delete(url='/phonebook/1234', expect_errors=True) # then assert_that(response.status_int, is_(404)) assert_that( response.json, equal_to({ 'status': 404, 'detail': 'Entry with the given ID does not exists: 1234' }))
def test_are_sources_equal_in_flow(self): assert_that( IsEqualToFlowComparisonLogic.are_sources_equal_in_flow( ["objectName1", "objectName2"], { "sources": [{ "name": "objectName1" }, { "name": "objectName2" }] }, ), is_(equal_to(True))) assert_that( IsEqualToFlowComparisonLogic.are_sources_equal_in_flow( ["objectName1"], {"sources": [{ "name": "UnknownObjectName" }]}, ), is_(equal_to(False)))
def test_are_network_services_included_in_flow(self): assert_that( IsIncludedInFlowComparisonLogic. are_network_services_included_in_flow( [LiteralService("TCP/123"), LiteralService("UDP/456")], { "services": [{ "services": ["TCP/123", "UDP/456"] }, { "services": ["TCP/456", "UDP/123"] }] }), is_(equal_to(True))) assert_that( IsIncludedInFlowComparisonLogic. are_network_services_included_in_flow( [LiteralService("TCP/1111")], { "services": [{ "services": ["TCP/123", "UDP/456"] }, { "services": ["TCP/456", "UDP/123"] }] }), is_(equal_to(False))) assert_that( IsIncludedInFlowComparisonLogic. are_network_services_included_in_flow( [LiteralService("TCP/123"), LiteralService("UDP/456")], {"services": [{ "services": ["TCP/*", "UDP/*", "TCP/123"] }]}), is_(equal_to(True))) assert_that( IsIncludedInFlowComparisonLogic. are_network_services_included_in_flow( [LiteralService("TCP/123"), LiteralService("UDP/456")], {"services": [{ "services": ["*"] }]}), is_(equal_to(True)))
def test(): for i in range(positive_tests): pos = randint(0, len(list) - 1) result = search(list, list[pos]) assert_that(result, is_(pos)) for i in range(negative_tests): pos = randint(0, len(list) - 1) try: search(list, list[pos] + 1) except ValueError: continue raise Exception('ValueError was not raised')
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 testDelegatesDescribeMismatchToNestedMatcher(self): self.assert_describe_mismatch( NeverMatch.mismatch_description, is_(NeverMatch()), 'hi')
def testSuccessfulMatchDoesNotGenerateMismatchDescription(self): self.assert_no_mismatch_description(is_('A'), 'A')
def testProvidesConvenientShortcutForIsInstanceOfOldStyleClass(self): self.assert_matches('should match', is_(OldClass), OldClass())
def testProvidesConvenientShortcutForIsInstanceOf(self): self.assert_matches('should match', is_(str), 'A'); self.assert_does_not_match('should not match', is_(int), 'A');
def testProvidesConvenientShortcutForIsEqualTo(self): self.assert_matches('should match', is_('A'), 'A'); self.assert_matches('should match', is_('B'), 'B'); self.assert_does_not_match('should not match', is_('A'), 'B'); self.assert_does_not_match('should not match', is_('B'), 'A'); self.assert_description("'A'", is_('A'));
def testDescriptionShouldPassThrough(self): self.assert_description('<True>', is_(equal_to(True)))