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

Ensure that filter values are URL encoded #42

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pyalex/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def url(self):
v_quote = [quote_plus(q) for q in v]
l_params.append(k + "=" + ",".join(v_quote))
elif k in ["filter", "sort"]:
l_params.append(k + "=" + _flatten_kv(v))
l_params.append(k + "=" + quote_plus(_flatten_kv(v)))
else:
l_params.append(k + "=" + quote_plus(str(v)))

Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ select = ["E", "F", "UP", "I", "B"]

[tool.ruff.lint.isort]
force-single-line = true

[tool.pytest.ini_options]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed to add this to get pytest to work with uv.

uv venv
uv pip compile pyproject.toml -o requirements.txt
uv pip install -r requirements.txt
pytest

Let me know if there's a preferred way of running the tests, I can remove this change.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Haven't played with uv yet, but will do soon. For now, all tests seem to pass with this new config. I will merge.

pythonpath = ["."]
addopts = "-v"
12 changes: 8 additions & 4 deletions tests/test_pyalex.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def test_works_multifilter():


def test_works_url():
url = "https://api.openalex.org/works?filter=publication_year:2020,is_oa:true"
url = "https://api.openalex.org/works?filter=publication_year%3A2020%2Cis_oa%3Atrue"

assert url == Works().filter(publication_year=2020, is_oa=True).url
assert url == Works().filter(publication_year=2020).filter(is_oa=True).url
Expand Down Expand Up @@ -258,7 +258,7 @@ def test_random_publishers():

def test_and_operator():
# https://github.com/J535D165/pyalex/issues/11
url = "https://api.openalex.org/works?filter=institutions.country_code:tw,institutions.country_code:hk,institutions.country_code:us,publication_year:2022" # noqa
url = "https://api.openalex.org/works?filter=institutions.country_code%3Atw%2Cinstitutions.country_code%3Ahk%2Cinstitutions.country_code%3Aus%2Cpublication_year%3A2022"

assert (
url
Expand Down Expand Up @@ -288,12 +288,12 @@ def test_and_operator():


def test_sample():
url = "https://api.openalex.org/works?filter=publication_year:2020,is_oa:true&sample=50"
url = "https://api.openalex.org/works?filter=publication_year%3A2020%2Cis_oa%3Atrue&sample=50"
assert url == Works().filter(publication_year=2020, is_oa=True).sample(50).url


def test_sample_seed():
url = "https://api.openalex.org/works?filter=publication_year:2020,is_oa:true&sample=50&seed=535" # noqa
url = "https://api.openalex.org/works?filter=publication_year%3A2020%2Cis_oa%3Atrue&sample=50&seed=535" # noqa
assert (
url
== Works().filter(publication_year=2020, is_oa=True).sample(50, seed=535).url
Expand Down Expand Up @@ -328,3 +328,7 @@ def test_autocomplete():
a = autocomplete("stockholm resilience")

assert len(a) > 5


def test_filter_urlencoding():
assert Works().filter(doi="10.1207/s15327809jls0703&4_2").count() == 1
Loading