From 8cb2b9a7b734056d5102bc94b0b6401b2fdbd6c890c511a679e3921f2fd8423e Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Sat, 30 Nov 2024 18:57:52 +0300 Subject: [PATCH] DRY: move commonly used strategies to single module --- pyac/tests/strategies.py | 30 ++++++++++++++++++++++++ pyac/tests/test_list.py | 32 +------------------------- pyac/tests/test_map.py | 41 +++------------------------------ pyac/tests/test_symmetric.py | 44 +++--------------------------------- 4 files changed, 37 insertions(+), 110 deletions(-) create mode 100644 pyac/tests/strategies.py diff --git a/pyac/tests/strategies.py b/pyac/tests/strategies.py new file mode 100644 index 0000000..b84dd7c --- /dev/null +++ b/pyac/tests/strategies.py @@ -0,0 +1,30 @@ +from hypothesis.strategies import binary +from hypothesis.strategies import booleans +from hypothesis.strategies import characters +from hypothesis.strategies import datetimes +from hypothesis.strategies import integers +from hypothesis.strategies import just +from hypothesis.strategies import none +from hypothesis.strategies import one_of +from hypothesis.strategies import text +from hypothesis.strategies import tuples +from hypothesis.strategies import uuids + +from pyac import Blob + + +blobs_st = tuples(integers(1, 20), binary(max_size=60)).map(lambda x: Blob(*x)) +unicode_allowed = characters(exclude_characters="\x00", exclude_categories=("Cs",)) +text_st = text(alphabet=unicode_allowed, max_size=32) +mapkey_st = text(alphabet=unicode_allowed, min_size=1, max_size=8) +any_st = one_of( + booleans(), + integers(), + just(0), + just(-1), + binary(max_size=32), + text_st, + none(), + uuids(), + datetimes(), +) diff --git a/pyac/tests/test_list.py b/pyac/tests/test_list.py index db73033..6f3f969 100644 --- a/pyac/tests/test_list.py +++ b/pyac/tests/test_list.py @@ -3,42 +3,12 @@ from unittest import TestCase from hypothesis import given from hypothesis.strategies import binary -from hypothesis.strategies import booleans -from hypothesis.strategies import characters -from hypothesis.strategies import datetimes -from hypothesis.strategies import integers -from hypothesis.strategies import just from hypothesis.strategies import lists -from hypothesis.strategies import none -from hypothesis.strategies import one_of -from hypothesis.strategies import text -from hypothesis.strategies import tuples -from hypothesis.strategies import uuids -from pyac import Blob from pyac import dumps from pyac import loads from pyac import NotEnoughData - - -blobs_st = tuples(integers(1, 20), binary(max_size=60)).map(lambda x: Blob(*x)) - -text_st = text( - alphabet=characters(exclude_characters="\x00", exclude_categories=("Cs",)), - max_size=32, -) - -any_st = one_of( - booleans(), - integers(), - just(0), - just(-1), - binary(max_size=32), - text_st, - none(), - uuids(), - datetimes(), -) +from tests.strategies import any_st class TestList(TestCase): diff --git a/pyac/tests/test_map.py b/pyac/tests/test_map.py index c3579b9..0d069ff 100644 --- a/pyac/tests/test_map.py +++ b/pyac/tests/test_map.py @@ -2,49 +2,14 @@ from unittest import TestCase from hypothesis import given from hypothesis.strategies import binary -from hypothesis.strategies import booleans -from hypothesis.strategies import characters -from hypothesis.strategies import datetimes from hypothesis.strategies import dictionaries -from hypothesis.strategies import integers -from hypothesis.strategies import just from hypothesis.strategies import lists -from hypothesis.strategies import none -from hypothesis.strategies import one_of -from hypothesis.strategies import text -from hypothesis.strategies import tuples -from hypothesis.strategies import uuids -from pyac import Blob -from pyac import dumps from pyac import DecodeError +from pyac import dumps from pyac import loads - - -blobs_st = tuples(integers(1, 20), binary(max_size=60)).map(lambda x: Blob(*x)) - -text_st = text( - alphabet=characters(exclude_characters="\x00", exclude_categories=("Cs",)), - max_size=32, -) - -any_st = one_of( - booleans(), - integers(), - just(0), - just(-1), - binary(max_size=32), - text_st, - none(), - uuids(), - datetimes(), -) - -mapkey_st = text( - alphabet=characters(exclude_characters="\x00", exclude_categories=("Cs",)), - min_size=1, - max_size=8, -) +from tests.strategies import any_st +from tests.strategies import mapkey_st class TestMap(TestCase): diff --git a/pyac/tests/test_symmetric.py b/pyac/tests/test_symmetric.py index 9eff3fe..8468cc1 100644 --- a/pyac/tests/test_symmetric.py +++ b/pyac/tests/test_symmetric.py @@ -3,58 +3,20 @@ from unittest import TestCase from hypothesis import given from hypothesis.strategies import binary -from hypothesis.strategies import booleans -from hypothesis.strategies import characters -from hypothesis.strategies import datetimes from hypothesis.strategies import deferred from hypothesis.strategies import dictionaries -from hypothesis.strategies import integers -from hypothesis.strategies import just from hypothesis.strategies import lists -from hypothesis.strategies import none -from hypothesis.strategies import one_of -from hypothesis.strategies import text -from hypothesis.strategies import tuples -from hypothesis.strategies import uuids -from pyac import Blob from pyac import dumps from pyac import loads +from tests.strategies import any_st +from tests.strategies import mapkey_st -blobs_st = tuples(integers(1, 20), binary(max_size=60)).map(lambda x: Blob(*x)) - -text_st = text( - alphabet=characters(exclude_characters="\x00", exclude_categories=("Cs",)), - max_size=32, -) - -mapkey_st = text( - alphabet=characters(exclude_characters="\x00", exclude_categories=("Cs",)), - min_size=1, - max_size=8, -) - -any_st = one_of( - booleans(), - integers(), - just(0), - just(-1), - binary(max_size=32), - text_st, - none(), - uuids(), - datetimes(), -) - everything_st = deferred( lambda: any_st | lists(everything_st, max_size=4) | - dictionaries( - mapkey_st, - everything_st, - max_size=4, - ) + dictionaries(mapkey_st, everything_st, max_size=4) ) -- 2.50.0