def test_build_with_file(wordstore): rodents = wordstore(['squirrel', 'rabbit', 'capybara']) mustelinae = wordstore(['wolverine', 'otter', 'mink']) birds = wordstore(['osprey', 'pigeon', 'wren']) stores = [rodents, mustelinae, birds] gold = rodents + mustelinae + birds lines = ['first', 'second', 'third'] with mock.patch( 'passphrase.build.scrape', side_effect=stores) as mock_scrape, tempfile.NamedTemporaryFile( 'w') as f: # Create sources. for line in lines: f.write(line) f.write('\n') f.seek(0) json_str = build(f.name) words = WordStore.from_json(json_str) assert words.store == gold.store assert mock_scrape.call_count == len(stores)
def test_build_with_url(wordstore): gold_wordstore = wordstore(['llama', 'rabbit', 'capybara']) with mock.patch('passphrase.build.scrape', return_value=gold_wordstore): words_json = build('http://nowhere.in.particular') words = WordStore.from_json(words_json) assert words.store == gold_wordstore.store
def load_database(src: str) -> WordStore: """Load WordStore from file path. Args: src: file path. Returns: word store. """ src = os.path.abspath(os.path.expanduser(src)) with open(src, 'r') as f: data = f.read() words = WordStore.from_json(data) return words
def test_populated(wordstore): words = wordstore(['something', 'in', 'the', 'way', 'she', 'moves']) json_str = words.to_json() reconstructed_words = WordStore.from_json(json_str) assert reconstructed_words.store == words.store
def test_empty(): words = WordStore() json_str = words.to_json() reconstructed_words = WordStore.from_json(json_str) assert reconstructed_words.store == words.store