diff --git a/.drone.yml b/.drone.yml index 50b299b..920f110 100644 --- a/.drone.yml +++ b/.drone.yml @@ -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 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..aae94a5 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,5 @@ +import pytest + +pytest_plugins = [ + "tries.simple_trie" +] diff --git a/tests/test_bytetrie_blackbox.py b/tests/test_bytetrie_blackbox.py new file mode 100644 index 0000000..19f84a8 --- /dev/null +++ b/tests/test_bytetrie_blackbox.py @@ -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" diff --git a/tests/tries/simple_trie.png b/tests/tries/simple_trie.png new file mode 100644 index 0000000..aac579d Binary files /dev/null and b/tests/tries/simple_trie.png differ diff --git a/tests/tries/simple_trie.py b/tests/tries/simple_trie.py new file mode 100644 index 0000000..1b48c86 --- /dev/null +++ b/tests/tries/simple_trie.py @@ -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