Python hypothesis.strategies.text() Examples
The following are 30
code examples of hypothesis.strategies.text().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
hypothesis.strategies
, or try the search function
.
Example #1
Source File: fuzzer.py From fuzz-lightyear with Apache License 2.0 | 7 votes |
def _fuzz_string( parameter: Dict[str, Any], required: bool = False, ) -> SearchStrategy: if parameter.get('in', None) == 'header': return st.text( # According to RFC 7230, non-ascii letters are deprecated, and there's # no telling what the server will do if they are sent. Since the intent # is not to break the server, but to send valid requests instead, we're # just going to limit it accordingly. alphabet=string.ascii_letters, ) # TODO: Handle a bunch of swagger string formats. # https://swagger.io/docs/specification/data-models/data-types/#string kwargs = {} # type: Dict[str, Any] if parameter.get('required', required): kwargs['min_size'] = 1 if not get_settings().unicode_enabled: kwargs['alphabet'] = string.printable return st.text(**kwargs)
Example #2
Source File: tough-bonus-problems.py From escape-from-automanual-testing with GNU Affero General Public License v3.0 | 6 votes |
def from_schema(schema): """Returns a strategy for objects that match the given schema.""" check_schema(schema) # TODO: actually handle constraints on number/string/array schemas return dict( null=st.none(), bool=st.booleans(), number=st.floats(allow_nan=False), string=st.text(), array=st.lists(st.nothing()), )[schema["type"]] # `@st.composite` is one way to write this - another would be to define a # bare function, and `return st.one_of(st.none(), st.booleans(), ...)` so # each strategy can be defined individually. Use whichever seems more # natural to you - the important thing in tests is usually readability!
Example #3
Source File: strategies.py From vistir with ISC License | 6 votes |
def urls(): """ Strategy for generating urls. """ return st.builds( parsed_url, scheme=st.sampled_from(uri_schemes), netloc=dns_names(), path=st.lists( st.text( max_size=64, alphabet=st.characters( blacklist_characters="/?#", blacklist_categories=("Cs",) ), ), min_size=1, max_size=10, ) .map(to_text) .map("".join), )
Example #4
Source File: signatures_test.py From bayesmark with Apache License 2.0 | 6 votes |
def sig_pair(): def separate(D): signatures, signatures_ref = {}, {} for kk in D: if len(D[kk]) == 1: v_ref, = D[kk] signatures_ref[kk] = np.asarray(v_ref) elif len(D[kk]) == 2: v, v_ref = D[kk] signatures[kk] = np.asarray(v) signatures_ref[kk] = np.asarray(v_ref) else: assert False return signatures, signatures_ref sig_dict = dictionaries(text(), tuples(bsigs()) | tuples(bsigs(), bsigs())) S = sig_dict.map(separate) return S
Example #5
Source File: test_prettyprinter.py From prettyprinter with MIT License | 6 votes |
def possibly_commented(strategy): @st.composite def _strategy(draw): value = draw(strategy) add_trailing_comment = False if isinstance(value, (list, tuple, set)): add_trailing_comment = draw(st.booleans()) add_comment = draw(st.booleans()) if add_trailing_comment: comment_text = draw(st.text(alphabet='abcdefghijklmnopqrstuvwxyz #\\\'"')) value = trailing_comment(value, comment_text) if add_comment: comment_text = draw(st.text(alphabet='abcdefghijklmnopqrstuvwxyz #\\\'"')) value = comment(value, comment_text) return value return _strategy()
Example #6
Source File: strategies.py From attrs with MIT License | 6 votes |
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, min_size=1, max_size=3) ) return attr.ib( default=c_attr._default, validator=c_attr._validator, repr=c_attr.repr, eq=c_attr.eq, order=c_attr.order, hash=c_attr.hash, init=c_attr.init, metadata=metadata, type=None, converter=c_attr.converter, )
Example #7
Source File: test_commands.py From schemathesis with MIT License | 6 votes |
def test_invalid_endpoint(cli, cli_args, workers): # When the app's schema contains errors # For example if its type is "int" but should be "integer" # And schema validation is disabled result = cli.run(*cli_args, f"--workers={workers}", "--validate-schema=false") # Then the whole Schemathesis run should fail assert result.exit_code == ExitCode.TESTS_FAILED, result.stdout # And standard Hypothesis error should not appear in the output assert "You can add @seed" not in result.stdout # And this endpoint should be marked as errored in the progress line lines = result.stdout.split("\n") if workers == 1: assert lines[10].startswith("POST /api/invalid E") else: assert lines[10] == "E" assert " POST: /api/invalid " in lines[13] # There shouldn't be a section end immediately after section start - there should be some error text # An internal error happened during a test run # Error: AssertionError assert not lines[14].startswith("=")
Example #8
Source File: test_output_mask.py From telepresence with Apache License 2.0 | 6 votes |
def generate_dictionary_with_fixed_tokens(draw): """ Builds random nested dictionary structure which is then used as JSON to mask two fixed "token" keys. Structure is based on TEST_JSON sample fixture defined above. """ base = draw( st.fixed_dictionaries({'token': st.text(printable, min_size=10)}) ) optional = draw( st.nothing() | st.dictionaries( st.text(ascii_letters, min_size=1), st.floats() | st.integers() | st.text(printable) | st.booleans() | st.nothing(), min_size=10, max_size=50 ) ) return {**base, **optional}
Example #9
Source File: test_browser.py From crocoite with MIT License | 6 votes |
def chromeResponseReceived (reqid, url): mimeTypeSt = st.one_of (st.none (), st.just ('text/html')) remoteIpAddressSt = st.one_of (st.none (), st.just ('127.0.0.1')) protocolSt = st.one_of (st.none (), st.just ('h2')) statusCodeSt = st.integers (min_value=100, max_value=999) typeSt = st.sampled_from (['Document', 'Stylesheet', 'Image', 'Media', 'Font', 'Script', 'TextTrack', 'XHR', 'Fetch', 'EventSource', 'WebSocket', 'Manifest', 'SignedExchange', 'Ping', 'CSPViolationReport', 'Other']) return st.fixed_dictionaries ({ 'requestId': reqid, 'timestamp': timestamp, 'type': typeSt, 'response': st.fixed_dictionaries ({ 'url': url, 'requestHeaders': chromeHeaders (), # XXX: make this optional 'headers': chromeHeaders (), 'status': statusCodeSt, 'statusText': asciiText, 'mimeType': mimeTypeSt, 'remoteIPAddress': remoteIpAddressSt, 'protocol': protocolSt, }) })
Example #10
Source File: dataset_strategy.py From segpy with GNU Affero General Public License v3.0 | 6 votes |
def stanza_line(draw): "Generate an extended textual header stanze non-header line." alphabet = list(PRINTABLE_ASCII_ALPHABET) # Remove the separator character alphabet.remove('=') key = draw(text(alphabet=alphabet, min_size=1, max_size=(CARD_LENGTH - 3) // 2)) value = draw(text(alphabet=alphabet, min_size=1, max_size=(CARD_LENGTH - 3) // 2)) line = '{} = {}'.format(key, value) assert len(line) <= CARD_LENGTH padded_line = '{:{width}}'.format(line, width=CARD_LENGTH) assert len(padded_line) == CARD_LENGTH return padded_line
Example #11
Source File: hypothesis_state.py From terraform.py with Apache License 2.0 | 6 votes |
def lineage_st(draw): """Hypothesis strategy for generated lineage strings.""" first = draw(st.text( alphabet = list('abcdef0123456789'), min_size=8, max_size=8)) second = draw(st.text( alphabet = list('abcdef0123456789'), min_size=4, max_size=4)) third = draw(st.text( alphabet = list('abcdef0123456789'), min_size=4, max_size=4)) fourth = draw(st.text( alphabet = list('abcdef0123456789'), min_size=4, max_size=4)) fifth = draw(st.text( alphabet = list('abcdef0123456789'), min_size=12, max_size=12)) return '{}-{}-{}-{}-{}'.format(first, second, third, fourth, fifth)
Example #12
Source File: hypothesis_state.py From terraform.py with Apache License 2.0 | 6 votes |
def s3_bucket_name_st(draw): """Hypothesis strategy for s3 bucket names. http://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-s3-bucket-naming-requirements.html """ char1 = draw(st.text( alphabet = list(ascii_lowercase + digits), min_size=1, max_size=1)) middle = draw(st.text( alphabet = list(ascii_lowercase + digits + '.-'), min_size = 4, max_size = 61).filter(lambda x: '..' not in x and '.-' not in x and '.-' not in x)) endchar = draw(st.text( alphabet = list(ascii_lowercase + digits + '.'), min_size = 1, max_size = 1)) return '{}{}{}'.format(char1, middle, endchar)
Example #13
Source File: hypothesis_state.py From terraform.py with Apache License 2.0 | 6 votes |
def remote_init_st(draw): """Hypothesis strategy to generate terraform remote init state.""" be_type = draw(st.sampled_from(['s3'])) ri_dict = { "version": 3, "serial": 0, "lineage": draw(lineage_st()), "backend": { "type": be_type, "config": draw(get_be_config_st(be_type)()), "hash": draw(st.text(alphabet=list(digits), min_size=18, max_size=18)) }, "modules": [ { "path": [ "root" ], "outputs": {}, "resources": {}, "depends_on": [] } ] } return ri_dict
Example #14
Source File: test_corpus.py From tmtoolkit with Apache License 2.0 | 6 votes |
def test_corpus_add_doc(): c = Corpus() with pytest.raises(ValueError): c.add_doc('', 'x') with pytest.raises(ValueError): c.add_doc(123, 'x') with pytest.raises(ValueError): c.add_doc('d1', None) c.add_doc('d1', 'd1 text') with pytest.raises(ValueError): c.add_doc('d1', 'd1 text') c.add_doc('d2', '') assert set(c.keys()) == {'d1', 'd2'}
Example #15
Source File: test_corpus.py From tmtoolkit with Apache License 2.0 | 6 votes |
def test_corpus_from_tabular(): for ext in ('csv', 'xlsx'): c = Corpus.from_tabular('tests/data/100NewsArticles.' + ext, 'article_id', 'text') assert len(c.docs) == 100 assert all(dl.startswith('100NewsArticles') for dl in c.doc_labels) c.add_tabular('tests/data/100NewsArticles.' + ext, 'article_id', 'text', prepend_columns=['title', 'subtitle'], doc_label_fmt='added-{id}') assert len(c.docs) == 200 n_added = 0 for dl, nchars in c.doc_lengths.items(): if dl.startswith('added'): n_added += 1 _, doc_id = dl.split('-') assert nchars >= c.doc_lengths['100NewsArticles-' + doc_id] assert n_added == 100 assert len(Corpus.from_tabular('tests/data/bt18_speeches_sample.csv', 0, 2)) == 1000
Example #16
Source File: _testtools.py From tmtoolkit with Apache License 2.0 | 6 votes |
def _strategy_2d_array(dtype, minval=0, maxval=None, **kwargs): if 'min_side' in kwargs: min_side = kwargs.pop('min_side') else: min_side = 1 if 'max_side' in kwargs: max_side = kwargs.pop('max_side') else: max_side = None if dtype is np.int: elems = st.integers(minval, maxval, **kwargs) elif dtype is np.float: elems = st.floats(minval, maxval, **kwargs) elif dtype is np.str: elems = st.text(min_size=minval, max_size=maxval, **kwargs) else: raise ValueError('no elements strategy for dtype', dtype) return arrays(dtype, array_shapes(2, 2, min_side, max_side), elements=elems)
Example #17
Source File: test_encryption.py From pycryptoki with Apache License 2.0 | 5 votes |
def test_get_string_from_list(self, list_val): """ _get_string_from_list w/ list of random text :param list_val: list of random text """ list_val = [b(x) for x in list_val] assert encrypt._get_string_from_list(list_val) == b"".join(list_val)
Example #18
Source File: test_dvol.py From dvol with Apache License 2.0 | 5 votes |
def path_segments(): """ Strategy for generating path segments that we support. """ # XXX: Fix the bug about empty volume names # XXX: Handle unicode / weird volume names by rejecting them in dvol # XXX: Impose a maximum volume name length (at least so rendering is easy!) # XXX: How do we handle case-insensitive file systems? # XXX: Fix more boring wrapping output bugs (112 was too large, here). return text( alphabet=letters, min_size=1, max_size=40).map(lambda t: t.lower())
Example #19
Source File: test_warc.py From crocoite with MIT License | 5 votes |
def jsonObject (): """ JSON-encodable objects """ return st.dictionaries (st.text (), st.one_of (st.integers (), st.text ()))
Example #20
Source File: test_forwarder.py From telepresence with Apache License 2.0 | 5 votes |
def labels(): """ Build random DNS labels. This can't build every possible DNS label. It just does enough to exercise the suffix detection logic (assuming that logic is independent of the particular bytes of the labels). """ return st.text( alphabet=ascii_lowercase, min_size=1, )
Example #21
Source File: strategies.py From vistir with ISC License | 5 votes |
def dns_labels(): """ Strategy for generating limited charset DNS labels. """ # This is too limited, but whatever return st.text( "abcdefghijklmnopqrstuvwxyz0123456789-", min_size=1, max_size=25 ).filter( lambda s: not any( [s.startswith("-"), s.endswith("-"), s.isdigit(), s[2:4] == "--"] ) )
Example #22
Source File: test_explode.py From KiField with MIT License | 5 votes |
def random_prefix(draw): #limited to unicode letters, see #https://en.wikipedia.org/wiki/Unicode_character_property#General_Category categories = ['Ll', 'Lt', 'Lm', 'Lo'] characters = st.lists(st.characters(whitelist_categories=categories), min_size = 1) prefix = st.text(alphabet = draw(characters), min_size = 1) return draw(prefix)
Example #23
Source File: test_base.py From flocker with Apache License 2.0 | 5 votes |
def match_text_content(matcher): """ Match the text of a ``Content`` instance. """ return AfterPreprocessing(lambda content: content.as_text(), matcher)
Example #24
Source File: test_analysis_api.py From OasisPlatform with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_input_generation_traceback_file_is_present___file_can_be_retrieved(self, file_content): with TemporaryDirectory() as d: with override_settings(MEDIA_ROOT=d): user = fake_user() analysis = fake_analysis(input_generation_traceback_file=fake_related_file(file=file_content, content_type='text/plain')) response = self.app.get( analysis.get_absolute_input_generation_traceback_file_url(), headers={ 'Authorization': 'Bearer {}'.format(AccessToken.for_user(user)) }, ) self.assertEqual(response.body, file_content) self.assertEqual(response.content_type, 'text/plain')
Example #25
Source File: test_analysis_tasks.py From OasisPlatform with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_input_generation_traceback_file_and_status_are_updated(self, traceback): with TemporaryDirectory() as d: with override_settings(MEDIA_ROOT=d): initiator = fake_user() analysis = fake_analysis() record_generate_input_failure(analysis.pk, initiator.pk, traceback) analysis.refresh_from_db() self.assertEqual(analysis.input_generation_traceback_file.file.read(), traceback.encode()) self.assertEqual(analysis.input_generation_traceback_file.content_type, 'text/plain') self.assertEqual(analysis.input_generation_traceback_file.creator, initiator) self.assertEqual(analysis.status, analysis.status_choices.INPUTS_GENERATION_ERROR) self.assertTrue(isinstance(analysis.task_finished, datetime.datetime))
Example #26
Source File: strategies.py From vistir with ISC License | 5 votes |
def legal_path_chars(): # Control characters blacklist = ["/"] if os.name == "nt": blacklist.extend(["<", ">", ":", '"', "\\", "|", "?", "*"]) return st.text( st.characters( blacklist_characters=blacklist, blacklist_categories=("C",), min_codepoint=32, ), min_size=1, max_size=10, )
Example #27
Source File: test_analysis_tasks.py From OasisPlatform with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_output_tracebackfile__and_status_are_updated(self, traceback): with TemporaryDirectory() as d: with override_settings(MEDIA_ROOT=d): initiator = fake_user() analysis = fake_analysis() record_run_analysis_failure(analysis.pk, initiator.pk, traceback) analysis.refresh_from_db() self.assertEqual(analysis.run_traceback_file.file.read(), traceback.encode()) self.assertEqual(analysis.run_traceback_file.content_type, 'text/plain') self.assertEqual(analysis.run_traceback_file.creator, initiator) self.assertEqual(analysis.status, analysis.status_choices.RUN_ERROR) self.assertTrue(isinstance(analysis.task_finished, datetime.datetime))
Example #28
Source File: test_analysis_tasks.py From OasisPlatform with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_output_file_and_status_are_updated(self, output_location, log_location, traceback_location, return_code): expected_status = Analysis.status_choices.RUN_COMPLETED if return_code == 0 else Analysis.status_choices.RUN_ERROR with TemporaryDirectory() as d: with override_settings(MEDIA_ROOT=d): initiator = fake_user() analysis = fake_analysis() Path(d, output_location).touch() Path(d, log_location).touch() Path(d, traceback_location).touch() record_run_analysis_result( ( os.path.join(d, output_location), os.path.join(d, traceback_location), os.path.join(d, log_location), return_code, ), analysis.pk, initiator.pk, ) analysis.refresh_from_db() if return_code == 0: self.assertEqual(analysis.output_file.filename, output_location) self.assertEqual(analysis.output_file.content_type, 'application/gzip') self.assertEqual(analysis.output_file.creator, initiator) else: self.assertEqual(analysis.output_file, None) self.assertEqual(analysis.run_log_file.filename, log_location) self.assertEqual(analysis.run_log_file.content_type, 'application/gzip') self.assertEqual(analysis.run_log_file.creator, initiator) self.assertEqual(analysis.run_traceback_file.filename, traceback_location) self.assertEqual(analysis.run_traceback_file.content_type, 'text/plain') self.assertEqual(analysis.run_traceback_file.creator, initiator) self.assertEqual(analysis.status, expected_status)
Example #29
Source File: test_prettyprinter.py From prettyprinter with MIT License | 5 votes |
def primitives(): return ( st.integers() | st.floats(allow_nan=False) | st.text() | st.binary() | st.datetimes(timezones=timezones() | st.none()) | st.dates() | st.times(timezones=timezones() | st.none()) | st.timedeltas() | st.booleans() | st.none() )
Example #30
Source File: test_hpack.py From hpack with MIT License | 5 votes |
def test_apache_trafficserver(self): # This test reproduces the bug in #110, using exactly the same header # data. d = Decoder() data = ( b'\x10\x07:status\x03200@\x06server\tATS/6.0.0' b'@\x04date\x1dTue, 31 Mar 2015 08:09:51 GMT' b'@\x0ccontent-type\ttext/html@\x0econtent-length\x0542468' b'@\rlast-modified\x1dTue, 31 Mar 2015 01:55:51 GMT' b'@\x04vary\x0fAccept-Encoding@\x04etag\x0f"5519fea7-a5e4"' b'@\x08x-served\x05Nginx@\x14x-subdomain-tryfiles\x04True' b'@\x07x-deity\thydra-lts@\raccept-ranges\x05bytes@\x03age\x010' b'@\x19strict-transport-security\rmax-age=86400' b'@\x03via2https/1.1 ATS (ApacheTrafficServer/6.0.0 [cSsNfU])' ) expect = [ (':status', '200'), ('server', 'ATS/6.0.0'), ('date', 'Tue, 31 Mar 2015 08:09:51 GMT'), ('content-type', 'text/html'), ('content-length', '42468'), ('last-modified', 'Tue, 31 Mar 2015 01:55:51 GMT'), ('vary', 'Accept-Encoding'), ('etag', '"5519fea7-a5e4"'), ('x-served', 'Nginx'), ('x-subdomain-tryfiles', 'True'), ('x-deity', 'hydra-lts'), ('accept-ranges', 'bytes'), ('age', '0'), ('strict-transport-security', 'max-age=86400'), ('via', 'https/1.1 ATS (ApacheTrafficServer/6.0.0 [cSsNfU])'), ] result = d.decode(data) assert result == expect # The status header shouldn't be indexed. assert len(d.header_table.dynamic_entries) == len(expect) - 1