Add simple_trie fixture and blackbox tests
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Armin Friedl 2020-10-10 15:39:34 +02:00
parent b491ce97f0
commit 6d39e15532
5 changed files with 83 additions and 2 deletions

View file

@ -7,8 +7,7 @@ steps:
image: python:3
commands:
- pip install mypy pytest
- mypy bytetrie/bytetrie.py
- mypy bytetrie/util.py
- mypy bytetrie/*.py
- python -m pytest
- name: publish

5
tests/conftest.py Normal file
View file

@ -0,0 +1,5 @@
import pytest
pytest_plugins = [
"tries.simple_trie"
]

View file

@ -0,0 +1,49 @@
import pytest
import sys
import os
sys.path.append("tests/tries/")
from bytetrie import ByteTrie
import simple_trie
def test_find_all(simple_trie):
r = simple_trie.find(b"A")
ls = [n.value() for n in r]
expected = ["A", "AA", "AACDEGG", "AACDEF", "AACDEH", "AB", "ABCDE"]
not_expected = ["AACDE"]
for e in expected:
assert e in ls
for ne in not_expected:
assert not ne in ls
def test_find_single_terminal(simple_trie):
r = simple_trie.find(b"AACDEH")
assert len(r) == 1
assert r[0].value() == "AACDEH"
assert r[0].key() == b"AACDEH"
def test_unknown_prefix_empty(simple_trie):
r = simple_trie.find(b"AAD")
assert len(r) == 0
def test_partial_prefix_find_subnodes(simple_trie):
r = simple_trie.find(b"AACD")
ls = [n.value() for n in r]
expected = ["AACDEGG", "AACDEF", "AACDEH"]
not_expected = ["A", "AA", "AB", "ABCDE", "AACDE"]
assert len(r) == 3
for e in expected:
assert e in ls
for ne in not_expected:
assert not ne in ls
def test_partial_prefix_find_terminal(simple_trie):
r = simple_trie.find(b"ABC")
assert len(r) == 1
assert r[0].key() == b"ABCDE"
assert r[0].value() == "ABCDE"

BIN
tests/tries/simple_trie.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

View file

@ -0,0 +1,28 @@
"""
Create a simple ByteTrie fixture for testing basic functionality. Without edge
cases. Single-valued.
See simple_trie.png
"""
import pytest
from bytetrie import ByteTrie
def insert(trie):
""" Shall only be used to insert strings """
t = trie
def _insert(*vals):
for val in vals:
t.insert(val.encode('utf-8'), val)
return _insert
@pytest.fixture
def simple_trie():
t = ByteTrie()
ins = insert(t)
ins("A")
ins("AA", "AB")
ins("ABCDE")
ins("AACDEF", "AACDEGG", "AACDEH")
return t