def test_empty_entitylist_reverts_to_weight_zero(self): """Check that an empty EntityList calculates its new weight as zero.""" test_object = EntityList() test_object.calculate_new_weight() self.assertEqual(0.0, test_object.weight, "Weight should revert to zero") self.assertEqual(-1.0, test_object.delta, "Delta should be -1.0")
def get_appropriate_entitylist(self, ranked): """ Given the parameters of ranked and type then return an EntityList object of an appropriate type :param ranked: boolean - is this list a ranked list :return: an appropriate EntityList object """ assert ranked is not None # entity_list = None if not ranked or self.list_type == 'none': entity_list = EntityList() elif self.list_type == 'knn': entity_list = KnnEntityList() elif self.list_type == 'polynomial': entity_list = PolynomialEntityList() elif self.list_type == 'exponential': entity_list = ExponentialEntityList() elif self.list_type == 'svr': entity_list = SvrEntityList() else: logger.warning("Unrecognised EntityList Type ('%s'). Returning an " "unranked EntityList." % self.list_type) entity_list = EntityList() return entity_list
def test_category_cannot_be_set_directly(self): """Check that the category of an EntityList cannot be set directly""" test_object = EntityList() try: test_object.category = "ABC" self.fail("Should have thrown an AttributeError") except AttributeError: pass
def test_entitylist_name_is_trimmed_both(self): """Check that an EntityList name has both leading and trailing space trimmed""" test_object = EntityList() test_object.name = " XYZ other " self.assertEqual( "XYZ other", test_object.name, "Name should have leading and trailing space trimmed")
def test_category_is_trimmed_right(self): """ Check that an EntityList category has trailing space trimmed as a by-product of the category name """ test_object = EntityList() test_object.category_name = "DEF " # noinspection PyUnresolvedReferences self.assertEqual("DEF", test_object.category, "Category should have trailing space trimmed")
def test_category_converts_to_uppercase_when_set(self): """ Check that the category of an EntityList can be set as a by-product of the category_name and the reported value is uppercase """ test_object = EntityList() test_object.category_name = "def" # noinspection PyUnresolvedReferences self.assertEqual("DEF", test_object.category, "Category should be DEF")
def test_two_lists_have_different_categories_by_default(self): """ Check that two EntityLists with no category name specified have different categories reported. """ test_object1 = EntityList() test_object2 = EntityList() # noinspection PyUnresolvedReferences self.assertTrue(test_object1.category != test_object2.category, "Categories should be unique to each list by default")
def test_category_is_trimmed_both_and_internally_subbed(self): """ Check that an EntityList category has all space trimmed/replaced as a by-product of the category name. Leading/trailing space is discarded and internal spaces are converted to hyphens """ test_object = EntityList() test_object.category_name = " XYZ other " # noinspection PyUnresolvedReferences self.assertEqual( "XYZ-OTHER", test_object.category, "Category name should have leading and trailing " "space trimmed")
def test_entitylist_category_name_is_empty_by_default(self): """Check that an EntityList category name is blank by default""" test_object = EntityList() self.assertIsNotNone(test_object.category_name, "Category name should not be None") self.assertFalse(test_object.category_name, "Category name should be empty by default")
def test_entity_can_be_appended_to_entitylist(self): """ Check that appending an Entity to the List extends the list and stores reference to the list in the Entity. """ test_entity = Entity("D") test_object = EntityList() test_object.append(test_entity) self.assertTrue(test_object, "List should now be true") self.assertEqual(1, len(test_object), "List should contain one element") self.assertTrue(test_entity in test_object, "Entity should be in the EntityList") self.assertTrue(test_entity.lists, "Entity's lists should now be True") self.assertEqual(1, len(test_entity.lists), "Entity's List of Lists should contain one element") self.assertTrue(test_object in test_entity.lists, "EntityList should be in the Entity's List of Lists")
def test_entitylist_reports_adjusted_weight_for_given_score(self): """ Given a score for an Entity (assumed to be in the list), return the weight of the list as if that score were not included. """ test_object = EntityList() ent1 = Entity("1") ent1.score = 2.4 test_object.append(ent1) ent2 = Entity("2") ent2.score = 14.2 test_object.append(ent2) test_object.calculate_new_weight() self.assertAlmostEqual( sqrt(2.4), test_object.get_corrected_list_weight(ent2.score), 1, "Corrected weight should be sqrt(2.4)") self.assertAlmostEqual( sqrt(14.2), test_object.get_corrected_list_weight(ent1.score), 1, "Corrected weight should be sqrt(14.2)")
def test_list_added_is_stored(self): """Test that when an Entity is added to an EntityList, the list is stored in the Entity and can be retrieved """ test_object = Entity("CC") list1 = EntityList() test_object.note_list(list1) self.assertEqual(1, len(test_object.lists), "Should be one entry in " "the list of lists") self.assertTrue(list1 in test_object.lists, "List should be in the " "list of lists")
def test_entitylist_reset(self): """ Check that an EntityList can be reset having first calculated a weight """ test_object = EntityList() ent1 = MagicMock(Entity) ent1.score = 2.4 test_object.append(ent1) ent2 = MagicMock(Entity) ent2.score = 14.2 test_object.append(ent2) test_object.calculate_new_weight() self.assertAlmostEqual(2.88097206, test_object.weight, 8, "Weight should be calculated as 2.88097206") self.assertAlmostEqual(1.88097206, test_object.delta, 8, "Delta should be 1.88097206") test_object.reset() self.assertEqual(1, test_object.weight, "Weight should be reset to 1") self.assertIsNone(test_object.delta, "Delta should be reset to None") # self.assertEqual(0.0, test_object.__total_entity_weight, # "Total Entity Weight should be zero") ent1.reset.assert_called_once() ent2.reset.assert_called_once()
def test_weight_for_entity_reports_zero_for_noncontained_entity(self): """Check that an EntityList returns zero when asked for the weight for a Entity that is not in its list of Entities""" test_object = EntityList() ent1 = Entity("1") test_object.append(ent1) ent2 = Entity("2") test_object.append(ent2) uncontained = Entity("3") self.assertEqual(0.0, test_object.get_weight_for_entity(uncontained), "Should return zero for a entity not in the list")
def test_entitylist_reports_zero_weight_for_single_entity_entitylist(self): """ Given a score for an Entity (the only entity in the list), return the weight of the list as if that score were not included i.e. zero. """ test_object = EntityList() ent1 = Entity("1") ent1.score = 96.4 test_object.append(ent1) test_object.calculate_new_weight() self.assertAlmostEqual( 0, test_object.get_corrected_list_weight(ent1.score), 1, "Corrected weight should be 0")
def test_weight_for_entity_returns_list_weight_for_contained_entity(self): """Check that an EntityList returns the list weight when asked for the weight for a Entity that is in its list of Entities""" test_object = EntityList() test_object.weight = 1234.567 ent1 = Entity("1") test_object.append(ent1) ent2 = Entity("2") test_object.append(ent2) returned_value = test_object.get_weight_for_entity(ent2) self.assertEqual( 1234.567, returned_value, "Should return the list weight for a" "contained Entity")
def test_category_is_unknown_number_by_default(self): """ Check that the category of an EntityList is of the pattern 'Unknown-nnn' by default """ test_object = EntityList() expected_pattern = compile("^Unknown-[0-9]{3}$") # noinspection PyUnresolvedReferences self.assertIsNotNone(test_object.category, "Category should not be None") # noinspection PyUnresolvedReferences self.assertTrue( expected_pattern.match(test_object.category), "Category should match 'Unknown-nnn' by default " "- got '{}'".format(test_object.category))
def test_entitylist_returns_absolute_delta_value(self): """ Check that an EntityList calculates its new weight from the Entities it contains and stores the real delta i.e. delta can be negative """ test_object = EntityList() ent1 = Entity("1") ent1.score = 0.4 test_object.append(ent1) ent2 = Entity("2") ent2.score = 0.3 test_object.append(ent2) returned_delta = test_object.calculate_new_weight() self.assertAlmostEqual(0.59160797831, test_object.weight, 8, "Weight should be calculated as 0.59160797831") self.assertAlmostEqual(-0.40839202169, test_object.delta, 8, "Delta should be -0.40839202169") self.assertAlmostEqual(-0.40839202169, returned_delta, 8, "Returned Delta should be -0.40839202169")
def test_entitylist_calculates_weight_from_entities(self): """ Check that an EntityList calculates its new weight from the Entities it contains """ test_object = EntityList() ent1 = Entity("1") ent1.score = 2.4 test_object.append(ent1) ent2 = Entity("2") ent2.score = 14.2 test_object.append(ent2) returned_delta = test_object.calculate_new_weight() self.assertAlmostEqual(2.88097206, test_object.weight, 8, "Weight should be calculated as 2.88097206") self.assertAlmostEqual(1.88097206, test_object.delta, 8, "Delta should be 1.88097206") self.assertAlmostEqual(1.88097206, returned_delta, 8, "Returned Delta should be 1.88097206")
def test_unranked_list_names_self_correctly(self): """Check that an unranked list correctly reports itself using the string naming convention""" from random import randint test_object = EntityList() test_object.is_ranked = False list_count = randint(1, 20) for x in range(0, list_count): entity = Entity(str(x)) test_object.append(entity) self.assertEqual( str(list_count) + "u", test_object.code_string(), "Got an unexpected code_string")
def test_entitylist_fails_adjusted_weight_on_empty_entitylist(self): """ Check that the get_corrected_list_weight() method fails with an exception on an empty list. """ test_object = EntityList() ent1 = Entity("1") ent1.score = 96.4 # test_object.append(ent1) test_object.calculate_new_weight() try: test_object.get_corrected_list_weight(ent1.score) raise Exception("Should have thrown an Exception") except ValueError: pass except Exception: raise Exception("Should have thrown a ValueError")
def test_entitylist_has_initial_base_score_stdev_one(self): """Check that an EntityList object has a base_score_stdev of 1 by default.""" test_object = EntityList() self.assertEqual(1.0, test_object.base_score_stdev, "Default EntityList base_score_stdev should be one")
def test_entitylist_has_initial_base_score_mean_zero(self): """Check that an EntityList object has a base_score_mean of 0 by default.""" test_object = EntityList() self.assertEqual(0.0, test_object.base_score_mean, "Default EntityList base_score_mean should be zero")
def test_weight_for_entity_reports_zero_for_none_entity(self): """Check that an EntityList returns zero when asked for the weight for a None Entity""" test_object = EntityList() self.assertEqual(0.0, test_object.get_weight_for_entity(None), "Should return zero for a None entity")
def test_entitylist_name_is_trimmed_right(self): """Check that an EntityList name has trailing space trimmed""" test_object = EntityList() test_object.name = "DEF " self.assertEqual("DEF", test_object.name, "Name should have trailing space trimmed")
def test_entitylist_has_initial_weight_one(self): """Check that an EntityList object has a weight of 1 by default.""" test_object = EntityList() self.assertEqual(1, test_object.weight, "Default EntityList weight should be one")
def test_category_name_is_trimmed_left(self): """Check that an EntityList category name has leading space trimmed""" test_object = EntityList() test_object.category_name = " ABC" self.assertEqual("ABC", test_object.category_name, "Category name should have leading space trimmed")
def test_entitylist_is_unranked_by_default(self): """Check that the default setting is 'unranked'""" test_object = EntityList() self.assertFalse(test_object.is_ranked, "Default EntityList should be unranked")
def test_entitylist_name_is_trimmed_left(self): """Check that an EntityList name has leading space trimmed""" test_object = EntityList() test_object.name = " ABC" self.assertEqual("ABC", test_object.name, "Name should have leading space trimmed")
def test_entitylist_can_be_set_ranked(self): """Check that we can set the ranked attribute""" test_object = EntityList() test_object.is_ranked = True self.assertTrue(test_object.is_ranked, "Should be possible to set an EntityList as ranked")