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

Add hypothesis tests. #136

Merged
merged 7 commits into from
Jan 4, 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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ mdformat-footnote = "*"
[tool.poetry.group.test.dependencies]
pytest = "*"
pytest-rerunfailures = "*"
hypothesis = "*"

[tool.poetry.group.doc.dependencies]
sphinx = "*"
Expand Down
38 changes: 37 additions & 1 deletion tests/test_text.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Copyright (c) 2023 Philip May
# Copyright (c) 2023-2024 Philip May
# This software is distributed under the terms of the MIT license
# which is available at https://opensource.org/licenses/MIT

from collections import Counter, defaultdict
from math import isclose

import pytest
from hypothesis import given, settings
from hypothesis.strategies import text

from mltb2.text import (
INVISIBLE_CHARACTERS,
Expand All @@ -21,6 +23,40 @@
)


@settings(max_examples=1000)
@given(text())
def test_remove_and_detect_invisible_characters_hypothesis(text: str):
result = remove_invisible_characters(text)
assert isinstance(result, str)
if has_invisible_characters(text):
assert len(result) < len(text)
else:
assert len(result) == len(text)


@settings(max_examples=1000)
@given(text())
def test_replace_and_detect_special_whitespaces_hypothesis(text: str):
result = replace_special_whitespaces(text)
assert isinstance(result, str)
text_whitespace_count = text.count(" ")
result_whitespace_count = result.count(" ")
if has_special_whitespaces(text):
assert text_whitespace_count < result_whitespace_count
else:
assert text_whitespace_count == result_whitespace_count


@settings(max_examples=1000)
@given(text())
def test_replace_multiple_whitespaces_hypothesis(text: str):
result = replace_multiple_whitespaces(text)
text_whitespace_count = text.count(" ")
result_whitespace_count = result.count(" ")
assert len(result) <= len(text)
assert result_whitespace_count <= text_whitespace_count


def test_remove_invisible_characters():
text = "Hello\u200bWorld\u00ad!"
result = remove_invisible_characters(text)
Expand Down
20 changes: 20 additions & 0 deletions tests/test_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,29 @@
# which is available at https://opensource.org/licenses/MIT


import pytest
from hypothesis import given, settings
from hypothesis.strategies import text

from mltb2.transformers import TransformersTokenCounter


@pytest.fixture(scope="module")
def deepset_gbert_base_token_counter() -> TransformersTokenCounter:
return TransformersTokenCounter("deepset/gbert-base")


@settings(max_examples=1000, deadline=None)
@given(text=text())
def test_TransformersTokenCounter_hypothesis( # noqa: N802
text: str, deepset_gbert_base_token_counter: TransformersTokenCounter
):
token_count = deepset_gbert_base_token_counter(text)

assert isinstance(token_count, int)
assert token_count >= 0


def test_TransformersTokenCounter_call_string(): # noqa: N802
transformers_token_counter = TransformersTokenCounter("deepset/gbert-base")
token_count = transformers_token_counter("Das ist ein Text.")
Expand Down