def peopleArgs(draw): return draw( one_of( fixed_dictionaries( dict( name=one_of(just(None), text()), email=emails().filter( lambda x: len(x) < 100 and '/' not in x), address=one_of(just(None), text()), )), fixed_dictionaries( dict( name=one_of(just(None), text()), email=emails().filter( lambda x: len(x) < 100 and '/' not in x), )), fixed_dictionaries( dict( name=one_of(just(None), text()), email=emails().filter( lambda x: len(x) < 100 and '/' not in x), )), fixed_dictionaries( dict(email=emails().filter( lambda x: len(x) < 100 and '/' not in x), ))))
def string_schema(schema: dict) -> st.SearchStrategy[str]: """Handle schemata for strings.""" # also https://json-schema.org/latest/json-schema-validation.html#rfc.section.7 min_size = schema.get("minLength", 0) max_size = schema.get("maxLength", float("inf")) strategy = st.text(min_size=min_size, max_size=schema.get("maxLength")) if "format" in schema: url_synonyms = [ "uri", "uri-reference", "iri", "iri-reference", "uri-template" ] domains = prov.domains() # type: ignore formats = { # A value of None indicates a known but unsupported format. **{name: rfc3339(name) for name in RFC3339_FORMATS}, "date": rfc3339("full-date"), "time": rfc3339("full-time"), # Hypothesis' provisional strategies are not type-annotated. # We should get a principled plan for them at some point I guess... "email": st.emails(), # type: ignore "idn-email": st.emails(), # type: ignore "hostname": domains, "idn-hostname": domains, "ipv4": prov.ip4_addr_strings(), # type: ignore "ipv6": prov.ip6_addr_strings(), # type: ignore **{ name: domains.map("https://{}".format) for name in url_synonyms }, "json-pointer": st.just(""), "relative-json-pointer": st.just(""), "regex": REGEX_PATTERNS, } if schema["format"] not in formats: raise InvalidArgument( f"Unsupported string format={schema['format']}") strategy = formats[schema["format"]] if "pattern" in schema: # pragma: no cover # This isn't really supported, but we'll do our best. strategy = strategy.filter( lambda s: re.search(schema["pattern"], string=s) is not None) elif "pattern" in schema: try: re.compile(schema["pattern"]) strategy = st.from_regex(schema["pattern"]) except re.error: # Patterns that are invalid in Python, or just malformed return st.nothing() # TODO: mypy should be able to tell that the lambda is returning a bool # without the explicit cast, but can't as of v 0.720 - report upstream. return strategy.filter(lambda s: bool(min_size <= len(s) <= max_size))
class TestSignInSchema: @given(email_address=emails()) def test_invalidates_nonexistent_email_address(self, email_address): errors = SignInSchema().validate({ "email_address": email_address, "password": "******" }) assert SignInSchema.custom_errors["nonexistent_email"] in errors["email_address"] @given( email_address=emails(), password=text(min_size=1), incorrect_password=text(min_size=1) ) def test_invalidates_incorrect_password_for_existing_email_address( self, email_address, password, incorrect_password ): with model_instance( User, first_name="Jane", last_name="Doe", email_address=email_address, password=password ): errors = SignInSchema().validate({ "email_address": email_address, "password": password + incorrect_password # in case both have the same value. }) assert SignInSchema.custom_errors["incorrect_password"] in errors["password"] @given(email_address=emails(), password=text(min_size=1)) def test_validates_correct_credentials_and_loads_user(self, email_address, password): with model_instance( User, first_name="John", last_name="Doe", email_address=email_address, password=password ) as test_user: credentials = { "email_address": email_address, "password": password } errors = SignInSchema().validate(credentials) user = SignInSchema().load(credentials) assert not errors assert test_user == user
def person_with_role(draw, **kwargs): defaults = { "contact_phone": french_phone_number, "image": None, "role__is_active": True, "role__is_staff": False, "role__is_superuser": False, "role__type": Role.PERSON_ROLE, } kwargs = {**defaults, **kwargs} email = to_strategy(kwargs.pop("email", st.emails())) role_kwargs = { k[len("role__"):]: to_strategy(kwargs.pop(k)) for k in list(kwargs) if k.startswith("role__") } person_kwargs = {k: to_strategy(v) for k, v in kwargs.items()} r = draw(from_model(Role, **role_kwargs)) p = draw(from_model(Person, role=st.just(r), **person_kwargs)) e = draw(email) p.add_email(e, primary=True) return p
def contact_infos(draw: _DrawType): """ Generates a `ContactInformation` object. :param draw: Hidden argument, used by Hypothesis. """ # empty lists for email and phone, for now return ContactInformation(None, draw(emails()), None, draw(human_names()))
class TestGit: def test_no_autocapture(self): code_ver = verta.code.Git(_autocapture=False) # protobuf message is empty assert not json_format.MessageToDict( code_ver._msg, including_default_value_fields=False, ).get('git') # may be {'git': {}} if fields are manually set to empty def test_repr(self): """Tests that __repr__() executes without error""" try: _git_utils.get_git_repo_root_dir() except OSError: pytest.skip("not in git repo") code_ver = verta.code.Git() assert code_ver.__repr__() @pytest.mark.skipif(not IN_GIT_REPO, reason="not in git repo") @pytest.mark.parametrize( ("repo_url", "branch", "tag", "commit_hash", "is_dirty"), get_git_test_autocapture_cases(), ) def test_autocapture(self, repo_url, branch, tag, commit_hash, is_dirty): code_ver = verta.code.Git( repo_url=repo_url, branch=branch, tag=tag, commit_hash=commit_hash, is_dirty=is_dirty, ) refs = [arg for arg in (branch, tag, commit_hash) if arg] ref = refs[0] if refs else _git_utils.get_git_commit_hash("HEAD") assert code_ver.repo_url == (repo_url or _git_utils.get_git_remote_url()) assert code_ver.branch == (branch or _git_utils.get_git_branch_name(ref)) assert code_ver.tag == (tag or _git_utils.get_git_commit_tag(ref) or None) # None if HEAD is not at a tag assert code_ver.commit_hash == (commit_hash or _git_utils.get_git_commit_hash(ref)) assert code_ver.is_dirty == (is_dirty if is_dirty is not None else _git_utils.get_git_commit_dirtiness(ref)) @hypothesis.given( repo_url=st.one_of(st.none(), st.emails()), branch=st.one_of(st.none(), st.text()), tag=st.one_of(st.none(), st.text()), commit_hash=st.one_of(st.none(), st.text()), is_dirty=st.one_of(st.none(), st.booleans()), ) def test_user_no_autocapture(self, repo_url, branch, tag, commit_hash, is_dirty): """Like test_no_autocapture, but with the public `autocapture` param.""" code_ver = verta.code.Git( repo_url=repo_url, branch=branch, tag=tag, commit_hash=commit_hash, is_dirty=is_dirty, autocapture=False, ) assert code_ver.repo_url == (repo_url or None) assert code_ver.branch == (branch or None) assert code_ver.tag == (tag or None) assert code_ver.commit_hash == (commit_hash or None) assert code_ver.is_dirty == (is_dirty or False)
def languages(draw: _DrawType): """ Generates a `Language` object with an arbitrary two-letter string as the code and something messier for the text ostensibly written in that language. :param draw: Hidden argument, used by Hypothesis. """ return Language(draw(emails()), draw(two_letter_codes()))
def referendum_contest_descriptions(draw: _DrawType, sequence_order: int, geo_units: List[GeopoliticalUnit]): """ Generates a tuple: a list of party-less candidates and a corresponding `ReferendumContestDescription`. :param draw: Hidden argument, used by Hypothesis. :param sequence_order: integer describing the order of this contest; make these sequential when generating many contests. :param geo_units: A list of `GeopoliticalUnit`; one of these goes into the `electoral_district_id` """ n = draw(integers(1, 3)) contest_candidates = draw(lists(candidates(None), min_size=n, max_size=n)) selection_descriptions = [ _candidate_to_selection_description(contest_candidates[i], i) for i in range(n) ] return ( contest_candidates, ReferendumContestDescription( object_id=str(draw(uuids())), electoral_district_id=geo_units[draw( integers(0, len(geo_units) - 1))].object_id, sequence_order=sequence_order, vote_variation=VoteVariationType.one_of_m, number_elected=1, votes_allowed=1, # should this be None or 1? name=draw(emails()), ballot_selections=selection_descriptions, ballot_title=draw(internationalized_texts()), ballot_subtitle=draw(internationalized_texts()), ), )
def string_schema(schema: dict) -> st.SearchStrategy[str]: """Handle schemata for strings.""" # also https://json-schema.org/latest/json-schema-validation.html#rfc.section.7 min_size = schema.get("minLength", 0) max_size = schema.get("maxLength", float("inf")) strategy: str = st.text(min_size=min_size, max_size=schema.get("maxLength")) if "format" in schema: url_synonyms = [ "uri", "uri-reference", "iri", "iri-reference", "uri-template" ] domains = prov.domains() strategy = { # A value of None indicates a known but unsupported format. **{name: rfc3339(name) for name in RFC3339_FORMATS}, "date": rfc3339("full-date"), "time": rfc3339("full-time"), "email": st.emails(), "idn-email": st.emails(), "hostname": domains, "idn-hostname": domains, "ipv4": prov.ip4_addr_strings(), "ipv6": prov.ip6_addr_strings(), **{ name: domains.map("https://{}".format) for name in url_synonyms }, "json-pointer": st.just(""), "relative-json-pointer": st.just(""), "regex": REGEX_PATTERNS, }.get(schema["format"]) if strategy is None: raise InvalidArgument( f"Unsupported string format={schema['format']}") if "pattern" in schema: # pragma: no cover # This isn't really supported, but we'll do our best. strategy = strategy.filter( lambda s: re.search(schema["pattern"], string=s) is not None) elif "pattern" in schema: try: re.compile(schema["pattern"]) strategy = st.from_regex(schema["pattern"]) except re.error: # Patterns that are invalid in Python, or just malformed strategy = st.nothing() return strategy.filter(lambda s: min_size <= len(s) <= max_size)
class TestCommitMetaData: @given( st.text(alphabet=NAME_ALPHABET, min_size=1), st.emails(), st.text(min_size=1), ) def test_auther_string(self, author, email, message): commit_data = under_test.CommitMetaData(author, email, message) assert author in commit_data.author_string() assert f"<{email}>" in commit_data.author_string()
def candidate_contest_descriptions( draw: _DrawType, sequence_order: int, party_list: List[Party], geo_units: List[GeopoliticalUnit], n: Optional[int] = None, m: Optional[int] = None, ): """ Generates a tuple: a `List[Candidate]` and a corresponding `CandidateContestDescription` for an n-of-m contest. :param draw: Hidden argument, used by Hypothesis. :param sequence_order: integer describing the order of this contest; make these sequential when generating many contests. :param party_list: A list of `Party` objects; each candidate's party is drawn at random from this list. :param geo_units: A list of `GeopoliticalUnit`; one of these goes into the `electoral_district_id` :param n: optional integer, specifying a particular value for n in this n-of-m contest, otherwise it's varied by Hypothesis. :param m: optional integer, specifying a particular value for m in this n-of-m contest, otherwise it's varied by Hypothesis. """ if n is None: n = draw(integers(1, 3)) if m is None: m = n + draw(integers(0, 3)) # for an n-of-m election party_ids = [p.get_party_id() for p in party_list] contest_candidates = draw( lists(candidates(party_list), min_size=m, max_size=m)) selection_descriptions = [ _candidate_to_selection_description(contest_candidates[i], i) for i in range(m) ] vote_variation = VoteVariationType.one_of_m if n == 1 else VoteVariationType.n_of_m return ( contest_candidates, CandidateContestDescription( object_id=str(draw(uuids())), electoral_district_id=geo_units[draw( integers(0, len(geo_units) - 1))].object_id, sequence_order=sequence_order, vote_variation=vote_variation, number_elected=n, votes_allowed=n, # should this be None or n? name=draw(emails()), ballot_selections=selection_descriptions, ballot_title=draw(internationalized_texts()), ballot_subtitle=draw(internationalized_texts()), primary_party_ids=party_ids, ), )
def string_schema(schema: dict) -> st.SearchStrategy[str]: """Handle schemata for strings.""" # also https://json-schema.org/latest/json-schema-validation.html#rfc.section.7 min_size = schema.get("minLength", 0) max_size = schema.get("maxLength", float("inf")) strategy: Any = st.text(min_size=min_size, max_size=schema.get("maxLength")) assert not ( "format" in schema and "pattern" in schema ), "format and regex constraints are supported, but not both at once." if "pattern" in schema: strategy = st.from_regex(schema["pattern"]) elif "format" in schema: url_synonyms = [ "uri", "uri-reference", "iri", "iri-reference", "uri-template" ] domains = prov.domains() # type: ignore strategy = { # A value of None indicates a known but unsupported format. **{name: rfc3339(name) for name in RFC3339_FORMATS}, "date": rfc3339("full-date"), "time": rfc3339("full-time"), "email": st.emails(), # type: ignore "idn-email": st.emails(), # type: ignore "hostname": domains, "idn-hostname": domains, "ipv4": prov.ip4_addr_strings(), # type: ignore "ipv6": prov.ip6_addr_strings(), # type: ignore **{ name: domains.map("https://{}".format) for name in url_synonyms }, "json-pointer": st.just(""), "relative-json-pointer": st.just(""), "regex": REGEX_PATTERNS, }.get(schema["format"]) if strategy is None: raise InvalidArgument( f"Unsupported string format={schema['format']}") return strategy.filter( lambda s: min_size <= len(s) <= max_size) # type: ignore
def test_request_add_valid(data, case): """Always return a 201 response when the book is in db and a valid email.""" case.body["email"] = data.draw(st.emails()) case.body["title"] = data.draw(st.sampled_from(list(books.values()))) response = case.call_asgi(app=app) try: validate_email(case.body["email"], check_deliverability=False) assert response.status_code == 201 except EmailNotValidError: # pragma: no cover # not all emails generated by hypothesis pass pydantic validation. assert response.status_code == 422
def election_descriptions(draw: _DrawType, max_num_parties: int = 3, max_num_contests: int = 3): """ Generates an `ElectionDescription` -- the top-level object describing an election. :param draw: Hidden argument, used by Hypothesis. :param max_num_parties: The largest number of parties that will be generated (default: 3) :param max_num_contests: The largest number of contests that will be generated (default: 3) """ assert max_num_parties > 0, "need at least one party" assert max_num_contests > 0, "need at least one contest" geo_units = [draw(geopolitical_units())] num_parties: int = draw(integers(1, max_num_parties)) # keep this small so tests run faster parties: List[Party] = draw(party_lists(num_parties)) num_contests: int = draw(integers(1, max_num_contests)) # generate a collection candidates mapped to contest descriptions candidate_contests: List[Tuple[List[Candidate], ContestDescription]] = [ draw(contest_descriptions(i, parties, geo_units)) for i in range(num_contests) ] assert len(candidate_contests) > 0 candidates_ = reduce( lambda a, b: a + b, [candidate_contest[0] for candidate_contest in candidate_contests], ) contests = [ candidate_contest[1] for candidate_contest in candidate_contests ] styles = [draw(ballot_styles(parties, geo_units))] # maybe later on we'll do something more complicated with dates start_date = draw(datetimes()) end_date = start_date return ElectionDescription( election_scope_id=draw(emails()), spec_version="v0.95", type=ElectionType.general, # good enough for now start_date=start_date, end_date=end_date, geopolitical_units=geo_units, parties=parties, candidates=candidates_, contests=contests, ballot_styles=styles, name=draw(internationalized_texts()), contact_information=draw(contact_infos()), )
def geopolitical_units(draw: _DrawType): """ Generates a `GeopoliticalUnit` object. :param draw: Hidden argument, used by Hypothesis. """ return GeopoliticalUnit( object_id=str(draw(uuids())), name=draw(emails()), type=draw(reporting_unit_types()), contact_information=draw(contact_infos()), )
def field_mappings(): global __default_field_mappings if __default_field_mappings is None: # Sized fields are handled in _get_strategy_for_field() # URL fields are not yet handled __default_field_mappings = { dm.SmallIntegerField: st.integers(-32768, 32767), dm.IntegerField: st.integers(-2147483648, 2147483647), dm.BigIntegerField: st.integers(-9223372036854775808, 9223372036854775807), dm.PositiveIntegerField: st.integers(0, 2147483647), dm.PositiveSmallIntegerField: st.integers(0, 32767), dm.BinaryField: st.binary(), dm.BooleanField: st.booleans(), dm.DateField: st.dates(), dm.DateTimeField: st.datetimes(timezones=get_tz_strat()), dm.DurationField: st.timedeltas(), dm.EmailField: emails(), dm.FloatField: st.floats(), dm.NullBooleanField: st.one_of(st.none(), st.booleans()), dm.TimeField: st.times(timezones=get_tz_strat()), dm.UUIDField: st.uuids(), } # SQLite does not support timezone-aware times, or timedeltas that # don't fit in six bytes of microseconds, so we override those db = getattr(django_settings, 'DATABASES', {}).get('default', {}) if db.get('ENGINE', '').endswith('.sqlite3'): # pragma: no branch sqlite_delta = timedelta(microseconds=2**47 - 1) __default_field_mappings.update({ dm.TimeField: st.times(), dm.DurationField: st.timedeltas(-sqlite_delta, sqlite_delta), }) return __default_field_mappings
class TestSample(TestCase): @given(pk=sql_ints(), sample_id=strat.text(), accession=strat.text(), barcode_id=strat.text(), organism=strat.text(), extraction_kit=strat.text(), comment=strat.text(), user=strat.emails()) @with_database def test_sample(self, **k): ss = models.SampleSheet.create(path=k['sample_id']) assert models.Sample.create(samplesheet=ss, **k)
def get_contest_description_well_formed( draw: _DrawType, ints=integers(1, 20), text=text(), emails=emails(), selections=get_selection_description_well_formed(), sequence_order: Optional[int] = None, electoral_district_id: Optional[str] = None, ) -> Tuple[str, ContestDescription]: object_id = f"{draw(emails)}-contest" if sequence_order is None: sequence_order = draw(ints) if electoral_district_id is None: electoral_district_id = f"{draw(emails)}-gp-unit" first_int = draw(ints) second_int = draw(ints) # TODO ISSUE #33: support more votes than seats for other VoteVariationType options number_elected = min(first_int, second_int) votes_allowed = number_elected selection_descriptions: List[SelectionDescription] = list() for i in range(max(first_int, second_int)): selection: Tuple[str, SelectionDescription] = draw(selections) _, selection_description = selection selection_description.sequence_order = i selection_descriptions.append(selection_description) contest_description = ContestDescription( object_id, electoral_district_id, sequence_order, VoteVariationType.n_of_m, number_elected, votes_allowed, draw(text), selection_descriptions, ) placeholder_selections = generate_placeholder_selections_from( contest_description, number_elected) return ( object_id, contest_description_with_placeholders_from(contest_description, placeholder_selections), )
def get_selection_description_well_formed( draw: _DrawType, ints=integers(1, 20), emails=emails(), candidate_id: Optional[str] = None, sequence_order: Optional[int] = None, ) -> Tuple[str, SelectionDescription]: if candidate_id is None: candidate_id = draw(emails) object_id = f"{candidate_id}-selection" if sequence_order is None: sequence_order = draw(ints) return (object_id, SelectionDescription(object_id, candidate_id, sequence_order))
def generate_data_for_require(record_data, input_type=None): if record_data['Is Email']: data = strategies.emails().example() elif record_data['Is Url']: regex = '^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$' data = strategies.from_regex(regex=regex).example() elif record_data['Max Length'] or record_data['Min Length']: min_size = int( common.parse_validation_type(record_data['Min Length']) [0]) if record_data['Min Length'] else None max_size = int( common.parse_validation_type(record_data['Max Length']) [0]) if record_data['Max Length'] else None data = strategies.text( min_size=min_size, max_size=max_size, alphabet=strategies.characters(min_codepoint=33, max_codepoint=126)).example() elif record_data['Min'] or record_data['Max']: min_value = int( common.parse_validation_type( record_data['Min'])[0]) if record_data['Min'] else None max_value = int( common.parse_validation_type( record_data['Max'])[0]) if record_data['Max'] else None data = strategies.integers(min_value=min_value, max_value=max_value).example() else: if input_type == 'number': data = strategies.integers().example() else: data = strategies.text(min_size=1, alphabet=strategies.characters( min_codepoint=33, max_codepoint=126)).example() return [{ 'data': data, 'is_valid': True }, { 'data': '', 'is_valid': False }]
class AddTest(unittest.TestCase): #数字 @settings(max_examples=3) @given(a=st.integers(), b=st.integers()) def test_case_01(self, a, b): print('a->', a) print('b->', b) c1 = a + b c2 = sum([a, b]) self.assertEqual(c1, c2) #email @settings(max_examples=4) @given(a=st.emails()) def test_case_02(self, a): print('a->', a) # print('b->',b) # c1=a+b # c2=sum([a,b]) self.assertIn('@', a)
def generate_data_for_email(): return [ { 'data': strategies.emails().example(), 'is_valid': True }, { 'data': strategies.text(min_size=1, max_size=100, alphabet=strategies.characters( min_codepoint=33, max_codepoint=126)).example(), 'is_valid': False }, { 'data': '@@gmail.com', 'is_valid': False }, ]
def field_mappings(): global __default_field_mappings if __default_field_mappings is None: # Sized fields are handled in _get_strategy_for_field() # URL fields are not yet handled __default_field_mappings = { dm.SmallIntegerField: st.integers(-32768, 32767), dm.IntegerField: st.integers(-2147483648, 2147483647), dm.BigIntegerField: st.integers(-9223372036854775808, 9223372036854775807), dm.PositiveIntegerField: st.integers(0, 2147483647), dm.PositiveSmallIntegerField: st.integers(0, 32767), dm.BinaryField: st.binary(), dm.BooleanField: st.booleans(), dm.DateField: st.dates(), dm.DateTimeField: st.datetimes(timezones=get_tz_strat()), dm.DurationField: st.timedeltas(), dm.EmailField: emails(), dm.FloatField: st.floats(), dm.NullBooleanField: st.one_of(st.none(), st.booleans()), dm.TimeField: st.times(timezones=get_tz_strat()), dm.UUIDField: st.uuids(), } # SQLite does not support timezone-aware times, or timedeltas that # don't fit in six bytes of microseconds, so we override those db = getattr(django_settings, 'DATABASES', {}).get('default', {}) if db.get('ENGINE', '').endswith('.sqlite3'): # pragma: no branch sqlite_delta = timedelta(microseconds=2 ** 47 - 1) __default_field_mappings.update({ dm.TimeField: st.times(), dm.DurationField: st.timedeltas(-sqlite_delta, sqlite_delta), }) return __default_field_mappings
except ImportError: # pragma: no cover pass else: def is_valid_email(s: str) -> bool: # Hypothesis' st.emails() occasionally generates emails like [email protected] # that are invalid according to email-validator, so we filter those out. try: email_validator.validate_email(s, check_deliverability=False) return True except email_validator.EmailNotValidError: # pragma: no cover return False # Note that these strategies deliberately stay away from any tricky Unicode # or other encoding issues; we're just trying to generate *something* valid. st.register_type_strategy(pydantic.EmailStr, st.emails().filter(is_valid_email)) # type: ignore[arg-type] st.register_type_strategy( pydantic.NameEmail, st.builds( '{} <{}>'.format, # type: ignore[arg-type] st.from_regex('[A-Za-z0-9_]+( [A-Za-z0-9_]+){0,5}', fullmatch=True), st.emails().filter(is_valid_email), ), ) # PyObject - dotted names, in this case taken from the math module. st.register_type_strategy( pydantic.PyObject, # type: ignore[arg-type] st.sampled_from( [cast(pydantic.PyObject, f'math.{name}') for name in sorted(vars(math)) if not name.startswith('_')] ),
dm.SmallIntegerField: integers_for_field(-32768, 32767), dm.IntegerField: integers_for_field(-2147483648, 2147483647), dm.BigIntegerField: integers_for_field(-9223372036854775808, 9223372036854775807), dm.PositiveIntegerField: integers_for_field(0, 2147483647), dm.PositiveSmallIntegerField: integers_for_field(0, 32767), dm.BooleanField: st.booleans(), dm.DateField: st.dates(), dm.EmailField: emails(), dm.FloatField: st.floats(), dm.NullBooleanField: st.one_of(st.none(), st.booleans()), dm.URLField: urls(), dm.UUIDField: st.uuids(), df.DateField: st.dates(), df.DurationField: st.timedeltas(), df.EmailField: emails(), df.FloatField:
# coding=utf-8 # # This file is part of Hypothesis, which may be found at # https://github.com/HypothesisWorks/hypothesis-python # # Most of this work is copyright (C) 2013-2018 David R. MacIver # ([email protected]), but it contains contributions by others. See # CONTRIBUTING.rst for a full list of people who may hold copyright, and # consult the git log if you need to determine who owns an individual # contribution. # # This Source Code Form is subject to the terms of the Mozilla Public License, # v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at http://mozilla.org/MPL/2.0/. # # END HEADER from __future__ import division, print_function, absolute_import from hypothesis import given from hypothesis.strategies import emails @given(emails()) def test_is_valid_email(address): local, at_, domain = address.rpartition('@') assert at_ == '@' assert local assert domain
assert mpu.string.is_iban("DE12") is False assert mpu.string.is_iban("") is False assert mpu.string.is_iban("ZZaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") is False assert mpu.string.is_iban("DEaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") is False def test_is_iban(): iban = "FR14 2004 1010 0505 0001 3M02 606" assert mpu.string.is_iban(iban) @pytest.mark.parametrize("illegal_default", ["foobar", True]) def test_is_none_illegal_default(illegal_default): with pytest.raises(ValueError): mpu.string.is_none("none", default=illegal_default) def test_is_none_not(): with pytest.raises(ValueError): mpu.string.is_none("foobar") @given(st.emails()) def test_is_email(email): assert mpu.string.is_email(email), f"is_email({email}) returned False" @given(st.ip_addresses(v=4)) def test_is_ipv4(ip): assert mpu.string.is_ipv4(str(ip)), f"is_ipv4({ip}) returned False"
pass AnyField = Union[dm.Field, df.Field] F = TypeVar("F", bound=AnyField) # Mapping of field types, to strategy objects or functions of (type) -> strategy _global_field_lookup = { dm.SmallIntegerField: st.integers(-32768, 32767), dm.IntegerField: st.integers(-2147483648, 2147483647), dm.BigIntegerField: st.integers(-9223372036854775808, 9223372036854775807), dm.PositiveIntegerField: st.integers(0, 2147483647), dm.PositiveSmallIntegerField: st.integers(0, 32767), dm.BinaryField: st.binary(), dm.BooleanField: st.booleans(), dm.DateField: st.dates(), dm.EmailField: emails(), dm.FloatField: st.floats(), dm.NullBooleanField: st.one_of(st.none(), st.booleans()), dm.URLField: urls(), dm.UUIDField: st.uuids(), df.DateField: st.dates(), df.DurationField: st.timedeltas(), df.EmailField: emails(), df.FloatField: st.floats(allow_nan=False, allow_infinity=False), df.IntegerField: st.integers(-2147483648, 2147483647), df.NullBooleanField: st.one_of(st.none(), st.booleans()), df.URLField: urls(), df.UUIDField: st.uuids(), } # type: Dict[Type[AnyField], Union[st.SearchStrategy, Callable[[Any], st.SearchStrategy]]]
@given(text(min_size=12, max_size=64, alphabet=ascii_letters + digits)) def test_alphanumeric(a_string): """ Generate alphanumeric sized strings like: 'LbkNCS4xl2XlEtu' 'z3M4jc1JxXQokvmUeAr6YpgT' 'vxDKNjBPHzxqD7egsD' """ assert a_string.isalnum() a_length = len(a_string) assert a_length >= 12 and a_length <= 64 @given(lists(emails(), min_size=1, max_size=10)) def test_email(email_list): """ Email addresses as per RFC 5322, section-3.4.1 """ assert all(x.count('@') == 1 for x in email_list) @given(lists(integers()), randoms()) def test_shuffle_is_noop(a_list, _random): """ Show intermediate steps in test using `note`. """ b_list = list(a_list) _random.shuffle(b_list) note("Shuffle: %r" % (b_list))
serialized_user = u.serialize(serialize_method=dict) serialized_user_skip = u.serialize(skip_list=['id'], serialize_method=dict) assert 'id' in serialized_user and 'id' not in serialized_user_skip def test_serialize_skip_list_ignores_invalid_key(user): u = user() serialized_user = u.serialize(serialize_method=dict) serialized_user_bad_key = u.serialize(serialize_method=dict, skip_list=['blarg']) assert serialized_user == serialized_user_bad_key @given(pk=integers(), display_name=text(min_size=1), email=emails(), password=text(min_size=1)) @settings(max_examples=10) # something about this takes forever! def test_deserialize_not_from_redis(pk, display_name, email, password): u = UserInfo(id=pk, display_name=display_name, email=email) u.set_password(password) ser = u.serialize(serialize_method=json.dumps) u2 = UserInfo.deserialize(json.loads(ser)) assert u == u2 @given(email=emails()) def test_is_anonymous(email): u = UserInfo(id=1, email=email) assert u.is_anonymous() == (email == '')
# Third party modules import hypothesis.strategies as s import mpu.string # Martins Python Utilities from hypothesis import given @given(s.emails()) def test_is_email(email): assert mpu.string.is_email(email), f"is_email({email}) returned False" @given(s.ip_addresses(v=4)) def test_is_ipv4(ip): assert mpu.string.is_ipv4(str(ip)), f"is_ipv4({ip}) returned False"
if False: from datetime import tzinfo # noqa from typing import Any, Type, Optional, List, Text, Callable, Union # noqa # Mapping of field types, to strategy objects or functions of (type) -> strategy _global_field_lookup = { dm.SmallIntegerField: st.integers(-32768, 32767), dm.IntegerField: st.integers(-2147483648, 2147483647), dm.BigIntegerField: st.integers(-9223372036854775808, 9223372036854775807), dm.PositiveIntegerField: st.integers(0, 2147483647), dm.PositiveSmallIntegerField: st.integers(0, 32767), dm.BinaryField: st.binary(), dm.BooleanField: st.booleans(), dm.DateField: st.dates(), dm.EmailField: emails(), dm.FloatField: st.floats(), dm.NullBooleanField: st.one_of(st.none(), st.booleans()), dm.URLField: urls(), dm.UUIDField: st.uuids(), df.DateField: st.dates(), df.DurationField: st.timedeltas(), df.EmailField: emails(), df.FloatField: st.floats(allow_nan=False, allow_infinity=False), df.IntegerField: st.integers(-2147483648, 2147483647), df.NullBooleanField: st.one_of(st.none(), st.booleans()), df.URLField: urls(), df.UUIDField: st.uuids(), }