Skip to content

Commit

Permalink
Fix mismatches in test data between 'total_results' and actual results
Browse files Browse the repository at this point in the history
  • Loading branch information
JWCook committed Sep 11, 2021
1 parent ee91a5f commit 5122237
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 26 deletions.
4 changes: 3 additions & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
@pytest.fixture(scope='function', autouse=True)
def patch_cached_session():
"""Disable request caching and rate-limiting during test session"""
with patch('pyinaturalist.api_requests.get_local_session', return_value=Session()):
with patch('pyinaturalist.api_requests.get_local_session', return_value=Session()), patch(
'pyinaturalist.client.ClientSession', Session
):
yield


Expand Down
21 changes: 12 additions & 9 deletions test/controllers/test_observation_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,35 @@
TaxonSummary,
User,
)
from test.conftest import load_sample_data
from test.sample_data import *


def test_from_id(requests_mock):
observation_id = 57754375
requests_mock.get(
f'{API_V1_BASE_URL}/observations',
json=load_sample_data('get_observations_node_page1.json'),
status_code=200,
[
{'json': SAMPLE_DATA['get_observations_node_page1'], 'status_code': 200},
{'json': SAMPLE_DATA['get_observations_node_page2'], 'status_code': 200},
],
)
client = iNatClient()
results = client.observations.from_id(observation_id)
results = client.observations.from_id(observation_id).all()

assert len(results) == 1 and isinstance(results[0], Observation)
assert len(results) == 2 and isinstance(results[0], Observation)
assert results[0].id == observation_id


def test_search(requests_mock):
requests_mock.get(
f'{API_V1_BASE_URL}/observations',
json=load_sample_data('get_observations_node_page1.json'),
json=SAMPLE_DATA['get_observations_node_page1'],
status_code=200,
)
client = iNatClient()
results = client.observations.search(taxon_name='Danaus plexippus', created_on='2020-08-27')
results = client.observations.search(
taxon_name='Danaus plexippus', created_on='2020-08-27'
).all()

assert isinstance(results[0], Observation)
assert results[0].id == 57754375
Expand All @@ -51,7 +54,7 @@ def test_search(requests_mock):
def test_histogram(requests_mock):
requests_mock.get(
f'{API_V1_BASE_URL}/observations/histogram',
json=load_sample_data('get_observation_histogram_day.json'),
json=SAMPLE_DATA['get_observation_histogram_day'],
status_code=200,
)
client = iNatClient()
Expand Down Expand Up @@ -111,7 +114,7 @@ def test_life_list(requests_mock):
def test_species_counts(requests_mock):
requests_mock.get(
f'{API_V1_BASE_URL}/observations/species_counts',
json=load_sample_data('get_observation_species_counts.json'),
json=SAMPLE_DATA['get_observation_species_counts'],
status_code=200,
)
client = iNatClient()
Expand Down
6 changes: 3 additions & 3 deletions test/controllers/test_project_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_from_id(requests_mock):
status_code=200,
)
client = iNatClient()
results = client.projects.from_id(project_id)
results = client.projects.from_id(project_id).all()
project = results[0]
assert len(results) == 2 and isinstance(project, Project)

Expand Down Expand Up @@ -47,7 +47,7 @@ def test_search(requests_mock):
lng=-123.08,
radius=400,
order_by='distance',
)
).all()

project = results[0]
assert len(results) == 5 and isinstance(project, Project)
Expand All @@ -66,7 +66,7 @@ def test_search__with_obs_fields(requests_mock):
status_code=200,
)
client = iNatClient()
results = client.projects.search(id=1234)
results = client.projects.search(id=1234).all()
obs_field = results[0].project_observation_fields[0]

assert isinstance(obs_field, ProjectObservationField)
Expand Down
14 changes: 7 additions & 7 deletions test/controllers/test_taxon_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@
from pyinaturalist.client import iNatClient
from pyinaturalist.constants import API_V1_BASE_URL
from pyinaturalist.models import Taxon
from test.conftest import load_sample_data
from test.sample_data import SAMPLE_DATA


def test_from_id(requests_mock):
taxon_id = 70118
requests_mock.get(
f'{API_V1_BASE_URL}/taxa/{taxon_id}',
json=load_sample_data('get_taxa_by_id.json'),
json=SAMPLE_DATA['get_taxa_by_id'],
status_code=200,
)

client = iNatClient()
results = client.taxa.from_id(taxon_id)
results = client.taxa.from_id(taxon_id).all()
assert len(results) == 1 and isinstance(results[0], Taxon)
assert results[0].id == taxon_id


def test_autocomplete(requests_mock):
requests_mock.get(
f'{API_V1_BASE_URL}/taxa/autocomplete',
json=load_sample_data('get_taxa_autocomplete.json'),
json=SAMPLE_DATA['get_taxa_autocomplete'],
status_code=200,
)

client = iNatClient()
results = client.taxa.autocomplete(q='vespi')
results = client.taxa.autocomplete(q='vespi').all()
assert len(results) == 10 and isinstance(results[0], Taxon)
assert results[0].id == 52747


def test_search(requests_mock):
requests_mock.get(
f'{API_V1_BASE_URL}/taxa',
json=load_sample_data('get_taxa.json'),
json=SAMPLE_DATA['get_taxa'],
status_code=200,
)

client = iNatClient()
results = client.taxa.search(q='vespi', rank=['genus', 'subgenus', 'species'])
results = client.taxa.search(q='vespi', rank=['genus', 'subgenus', 'species']).all()
assert len(results) == 30 and isinstance(results[0], Taxon)
assert results[0].id == 70118
2 changes: 1 addition & 1 deletion test/sample_data/get_taxa.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"total_results": 35,
"total_results": 30,
"page": 1,
"per_page": 30,
"results": [
Expand Down
2 changes: 1 addition & 1 deletion test/sample_data/get_taxa_autocomplete.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"total_results": 47,
"total_results": 10,
"page": 1,
"per_page": 30,
"results": [
Expand Down
6 changes: 2 additions & 4 deletions test/v1/test_taxa.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def test_get_taxa(requests_mock):
response = get_taxa(q='vespi', rank=['genus', 'subgenus', 'species'])
first_result = response['results'][0]

assert len(response['results']) == 30
assert response['total_results'] == 35
assert len(response['results']) == response['total_results'] == 30
assert first_result['id'] == 70118
assert first_result['name'] == 'Nicrophorus vespilloides'
assert first_result['rank'] == 'species'
Expand Down Expand Up @@ -89,8 +88,7 @@ def test_get_taxa_autocomplete(requests_mock):
response = get_taxa_autocomplete(q='vespi')
first_result = response['results'][0]

assert len(response['results']) == 10
assert response['total_results'] == 47
assert len(response['results']) == response['total_results'] == 10
assert first_result['matched_term'] == 'Vespidae'
assert first_result['id'] == 52747
assert first_result['name'] == 'Vespidae'
Expand Down

0 comments on commit 5122237

Please sign in to comment.