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)
)
python类text()的实例源码
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 list_of_strings(draw):
return [draw(text(min_size=1, average_size=70))
for i in range(draw(integers(min_value=0, max_value=100)))]
def _device_list(minimum):
"""
Get a device generating strategy.
:param int minimum: the minimum number of devices, must be at least 0
"""
return strategies.lists(
strategies.text(
alphabet=string.ascii_letters + "/",
min_size=1
),
min_size=minimum
)
def list_of_strings(draw):
return [draw(text(min_size=1, average_size=70))
for i in range(draw(integers(min_value=0, max_value=100)))]
def valid_identifier(**kwargs):
"""Return a strategy which generates a valid Python Identifier"""
if 'min_size' not in kwargs:
kwargs['min_size'] = 4
return hs.text(alphabet="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", **kwargs)\
.filter(lambda x: x[0].isalpha() and x.isidentifier() and not (iskeyword(x)))
def _parse_text(source: Union[str, NodeNG]) -> Tuple[astroid.Module, TypeInferer]:
"""Parse source code text and output an AST with type inference performed."""
# TODO: apparently no literal syntax for empty set in Python3, also cannot do set()
# TODO: Deal with special case later.
# if isinstance(source, astroid.Set) and len(list(source.elts)) == 0:
# source = f'{set({})}'
if not isinstance(source, str): # It's an astroid node
source = source.as_string()
module = astroid.parse(source)
type_inferer = TypeInferer()
type_inferer.environment_transformer().visit(module)
type_inferer.type_inference_transformer().visit(module)
return module, type_inferer
def urls():
"""
Strategy for generating ``twisted.python.url.URL``\s.
"""
return s.builds(
URL,
scheme=s.just(u'https'),
host=dns_names(),
path=s.lists(s.text(
max_size=64,
alphabet=s.characters(blacklist_characters=u'/?#',
blacklist_categories=('Cs',))
), min_size=1, max_size=10))
def test_start_responding(self, token):
"""
Calling ``start_responding`` makes an appropriate resource available.
"""
challenge = challenges.HTTP01(token=token)
response = challenge.response(RSA_KEY_512)
responder = HTTP01Responder()
challenge_resource = Resource()
challenge_resource.putChild(b'acme-challenge', responder.resource)
root = Resource()
root.putChild(b'.well-known', challenge_resource)
client = StubTreq(root)
encoded_token = challenge.encode('token')
challenge_url = URL(host=u'example.com', path=[
u'.well-known', u'acme-challenge', encoded_token]).asText()
self.assertThat(client.get(challenge_url),
succeeded(MatchesStructure(code=Equals(404))))
responder.start_responding(u'example.com', challenge, response)
self.assertThat(client.get(challenge_url), succeeded(MatchesAll(
MatchesStructure(
code=Equals(200),
headers=AfterPreprocessing(
methodcaller('getRawHeaders', b'content-type'),
Equals([b'text/plain']))),
AfterPreprocessing(methodcaller('content'), succeeded(
Equals(response.key_authorization.encode('utf-8'))))
)))
# Starting twice before stopping doesn't break things
responder.start_responding(u'example.com', challenge, response)
self.assertThat(client.get(challenge_url),
succeeded(MatchesStructure(code=Equals(200))))
responder.stop_responding(u'example.com', challenge, response)
self.assertThat(client.get(challenge_url),
succeeded(MatchesStructure(code=Equals(404))))
def test_headers_to_scrapy():
assert headers_to_scrapy(None) == Headers()
assert headers_to_scrapy({}) == Headers()
assert headers_to_scrapy([]) == Headers()
html_headers = Headers({'Content-Type': 'text/html'})
assert headers_to_scrapy({'Content-Type': 'text/html'}) == html_headers
assert headers_to_scrapy([('Content-Type', 'text/html')]) == html_headers
assert headers_to_scrapy([{'name': 'Content-Type', 'value': 'text/html'}]) == html_headers
def test_headers_to_scrapy():
assert headers_to_scrapy(None) == Headers()
assert headers_to_scrapy({}) == Headers()
assert headers_to_scrapy([]) == Headers()
html_headers = Headers({'Content-Type': 'text/html'})
assert headers_to_scrapy({'Content-Type': 'text/html'}) == html_headers
assert headers_to_scrapy([('Content-Type', 'text/html')]) == html_headers
assert headers_to_scrapy([{'name': 'Content-Type', 'value': 'text/html'}]) == html_headers
def test_headers_to_scrapy():
assert headers_to_scrapy(None) == Headers()
assert headers_to_scrapy({}) == Headers()
assert headers_to_scrapy([]) == Headers()
html_headers = Headers({'Content-Type': 'text/html'})
assert headers_to_scrapy({'Content-Type': 'text/html'}) == html_headers
assert headers_to_scrapy([('Content-Type', 'text/html')]) == html_headers
assert headers_to_scrapy([{'name': 'Content-Type', 'value': 'text/html'}]) == html_headers
def test_identity_should_return_first_argument(text, integer):
assert identity(text) is text
assert identity(integer) is integer
def test_eq(text):
assert eq(text, text)
assert eq(text)(text)
def test_fold(integer, text, boolean):
dictionary = {'key': 'value'}
assert First(text).fold(identity) is text
assert All(boolean).fold(identity) is boolean
assert Sum(integers).fold(identity) is integers
assert Map(dictionary).fold(identity) is dictionary
def test_write(self, text):
fake_stderr = FakeOutput()
fake_stdout = FakeOutput()
output = Console(output=fake_stdout, error=fake_stderr)
output.write(*text)
assert fake_stdout.last_received == " ".join(text) + "\n"
# Asserts that write_debug only writes when output.debug is True.
def test_write_if_debug(self, info, debug):
fake_stderr = FakeOutput()
fake_stdout = FakeOutput()
output = Console(output=fake_stdout, error=fake_stderr)
output.write(info)
output.write_debug(debug)
assert fake_stdout.last_received == info + "\n"
output.debug = True
output.write(info)
output.write_debug(debug)
assert fake_stdout.last_received == "DEBUG: %s\n" % debug
# Asserts that the text is written to stderr with "ERROR: ".
def test_write_stderr(self, text):
fake_stderr = FakeOutput()
fake_stdout = FakeOutput()
output = Console(output=fake_stdout, error=fake_stderr)
output.write_error(*text)
assert fake_stderr.last_received == "ERROR: %s\n" % " ".join(text)
# Asserts that an end is properly appended to all written text.
def test_supply_end(self, text, end):
fake_stderr = FakeOutput()
fake_stdout = FakeOutput()
output = Console(output=fake_stdout, error=fake_stderr)
output.debug = True
output.write(text, end=end)
assert fake_stdout.last_received == text + end
output.write_debug(text, end=end)
assert fake_stdout.last_received == "DEBUG: " + text + end
output.write_error(text, end=end)
assert fake_stderr.last_received == "ERROR: " + text + end
def test_to_long_fail(self, fail_val):
"""
to_long() with incompatible params:
:param fail_val: random data of known incompatible types (floats, text)
"""
self.force_fail(fail_val, to_long, TypeError)