def test_does_not_simplify_into_surrogates(): f = find(text(average_size=25.0), lambda x: x >= u'\udfff') assert f == u'\ue000' f = find( text(average_size=25.0), lambda x: len([t for t in x if t >= u'\udfff']) >= 10) assert f == u'\ue000' * 10
def subtitles(strict=True): '''A Hypothesis strategy to generate Subtitle objects.''' # max_value settings are just to avoid overflowing TIMEDELTA_MAX_DAYS by # using arbitrary low enough numbers timestamp_strategy = timedeltas(min_value=0, max_value=999999) # If we want to test \r, we'll test it by ourselves. It makes testing # harder without because we don't get the same outputs as inputs on Unix. content_strategy = st.text(min_size=1).filter(lambda x: '\r' not in x) proprietary_strategy = st.text().filter( lambda x: all(eol not in x for eol in '\r\n') ) if strict: content_strategy = content_strategy.filter(is_strictly_legal_content) subtitle_strategy = st.builds( srt.Subtitle, index=st.integers(min_value=0), start=timestamp_strategy, end=timestamp_strategy, proprietary=proprietary_strategy, content=content_strategy, ) return subtitle_strategy
def _get_strategy_for_field(f): if f.choices: choices = [] for value, name_or_optgroup in f.choices: if isinstance(name_or_optgroup, (list, tuple)): choices.extend(key for key, _ in name_or_optgroup) else: choices.append(value) if isinstance(f, (dm.CharField, dm.TextField)) and f.blank: choices.insert(0, u'') strategy = st.sampled_from(choices) elif type(f) == dm.SlugField: strategy = st.text(alphabet=string.ascii_letters + string.digits, min_size=(None if f.blank else 1), max_size=f.max_length) elif type(f) == dm.GenericIPAddressField: lookup = {'both': ip4_addr_strings() | ip6_addr_strings(), 'ipv4': ip4_addr_strings(), 'ipv6': ip6_addr_strings()} strategy = lookup[f.protocol.lower()] elif type(f) in (dm.TextField, dm.CharField): strategy = st.text(min_size=(None if f.blank else 1), max_size=f.max_length) elif type(f) == dm.DecimalField: bound = Decimal(10 ** f.max_digits - 1) / (10 ** f.decimal_places) strategy = st.decimals(min_value=-bound, max_value=bound, places=f.decimal_places) else: strategy = field_mappings().get(type(f), st.nothing()) if f.validators: strategy = strategy.filter(validator_to_filter(f)) if f.null: strategy = st.one_of(st.none(), strategy) return strategy
def test_can_simplify_text_through_a_morpher(rnd): m = find( morphers, lambda x: bool(x.become(s.text())), random=rnd, settings=Settings(database=None) ) with BuildContext(): assert m.clean_slate().become(s.text()) == u'0'
def ref_with_vcf_dicts_strategy_factory(draw): ''' Generate vcf records for randomish locations along a randomishly generated reference sequence. Each vcf record generator will have a randomish sized "chunk" of the reference to use Returns (reference sequence(str), iterable(vcf dicts)) ''' seq = draw(st.text(alphabet='ACGT', min_size=10, max_size=20)) size = len(seq) # This gets you a list of numbers that are randomish and increasing ranges = draw(rolling_sum(1, 3, int(size/2)).map(lambda xs: ifilter(lambda x: x < size, xs)) )#.filter(_not(bool))) # Stream lets you re-use a generator without draining it. # Pairs will hold start/stop values for each part of sequence pairs = Stream() << partition(2, ranges) # POSs will contain the start position of each vcf row POSs = Stream() << imap(operator.itemgetter(0), pairs) # VCF files start at index 1; python starts at 0 pairs_offset_1 = imap(lambda x: (x[0] - 1, x[1] - 1), pairs) #grab the pieces of the reference to build our elts from chunks = map(lambda x: seq[x[0]:x[1]], pairs_offset_1) #random chromosome name chrom = draw(st.text(string.ascii_letters)) # Draw a new record for each of the Positions we have made vcfs = map(compose(draw, partial(vcf_dict_strategy_factory, chrom)), POSs, chunks) #TODO: ranges must be non-empty. Assuming vcfs for now. # vcfs can be a a generator #assume(len(vcfs) > 0) return (seq, vcfs)
def api_results(min_size=0, max_size=20, hook_types=None): count = integers(min_value=min_size, max_value=max_size).example() hook_types = hook_types or get_hook_names() return fixed_dictionaries( { "count": just(count), "next": none(), "prev": none(), "results": lists( fixed_dictionaries( { "name": text(min_size=1), "latest_version": integers(min_value=0), "content": fixed_dictionaries( { "hook_type": sampled_from(hook_types), "version": integers(min_value=0), "description": text(min_size=1), "download_url": text(min_size=1), "checksum": text(min_size=1), } ), } ), min_size=count, max_size=count, ), } )
def docker_controller(with_envvars=True, with_labels=True, **kw): """ Generate a DockerController model with (optional) envvars and labels. """ # The slug is used in places where whitespace and colons are problematic, # so we remove them from the generated value. # TODO: Build a proper SlugField strategy. # TODO: Figure out why the field validation isn't being applied. # Slugs must be domain-name friendly - used in the "generic" domain slug = text(string.ascii_letters + string.digits + '-') kw.setdefault("slug", slug) kw.setdefault("owner", models(User, is_active=just(True))) kw.setdefault("organization", models(Organization, slug=slug)) # Prevent Hypothesis from generating domains with invalid characters domain_urls = text(string.ascii_letters + string.digits + '-.') kw.setdefault("domain_urls", domain_urls) # The model generator sees `controller_ptr` (from the PolymorphicModel # magic) as a mandatory field and objects if we don't provide a value for # it. controller = models(DockerController, controller_ptr=default_value, **kw) if with_envvars: controller = controller.flatmap(add_envvars) if with_labels: controller = controller.flatmap(add_labels) return controller
def subtitles(strict=True): """A Hypothesis strategy to generate Subtitle objects.""" # max_value settings are just to avoid overflowing TIMEDELTA_MAX_DAYS by # using arbitrary low enough numbers. # # We also skip subs with start time >= end time, so we split them into two # groups to avoid overlap. start_timestamp_strategy = timedeltas(min_value=0, max_value=500000) end_timestamp_strategy = timedeltas(min_value=500001, max_value=999999) # If we want to test \r, we'll test it by ourselves. It makes testing # harder without because we don't get the same outputs as inputs on Unix. content_strategy = st.text(min_size=1).filter(lambda x: "\r" not in x) proprietary_strategy = st.text().filter( lambda x: all(eol not in x for eol in "\r\n") ) if strict: content_strategy = content_strategy.filter(is_strictly_legal_content) subtitle_strategy = st.builds( srt.Subtitle, index=st.integers(min_value=0), start=start_timestamp_strategy, end=end_timestamp_strategy, proprietary=proprietary_strategy, content=content_strategy, ) return subtitle_strategy
def jenkins_build_results(inQueue=None, builds=None): """Create a strategy for generating Jenkins API information for a job. :param strategy inQueue: strategy for the inQueue key, or None to use the default. :param strategy builds: strategy for populating the builds key, or None for the default. The special value `NO_BUILDS` will mean that the builds key is not in the resulting dict at all. :return strategy: a strategy. """ strats = [] if inQueue is None: inQueue = booleans() strats.append(just(pmap())) without_builds = fixed_dictionaries(dict( inQueue=inQueue)) if builds is None or builds is NO_BUILDS: strats.append(without_builds) if builds is None: builds = lists(jenkins_builds, average_size=1) if builds is not NO_BUILDS: with_builds = fixed_dictionaries(dict( inQueue=inQueue, builds=builds, property=dictionaries( text(max_size=2), text(max_size=2), average_size=1, max_size=2))) strats.append(with_builds) return one_of(*strats)
def stanza_header(draw): "Generate an extended textual header stanza header." alphabet = list(PRINTABLE_ASCII_ALPHABET) # Remove the separator character alphabet.remove(':') org = draw(text(alphabet=alphabet, min_size=1, max_size=(CARD_LENGTH - 6) // 2)) assume(not org.isspace()) assume('))' not in org) name = draw(text(alphabet=alphabet, min_size=1, max_size=(CARD_LENGTH - 6) // 2)) assume(not org.isspace()) assume('))' not in name) header = '(({}: {}))'.format(org, name) assert len(header) <= CARD_LENGTH padded_header = '{:{width}}'.format(header, width=CARD_LENGTH) assert len(padded_header) == CARD_LENGTH return padded_header
def test_does_not_simplify_into_surrogates(): f = minimal(text(), lambda x: x >= u"\udfff") assert f == u"\ue000" size = 5 f = minimal(text(min_size=size), lambda x: sum(t >= u"\udfff" for t in x) >= size) assert f == u"\ue000" * size
def services( draw, service_id=uuids(), name=text(), description=text(), is_available=booleans() ) -> IService: return Service( draw(service_id), draw(name), draw(description), draw(is_available) )
def http_query_args(): """ Strategy for generating some UTF-8 key/value-list pairs usable as query arguments in a request path. """ return dictionaries( keys=text().map(lambda x: x.encode("utf-8")), values=lists(text().map(lambda x: x.encode("utf-8")), min_size=1), )
def jobs( draw, services=service_generator(), parameters=dictionaries(text(), text()), ) -> Job: return Job.new( draw(services), draw(parameters) )
def api_exceptions( draw, status_codes=integers(min_value=400, max_value=599), titles=text(), details=text() ) -> APIExceptionInterface: return _APIException( draw(status_codes), draw(titles), draw(details) )
def values(self): if PY26: base = s.text(alphabet=[chr(i) for i in hrange(1, 128)]) else: base = s.text() if self.seen_strings: return s.sampled_from(sorted(self.seen_strings)) | base else: return base
def _get_strategy_for_field(f): # type: (Type[dm.Field]) -> st.SearchStrategy[Any] if f.choices: choices = [] # type: list for value, name_or_optgroup in f.choices: if isinstance(name_or_optgroup, (list, tuple)): choices.extend(key for key, _ in name_or_optgroup) else: choices.append(value) if isinstance(f, (dm.CharField, dm.TextField)) and f.blank: choices.insert(0, u'') strategy = st.sampled_from(choices) elif type(f) == dm.SlugField: strategy = st.text(alphabet=string.ascii_letters + string.digits, min_size=(0 if f.blank else 1), max_size=f.max_length) elif type(f) == dm.GenericIPAddressField: lookup = {'both': ip4_addr_strings() | ip6_addr_strings(), 'ipv4': ip4_addr_strings(), 'ipv6': ip6_addr_strings()} strategy = lookup[f.protocol.lower()] elif type(f) in (dm.TextField, dm.CharField): strategy = st.text( alphabet=st.characters(blacklist_characters=u'\x00', blacklist_categories=('Cs',)), min_size=(0 if f.blank else 1), max_size=f.max_length, ) # We can infer a vastly more precise strategy by considering the # validators as well as the field type. This is a minimal proof of # concept, but we intend to leverage the idea much more heavily soon. # See https://github.com/HypothesisWorks/hypothesis-python/issues/1116 re_validators = [ v for v in f.validators if isinstance(v, validators.RegexValidator) and not v.inverse_match ] if re_validators: regexes = [re.compile(v.regex, v.flags) if isinstance(v.regex, str) else v.regex for v in re_validators] # This strategy generates according to one of the regexes, and # filters using the others. It can therefore learn to generate # from the most restrictive and filter with permissive patterns. # Not maximally efficient, but it makes pathological cases rarer. # If you want a challenge: extend https://qntm.org/greenery to # compute intersections of the full Python regex language. strategy = st.one_of(*[st.from_regex(r) for r in regexes]) elif type(f) == dm.DecimalField: bound = Decimal(10 ** f.max_digits - 1) / (10 ** f.decimal_places) strategy = st.decimals(min_value=-bound, max_value=bound, places=f.decimal_places) else: strategy = field_mappings().get(type(f), st.nothing()) if f.validators: strategy = strategy.filter(validator_to_filter(f)) if f.null: strategy = st.one_of(st.none(), strategy) return strategy
def dynamic_values(draw): non_matching_1 = draw(st.text().filter(not_empty_string)) exclusions = (non_matching_1, '') matching_value = draw(st.text().filter(lambda x: x not in exclusions)) return { "matching": matching_value, "non_matching": [ non_matching_1, ] }
def translated_field(draw, name, allow_missing=True, languages=LANGUAGES): result = {} for lang in languages: if allow_missing: val = draw(one_of(text(max_size=50), none())) else: val = draw(text(min_size=1, max_size=50)) if val is not None: result['{}_{}'.format(name, lang)] = val return result
def url(schemes=[], userpass=False, port=False, url=False, query=False, fragment=False): if schemes: scheme = st.just(random.choice(schemes)) else: scheme = st.text(alphabet=ascii_lowercase+digits, min_size=2) d = {'scheme': scheme, 'domain': st.lists( st.text( alphabet=ascii_lowercase + digits, min_size=1, max_size=63), min_size=1, max_size=3), 'tld': st.text(alphabet=ascii_lowercase, min_size=2, max_size=63)} if userpass: d['user'] = st.text(alphabet=ascii_lowercase + digits) d['passwd'] = st.text(alphabet=ascii_lowercase + digits) if port: d['port'] = st.integers(min_value=0, max_value=65535) if url: d['url'] = st.lists(st.text()) if query: d['query'] = st.lists(st.tuples( st.text(alphabet=ascii_lowercase, min_size=1), st.text(alphabet=ascii_lowercase + digits, min_size=1))) if fragment: d['fragment'] = st.text() urlst = strategy(st.fixed_dictionaries(d)) return urlst.map(to_url).filter(max_len)
def test_dictionary(dict_class): assert minimal(dictionaries(keys=integers(), values=text(), dict_class=dict_class)) == dict_class() x = minimal(dictionaries(keys=integers(), values=text(), dict_class=dict_class), lambda t: len(t) >= 3) assert isinstance(x, dict_class) assert set(x.values()) == set((u"",)) for k in x: if k < 0: assert k + 1 in x if k > 0: assert k - 1 in x
def requirements_list(draw, length, answers=False): if answers: elements = text() if length is not None else one_of(text(), none()) else: elements = text(min_size=1, average_size=10, max_size=300, alphabet=_descriptive_alphabet).filter( partial(_word_count_filter, max_words=30) ) return draw(lists( elements=elements, min_size=length, max_size=length if length is not None else 10 ))
def simple_attrs_with_metadata(draw): """ Create a simple attribute with arbitrary metadata. """ c_attr = draw(simple_attrs) keys = st.booleans() | st.binary() | st.integers() | st.text() vals = st.booleans() | st.binary() | st.integers() | st.text() metadata = draw(st.dictionaries(keys=keys, values=vals)) return attr.ib(c_attr._default, c_attr._validator, c_attr.repr, c_attr.cmp, c_attr.hash, c_attr.init, c_attr.convert, metadata)
def nested_checkboxes(draw, options_list_strategy=None, optional_keys=st.lists(st.sampled_from(['value', 'description']))): option = { 'label': st.text() } for k in draw(optional_keys): option[k] = st.text() if options_list_strategy is not None: option['options'] = options_list_strategy return draw(st.fixed_dictionaries(option))
def regex_strategy(regex, fullmatch): if not hasattr(regex, "pattern"): regex = re.compile(regex) is_unicode = isinstance(regex.pattern, text_type) parsed = sre_parse.parse(regex.pattern, flags=regex.flags) if not parsed: if is_unicode: return st.text() else: return st.binary() if is_unicode: base_padding_strategy = st.text() empty = st.just(u"") newline = st.just(u"\n") else: base_padding_strategy = st.binary() empty = st.just(b"") newline = st.just(b"\n") right_pad = base_padding_strategy left_pad = base_padding_strategy if fullmatch: right_pad = empty elif parsed[-1][0] == sre.AT: if parsed[-1][1] == sre.AT_END_STRING: right_pad = empty elif parsed[-1][1] == sre.AT_END: if regex.flags & re.MULTILINE: right_pad = st.one_of( empty, st.builds(operator.add, newline, right_pad) ) else: right_pad = st.one_of(empty, newline) if fullmatch: left_pad = empty elif parsed[0][0] == sre.AT: if parsed[0][1] == sre.AT_BEGINNING_STRING: left_pad = empty elif parsed[0][1] == sre.AT_BEGINNING: if regex.flags & re.MULTILINE: left_pad = st.one_of(empty, st.builds(operator.add, left_pad, newline)) else: left_pad = empty base = base_regex_strategy(regex, parsed).filter(regex.search) return maybe_pad(regex, base, left_pad, right_pad)
def test_can_simplify_lists_of_morphers_with_mixed_types(): m = find( s.lists(morphers, min_size=2), lambda xs: xs[0].become(s.text()) and xs[-1].become(s.integers())) assert len(m) == 2 for x in m: x.clear() m_as_ints = [x.become(s.integers()) for x in m] for x in m: x.clear() m_as_text = [x.become(s.text()) for x in m] assert u'0' in m_as_text assert 1 in m_as_ints
def jobs( draw, ids=uuids(), statuses=sampled_from(JobInterface.JobStatus), parameters=dictionaries(text(), text()), results=dictionaries(text(), text()), dates_submitted=datetimes(), registration_schemas=dictionaries(text(), text()), result_schemas=dictionaries(text(), text()) ) -> JobInterface: """ :param draw: A function that can take a strategy and draw a datum from it :param ids: A hypothesis strategy (statisticians should read "random variable"), that represents the set of all valid job IDs :param statuses: A hypothesis strategy that samples from the set of all allowed job statuses :param parameters: A hypothesis strategy that samples from all job parameters :param results: A hypothesis strategy that represents the possible results :param dates_submitted: A hypothesis strategy that represents the possible dates that can be submitted :param registration_schemas: The possible job registration schemas :param result_schemas: The possible job result schemas :return: A randomly-generated implementation of :class:`JobInterface` """ return Job( draw(ids), draw(statuses), draw(parameters), draw(results), draw(dates_submitted), draw(registration_schemas), draw(result_schemas) )
def test_non_trivial_json(): json = st.deferred(lambda: st.none() | st.floats() | st.text() | lists | objects) lists = st.lists(json) objects = st.dictionaries(st.text(), json) assert minimal(json) is None small_list = minimal(json, lambda x: isinstance(x, list) and x) assert small_list == [None] x = minimal(json, lambda x: isinstance(x, dict) and isinstance(x.get(""), list)) assert x == {"": []}
def api_metadata( draw, maintainer_names=text(), versions=text() ) -> APIMetadataInterface: """ :param draw: A function supplied by the ``@composite`` decorator, which knows how to draw a randomly-generated sample from the strategies provided by ``hypothesis`` :param maintainer_names: A generator for maintainer names :param versions: A generator for API versions :return: A randomly-generated API Metadata model """ return _APIMetadata( draw(maintainer_names), draw(versions) )
def test_can_find_unique_lists_of_non_set_order(): ls = minimal( lists(text(), unique=True), lambda x: list(set(reversed(x))) != x ) assert len(set(ls)) == len(ls) assert len(ls) == 2
import hypothesis import hypothesis.strategies as st import mock import pytest from six import PY2, PY3 from notebooker.web.handle_overrides import _handle_overrides_safe, handle_overrides IMPORT_REGEX = re.compile( r"^(from [a-zA-Z0-9_.]+ )?import (?P<import_target>[a-zA-Z0-9_.]+)( as (?P<name>.+))?$" ) VARIABLE_ASSIGNMENT_REGEX = re.compile( r"^(?P<variable_name>[a-zA-Z_]+) *= *(?P<value>.+)$") @hypothesis.given(st.text()) def test_handle_overrides_handles_anything_cleanly_no_process_junk(text): # Check that it doesn't just crash with random input with mock.patch("notebooker.web.handle_overrides.subprocess.check_output" ) as popen: popen.side_effect = lambda args: mock.MagicMock( res=_handle_overrides_safe(args[4], args[6])) handle_overrides(text, issues=[]) @hypothesis.given(st.from_regex(VARIABLE_ASSIGNMENT_REGEX)) def test_handle_overrides_handles_anything_cleanly_no_process_variable(text): with mock.patch("notebooker.web.handle_overrides.subprocess.check_output" ) as popen: popen.side_effect = lambda args: mock.MagicMock( res=_handle_overrides_safe(args[4], args[6]))
from hypothesis import given from hypothesis.strategies import text from iso3166 import countries import pytest from profiles.exceptions import AffiliationNotFound from profiles.models import Address, Affiliation, Date, Name, Profile @given(text(), text(), text()) def test_it_can_be_printed(id_, name, orcid): profile = Profile(id_, Name(name), orcid) assert repr(profile) == "<Profile {!r}>".format(id_) @given(text(), text(), text()) def test_it_has_an_id(id_, name, orcid): profile = Profile(id_, Name(name), orcid) assert profile.id == '{}'.format(id_) @given(text(), text(), text()) def test_it_has_a_name(id_, name, orcid): profile = Profile(id_, Name(name), orcid) assert profile.name == Name(name) @given(text(), text(), text())
[2] http://www.ietf.org/rfc/rfc1738.txt """ assert urlmatch.UrlPattern(pattern).matches(QUrl(url)) def test_urlpattern_benchmark(benchmark): url = QUrl('https://www.example.com/barfoobar') def run(): up = urlmatch.UrlPattern('https://*.example.com/*foo*') up.matches(url) benchmark(run) URL_TEXT = hst.text(alphabet=string.ascii_letters) @hypothesis.given(pattern=hst.builds( lambda *a: ''.join(a), # Scheme hst.one_of(hst.just('*'), hst.just('http'), hst.just('file')), # Separator hst.one_of(hst.just(':'), hst.just('://')), # Host hst.one_of(hst.just('*'), hst.builds(lambda *a: ''.join(a), hst.just('*.'), URL_TEXT), URL_TEXT), # Port hst.one_of( hst.just(''),
from string import ascii_letters import pytest from hypothesis import assume, example, given from hypothesis.strategies import text from Levenshtein import distance from utils import get_modified_distance @given(text(alphabet=ascii_letters), text(alphabet=ascii_letters)) @example("", "") @example("some_word", "") def test_get_distance_basic_lev(spelling: str, normal_form: str): """ make sure the modified distance gets basic lev distance right """ assume("h" not in spelling.lower() and "h" not in normal_form.lower()) assert get_modified_distance(spelling, normal_form) == distance( spelling.lower(), normal_form.lower() ) @pytest.mark.parametrize( ("spelling", "normal_form", "expected_distance"), ( ["atâk", "atâhk", 0.5], # an 'h' in a rime is not added ["ah", "a", 0.5], # an extra 'h' in a rime is added ["atahk", "atâhk", 0.5], # a circumflex is not added ["wâpamew", "wâpamêw", 0], # a circumflex on 'e' is omitted, don't punish ["atak", "atâhk", 1], # both circumflex and h in a rime is not added
try: from unittest.mock import patch except ImportError: from mock import patch PYTHON = supported_languages['.py'] PYCCO_SOURCE = 'pycco/main.py' FOO_FUNCTION = """def foo():\n return True""" def get_language(choice): return choice(list(supported_languages.values())) @given(lists(text()), text()) def test_shift(fragments, default): if fragments == []: assert p.shift(fragments, default) == default else: fragments2 = copy.copy(fragments) head = p.shift(fragments, default) assert [head] + fragments == fragments2 @given(text(), booleans(), text(min_size=1)) @example("/foo", True, "0") def test_destination(filepath, preserve_paths, outdir): dest = p.destination(filepath, preserve_paths=preserve_paths, outdir=outdir)
class PortfolioApi(WebTestMixin, TestCase): def test_user_is_not_authenticated___response_is_forbidden(self): portfolio = fake_portfolio() response = self.app.get(portfolio.get_absolute_url(), expect_errors=True) self.assertIn(response.status_code, [401,403]) def test_user_is_authenticated_object_does_not_exist___response_is_404(self): user = fake_user() portfolio = fake_portfolio() response = self.app.get( reverse('portfolio-detail', kwargs={'version': 'v1', 'pk': portfolio.pk + 1}), expect_errors=True, headers={ 'Authorization': 'Bearer {}'.format(AccessToken.for_user(user)) } ) self.assertEqual(404, response.status_code) def test_name_is_not_provided___response_is_400(self): user = fake_user() response = self.app.post( reverse('portfolio-list', kwargs={'version': 'v1'}), expect_errors=True, headers={ 'Authorization': 'Bearer {}'.format(AccessToken.for_user(user)) }, params={}, content_type='application/json', ) self.assertEqual(400, response.status_code) @given(name=text(alphabet=' \t\n\r', max_size=10)) def test_cleaned_name_is_empty___response_is_400(self, name): user = fake_user() response = self.app.post( reverse('portfolio-list', kwargs={'version': 'v1'}), expect_errors=True, headers={ 'Authorization': 'Bearer {}'.format(AccessToken.for_user(user)) }, params=json.dumps({'name': name}), content_type='application/json' ) self.assertEqual(400, response.status_code) @given(name=text(alphabet=string.ascii_letters, max_size=10, min_size=1)) def test_cleaned_name_is_present___object_is_created(self, name): self.maxDiff = None with TemporaryDirectory() as d: with override_settings(MEDIA_ROOT=d): user = fake_user() response = self.app.post( reverse('portfolio-list', kwargs={'version': 'v1'}), headers={ 'Authorization': 'Bearer {}'.format(AccessToken.for_user(user)) }, params=json.dumps({'name': name}), content_type='application/json' ) self.assertEqual(201, response.status_code) portfolio = Portfolio.objects.get(pk=response.json['id']) portfolio.accounts_file = fake_related_file() portfolio.location_file = fake_related_file() portfolio.reinsurance_scope_file = fake_related_file() portfolio.reinsurance_info_file = fake_related_file() portfolio.save() response = self.app.get( portfolio.get_absolute_url(), headers={ 'Authorization': 'Bearer {}'.format(AccessToken.for_user(user)) }, ) self.assertEqual(200, response.status_code) self.assertEqual({ 'id': portfolio.pk, 'name': name, 'created': portfolio.created.strftime('%Y-%m-%dT%H:%M:%S.%fZ'), 'modified': portfolio.modified.strftime('%Y-%m-%dT%H:%M:%S.%fZ'), 'accounts_file': { "uri": response.request.application_url + portfolio.get_absolute_accounts_file_url(), "name": portfolio.accounts_file.filename, "stored": str(portfolio.accounts_file.file) }, 'location_file': { "uri": response.request.application_url + portfolio.get_absolute_location_file_url(), "name": portfolio.location_file.filename, "stored": str(portfolio.location_file.file) }, 'reinsurance_info_file': { "uri": response.request.application_url + portfolio.get_absolute_reinsurance_info_file_url(), "name": portfolio.reinsurance_info_file.filename, "stored": str(portfolio.reinsurance_info_file.file) }, 'reinsurance_scope_file': { "uri": response.request.application_url + portfolio.get_absolute_reinsurance_scope_file_url(), "name": portfolio.reinsurance_scope_file.filename, "stored": str(portfolio.reinsurance_scope_file.file) }, }, response.json)
from datetime import date from hypothesis.strategies import lists, tuples, just, text, integers, dates bgm_info_field = ( tuples(just("description"), text()) | tuples(just("filename"), text()) | tuples(just("mark"), text()) | tuples(just("youtube"), text()) ) bgm_info_data = lists(bgm_info_field).map(dict) metadata_field = ( tuples(just("album-artist"), text()) | tuples(just("artist"), text()) | tuples(just("title"), text()) | tuples(just("year"), integers().map(str)) ) source_field = ( tuples(just("client"), text()) | tuples(just("date"), dates().map(date.isoformat)) | tuples(just("structure"), text()) | tuples(just("version"), text()) )
def resolve_Match(thing): if thing.__args__[0] == bytes: return (st.binary(min_size=1).map( lambda c: re.match(b".", c, flags=re.DOTALL)).filter(bool)) return st.text().map(lambda c: re.match(".", c, flags=re.DOTALL)).filter( bool)
type(None): st.none(), bool: st.booleans(), int: st.integers(), float: st.floats(), complex: st.complex_numbers(), fractions.Fraction: st.fractions(), decimal.Decimal: st.decimals(), str: st.text(), bytes: st.binary(), datetime.datetime: st.datetimes(), datetime.date: st.dates(), datetime.time: st.times(), datetime.timedelta: st.timedeltas(), datetime.timezone: st.builds(datetime.timezone, offset=utc_offsets) | st.builds(datetime.timezone, offset=utc_offsets, name=st.text(st.characters())),
match=f'Run with run_id ' f'{non_existing_run_id} does not ' f'exist in the database'): _ = load_by_id(non_existing_run_id) @pytest.mark.usefixtures('experiment') def test_load_by_id_for_none(): with pytest.raises(ValueError, match='run_id has to be a positive integer, ' 'not None.'): _ = load_by_id(None) @settings(deadline=None, max_examples=6) @given(experiment_name=hst.text(min_size=1), sample_name=hst.text(min_size=1), dataset_name=hst.text( hst.characters(whitelist_categories=_unicode_categories), min_size=1)) @pytest.mark.usefixtures("empty_temp_db") def test_add_experiments(experiment_name, sample_name, dataset_name): global n_experiments n_experiments += 1 _ = new_experiment(experiment_name, sample_name=sample_name) exps = experiments() assert len(exps) == n_experiments exp = exps[-1] assert exp.name == experiment_name assert exp.sample_name == sample_name
from hypothesis import given from hypothesis.strategies import characters, text import lzw # only bytes strings supported! @given(x=text(alphabet=characters(max_codepoint=255))) def test_property(x): assert lzw.decompress(lzw.compress(x)) == x test_property()
exceptions_from_error_tuple, filename_for_module, handle_in_app_impl, iter_event_stacktraces, to_base64, from_base64, ) from sentry_sdk._compat import text_type, string_types try: from hypothesis import given import hypothesis.strategies as st except ImportError: pass else: any_string = st.one_of(st.binary(), st.text()) @given(x=any_string) def test_safe_repr_never_broken_for_strings(x): r = safe_repr(x) assert isinstance(r, text_type) assert u"broken repr" not in r def test_safe_repr_regressions(): assert u"лошадь" in safe_repr(u"лошадь") @pytest.mark.xfail( sys.version_info < (3, ), reason="Fixing this in Python 2 would break other behaviors",
import hypothesis from hypothesis import strategies from qutebrowser.config import configtypes, configexc def gen_classes(): for _name, member in inspect.getmembers(configtypes, inspect.isclass): if member is configtypes.BaseType: pass elif member is configtypes.MappingType: pass elif member is configtypes.FormatString: yield functools.partial(member, fields=['a', 'b']) elif issubclass(member, configtypes.BaseType): yield member @pytest.mark.parametrize('klass', gen_classes()) @hypothesis.given(strategies.text()) @hypothesis.example('\x00') def test_configtypes_hypothesis(klass, s): if (klass in [configtypes.File, configtypes.UserStyleSheet] and sys.platform == 'linux' and not os.environ.get('DISPLAY', '')): pytest.skip("No DISPLAY available") try: klass().validate(s) except configexc.ValidationError: pass
class AuthorizationTests(SyncTestCase): """ Tests for the authorization requirements for resources beneath ``/v1``. """ @given( good_token=tokens(), bad_tokens=lists(tokens()), child_segments=lists(text()), ) def test_unauthorized(self, good_token, bad_tokens, child_segments): """ If the correct bearer token is not given in the **Authorization** header of the request then the response code is UNAUTHORIZED. :param bytes good_token: A bearer token which, when presented, should authorize access to the resource. :param bad_tokens: A list of bearer token which, when presented all at once, should not authorize access to the resource. If this is empty no tokens are presented at all. If it contains more than one element then it creates a bad request with multiple authorization header values. :param [unicode] child_segments: Additional path segments to add to the request path beneath **v1**. """ # We're trying to test the *unauthorized* case. Don't randomly hit # the authorized case by mistake. assume([good_token] != bad_tokens) def get_auth_token(): return good_token root = magic_folder_resource(get_auth_token, Resource()) treq = StubTreq(root) url = DecodedURL.from_text(u"http://example.invalid./v1").child( *child_segments) encoded_url = url_to_bytes(url) # A request with no token at all or the wrong token should receive an # unauthorized response. headers = {} if bad_tokens: headers[b"Authorization"] = list( u"Bearer {}".format(bad_token).encode("ascii") for bad_token in bad_tokens) self.assertThat( treq.get( encoded_url, headers=headers, ), succeeded(matches_response(code_matcher=Equals(UNAUTHORIZED)), ), ) @given( auth_token=tokens(), child_segments=lists(path_segments()), content=binary(), ) def test_authorized(self, auth_token, child_segments, content): """ If the correct bearer token is not given in the **Authorization** header of the request then the response code is UNAUTHORIZED. :param bytes auth_token: A bearer token which, when presented, should authorize access to the resource. :param [unicode] child_segments: Additional path segments to add to the request path beneath **v1**. :param bytes content: The bytes we expect to see on a successful request. """ def get_auth_token(): return auth_token # Since we don't want to exercise any real magic-folder application # logic we'll just magic up the child resource being requested. branch = Data( content, b"application/binary", ) segments_remaining = child_segments[:] while segments_remaining: name = segments_remaining.pop() resource = Resource() resource.putChild(name.encode("utf-8"), branch) branch = resource root = magic_folder_resource( get_auth_token, v1_resource=branch, ) treq = StubTreq(root) url = DecodedURL.from_text(u"http://example.invalid./v1").child( *child_segments) encoded_url = url_to_bytes(url) # A request with no token at all or the wrong token should receive an # unauthorized response. headers = { b"Authorization": u"Bearer {}".format(auth_token).encode("ascii"), } self.assertThat( treq.get( encoded_url, headers=headers, ), succeeded( matches_response( code_matcher=Equals(OK), body_matcher=Equals(content), ), ), )
def test_minimize_string_to_empty(): assert minimal(text()) == u''
def chromeReqResp(): # XXX: will this gnerated the same url for all testcases? reqid = st.shared(st.text(), 'reqresp') url = st.shared(urlsStr(), 'reqresp') return st.tuples(chromeRequestWillBeSent(reqid, url), chromeResponseReceived(reqid, url))
size = 15 set_of_sets = minimal( sets(frozensets(elements)), lambda s: len(s) >= size ) assert frozenset() in set_of_sets assert len(set_of_sets) == size for s in set_of_sets: if len(s) > 1: assert any( s != t and t.issubset(s) for t in set_of_sets ) @pytest.mark.parametrize( (u'string',), [(text(),), (binary(),)], ids=[u'text', u'binary()'] ) def test_minimal_unsorted_strings(string): def dedupe(xs): result = [] for x in xs: if x not in result: result.append(x) return result result = minimal( lists(string).map(dedupe), lambda xs: assume(len(xs) >= 5) and sorted(xs) != xs ) assert len(result) == 5
st.one_of(st.none(), st.integers(min_value=1, max_value=2**16 - 1)), 'path': pathSt, 'query_string': st.text(), 'fragment': st.text(), }) return st.builds(lambda x: URL.build(**x), args) def urlsStr(): return st.builds(lambda x: str(x), urls()) asciiText = st.text(st.characters(min_codepoint=32, max_codepoint=126)) def chromeHeaders(): # token as defined by https://tools.ietf.org/html/rfc7230#section-3.2.6 token = st.sampled_from( 'abcdefghijklmnopqrstuvwxyz0123456789!#$%&\'*+-.^_`|~') # XXX: the value should be asciiText without leading/trailing spaces return st.dictionaries(token, token) def fixedDicts(fixed, dynamic): return st.builds(lambda x, y: x.update(y), st.fixed_dictionaries(fixed), st.lists(dynamic))
class PortfolioApiCreateAnalysis(WebTestMixin, TestCase): def test_user_is_not_authenticated___response_is_forbidden(self): portfolio = fake_portfolio() response = self.app.get(portfolio.get_absolute_create_analysis_url(), expect_errors=True) self.assertIn(response.status_code, [401,403]) def test_user_is_authenticated_object_does_not_exist___response_is_404(self): user = fake_user() portfolio = fake_portfolio() response = self.app.post( reverse('portfolio-create-analysis', kwargs={'version': 'v1', 'pk': portfolio.pk + 1}), expect_errors=True, headers={ 'Authorization': 'Bearer {}'.format(AccessToken.for_user(user)) } ) self.assertEqual(404, response.status_code) def test_name_is_not_provided___response_is_400(self): user = fake_user() model = fake_analysis_model() portfolio = fake_portfolio() response = self.app.post( portfolio.get_absolute_create_analysis_url(), expect_errors=True, headers={ 'Authorization': 'Bearer {}'.format(AccessToken.for_user(user)) }, params={'model': model.pk}, content_type='application/json', ) self.assertEqual(400, response.status_code) def test_portfolio_does_not_have_location_file_set___response_is_400(self): user = fake_user() model = fake_analysis_model() portfolio = fake_portfolio() response = self.app.post( portfolio.get_absolute_create_analysis_url(), expect_errors=True, headers={ 'Authorization': 'Bearer {}'.format(AccessToken.for_user(user)) }, params=json.dumps({'name': 'name', 'model': model.pk}), content_type='application/json', ) self.assertEqual(400, response.status_code) self.assertIn('"location_file" must not be null', response.json['portfolio']) def test_model_is_not_provided___response_is_400(self): user = fake_user() portfolio = fake_portfolio() response = self.app.post( portfolio.get_absolute_create_analysis_url(), expect_errors=True, headers={ 'Authorization': 'Bearer {}'.format(AccessToken.for_user(user)) }, params=json.dumps({'name': 'name'}), content_type='application/json', ) self.assertEqual(400, response.status_code) def test_model_does_not_exist___response_is_400(self): user = fake_user() portfolio = fake_portfolio() model = fake_analysis_model() response = self.app.post( portfolio.get_absolute_create_analysis_url(), expect_errors=True, headers={ 'Authorization': 'Bearer {}'.format(AccessToken.for_user(user)) }, params=json.dumps({'name': 'name', 'model': model.pk + 1}), content_type='application/json', ) self.assertEqual(400, response.status_code) @given(name=text(alphabet=' \t\n\r', max_size=10)) def test_cleaned_name_is_empty___response_is_400(self, name): user = fake_user() model = fake_analysis_model() portfolio = fake_portfolio() response = self.app.post( portfolio.get_absolute_create_analysis_url(), expect_errors=True, headers={ 'Authorization': 'Bearer {}'.format(AccessToken.for_user(user)) }, params=json.dumps({'name': name, 'model': model.pk}), content_type='application/json' ) self.assertEqual(400, response.status_code) @given(name=text(alphabet=string.ascii_letters, max_size=10, min_size=1)) def test_cleaned_name_and_model_are_present___object_is_created_inputs_are_generated(self, name): with patch('src.server.oasisapi.analyses.models.Analysis.generate_inputs', autospec=True) as generate_mock: with TemporaryDirectory() as d: with override_settings(MEDIA_ROOT=d): self.maxDiff = None user = fake_user() model = fake_analysis_model() portfolio = fake_portfolio(location_file=fake_related_file()) response = self.app.post( portfolio.get_absolute_create_analysis_url(), headers={ 'Authorization': 'Bearer {}'.format(AccessToken.for_user(user)) }, params=json.dumps({'name': name, 'model': model.pk}), content_type='application/json' ) self.assertEqual(201, response.status_code) analysis = Analysis.objects.get(pk=response.json['id']) analysis.settings_file = fake_related_file() analysis.input_file = fake_related_file() analysis.lookup_errors_file = fake_related_file() analysis.lookup_success_file = fake_related_file() analysis.lookup_validation_file = fake_related_file() analysis.input_generation_traceback_file = fake_related_file() analysis.output_file = fake_related_file() analysis.run_traceback_file = fake_related_file() analysis.save() response = self.app.get( analysis.get_absolute_url(), headers={ 'Authorization': 'Bearer {}'.format(AccessToken.for_user(user)) }, ) self.assertEqual(200, response.status_code) self.assertEqual(response.json['id'], analysis.pk) self.assertEqual(response.json['created'], analysis.created.strftime('%Y-%m-%dT%H:%M:%S.%fZ')) self.assertEqual(response.json['modified'], analysis.modified.strftime('%Y-%m-%dT%H:%M:%S.%fZ')) self.assertEqual(response.json['name'], name) self.assertEqual(response.json['portfolio'], portfolio.pk) self.assertEqual(response.json['model'], model.pk) self.assertEqual(response.json['settings_file'], response.request.application_url + analysis.get_absolute_settings_file_url()) self.assertEqual(response.json['input_file'], response.request.application_url + analysis.get_absolute_input_file_url()) self.assertEqual(response.json['lookup_errors_file'], response.request.application_url + analysis.get_absolute_lookup_errors_file_url()) self.assertEqual(response.json['lookup_success_file'], response.request.application_url + analysis.get_absolute_lookup_success_file_url()) self.assertEqual(response.json['lookup_validation_file'], response.request.application_url + analysis.get_absolute_lookup_validation_file_url()) self.assertEqual(response.json['input_generation_traceback_file'], response.request.application_url + analysis.get_absolute_input_generation_traceback_file_url()) self.assertEqual(response.json['output_file'], response.request.application_url + analysis.get_absolute_output_file_url()) self.assertEqual(response.json['run_traceback_file'], response.request.application_url + analysis.get_absolute_run_traceback_file_url()) generate_mock.assert_called_once_with(analysis, user)
def test_unique_lists_of_single_characters(): x = minimal( lists(text(max_size=1), unique=True, min_size=5) ) assert sorted(x) == ['', '0', '1', '2', '3']
def test_build_idx_line_map(): example = """ Here's a place where I'd like a sorted listing of all line breaks in this string. This way, given an index, I can bisect into it and get a quick answer to where in this program is my index, instead of having to seek through each time which is unbelievably expensive. That was an empty line, but it should've shown up. """ for pos in build_idx_line_map(example): assert example[pos] == "\n" @given(text()) def test_build_idx_line_map_generally(passed): for pos in build_idx_line_map(passed): assert passed[pos] == "\n" # this demonstrates that there's no cache invalidation set up but # this shows that it works programmatically # another way would be to tag things but now everything using memoize has to be # aware of memoize # instead the restriction is you should only memoize on immutable values def test_memoize_caches(): state = {'network': 'up'} @memoize def potentially_expensive_network_request(key):
def test_minimize_longer_list_of_strings(): assert minimal(lists(text()), lambda x: len(x) >= 10) == [u''] * 10
def test_option_none_equals_none(): xs = Nothing ys = Nothing assert xs == ys def test_option_none_not_equals_some(): xs = Some(42) ys = Nothing assert xs != ys assert ys != xs @given(st.one_of(st.integers(), st.text(), st.floats()), st.one_of(st.integers(), st.text(), st.floats())) def test_option_some_equals_some(a: Any, b: Any): xs = Some(a) ys = Some(b) assert xs == ys if a == b else xs != ys def test_option_some_map_piped(): xs = Some(42) ys: Option[int] = xs.pipe(option.map(lambda x: x + 1)) for y in ys: assert y == 43 break
def test_minimize_longer_string(): assert minimal(text(), lambda x: len(x) >= 10) == u'0' * 10
floats, complex_numbers, fixed_dictionaries, ) from pyresult import is_ok, is_error, value, ok, error from pydecoder.json import getter from pydecoder.fields import hardcoded, optional, required DATA_STRATEGY = fixed_dictionaries({ 'field': one_of( none(), integers(), text(), booleans(), floats(), complex_numbers(), ), }) def equal(left, right): if (isinstance(left, (float, complex)) and isnan(left) and isinstance(right, (float, complex)) and isnan(right)): return True return left == right
def test_minimize_mixed_list(): mixed = minimal(lists(integers() | text()), lambda x: len(x) >= 10) assert set(mixed).issubset(set((0, u'')))
# coding=utf-8 # # This file is part of Hypothesis, which may be found at # https://github.com/HypothesisWorks/hypothesis/ # # Most of this work is copyright (C) 2013-2019 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 https://mozilla.org/MPL/2.0/. # # END HEADER from __future__ import absolute_import, division, print_function import unicodedata from hypothesis import given, settings from hypothesis.strategies import text @given(text(min_size=1, max_size=1)) @settings(max_examples=2000) def test_does_not_generate_surrogates(t): assert unicodedata.category(t) != u"Cs"
def test_minimize_one_of(): for _ in hrange(100): assert minimal(integers() | text() | booleans()) in ( 0, u'', False )
def textual_header_line(draw): return draw( text(alphabet=PRINTABLE_ASCII_ALPHABET, min_size=CARD_LENGTH, max_size=CARD_LENGTH))
"rseed", "src", "testcmds", "debug", "nocov", ] for e in expected_args: assert hasattr(args, e) #################################################################################################### # PROPERTY TESTS #################################################################################################### TEXT_STRATEGY = st.text(alphabet=st.characters(blacklist_categories=("Cs", "Cc", "Po")), min_size=1) # no arguments, so no given assumption def test_cli_epilog_invariant(): """Property: 1. cli-epilog always returns a string value for screen printing """ result = cli_epilog() assert isinstance(result, str) assert len(result) > 1 @given(TEXT_STRATEGY.map(lambda x: Path(x)), st.integers(), st.integers()) def test_cli_summary_report_invariant(mock_args, mock_TrialTimes, s, lm, li):