Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(pynanoid): simplify test suite #4

Merged
merged 1 commit into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ python-source = "src"
strip = true

[tool.pdm.dev-dependencies]
dev = [
"ruff>=0.6.1",
"mypy>=1.11.1",
"pip>=24.2",
"platformdirs>=4.2.2",
]
dev = ["ruff>=0.6.1", "mypy>=1.11.1", "pip>=24.2"]
test = [
"pytest>=8.3.2",
"pytest-cov>=5.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/pynanoid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
try:
# prioritize using the compiled versions if available
from ._pynanoid import generate, non_secure_generate
except ImportError:
except ImportError: # pragma: no cover
from .nanoid import generate, non_secure_generate


Expand Down
16 changes: 8 additions & 8 deletions src/pynanoid/nanoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ def non_secure_generate(alphabet: str = ALPHABET, size: int = SIZE) -> str:
Returns:
A NanoID of `size` length.
"""
if alphabet == "": # pragma: no cover
raise ValueError("Alphabet cannot be empty")
if size < 1: # pragma: no cover
raise ValueError("Size cannot be less than 1")
if alphabet == "":
raise ValueError("alphabet cannot be empty")
if size < 1:
raise ValueError("size cannot be less than 1")

alphabet_len = len(alphabet)

Expand Down Expand Up @@ -71,10 +71,10 @@ def generate_custom(
Returns:
A NanoID of `size` length.
"""
if alphabet == "": # pragma: no cover
raise ValueError("Alphabet cannot be empty")
if size < 1: # pragma: no cover
raise ValueError("Size cannot be less than 1")
if alphabet == "":
raise ValueError("alphabet cannot be empty")
if size < 1:
raise ValueError("size cannot be less than 1")

alphabet_len = len(alphabet)

Expand Down
52 changes: 0 additions & 52 deletions tests/generate_test.py

This file was deleted.

23 changes: 0 additions & 23 deletions tests/method_test.py

This file was deleted.

68 changes: 0 additions & 68 deletions tests/nanoid_test.py

This file was deleted.

53 changes: 0 additions & 53 deletions tests/non_secure_test.py

This file was deleted.

144 changes: 144 additions & 0 deletions tests/test_pynanoid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import re

import pytest
from hypothesis import given
from hypothesis import strategies as st

from pynanoid import generate, non_secure_generate
from pynanoid.nanoid import (
ALPHABET,
generate_custom,
)
from pynanoid.nanoid import (
generate as py_generate,
)
from pynanoid.nanoid import (
non_secure_generate as py_non_secure_generate,
)


@given(
alphabet=st.text(min_size=1),
size=st.integers(min_value=1, max_value=5000),
)
@pytest.mark.parametrize(
"generate_fn",
[generate, py_generate],
ids=["rust", "python"],
)
def test_correct_length(alphabet: str, size: int, generate_fn): # noqa: ANN001
assert len(generate_fn(alphabet, size)) == size


@given(
alphabet=st.text(min_size=1),
size=st.integers(min_value=1, max_value=5000),
)
@pytest.mark.parametrize(
"generate_fn",
[non_secure_generate, py_non_secure_generate],
ids=["rust", "python"],
)
def test_correct_length_non_secure(alphabet: str, size: int, generate_fn): # noqa: ANN001
assert len(generate_fn(alphabet, size)) == size


@pytest.mark.parametrize(
"generate_fn",
[generate, py_generate],
ids=["rust", "python"],
)
def test_has_no_collisions(generate_fn): # noqa: ANN001
count = 100_000
used = set()
for _ in range(count):
id_ = generate_fn()
assert id_ not in used
used.add(id_)


@given(size=st.integers(min_value=1, max_value=5000))
@pytest.mark.parametrize(
"generate_fn",
[generate, py_generate],
ids=["rust", "python"],
)
def test_generates_url_friendly_id(generate_fn, size: int): # noqa: ANN001
regex = re.compile(r"^[0-9A-Za-z_-]+$")
id_ = generate_fn(size=size)
assert len(id_) == size
assert regex.match(id_)


@given(size=st.integers(min_value=1, max_value=5000))
@pytest.mark.parametrize(
"generate_fn",
[non_secure_generate, py_non_secure_generate],
ids=["rust", "python"],
)
def test_generates_url_friendly_id_non_secure(generate_fn, size: int): # noqa: ANN001
regex = re.compile(r"^[0-9A-Za-z_-]+$")
id_ = generate_fn(size=size)
assert len(id_) == size
assert regex.match(id_)


@given(size=st.integers(min_value=1, max_value=5000))
@pytest.mark.parametrize(
"generate_fn",
[generate, py_generate],
ids=["rust", "python"],
)
def test_error_on_empty_alphabet(generate_fn, size: int): # noqa: ANN001
with pytest.raises(ValueError, match="alphabet cannot be empty"):
generate_fn(alphabet="", size=size)


@given(size=st.integers(min_value=1, max_value=5000))
@pytest.mark.parametrize(
"generate_fn",
[non_secure_generate, py_non_secure_generate],
ids=["rust", "python"],
)
def test_error_on_empty_alphabet_non_secure(generate_fn, size: int): # noqa: ANN001
with pytest.raises(ValueError, match="alphabet cannot be empty"):
generate_fn(alphabet="", size=size)


@given(alphabet=st.text(min_size=1))
@pytest.mark.parametrize(
"generate_fn",
[generate, py_generate],
ids=["rust", "python"],
)
def test_error_on_zero_size(generate_fn, alphabet: str): # noqa: ANN001
with pytest.raises(ValueError, match="size cannot be (less than 1|zero)"):
generate_fn(alphabet, size=0)


@given(alphabet=st.text(min_size=1))
@pytest.mark.parametrize(
"generate_fn",
[non_secure_generate, py_non_secure_generate],
ids=["rust", "python"],
)
def test_error_on_zero_size_non_secure(generate_fn, alphabet: str): # noqa: ANN001
with pytest.raises(ValueError, match="size cannot be (less than 1|zero)"):
generate_fn(alphabet, size=0)


def test_generate_custom_randgen():
sequence = [2, 255, 3, 7, 7, 7, 7, 7, 0, 1]

def rand(size: int) -> bytes:
random_bytes = []
for i in range(0, size, len(sequence)):
random_bytes += sequence[0 : size - i]
return bytes(random_bytes)

assert generate_custom(rand, "abcde", 4) == "cdac"


def test_default_alphabet_ascii():
regex = re.compile(r"^[0-9A-Za-z_-]+$")
assert regex.match(ALPHABET)
Loading