Last active
January 26, 2022 15:07
-
-
Save sportebois/1e1eab1caea2323071e6cd2a9927eb80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# file: tests/test_dry_demo_01.py | |
import re | |
from hypothesis import assume, example, given, strategies | |
from app.dry_demo_01 import drn_pattern, is_valid_project_drn | |
valid_drn = strategies.from_regex(drn_pattern) | |
@strategies.composite | |
def invalid_drn(draw): | |
"""Generate random invalid DRN identifiers""" | |
s = draw( | |
strategies.one_of( | |
# Pretty much any random string | |
strategies.text(alphabet=strategies.characters(blacklist_categories=("Cc", "Cs")), min_size=0), | |
# Some obvious bounds | |
strategies.sampled_from( | |
["", "zzz:a29157:a3a0632b8a10427ebcd2ca1d51097cf8"] | |
), | |
) | |
) | |
# If the random string ends up being a valid value, we reject it | |
assume(re.match(drn_pattern, s) is None) | |
return s | |
@given(drn=valid_drn) | |
@example(drn="drn:ac0a09:a99b52bd10614c29adda8131f7a523f1") | |
def test_valid_patterns_are_validated(drn): | |
assert is_valid_project_drn(drn) is True | |
@given(drn=invalid_drn()) | |
@example(drn="arn:121f23:4c324bd260974d5f94cab825a644958e") # Invalid prefix | |
@example(drn="drn::4c324bd260974d5f94cab825a644958e") # missing workspace | |
@example(drn="arn:121f23:") # missing resource id | |
@example(drn="arn:1234567:4c324bd260974d5f94cab825a644958e") # Workspace too long | |
def test_invalid_patterns_are_not_validated(drn): | |
assert is_valid_project_drn(drn) is False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment