Skip to content

Commit

Permalink
PEP-8-ted 'test_trie'
Browse files Browse the repository at this point in the history
  • Loading branch information
superbobry committed Apr 21, 2015
1 parent 1a6c74c commit 8a66ca7
Showing 1 changed file with 42 additions and 31 deletions.
73 changes: 42 additions & 31 deletions tests/test_trie.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import, unicode_literals
import tempfile
import string
import random

import pickle
import random
import string
import tempfile

import pytest
import datrie
import pytest


def test_trie():
trie = datrie.Trie(string.printable)
assert trie.is_dirty() == True
assert trie.is_dirty()

assert 'foo' not in trie
assert 'Foo' not in trie
Expand All @@ -29,7 +32,8 @@ def test_trie():
assert trie['Foo'] == 10

with pytest.raises(KeyError):
x = trie['bar']
trie['bar']


def test_trie_invalid_alphabet():
t = datrie.Trie('abc')
Expand All @@ -46,6 +50,7 @@ def test_trie_invalid_alphabet():
with pytest.raises(KeyError):
t['e']


def test_trie_save_load():
fd, fname = tempfile.mkstemp()
trie = datrie.Trie(string.printable)
Expand All @@ -64,6 +69,7 @@ def test_trie_save_load():
assert trie2['foovar'] == 2
assert trie2['Foo'] == 'vasia'


def test_save_load_base():
fd, fname = tempfile.mkstemp()
trie = datrie.BaseTrie(alphabet=string.printable)
Expand Down Expand Up @@ -106,8 +112,6 @@ def test_trie_file_io():
assert len(trie2) == len(trie)




def test_trie_unicode():
# trie for lowercase Russian characters
trie = datrie.Trie(ranges=[('а', 'я')])
Expand All @@ -119,6 +123,7 @@ def test_trie_unicode():
assert trie['б'] == 2
assert trie['аб'] == 'vasia'


def test_trie_ascii():
trie = datrie.Trie(string.ascii_letters)
trie['x'] = 1
Expand All @@ -129,6 +134,7 @@ def test_trie_ascii():
assert trie['y'] == 'foo'
assert trie['xx'] == 2


def test_trie_items():
trie = datrie.Trie(string.ascii_lowercase)
trie['foo'] = 10
Expand All @@ -138,6 +144,7 @@ def test_trie_items():
assert trie.items() == [('bar', 'foo'), ('foo', 10), ('foobar', 30)]
assert trie.keys() == ['bar', 'foo', 'foobar']


def test_trie_suffixes():
trie = datrie.Trie(string.ascii_lowercase)

Expand Down Expand Up @@ -192,16 +199,23 @@ def test_trie_keys_prefix(self):
assert trie.keys('foobarzart') == ['foobarzartic']
assert trie.keys('foo') == ['foo', 'foobar', 'foobarzartic', 'foovar']
assert trie.keys('foobar') == ['foobar', 'foobarzartic']
assert trie.keys('') == ['bar', 'foo', 'foobar', 'foobarzartic', 'foovar']
assert trie.keys('') == [
'bar', 'foo', 'foobar', 'foobarzartic', 'foovar'
]
assert trie.keys('x') == []

def test_trie_items_prefix(self):
trie = self._trie()
assert trie.items('foobarz') == [('foobarzartic', None)]
assert trie.items('foobarzart') == [('foobarzartic', None)]
assert trie.items('foo') == [('foo', 10), ('foobar', 30), ('foobarzartic', None), ('foovar', 40)]
assert trie.items('foo') == [
('foo', 10), ('foobar', 30), ('foobarzartic', None), ('foovar', 40)
]
assert trie.items('foobar') == [('foobar', 30), ('foobarzartic', None)]
assert trie.items('') == [('bar', 20), ('foo', 10), ('foobar', 30), ('foobarzartic', None), ('foovar', 40)]
assert trie.items('') == [
('bar', 20), ('foo', 10), ('foobar', 30),
('foobarzartic', None), ('foovar', 40)
]
assert trie.items('x') == []

def test_trie_values_prefix(self):
Expand All @@ -216,7 +230,8 @@ def test_trie_values_prefix(self):

class TestPrefixSearch(object):

WORDS = ['producers', 'producersz', 'pr', 'pool', 'prepare', 'preview', 'prize', 'produce', 'producer', 'progress']
WORDS = ['producers', 'producersz', 'pr', 'pool', 'prepare', 'preview',
'prize', 'produce', 'producer', 'progress']

def _trie(self):
trie = datrie.Trie(string.ascii_lowercase)
Expand Down Expand Up @@ -259,13 +274,13 @@ def test_trie_prefixes(self):
assert values == [3, 8, 9, 1]

items = trie.prefix_items('producers')
assert items == [('pr', 3), ('produce', 8), ('producer', 9), ('producers', 1)]
assert items == [('pr', 3), ('produce', 8),
('producer', 9), ('producers', 1)]

assert trie.prefixes('vasia') == []
assert trie.prefix_values('vasia') == []
assert trie.prefix_items('vasia') == []


def test_has_keys_with_prefix(self):
trie = self._trie()

Expand All @@ -282,7 +297,6 @@ def test_has_keys_with_prefix(self):
assert not trie.has_keys_with_prefix('ops')
assert not trie.has_keys_with_prefix('progn')


def test_longest_prefix(self):
trie = self._trie()

Expand All @@ -293,9 +307,9 @@ def test_longest_prefix(self):
assert trie.longest_prefix('producers') == 'producers'
assert trie.longest_prefix('progressor') == 'progress'

assert trie.longest_prefix('paol', default=None) == None
assert trie.longest_prefix('p', default=None) == None
assert trie.longest_prefix('z', default=None) == None
assert trie.longest_prefix('paol', default=None) is None
assert trie.longest_prefix('p', default=None) is None
assert trie.longest_prefix('z', default=None) is None

with pytest.raises(KeyError):
trie.longest_prefix('z')
Expand All @@ -316,9 +330,10 @@ def test_longest_prefix_item(self):
assert trie.longest_prefix_item('producers') == ('producers', 1)
assert trie.longest_prefix_item('progressor') == ('progress', 10)

assert trie.longest_prefix_item('paol', default=(None, None)) == (None, None)
assert trie.longest_prefix_item('p', default=(None, None)) == (None, None)
assert trie.longest_prefix_item('z', default=(None, None)) == (None, None)
dummy = (None, None)
assert trie.longest_prefix_item('paol', default=dummy) == dummy
assert trie.longest_prefix_item('p', default=dummy) == dummy
assert trie.longest_prefix_item('z', default=dummy) == dummy

with pytest.raises(KeyError):
trie.longest_prefix_item('z')
Expand All @@ -333,20 +348,19 @@ def test_longest_prefix_value(self):
assert trie.longest_prefix_value('producers') == 1
assert trie.longest_prefix_value('progressor') == 10

assert trie.longest_prefix_value('paol', default=None) == None
assert trie.longest_prefix_value('p', default=None) == None
assert trie.longest_prefix_value('z', default=None) == None
assert trie.longest_prefix_value('paol', default=None) is None
assert trie.longest_prefix_value('p', default=None) is None
assert trie.longest_prefix_value('z', default=None) is None

with pytest.raises(KeyError):
trie.longest_prefix_value('z')



def test_trie_fuzzy():
russian = 'абвгдеёжзиклмнопрстуфхцчъыьэюя'
alphabet = russian.upper() + string.ascii_lowercase
words = list(set([
"".join([random.choice(alphabet) for x in range(random.randint(2,10))])
"".join(random.choice(alphabet) for x in range(random.randint(8, 16)))
for y in range(1000)
]))

Expand All @@ -357,12 +371,9 @@ def test_trie_fuzzy():
for index, word in enumerated_words:
trie[word] = index

assert len(trie) == len(words)

random.shuffle(enumerated_words)
for index, word in enumerated_words:
assert word in trie, word
assert trie[word] == index, (word, index)


#def test_large_trie():
# zf = zipfile.ZipFile('words100k.txt.zip')
# words = zf.open(zf.namelist()[0]).read().decode('utf8').splitlines()

0 comments on commit 8a66ca7

Please sign in to comment.