diff --git a/.github/workflows/tests_and_coverage.yml b/.github/workflows/tests_and_coverage.yml index c19dae1..a9da1a8 100644 --- a/.github/workflows/tests_and_coverage.yml +++ b/.github/workflows/tests_and_coverage.yml @@ -32,7 +32,7 @@ jobs: run: pip list - name: Run tests and show coverage on the command line - run: coverage run --source=cantok --omit="*tests*" -m pytest --cache-clear --assert=plain && coverage report -m + run: coverage run --source=cantok --omit="*tests*" -m pytest --cache-clear --assert=plain && coverage report -m --fail-under=100 - name: Upload reports to codecov env: @@ -45,4 +45,4 @@ jobs: ./codecov -t ${CODECOV_TOKEN} - name: Run tests and show the branch coverage on the command line - run: coverage run --branch --source=cantok --omit="*tests*" -m pytest --cache-clear --assert=plain && coverage report -m + run: coverage run --branch --source=cantok --omit="*tests*" -m pytest --cache-clear --assert=plain && coverage report -m --fail-under=100 diff --git a/.github/workflows/tests_and_coverage_old.yml b/.github/workflows/tests_and_coverage_old.yml index 957fb70..e959fb1 100644 --- a/.github/workflows/tests_and_coverage_old.yml +++ b/.github/workflows/tests_and_coverage_old.yml @@ -32,7 +32,7 @@ jobs: run: pip list - name: Run tests and show coverage on the command line - run: coverage run --source=cantok --omit="*tests*" -m pytest --cache-clear --assert=plain && coverage report -m + run: coverage run --source=cantok --omit="*tests*" -m pytest --cache-clear --assert=plain && coverage report -m --fail-under=100 - name: Upload reports to codecov env: @@ -45,4 +45,4 @@ jobs: ./codecov -t ${CODECOV_TOKEN} - name: Run tests and show the branch coverage on the command line - run: coverage run --branch --source=cantok --omit="*tests*" -m pytest --cache-clear --assert=plain && coverage report -m + run: coverage run --branch --source=cantok --omit="*tests*" -m pytest --cache-clear --assert=plain && coverage report -m --fail-under=100 diff --git a/cantok/tokens/abstract_token.py b/cantok/tokens/abstract_token.py index a81246b..b34483a 100644 --- a/cantok/tokens/abstract_token.py +++ b/cantok/tokens/abstract_token.py @@ -73,7 +73,9 @@ class AbstractToken(ABC): rollback_if_nondirect_polling = False def __init__(self, *tokens: 'AbstractToken', cancelled: bool = False) -> None: - self.tokens = tokens + from cantok import DefaultToken + + self.tokens = [token for token in tokens if not isinstance(token, DefaultToken)] self._cancelled = cancelled self.lock = RLock() diff --git a/pyproject.toml b/pyproject.toml index df9f207..66e1ef9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "cantok" -version = "0.0.24" +version = "0.0.25" authors = [ { name="Evgeniy Blinov", email="zheni-b@yandex.ru" }, ] diff --git a/tests/units/tokens/test_abstract_token.py b/tests/units/tokens/test_abstract_token.py index 6db0edb..5d17dee 100644 --- a/tests/units/tokens/test_abstract_token.py +++ b/tests/units/tokens/test_abstract_token.py @@ -120,11 +120,11 @@ def test_str(token_fabric): @pytest.mark.parametrize( 'first_token_fabric', - ALL_TOKENS_FABRICS + [DefaultToken], + ALL_TOKENS_FABRICS, ) @pytest.mark.parametrize( 'second_token_fabric', - ALL_TOKENS_FABRICS + [DefaultToken], + ALL_TOKENS_FABRICS, ) def test_add_tokens(first_token_fabric, second_token_fabric): first_token = first_token_fabric() @@ -138,6 +138,36 @@ def test_add_tokens(first_token_fabric, second_token_fabric): assert tokens_sum.tokens[1] is second_token +@pytest.mark.parametrize( + 'second_token_fabric', + ALL_TOKENS_FABRICS, +) +def test_add_tokens_and_first_is_default_token(second_token_fabric): + first_token = DefaultToken() + second_token = second_token_fabric() + + tokens_sum = first_token + second_token + + assert isinstance(tokens_sum, SimpleToken) + assert len(tokens_sum.tokens) == 1 + assert tokens_sum.tokens[0] is second_token + + +@pytest.mark.parametrize( + 'first_token_fabric', + ALL_TOKENS_FABRICS, +) +def test_add_tokens_and_second_one_is_default_token(first_token_fabric): + first_token = first_token_fabric() + second_token = DefaultToken() + + tokens_sum = first_token + second_token + + assert isinstance(tokens_sum, SimpleToken) + assert len(tokens_sum.tokens) == 1 + assert tokens_sum.tokens[0] is first_token + + @pytest.mark.parametrize( 'token_fabric', ALL_TOKENS_FABRICS + [DefaultToken], @@ -405,3 +435,14 @@ def cancel_with_timeout(token): finish_time = perf_counter() assert finish_time - start_time >= timeout + + +@pytest.mark.parametrize( + 'token_fabric', + ALL_TOKENS_FABRICS, +) +def test_insert_default_token_to_another_tokens(token_fabric): + token = token_fabric(DefaultToken()) + + assert not isinstance(token, DefaultToken) + assert len(token.tokens) == 0 diff --git a/tests/units/tokens/test_default_token.py b/tests/units/tokens/test_default_token.py index 5c19209..45df234 100644 --- a/tests/units/tokens/test_default_token.py +++ b/tests/units/tokens/test_default_token.py @@ -55,10 +55,17 @@ def test_str_for_default_token(): @pytest.mark.skipif(sys.version_info >= (3, 10), reason='Format of this exception messages was changed.') def test_you_cannot_neste_another_token_to_default_one_old_pythons(): with pytest.raises(TypeError, match=full_match('__init__() takes 1 positional argument but 2 were given')): - DefaultToken(SimpleToken(TypeError)) + DefaultToken(SimpleToken()) @pytest.mark.skipif(sys.version_info < (3, 10), reason='Format of this exception messages was changed.') def test_you_cannot_neste_another_token_to_default_one_new_pythons(): with pytest.raises(TypeError, match=full_match('DefaultToken.__init__() takes 1 positional argument but 2 were given')): - DefaultToken(SimpleToken(TypeError)) + DefaultToken(SimpleToken()) + + +def test_default_plus_default(): + empty_sum = DefaultToken() + DefaultToken() + + assert isinstance(empty_sum, SimpleToken) + assert len(empty_sum.tokens) == 0