-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dunderrrrrr/pytest
Add pytest and workflow
- Loading branch information
Showing
8 changed files
with
273 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
name: Push | ||
on: [push] | ||
|
||
jobs: | ||
pytest: | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
python-version: [3.12] | ||
poetry-version: [1.8.3] | ||
os: [ubuntu-latest] | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-python@v2 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Run image | ||
uses: abatilo/[email protected] | ||
with: | ||
poetry-version: ${{ matrix.poetry-version }} | ||
- name: Install dependencies | ||
run: poetry install | ||
- name: Run tests | ||
run: poetry run pytest | ||
ruff: | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
python-version: [3.12] | ||
poetry-version: [1.8.3] | ||
os: [ubuntu-latest] | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-python@v2 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Run image | ||
uses: abatilo/[email protected] | ||
with: | ||
poetry-version: ${{ matrix.poetry-version }} | ||
- name: Install dependencies | ||
run: poetry install | ||
- name: Check ruff | ||
run: poetry run ruff check . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import pytest | ||
from blocket_api.blocket import BlocketAPI, LimitError | ||
|
||
api = BlocketAPI("token") | ||
|
||
|
||
def test_limit_errors(): | ||
with pytest.raises(LimitError): | ||
api.get_listings(limit=100) | ||
|
||
with pytest.raises(LimitError): | ||
api.custom_search("saab", limit=100) | ||
|
||
|
||
def test_typeerrors(): | ||
with pytest.raises(TypeError): | ||
api.custom_search() # missing search query | ||
|
||
with pytest.raises(TypeError): | ||
BlocketAPI() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import respx | ||
import pytest | ||
from httpx import Response | ||
from blocket_api.blocket import BASE_URL, APIError, BlocketAPI, _make_request | ||
|
||
api = BlocketAPI("token") | ||
|
||
|
||
def test_make_request_no_raise(): | ||
_make_request(url=f"{BASE_URL}/not_found", token="token", raise_for_status=False) | ||
|
||
|
||
@respx.mock | ||
def test_make_request_raise_404(): | ||
respx.get(f"{BASE_URL}/not_found").mock( | ||
return_value=Response(status_code=404), | ||
) | ||
with pytest.raises(APIError): | ||
_make_request(url=f"{BASE_URL}/not_found", token="token", raise_for_status=True) | ||
|
||
|
||
@respx.mock | ||
def test_make_request_raise_401(): | ||
respx.get(f"{BASE_URL}/unauthorized").mock( | ||
return_value=Response(status_code=401), | ||
) | ||
with pytest.raises(APIError): | ||
_make_request(url=f"{BASE_URL}/unauthorized", token="token") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import respx | ||
from httpx import Response | ||
from blocket_api.blocket import BASE_URL, BlocketAPI, Region | ||
|
||
api = BlocketAPI("token") | ||
|
||
|
||
@respx.mock | ||
def test_saved_searches(): | ||
""" | ||
Make sure mobility saved searches are merged with v2/searches. | ||
""" | ||
respx.get(f"{BASE_URL}/saved/v2/searches").mock( | ||
return_value=Response( | ||
status_code=200, | ||
json={ | ||
"data": [ | ||
{"id": "1", "name": '"buggy", Bilar säljes i hela Sverige'}, | ||
{"id": "2", "name": "Cyklar säljes i flera kommuner"}, | ||
], | ||
}, | ||
), | ||
) | ||
respx.get(f"{BASE_URL}/mobility-saved-searches/v1/searches").mock( | ||
return_value=Response( | ||
status_code=200, | ||
json={"data": [{"id": "3", "name": "Bilar säljes i hela Sverige"}]}, | ||
), | ||
) | ||
assert api.saved_searches() == [ | ||
{"id": "1", "name": '"buggy", Bilar säljes i hela Sverige'}, | ||
{"id": "2", "name": "Cyklar säljes i flera kommuner"}, | ||
{"id": "3", "name": "Bilar säljes i hela Sverige"}, | ||
] | ||
|
||
|
||
@respx.mock | ||
def test_for_search_id(): | ||
respx.get(f"{BASE_URL}/saved/v2/searches_content/123?lim=99").mock( | ||
return_value=Response(status_code=200, json={"data": "listings-data"}), | ||
) | ||
assert api.get_listings(search_id=123) == {"data": "listings-data"} | ||
|
||
|
||
@respx.mock | ||
def test_for_search_id_mobility(): | ||
respx.get(f"{BASE_URL}/saved/v2/searches_content/123?lim=99").mock( | ||
return_value=Response(status_code=404), | ||
) | ||
respx.get(f"{BASE_URL}/mobility-saved-searches/v1/searches/123/ads?lim=99").mock( | ||
return_value=Response(status_code=200, json={"data": "mobility-data"}), | ||
) | ||
assert api.get_listings(search_id=123) == {"data": "mobility-data"} | ||
|
||
|
||
@respx.mock | ||
def test_custom_search(): | ||
respx.get( | ||
f"{BASE_URL}/search_bff/v2/content?lim=99&q=saab&r=20&status=active" | ||
).mock( | ||
return_value=Response(status_code=200, json={"data": {"location": "halland"}}), | ||
) | ||
api.custom_search("saab", Region.halland) |