Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧪 Integrate Hypothesis in tests #860

Merged
merged 25 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ repos:
alias: mypy-py313
name: MyPy, for Python 3.13
additional_dependencies:
- hypothesis
- lxml # dep of `--txt-report`, `--cobertura-xml-report` & `--html-report`
- multidict
- pytest
Expand All @@ -128,6 +129,7 @@ repos:
alias: mypy-py312
name: MyPy, for Python 3.12
additional_dependencies:
- hypothesis
- lxml # dep of `--txt-report`, `--cobertura-xml-report` & `--html-report`
- multidict
- pytest
Expand All @@ -143,6 +145,7 @@ repos:
alias: mypy-py310
name: MyPy, for Python 3.10
additional_dependencies:
- hypothesis
- lxml # dep of `--txt-report`, `--cobertura-xml-report` & `--html-report`
- multidict
- pytest
Expand All @@ -160,6 +163,7 @@ repos:
alias: mypy-py38
name: MyPy, for Python 3.8
additional_dependencies:
- hypothesis
- lxml # dep of `--txt-report`, `--cobertura-xml-report` & `--html-report`
- multidict
- pytest
Expand Down
1 change: 1 addition & 0 deletions CHANGES/860.contrib.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Started testing with hypothesis -- by :user:`webknjaz` and :user:`bdraco`.
bdraco marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions requirements/test.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-r cython.txt
covdefaults
hypothesis>=6.0
idna==3.10
multidict==6.1.0
pytest==8.3.3
Expand Down
131 changes: 131 additions & 0 deletions tests/test_quoting.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from typing import Type

import pytest
from hypothesis import HealthCheck, assume, example, given, note, settings
from hypothesis import strategies as st

from yarl._quoting import NO_EXTENSIONS
from yarl._quoting_py import _Quoter as _PyQuoter
Expand Down Expand Up @@ -455,3 +459,130 @@ def test_quoter_path_with_plus(quoter):
def test_unquoter_path_with_plus(unquoter):
s = "/test/x+y%2Bz/:+%2B/"
assert "/test/x+y+z/:++/" == unquoter(unsafe="+")(s)


@given(safe=st.text(), protected=st.text(), qs=st.booleans(), requote=st.booleans())
def test_fuzz__PyQuoter(safe, protected, qs, requote):
_PyQuoter(safe=safe, protected=protected, qs=qs, requote=requote)


@given(ignore=st.text(), unsafe=st.text(), qs=st.booleans())
def test_fuzz__PyUnquoter(ignore, unsafe, qs):
_PyUnquoter(ignore=ignore, unsafe=unsafe, qs=qs)
bdraco marked this conversation as resolved.
Show resolved Hide resolved


@example(text_input="0")
@settings(suppress_health_check=(HealthCheck.function_scoped_fixture,))
@given(
text_input=st.text(
alphabet=st.characters(max_codepoint=127, blacklist_characters="%")
)
)
def test_quote_unquote_parameter(
quoter: Type[_PyQuoter],
unquoter: Type[_PyUnquoter],
text_input: str,
) -> None:
quote = quoter()
unquote = unquoter()
text_quoted = quote(text_input)
note(f"text_quoted={text_quoted!r}")
text_output = unquote(text_quoted)
assert text_input == text_output


@example(text_input="0")
@settings(suppress_health_check=(HealthCheck.function_scoped_fixture,))
@given(
text_input=st.text(
alphabet=st.characters(max_codepoint=127, blacklist_characters="%")
)
)
def test_quote_unquote_parameter_requote(
quoter: Type[_PyQuoter],
unquoter: Type[_PyUnquoter],
text_input: str,
) -> None:
quote = quoter(requote=True)
unquote = unquoter()
text_quoted = quote(text_input)
note(f"text_quoted={text_quoted!r}")
text_output = unquote(text_quoted)
assert text_input == text_output


@example(text_input="0")
@settings(suppress_health_check=(HealthCheck.function_scoped_fixture,))
@given(
text_input=st.text(
alphabet=st.characters(max_codepoint=127, blacklist_characters="%")
)
)
def test_quote_unquote_parameter_path_safe(
quoter: Type[_PyQuoter],
unquoter: Type[_PyUnquoter],
text_input: str,
) -> None:
quote = quoter()
unquote = unquoter(ignore="/%", unsafe="+")
assume("+" not in text_input and "/" not in text_input)
text_quoted = quote(text_input)
note(f"text_quoted={text_quoted!r}")
text_output = unquote(text_quoted)
assert text_input == text_output
bdraco marked this conversation as resolved.
Show resolved Hide resolved


if not NO_EXTENSIONS and False:
bdraco marked this conversation as resolved.
Show resolved Hide resolved
test_quote_unquote_parameter = example(
quoter=_PyQuoter,
unquoter=_CUnquoter,
text_input="0",
)(test_quote_unquote_parameter)
Fixed Show fixed Hide fixed

test_quote_unquote_parameter = example(
quoter=_CQuoter,
unquoter=_PyUnquoter,
text_input="0",
)(test_quote_unquote_parameter)

test_quote_unquote_parameter = example(
quoter=_CQuoter,
unquoter=_CUnquoter,
text_input="0",
)(test_quote_unquote_parameter)

test_quote_unquote_parameter_requote = example(
quoter=_PyQuoter,
unquoter=_CUnquoter,
text_input="0",
)(test_quote_unquote_parameter_requote)

test_quote_unquote_parameter_requote = example(
quoter=_CQuoter,
unquoter=_PyUnquoter,
text_input="0",
)(test_quote_unquote_parameter_requote)

test_quote_unquote_parameter_requote = example(
quoter=_CQuoter,
unquoter=_CUnquoter,
text_input="0",
)(test_quote_unquote_parameter_requote)

test_quote_unquote_parameter_path_safe = example(
quoter=_PyQuoter,
unquoter=_CUnquoter,
text_input="0",
)(test_quote_unquote_parameter_path_safe)

test_quote_unquote_parameter_path_safe = example(
quoter=_CQuoter,
unquoter=_PyUnquoter,
text_input="0",
)(test_quote_unquote_parameter_path_safe)

test_quote_unquote_parameter_path_safe = example(
quoter=_CQuoter,
unquoter=_CUnquoter,
text_input="0",
)(test_quote_unquote_parameter_path_safe)
Loading