def test_one(self): results = SearchResults(2) results._add_hit(0, 9, 0.1) results._add_hit(1, 8, 0.8) results.reorder_all("reverse") self.assertListEquals(results[0], [(9, 0.1)]) self.assertListEquals(results[1], [(8, 0.8)])
def test_size_2(self): results = SearchResults(5) results._add_hit(1, 5, 0.2) results._add_hit(1, 6, 0.4) self.assertListEquals(results[1], [(5, 0.2), (6, 0.4)]) results.reorder_all("increasing-score") self.assertListEquals(results[1], [(5, 0.2), (6, 0.4)]) results.reorder_all("decreasing-score") self.assertListEquals(results[1], [(6, 0.4), (5, 0.2)])
def test_two(self): results = SearchResults(2) results._add_hit(0, 1, 0.1) results._add_hit(0, 2, 0.8) results._add_hit(1, 2, 0.8) results._add_hit(1, 3, 0.6) results.reorder_all("reverse") self.assertListEquals(results[0], [(2, 0.8), (1, 0.1)]) self.assertListEquals(results[1], [(3, 0.6), (2, 0.8)])
def test_two(self): results = SearchResults(2) results._add_hit(0, 1, 0.1) results._add_hit(0, 2, 0.8) results._add_hit(1, 2, 0.8) results._add_hit(1, 3, 0.6) results.reorder_all("move-closest-first") self.assertListEquals(results[0], [(2, 0.8), (1, 0.1)]) self.assertListEquals(results[1], [(2, 0.8), (3, 0.6)])
def test_regression_error_where_duplicate_indices_did_not_sort_correctly(self): # The id case doesn't happen in real code, since duplicate # indices are not possible. However, I suspect that the real # issue is with duplicate primary keys, so duplicate scores # might trigger the same problem. It's easiest to test with # indices. results = SearchResults(1) ids = range(5) * 2 for id in ids: results._add_hit(0, id, id/10.0) results.reorder_all("increasing-index") self.assertListEquals(results[0].get_indices(), sorted(ids))
def test_regression_error_where_duplicate_indices_did_not_sort_correctly( self): # The id case doesn't happen in real code, since duplicate # indices are not possible. However, I suspect that the real # issue is with duplicate primary keys, so duplicate scores # might trigger the same problem. It's easiest to test with # indices. results = SearchResults(1) ids = range(5) * 2 for id in ids: results._add_hit(0, id, id / 10.0) results.reorder_all("increasing-index") self.assertListEquals(results[0].get_indices(), sorted(ids))
def test_index_as_secondary_sort(self): # Timsort preserves input order. test_random_values uses # sequentially ordered indicies so can't tell the difference # between input order and index order. Here I reverse the # order so I can really test tie-breaking. for name in ("increasing-score", "decreasing-score", "increasing-index", "decreasing-index"): results = SearchResults(1) expected = [] for i in range(300): score = random_scores[i] results._add_hit(0, 400-i, score) expected.append((400-i, score)) results.reorder_all(name) expected.sort(key = _get_sort_key[name]) self.assertListEquals(results[0], expected, "error in %s (300)" % (name,))
def test_index_as_secondary_sort(self): # Timsort preserves input order. test_random_values uses # sequentially ordered indicies so can't tell the difference # between input order and index order. Here I reverse the # order so I can really test tie-breaking. for name in ("increasing-score", "decreasing-score", "increasing-index", "decreasing-index"): results = SearchResults(1) expected = [] for i in range(300): score = random_scores[i] results._add_hit(0, 400 - i, score) expected.append((400 - i, score)) results.reorder_all(name) expected.sort(key=_get_sort_key[name]) self.assertListEquals(results[0], expected, "error in %s (300)" % (name, ))
def test_random_values(self): # The underlying timsort does merge sorts of 64 element # blocks. Hence some of the code is not exercised unless the # input is at least 128 elements long. for size in (3, 5, 10, 20, 70, 100, 400): results = SearchResults(1) expected = [] for i in range(size): score = random_scores[i] expected.append((i, score)) results._add_hit(0, i, score) self.assertListEquals(results[0], expected) for name in ("increasing-score", "decreasing-score", "increasing-index", "decreasing-index"): results.reorder_all(name) expected.sort(key = _get_sort_key[name]) self.assertListEquals(results[0], expected, "error in %s:%d" % (name, size))
def test_random_values(self): # The underlying timsort does merge sorts of 64 element # blocks. Hence some of the code is not exercised unless the # input is at least 128 elements long. for size in (3, 5, 10, 20, 70, 100, 400): results = SearchResults(1) expected = [] for i in range(size): score = random_scores[i] expected.append((i, score)) results._add_hit(0, i, score) self.assertListEquals(results[0], expected) for name in ("increasing-score", "decreasing-score", "increasing-index", "decreasing-index"): results.reorder_all(name) expected.sort(key=_get_sort_key[name]) self.assertListEquals(results[0], expected, "error in %s:%d" % (name, size))
def test_three(self): results = SearchResults(3) results._add_hit(0, 1, 0.1) results._add_hit(0, 2, 0.8) results._add_hit(0, 3, 0.6) results._add_hit(1, 12, 0.8) results._add_hit(1, 22, 0.1) results._add_hit(1, 32, 0.6) results._add_hit(2, 12, 0.6) results._add_hit(2, 32, 0.1) results._add_hit(2, 22, 0.8) results.reorder_all("reverse") self.assertListEquals(results[0], [(3, 0.6), (2, 0.8), (1, 0.1)]) self.assertListEquals(results[1], [(32, 0.6), (22, 0.1), (12, 0.8)]) self.assertListEquals(results[2], [(22, 0.8), (32, 0.1), (12, 0.6)])
def test_bad_order(self): results = SearchResults(5) with self.assertRaisesRegexp(ValueError, "Unknown sort order"): results.reorder_all("xyzzy")
def test_size_0(self): results = SearchResults(5) results.reorder_all() self.assertListEquals(results[0], [])
def test_size_1(self): results = SearchResults(5) results._add_hit(1, 5, 0.2) results.reorder_all() self.assertListEquals(results[1], [(5, 0.2)])
def test_empty(self): results = SearchResults(2) results.reorder_all("reverse") self.assertEquals(len(results), 2) self.assertEquals(len(results[0]), 0) self.assertEquals(len(results[1]), 0)
def test_bad_order(self): results = SearchResults(5) with self.assertRaisesRegexp(ValueError, "Unknown ordering"): results.reorder_all("xyzzy")
def test_empty(self): results = SearchResults(2) results.reorder_all("move-closest-first") self.assertEquals(len(results), 2) self.assertEquals(len(results[0]), 0) self.assertEquals(len(results[1]), 0)
def test_default_ordering_2(self): results = SearchResults(5) results._add_hit(1, 5, 0.2) results._add_hit(1, 6, 0.4) results.reorder_all() self.assertListEquals(results[1], [(6, 0.4), (5, 0.2)])