1.1 --- a/test.py Mon Nov 22 23:50:03 2010 +0100
1.2 +++ b/test.py Tue Nov 23 00:22:52 2010 +0100
1.3 @@ -17,7 +17,7 @@
1.4 pass
1.5
1.6 try:
1.7 - for dirname in ("test_index", "test_index2", "test_index3"):
1.8 + for dirname in ("test_index", "test_index2", "test_index3", "test_indexT"):
1.9 for filename in os.listdir(dirname):
1.10 os.remove(os.path.join(dirname, filename))
1.11 os.rmdir(dirname)
1.12 @@ -534,6 +534,78 @@
1.13
1.14 index.close()
1.15
1.16 +docs2 = [
1.17 + ((1, 0), "The cat sat on the mat"),
1.18 + ((1, 2), "Every good boy deserves football"),
1.19 + ((13, 1), "One good turn deserves another"),
1.20 + ((14, 0), "Every man for himself"),
1.21 + ((14, 25), "Red sky at night shepherd's delight"),
1.22 + ((36, 12), "She sells sea shells on the sea shore")
1.23 + ]
1.24 +
1.25 +doc_tests2 = [
1.26 + ("Every", 2, [((1, 2), [(0, 0)]), ((14, 0), [(0, 0)])]),
1.27 + ("good", 2, [((1, 2), [(1, 6)]), ((13, 1), [(1, 4)])]),
1.28 + ("deserves", 2, [((1, 2), [(3, 15)]), ((13, 1), [(3, 14)])]),
1.29 + ("sea", 2, [((36, 12), [(2, 10), (6, 28)])])
1.30 + ]
1.31 +
1.32 +position_tests2 = [
1.33 + ("Every", (14, 0), [(0, 0)]),
1.34 + ("sea", (36, 12), [(2, 10), (6, 28)]),
1.35 + ("shells", (1, 0), None),
1.36 + ("shells", (37, 0), None)
1.37 + ]
1.38 +
1.39 +phrase_tests2 = [
1.40 + (["good", "boy"], [((1, 2), [(1, 6), (2, 11)])]),
1.41 + (["on", "the"], [((1, 0), [(3, 12), (4, 15)]), ((36, 12), [(4, 21), (5, 24)])]),
1.42 + (["sea", "shore"], [((36, 12), [(6, 28), (7, 32)])])
1.43 + ]
1.44 +
1.45 +index = Index("test_indexT", 3, 2, 3, 6)
1.46 +wi = index.get_writer()
1.47 +for docnum, text in docs2:
1.48 + doc = Document(docnum)
1.49 + offset = 0
1.50 + for position, term in enumerate(text.split()):
1.51 + doc.add_position(term, (position, offset))
1.52 + offset += len(term) + 1 # assume one space after the term
1.53 + doc.add_field(123, text)
1.54 + wi.add_document(doc)
1.55 +wi.close()
1.56 +
1.57 +rd = index.get_reader()
1.58 +
1.59 +print "- (Test searching.)"
1.60 +
1.61 +for term, frequency, doc_positions in doc_tests2:
1.62 + dp = list(rd.find_positions(term))
1.63 + print doc_positions == dp, doc_positions, dp
1.64 + fr = rd.get_frequency(term)
1.65 + print frequency == fr, frequency, fr
1.66 +
1.67 +print "- (Test fields.)"
1.68 +
1.69 +for docnum, text in docs2:
1.70 + df = dict(rd.get_fields(docnum))
1.71 + print df[123] == text, text, df[123]
1.72 +
1.73 +print "- (Test navigation.)"
1.74 +
1.75 +for term, docnum, positions in position_tests2:
1.76 + dp = rd.find_positions(term)
1.77 + pos = dp.from_document(docnum)
1.78 + print positions is None and pos is None or pos is not None and positions == list(pos), positions, pos
1.79 +
1.80 +print "- (Test phrases.)"
1.81 +
1.82 +for terms, results in phrase_tests2:
1.83 + res = list(rd.find_common_positions(terms))
1.84 + print results == res, results, res
1.85 +
1.86 +index.close()
1.87 +
1.88 print "- Test index updates."
1.89
1.90 index = Index("test_index")