Skip to content

Commit

Permalink
tests: modernize
Browse files Browse the repository at this point in the history
  • Loading branch information
BoboTiG committed Mar 25, 2023
1 parent 082599f commit 32f34e5
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 71 deletions.
2 changes: 0 additions & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
20 changes: 7 additions & 13 deletions tests/test_binary_trie.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import, unicode_literals

import pickle
from uuid import uuid4

Expand Down Expand Up @@ -83,7 +79,7 @@ def test_get(keys):
def test_saveload(tmpdir_factory, keys):
trie = marisa_trie.BinaryTrie(keys)

dirname = str(uuid4()) + "_"
dirname = f"{uuid4()}_"
path = str(tmpdir_factory.mktemp(dirname).join("trie.bin"))
trie.save(path)

Expand All @@ -98,7 +94,7 @@ def test_saveload(tmpdir_factory, keys):
def test_mmap(tmpdir_factory, keys):
trie = marisa_trie.BinaryTrie(keys)

dirname = str(uuid4()) + "_"
dirname = f"{uuid4()}_"
path = str(tmpdir_factory.mktemp(dirname).join("trie.bin"))
trie.save(path)

Expand Down Expand Up @@ -206,7 +202,7 @@ def test_keys():
def test_keys_prefix():
keys = [b"foo", b"f", b"foobar", b"bar"]
trie = marisa_trie.BinaryTrie(keys)
assert set(trie.keys(b"fo")) == set([b"foo", b"foobar"])
assert set(trie.keys(b"fo")) == {b"foo", b"foobar"}
assert trie.keys(b"foobarz") == []


Expand All @@ -230,12 +226,10 @@ def test_items():
def test_items_prefix():
keys = [b"foo", b"f", b"foobar", b"bar"]
trie = marisa_trie.BinaryTrie(keys)
assert set(trie.items(b"fo")) == set(
[
(b"foo", trie[b"foo"]),
(b"foobar", trie[b"foobar"]),
]
)
assert set(trie.items(b"fo")) == {
(b"foo", trie[b"foo"]),
(b"foobar", trie[b"foobar"]),
}


@given(st.sets(text))
Expand Down
4 changes: 0 additions & 4 deletions tests/test_bytes_trie.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import, unicode_literals

import io
import pickle

Expand Down
6 changes: 2 additions & 4 deletions tests/test_packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import re
import subprocess
from email import message_from_string
from pathlib import Path
from pkg_resources import get_distribution
from textwrap import dedent

from readme_renderer.rst import render

Expand Down Expand Up @@ -51,7 +49,7 @@ def test_check_pypi_rendering():

package = get_distribution("marisa-trie")
pkg_info = message_from_string(package.get_metadata("PKG-INFO"))
metadata = {k: v for k, v in pkg_info.items()}
metadata = dict(pkg_info.items())
lines = metadata["Summary"].splitlines()
description = lines.pop(0) + "\n"
description += "\n".join(l[8:] for l in lines)
Expand All @@ -60,5 +58,5 @@ def test_check_pypi_rendering():
rendering = render(description, stream=warnings)
print(description)
print(warnings)
assert str(warnings) == ""
assert not str(warnings)
assert rendering is not None
4 changes: 0 additions & 4 deletions tests/test_record_trie.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import

import io
import pickle

Expand Down
54 changes: 20 additions & 34 deletions tests/test_trie.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import, unicode_literals

import pickle
from uuid import uuid4

Expand Down Expand Up @@ -85,7 +81,7 @@ def test_get(keys):
def test_saveload(tmpdir_factory, keys):
trie = marisa_trie.Trie(keys)

dirname = str(uuid4()) + "_"
dirname = f"{str(uuid4())}_"
path = str(tmpdir_factory.mktemp(dirname).join("trie.bin"))
trie.save(path)

Expand All @@ -100,7 +96,7 @@ def test_saveload(tmpdir_factory, keys):
def test_mmap(tmpdir_factory, keys):
trie = marisa_trie.Trie(keys)

dirname = str(uuid4()) + "_"
dirname = f"{str(uuid4())}_"
path = str(tmpdir_factory.mktemp(dirname).join("trie.bin"))
trie.save(path)

Expand Down Expand Up @@ -201,30 +197,22 @@ def test_prefixes():
def test_iter_prefixes_with_keys():
trie = marisa_trie.Trie(["foo", "f", "foobar", "bar"])

assert set(trie.iter_prefixes_with_ids("foobar")) == set(
[
("f", trie["f"]),
("foo", trie["foo"]),
("foobar", trie["foobar"]),
]
)
assert set(trie.iter_prefixes_with_ids("foo")) == set(
[
("f", trie["f"]),
("foo", trie["foo"]),
]
)
assert set(trie.iter_prefixes_with_ids("bar")) == set(
[
("bar", trie["bar"]),
]
)
assert set(trie.iter_prefixes_with_ids("b")) == set()
assert set(trie.iter_prefixes_with_ids("foobar")) == {
("f", trie["f"]),
("foo", trie["foo"]),
("foobar", trie["foobar"]),
}
assert set(trie.iter_prefixes_with_ids("foo")) == {
("f", trie["f"]),
("foo", trie["foo"]),
}
assert set(trie.iter_prefixes_with_ids("bar")) == {("bar", trie["bar"])}
assert not set(trie.iter_prefixes_with_ids("b"))

for test_key in ["foobar", "foo", "bar", "b"]:
assert list(trie.iter_prefixes_with_ids(test_key)) == list(
assert list(trie.iter_prefixes_with_ids(test_key)) == [
(prefix, trie[prefix]) for prefix in trie.prefixes(test_key)
)
]

def test_keys():
keys = ["foo", "f", "foobar", "bar"]
Expand All @@ -235,7 +223,7 @@ def test_keys():
def test_keys_prefix():
keys = ["foo", "f", "foobar", "bar"]
trie = marisa_trie.Trie(keys)
assert set(trie.keys("fo")) == set(["foo", "foobar"])
assert set(trie.keys("fo")) == {"foo", "foobar"}
assert trie.keys("foobarz") == []


Expand All @@ -259,12 +247,10 @@ def test_items():
def test_items_prefix():
keys = ["foo", "f", "foobar", "bar"]
trie = marisa_trie.Trie(keys)
assert set(trie.items("fo")) == set(
[
("foo", trie["foo"]),
("foobar", trie["foobar"]),
]
)
assert set(trie.items("fo")) == {
("foo", trie["foo"]),
("foobar", trie["foobar"]),
}


@given(st.sets(text))
Expand Down
14 changes: 4 additions & 10 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import string

try:
from collections.abc import Mapping
except ImportError:
from collections import Mapping
from collections.abc import Mapping

import hypothesis.strategies as st

text = st.text("абвгдеёжзиклмнопрстуфхцчъыьэюя" + string.ascii_lowercase)
text = st.text(f"абвгдеёжзиклмнопрстуфхцчъыьэюя{string.ascii_lowercase}")

__all__ = ("Mapping", text)

0 comments on commit 32f34e5

Please sign in to comment.