def test_default_get_words_from_text(self): with TempFiles(1) as files: print_lines_to_file(['Hi baby', 'How are', 'you?'], files[0].name) correct_result = {'hi', 'baby', 'how', 'are', 'you'} with open(files[0].name, 'r') as f: result = dic_cr.get_words_from_text(f) self.assertSetEqual(correct_result, result)
def test_default_get_words_from_dict(self): with TempFiles(1) as files: print_lines_to_file(['apple', 'bobby', 'hello'], files[0].name) correct_result = {'apple', 'bobby', 'hello'} with open(files[0].name, 'r') as f: result = dic_cr.get_words_from_dict(f) self.assertSetEqual(correct_result, result)
def test_create_with_zero_length_text(self): with TempFiles(2) as files: output = files[1] args = prepare_args(ArgsTypes.CREATE, files) args.encoding = 'utf8' dic_cr.create(args) correct_result = [] assert_file_lines_with_list(self, output.name, correct_result)
def test_append_with_zero_length_dict(self): with TempFiles(2) as files: first_dict = files[0] second_dict = files[1] print_lines_to_file(['bobby'], second_dict.name) args = prepare_args(ArgsTypes.APPEND, files) dic_cr.append(args) correct_result = ['bobby'] assert_file_lines_with_list(self, first_dict.name, correct_result)
def test_merge_with_zero_length_dict(self): with TempFiles(3) as files: first_dict = files[0] print_lines_to_file(['apple', 'hello'], first_dict.name) third_dict = files[2] args = prepare_args(ArgsTypes.MERGE, files) dic_cr.merge(args) correct_result = ['apple', 'hello'] assert_file_lines_with_list(self, third_dict.name, correct_result)
def test_get_words_from_text_with_numbers(self): with TempFiles(1) as files: print_lines_to_file( ['Прив8ет', 'как', 'дела?', '8 9123 8-800-555-35-35'], files[0].name) correct_result = {'как', 'дела'} with open(files[0].name, 'r', encoding='utf8') as f: result = dic_cr.get_words_from_text(f) self.assertSetEqual(correct_result, result)
def is_dictionary(filename): dict_hash = md5(filename) with TempFiles(1) as files: args = Args args.first_dict = files[0].name args.second_dict = filename dic_cr.append(args) new_dict_hash = md5(files[0].name) return new_dict_hash == dict_hash
def test_add_with_wrong_word(self): with TempFiles(1) as files: dictionary = files[0] print_lines_to_file(['apple', 'hello'], dictionary.name) args = Args args.dict = dictionary.name args.word = 'lsakdf====--://' dic_cr.add(args) correct_result = ['apple', 'hello'] assert_file_lines_with_list(self, dictionary.name, correct_result)
def test_default_add(self): with TempFiles(1) as files: dictionary = files[0] print_lines_to_file(['apple', 'hello'], dictionary.name) args = Args args.dict = dictionary.name args.word = 'mango' dic_cr.add(args) correct_result = sorted(['apple', 'mango', 'hello']) assert_file_lines_with_list(self, dictionary.name, correct_result)
def test_get_words_from_text_with_encoding(self): with TempFiles(1) as files: with open(files[0].name, 'w', encoding='cp1251') as f: print('Привет', file=f) print('как', file=f) print('дела?', file=f) correct_result = {'привет', 'как', 'дела'} with open(files[0].name, 'r', encoding='cp1251') as f: result = dic_cr.get_words_from_text(f) self.assertSetEqual(correct_result, result)
def test_default_load_dictionary(self): with TempFiles(1) as files: tmp = files[0] with open(tmp.name, 'w') as f: print('apple', file=f) print('help', file=f) trie = ldc.load_dictionary(f.name) self.assertTrue(ldc.is_word_in_dictionary(trie, 'apple')) self.assertTrue(ldc.is_word_in_dictionary(trie, 'help')) self.assertFalse(ldc.is_word_in_dictionary(trie, 'hello'))
def test_default_get_words_from_text_with_line_breaks(self): with TempFiles(1) as files: text = files[0] print_lines_to_file(['Hey Boys how are you do-', 'ing today?'], text.name) correct_result = { 'hey', 'boys', 'how', 'are', 'you', 'doing', 'today' } with open(files[0].name, 'r', encoding='utf8') as f: result = dic_cr.get_words_from_text(f) self.assertSetEqual(correct_result, result)
def test_default_create(self): with TempFiles(2) as files: text = files[0] print_lines_to_file(["This is test text.", 'I like python!'], text.name) output = files[1] args = prepare_args(ArgsTypes.CREATE, files) args.encoding = 'utf8' dic_cr.create(args) correct_result = sorted( ['this', 'is', 'test', 'text', 'i', 'like', 'python']) assert_file_lines_with_list(self, output.name, correct_result)
def test_get_words_from_text_with_dashes(self): with TempFiles(1) as files: text = files[0] print_lines_to_file([ 'Hey Boys bla-bla how are you do-', 'ing today? I fee-', 'l yourself very good. Hey -' ], text.name) correct_result = { 'bla-bla', 'hey', 'boys', 'how', 'are', 'you', 'doing', 'today', 'i', 'feel', 'yourself', 'very', 'good' } with open(files[0].name, 'r', encoding='utf8') as f: result = dic_cr.get_words_from_text(f) self.assertSetEqual(correct_result, result)
def test_get_words_from_text_with_no_text(self): with TempFiles(1) as files: correct_result = set() with open(files[0].name, 'r') as f: result = dic_cr.get_words_from_text(f) self.assertSetEqual(correct_result, result)
def test_load_dictionary_without_words(self): with TempFiles(1) as files: tmp = files[0] trie = ldc.load_dictionary(tmp.name) self.assertFalse(ldc.is_word_in_dictionary(trie, 'apple')) self.assertFalse(ldc.is_word_in_dictionary(trie, 'help'))