def test_dump_load_small_index(): index = InvertedIndex() index.inverted_index = _SMALL_INDEX small_index_path = 'small.index' with open(small_index_path, 'wb') as fd: index.dump(fd) with open(small_index_path, 'rb') as fd: index.load(fd) assert index.inverted_index == _SMALL_INDEX, 'dumped and loaded index not the same'
def test_init_empty_inverted_index_do_not_raise_exception(): word_to_docs_mapping = {} inverted_index = InvertedIndex(word_to_docs_mapping) storage_policy = JsonStoragePolicy() inverted_index.dump(INDEX_TMP_PATH, storage_policy=storage_policy) loaded_inverted_index = inverted_index.load(INDEX_TMP_PATH, storage_policy=storage_policy) assert word_to_docs_mapping == word_to_docs_mapping storage_policy = PickleStoragePolicy() inverted_index.dump(INDEX_TMP_PATH, storage_policy=storage_policy) loaded_inverted_index = inverted_index.load(INDEX_TMP_PATH, storage_policy=storage_policy) assert word_to_docs_mapping == word_to_docs_mapping
def test_dump_index(mock_file): """ Check if we can dump index. """ my_index = InvertedIndex() my_index.build(mock_file) my_index.dump('my_Test.index') expected_calls = [ "call('my_Test.index', 'wb')", "call().write(b'\\x03\\x00\\x00\\x00')", "call().write(b'\\x00\\x02')", "call().write(b'me')", "call().write(b'\\x00\\x01')", "call().write(b'\\x00\\x04')", "call().write(b'test')", "call().write(b'\\x00\\x05')", "call().write(b'first')" ] all_calls = [str(call) for call in mock_file.mock_calls] for call in expected_calls: assert call in all_calls
def test_inverted_index_dump(wiki_docs): index_dict = build_inverted_index(wiki_docs) inv_index = InvertedIndex(index_dict) inv_index.dump('inv_index.dat') assert True
def test_dump_small_index(): index = InvertedIndex() index.inverted_index = _SMALL_INDEX small_index_path = 'small.index' index.dump(fd=open(small_index_path, 'wb')) assert os.path.getsize(small_index_path) > 0, 'build index with zero size'