def test_get_window_two_result(self): result = self.x.multiple_search_lim('тестов', 0, 1) res1 = result['test_window_three.txt'][0] res2 = result['test_window_three.txt'][1] t = [Position(18, 24, 0), Position(18, 24, 1)] ideal = {'test_window_three.txt': t} self.assertEqual(result, ideal)
def test_get_window_two_result(self): result = self.x.find_supplemented_window_lim('тестов', 1, 0, 1, [(0, 2)]) res1 = result['test_window_three.txt'][0] res2 = result['test_window_three.txt'][1] result1 = self.x.find_supplemented_window_lim_v2( 'тестов', 1, 0, 1, [(0, 2)]) res11 = result1['test_window_three.txt'][0] res21 = result1['test_window_three.txt'][1] win1 = TokenWindow(self.strr3.replace('\n', ''), [Position(18, 24, 0)], 0, 25) win2 = TokenWindow(self.strr4, [Position(18, 24, 1)], 0, 25) ideal = {'test_window_three.txt': [win1, win2]} self.assertEqual(res1.allString, win1.allString) self.assertEqual(res1, win1) self.assertEqual(res2.allString, win2.allString) self.assertEqual(res2, win2) self.assertEqual(result, ideal) self.assertEqual(res11.allString, win1.allString) self.assertEqual(res11, win1) self.assertEqual(res21.allString, win2.allString) self.assertEqual(res21, win2) self.assertEqual(result1, ideal)
def test_get_window_two_result3(self): result = self.x.multiple_search_lim_gen('тестов', 0, 1) t = self.x.merge_and_sort_lists( [[Position(18, 24, 0), Position(18, 24, 1)]]) ideal = {'test_window_three.txt': t} self.assertEqual(list(result['test_window_three.txt']), list(ideal['test_window_three.txt']))
def test_get_window_two_word(self): result = self.x.find_supplemented_window_lim_v3( 'Мы работы', 2, 0, 1, [(0, 2)]) res = result['test_window_two.txt'][0] win = TokenWindow( self.strr2, [Position(0, 2, 0), Position(32, 38, 0)], 0, 49) ideal = {'test_window_two.txt': [win]} self.assertEqual(res.allString, win.allString) self.assertEqual(res, win) self.assertEqual(result, ideal)
def test_two_identical_words(self): test = open("text.txt", 'w' ) test.write("my my") test.close() self.indexer.index("text.txt") words1 = dict(shelve.open("database")) words2 = { "my":{"text.txt": [Position(0, 2), Position(3, 5)] }} self.assertEqual(words1, words2)
def test_indexing(self): """test if the program is working correctly when indexing a single file """ f = open('test.txt', 'w') f.write('this is a test test') f.close() self.i.indexing('test.txt') db_dict = dict(shelve.open('dbase')) dictionary = { 'this': {'test.txt': [Position(0, 4)]}, 'is': {'test.txt': [Position(5, 7)]}, 'a': {'test.txt': [Position(8, 9)]}, 'test': {'test.txt': [Position(10, 14), Position(15, 19)]} } self.assertEqual(db_dict, dictionary)
def test_two_files(self): test = open("text.txt", 'w' ) test.write("test") test.close() test = open("text1.txt", 'w' ) test.write("my my") test.close() self.indexer.indexing("text.txt") self.indexer = Indexer('database') self.indexer.indexing("text1.txt") words1 = dict(shelve.open("database")) words2 = { "my":{"text.txt": [Position(0, 2), Position(3, 5)]}, "test":{"text.txt": [Position(0, 4)] }} self.assertEqual(words1, words2)
def test_get_window_begin(self): result = self.x.find_window('sun', 1) res = result['test_window_one.txt'][0] win = TokenWindow(self.strr, [Position(0, 3, 0)], 0, 10) ideal = {'test_window_one.txt': [win]} self.assertEqual(res.allString, win.allString) self.assertEqual(res, win) self.assertEqual(result, ideal)
def test_one_word(self): file = open('test.txt', 'w') file.write('indexator') file.close() self.indexator.indextie('test.txt') data_dict = dict(shelve.open('database')) dictionary = {'indexator': {'test.txt': [Position(0, 9)]}} self.assertEqual(data_dict, dictionary)
def test_many_files(self): file_one = open('testfile_1.txt', 'w') file_one.write(' Ф 12 !!! @ # Alina is a student)))') file_one.close() file_two = open('testfile_2.txt', 'w') file_two.write('Alina') file_two.close() self.indexator.get_index('testfile_1.txt') self.indexator.get_index('testfile_2.txt') del self.indexator base_dict = dict(shelve.open('database')) cool_result = { 'Ф': { 'testfile_1.txt': [Position(1, 2)] }, '12': { 'testfile_1.txt': [Position(3, 5)] }, 'Alina': { 'testfile_1.txt': [Position(14, 19)], 'testfile_2.txt': [Position(0, 5)] }, 'is': { 'testfile_1.txt': [Position(20, 22)] }, 'a': { 'testfile_1.txt': [Position(23, 24)] }, 'student': { 'testfile_1.txt': [Position(25, 32)] } } self.assertEqual(base_dict, cool_result) os.remove('testfile_1.txt') os.remove('testfile_2.txt')
def test_get_window_simple2(self): result = self.x.find_supplemented_window_lim_v3( 'нашу', 2, 0, 1, [(0, 1)]) res = result['test_window_two.txt'][0] win = TokenWindow(self.strr2, [Position(13, 17, 0)], 0, 49) ideal = {'test_window_two.txt': [win]} self.assertEqual(res.allString, win.allString) self.assertEqual(res, win) self.assertEqual(result, ideal)
def test_get_window_simple_two_line(self): result = self.x.find_supplemented_window_lim_v3( 'Вторая', 1, 0, 1, [(0, 1)]) res = result['test_window_three.txt'][0] win = TokenWindow(self.strr4, [Position(0, 6, 1)], 0, 25) ideal = {'test_window_three.txt': [win]} self.assertEqual(res.allString, win.allString) self.assertEqual(res, win) self.assertEqual(result, ideal)
def test_get_window_end(self): result = self.x.find_supplemented_window_lim_v3( 'good', 1, 0, 1, [(0, 1)]) res = result['test_window_one.txt'][0] win = TokenWindow(self.strr, [Position(46, 50, 0)], 0, 50) ideal = {'test_window_one.txt': [win]} self.assertEqual(res.allString, win.allString) self.assertEqual(res, win) self.assertEqual(result, ideal)
def test_many_words(self): file = open('test.txt', 'w') file.write('testing my indexator') file.close() self.indexator.indextie('test.txt') data_dict = dict(shelve.open('database')) dictionary = { 'testing': { 'test.txt': [Position(0, 7)] }, 'my': { 'test.txt': [Position(8, 10)] }, 'indexator': { 'test.txt': [Position(11, 20)] } } self.assertEqual(data_dict, dictionary)
def test_get_window_simple(self): result = self.x.find_window('tree', 2) res = result['test_window_one.txt'][0] win = TokenWindow(self.strr, [Position(11, 15, 0)], 0, 27) ideal = {'test_window_one.txt': [win]} self.assertEqual(res.allString, win.allString) self.assertEqual(res, win) self.assertEqual(result, ideal)
def test_get_window_two_result3(self): result = self.x.find_supplemented_window_lim_v3( 'тестов', 1, 0, 1, [(1, 2)]) res1 = result['test_window_three.txt'][0] win1 = TokenWindow(self.strr4.replace('\n', ''), [Position(18, 24, 1)], 0, 25) ideal = {'test_window_three.txt': [win1]} self.assertEqual(res1.allString, win1.allString) self.assertEqual(res1, win1) self.assertEqual(result, ideal)
def test_one_word(self): test = open("text.txt", 'w' ) test.write("programming") test.close() self.indexer.indexing("text.txt") words1 = dict(shelve.open("database")) words2 = { "programming":{"text.txt": [Position(0, 10)] }} self.assertEqual(words1, words2)
def test_two_files(self): file1 = open('test1.txt', 'w') file1.write('file number one') file1.close() self.indexator.indextie('test1.txt') test2 = open('test2.txt', 'w') test2.write('file number two') test2.close() self.indexator.indextie('test2.txt') data_dict = dict(shelve.open('database')) dictionary = { 'file': { 'test1.txt': [Position(0, 4)], 'test2.txt': [Position(0, 4)] }, 'number': { 'test1.txt': [Position(5, 11)], 'test2.txt': [Position(5, 11)] }, 'one': { 'test1.txt': [Position(12, 15)] }, 'two': { 'test2.txt': [Position(12, 15)] } } self.assertEqual(data_dict, dictionary)
def test_database_one_token(self): test_file = open('testfile.txt', 'w') test_file.write('Alina') test_file.close() self.indexator.get_index('testfile.txt') del self.indexator base_dict = dict(shelve.open('database')) cool_result = { 'Alina': { 'testfile.txt': [Position(0, 5)] } } # perfect dictionary self.assertEqual(base_dict, cool_result) os.remove('testfile.txt')
def test_database_continued(self): file = open('test.txt', 'w') file.write('Baikal') file.close() self.x.prescribe_index('test.txt') file = open('test2.txt', 'w') file.write('Baikal is a lake') file.close() self.x.prescribe_index('test2.txt') files = os.listdir(path=".") flag = False for f in files: if f.startswith('database.'): flag = True elif f.startswith('database'): if f == 'database': flag = True self.assertEqual(flag, True) db_dict = dict(self.x.db) standart_result = { 'Baikal': { 'test2.txt': [Position(0, 6, 0)], 'test.txt': [Position(0, 6, 0)] }, 'is': { 'test2.txt': [Position(7, 9, 0)] }, 'a': { 'test2.txt': [Position(10, 11, 0)] }, 'lake': { 'test2.txt': [Position(12, 16, 0)] } } self.assertEqual(db_dict, standart_result) os.remove('test2.txt') os.remove('test.txt')
def test_indexing_multiple_files(self): """test if the program is working correctly when indexing multiple files """ p = open('test.txt', 'w') p.write('is this a test?') p.close() self.i.indexing('test.txt') s = open('ts.txt', 'w') s.write('yes, it is a test') s.close() self.i.indexing('ts.txt') db_dict = dict(shelve.open('dbase')) dictionary = { 'is': {'test.txt': [Position(0, 2)], 'ts.txt': [Position(8, 10)]}, 'this': {'test.txt': [Position(3, 7)]}, 'a': {'test.txt': [Position(8, 9)], 'ts.txt': [Position(11, 12)]}, 'test': {'test.txt': [Position(10, 14)], 'ts.txt': [Position(13, 17)]}, 'yes': {'ts.txt': [Position(0, 3)]}, 'it': {'ts.txt': [Position(5, 7)]} } self.assertEqual(db_dict, dictionary)
def test_get_window_simple(self): result = self.x.find_supplemented_window_lim('tree', 2, 0, 1, [(0, 1)]) res = result['test_window_one.txt'][0] result1 = self.x.find_supplemented_window_lim_v2( 'tree', 2, 0, 1, [(0, 1)]) res1 = result1['test_window_one.txt'][0] win = TokenWindow(self.strr, [Position(11, 15, 0)], 0, 50) ideal = {'test_window_one.txt': [win]} self.assertEqual(res.allString, win.allString) self.assertEqual(res, win) self.assertEqual(result, ideal) self.assertEqual(res1.allString, win.allString) self.assertEqual(res1, win) self.assertEqual(result1, ideal)
def test_database_created(self): file = open('test.txt', 'w') file.write('Baikal') file.close() self.x.prescribe_index('test.txt') ''' The method listdir() returns a list containing the names of the entries in the directory given by path. ''' files = os.listdir(path=".") flag = False for f in files: if f.startswith('database.'): flag = True elif f.startswith('database'): if f == 'database': flag = True self.assertEqual(flag, True) db_dict = dict(self.x.db) standart_result = {'Baikal': {'test.txt': [Position(0, 6, 0)]}} self.assertEqual(db_dict, standart_result) os.remove('test.txt')
import unittest from searchengine import SearchEngine from indexer import Position import shelve import os file1 = " The Earth is round" file2 = " The sun is round" idealdict = { 'sun': { 'file2.txt': [Position(5, 8, 0)] }, 'The': { 'file1.txt': [Position(1, 4, 0)], 'file2.txt': [Position(1, 4, 0)] }, 'Earth': { 'file1.txt': [Position(5, 10, 0)] }, 'round': { 'file1.txt': [Position(14, 19, 0)], 'file2.txt': [Position(12, 17, 0)] }, 'is': { 'file1.txt': [Position(11, 13, 0)], 'file2.txt': [(9, 11, 0)] } } class TestMySearchEngine(unittest.TestCase):
def test_get_window_simple(self): result = self.x.multiple_search_lim('tree', 0, 1) res = result['test_window_one.txt'][0] t = [Position(11, 15, 0)] ideal = {'test_window_one.txt': t} self.assertEqual(result, ideal)
def test_get_window_end(self): result = self.x.multiple_search_lim('good', 0, 1) res = result['test_window_one.txt'][0] t = [Position(46, 50, 0)] ideal = {'test_window_one.txt': t} self.assertEqual(result, ideal)
def test_get_window_simple2(self): result = self.x.multiple_search_lim('нашу', 0, 1) res = result['test_window_two.txt'][0] t = [Position(13, 17, 0)] ideal = {'test_window_two.txt': t} self.assertEqual(result, ideal)
def test_get_window_simple_two_line(self): result = self.x.multiple_search_lim('Вторая', 0, 1) res = result['test_window_three.txt'][0] t = [Position(0, 6, 1)] ideal = {'test_window_three.txt': t} self.assertEqual(result, ideal)
def test_search_two_tokens(self): result = self.x.multiple_search('The sun') self.assertIsInstance(result, dict) self.assertEqual(result, {'file2.txt': [Position(1, 4, 0), Position(5, 8, 0)]})
import unittest from searchengine import SearchEngine, TokenWindow from indexer import Position import os idealdict = { 'sun': { 'test_window_one.txt': [Position(0, 3, 0)] }, 'window': { 'test_window_one.txt': [Position(4, 10, 0)] }, 'tree': { 'test_window_one.txt': [Position(11, 15, 0)] }, 'apple': { 'test_window_one.txt': [Position(16, 21, 0)] }, 'juse': { 'test_window_one.txt': [Position(23, 27, 0)] }, 'border': { 'test_window_one.txt': [Position(28, 34, 0)] }, 'films': { 'test_window_one.txt': [Position(35, 40, 0)] }, '23': { 'test_window_one.txt': [Position(41, 43, 0)] }, '45': {