def test_similarity_missing_attribute(self): """Verify a missing attribute hurts similarity.""" self.obj1.item1.similarity.return_value = Similarity(1.0) self.obj1.item2.similarity.return_value = Similarity(1.0) del self.obj2.item2 # Testing: % similarity = (self.obj1 % self.obj2) self.assertFalse(similarity) self.assertEqual(0.25, similarity)
def test_similarity_none_attribute(self): """Verify an empty attribute hurts similarity.""" self.obj1.item1.similarity.return_value = Similarity(1.0) self.obj1.item2.similarity.return_value = Similarity(1.0) self.obj2.item2 = None # Testing: % similarity = (self.obj1 % self.obj2) self.assertFalse(similarity) self.assertEqual(0.25, similarity)
def test_similarity_true(self): """Verify two compound comparables can be compared for similarity.""" self.obj1.item1.similarity.return_value = Similarity(0.0) self.obj1.item2.similarity.return_value = Similarity(1.0) # Testing: % similarity = (self.obj1 % self.obj2) self.assertTrue(similarity) self.assertEqual(0.75, similarity) self.obj1.item1.similarity.assert_called_once_with(self.obj2.item1) self.obj1.item2.similarity.assert_called_once_with(self.obj2.item2)
def test_similarity_none_attributes(self): """Verify two empty attributes are not included in similarity.""" self.obj1.item1.similarity.return_value = Similarity(1.0) self.obj1.item2.similarity.return_value = Similarity(1.0) self.obj1.item2 = None self.obj2.item2 = None # Testing: % similarity = (self.obj1 % self.obj2) self.assertTrue(similarity) self.assertEqual(1.0, similarity)
def test_similarity_false(self): """Verify two simple comparables can be compared for non-similarity.""" sim = Similarity(0.90, threshold=0.95) with patch.object(self.Simple, 'similarity', Mock(return_value=sim)): similarity = (self.obj1 % self.obj2) self.assertFalse(similarity) self.obj1.similarity.assert_called_once_with(self.obj2)
def test_sub(self): """Verify two similarities can be subtracted.""" self.assertEqual(Similarity(0.42), Similarity(0.43) - Similarity(0.01))
def test_imul_with_number(self): """Verify a similarity can be multiplied to by a number.""" similarity = Similarity(0.6) similarity *= 0.7 self.assertEqual(Similarity(0.42), similarity)
def test_isub(self): """Verify a similarity can be subtracted from.""" similarity = Similarity(0.43) similarity -= Similarity(0.01) self.assertEqual(Similarity(0.42), similarity)
def test_sub_with_number(self): """Verify a number can be subtracted from a similarity.""" self.assertEqual(Similarity(0.42), Similarity(0.43) - 0.01)
def test_sorting(self): """Verify similarities can be sorted.""" sims = [Similarity(1), Similarity(0), 0.5] sims.sort() self.assertEqual([Similarity(0), 0.5, Similarity(1)], sims)
def test_radd_with_number(self): """Verify a similarity can be added to a number.""" self.assertEqual(Similarity(0.42), 0.4 + Similarity(0.02))
def test_bool_true_with_threshold(self): """Verify a similarity of <1.0 is True with a threshold.""" self.assertTrue(Similarity(0.89, threshold=0.88))
def test_float_equal(self): """Verify similarities and floats can be compared for equality.""" self.assertEqual(Similarity(0.42), 0.42)
def test_str(self): """Verify similarity objects can be represented as strings.""" self.assertEqual("100.0% similar", str(Similarity(1.0))) self.assertEqual("99.0% similar", str(Similarity(0.99))) self.assertEqual("0.0% similar", str(Similarity(0.0)))
def test_bool_true(self): """Verify a similarity of 1.0 is True.""" self.assertTrue(Similarity(1.0))
def test_imul(self): """Verify a similarity can be multiplied to.""" similarity = Similarity(0.6) similarity *= Similarity(0.7) self.assertEqual(Similarity(0.42), similarity)
def test_mul_with_number(self): """Verify a number can be multiplied with a similarity.""" self.assertEqual(Similarity(0.42), Similarity(0.6) * 0.7)
def test_mul(self): """Verify two similarities can be multiplied.""" self.assertEqual(Similarity(0.42), Similarity(0.6) * Similarity(0.7))
def test_rsub_with_number(self): """Verify a similarity can be subtracted from a number.""" self.assertEqual(Similarity(0.42), 0.43 - Similarity(0.01))
def test_isub_with_number(self): """Verify a number can be subtracted from a similarity.""" similarity = Similarity(0.43) similarity -= 0.01 self.assertEqual(Similarity(0.42), similarity)
def test_repr(self): """Verify object representation works for similarity objects.""" sim = Similarity(0.89, threshold=0.87) self.assertEqual("Similarity(0.89, threshold=0.87)", repr(sim))
def test_rmul_with_number(self): """Verify a similarity can multiplied with a number.""" self.assertEqual(Similarity(0.42), 0.6 * Similarity(0.7))
def test_bool_false(self): """Verify a similarity of <1.0 if False.""" self.assertFalse(Similarity(0.99))
def test_abs(self): """Verify absolute value works for similarities.""" self.assertEqual(Similarity(0.42), abs(Similarity(-0.42)))
def test_bool_false_with_threshold(self): """Verify a similarity is False if under the threshold.""" self.assertFalse(Similarity(0.89, threshold=0.90))
def test_round(self): """Verify a similarity can be rounded.""" self.assertEqual(0.42, round(Similarity(0.421), 2))
def test_float_not_equal(self): """Verify similarities and floats can be compared for inequality.""" self.assertNotEqual(0.12, Similarity(0.13))
def test_similarity_constructor(self): """Verify a default Similarity is created correctly.""" self.assertEqual(Similarity(0.0, 1.0), self.obj1.Similarity())
def test_add(self): """Verify two similarities can be added.""" self.assertEqual(Similarity(0.42), Similarity(0.4) + Similarity(0.02))
def test_iadd_with_number(self): """Verify a similarity can be added to by a number.""" similarity = Similarity(0.4) similarity += 0.02 self.assertEqual(Similarity(0.42), similarity)