Skip to content

Commit

Permalink
Update tests add response for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LePetitTim committed Jul 18, 2023
1 parent 5c90d96 commit f647551
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 10 deletions.
4 changes: 2 additions & 2 deletions georiviere/observations/management/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def handle(self, *args, **options):
self.stdout.write('Get station from API {0}'.format(response.url))
try:
response_content = response.json()
except json.decoder.JSONDecodeError:
except json.JSONDecodeError:
self.stdout.write('Response is not a json')
return
if verbosity >= 1:
Expand All @@ -65,7 +65,7 @@ def handle(self, *args, **options):
self.stdout.write('Import next page from {0}'.format(response.url))
try:
response_content = response.json()
except requests.exceptions.JSONDecodeError:
except json.JSONDecodeError:
self.stdout.write('Response is not a json')
return
results = response_content['data']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"first": "https://hubeau.eaufrance.fr/api/v1/hydrobio/indices?code_station_hydrobio=06000874&page=1&size=1000",
"last": null,
"prev": null,
"next": null,
"next": "https://hubeau.eaufrance.fr/api/v1/hydrobio/indices?code_station_hydrobio=06000874&page=3&size=1000",
"api_version": "1.0.0",
"data": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"count": 0,
"first": "https://hubeau.eaufrance.fr/api/v1/hydrobio/indices?code_station_hydrobio=06000874&page=2&size=1000",
"last": null,
"prev": null,
"next": "",
"api_version": "1.0.0",
"data": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"first": "https://hubeau.eaufrance.fr/api/v1/hydrobio/stations_hydrobio?code_station_hydrobio=06000874&page=1&size=1000",
"last": null,
"prev": null,
"next": null,
"next": "https://hubeau.eaufrance.fr/api/v1/hydrobio/stations_hydrobio?code_station_hydrobio=06000874&page=3&size=1000",
"api_version": "1.0.0",
"data": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"count": 0,
"first": "https://hubeau.eaufrance.fr/api/v1/hydrobio/stations_hydrobio?code_station_hydrobio=06000874&page=2&size=1000",
"last": null,
"prev": null,
"next": "",
"api_version": "1.0.0",
"data": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"first": "https://hubeau.eaufrance.fr/api/v1/hydrobio/taxons?code_station_hydrobio=06000874&page=1&size=1000",
"last": null,
"prev": null,
"next": null,
"next": "https://hubeau.eaufrance.fr/api/v1/hydrobio/taxons?code_station_hydrobio=06000874&page=3&size=1000",
"api_version": "1.0.0",
"data": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"count": 45,
"first": "https://hubeau.eaufrance.fr/api/v1/hydrobio/taxons?code_station_hydrobio=06000874&page=1&size=1000",
"last": null,
"prev": null,
"next": null,
"api_version": "1.0.0",
"data": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"first": "https://hubeau.eaufrance.fr/api/v1/hydrometrie/referentiel/stations?code_site=U2012010&code_site=U2015410&format=json&page=1&size=20",
"last": null,
"prev": null,
"next": null,
"next": "https://hubeau.eaufrance.fr/api/v1/hydrometrie/referentiel/stations?code_site=U2012010&code_site=U2015410&format=json&page=3&size=20",
"api_version": "1.0.1",
"data": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"count": 2,
"first": "https://hubeau.eaufrance.fr/api/v1/hydrometrie/referentiel/stations?code_site=U2012010&code_site=U2015410&format=json&page=2&size=20",
"last": null,
"prev": null,
"next": null,
"api_version": "1.0.1",
"data": [
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"first": "https://hubeau.eaufrance.fr/api/v1/qualite_rivieres/station_pc?format=json&code_departement=11&page=1&size=1000",
"last": null,
"prev": null,
"next": null,
"next": "https://hubeau.eaufrance.fr/api/v1/qualite_rivieres/station_pc?format=json&code_departement=11&page=3&size=1000",
"api_version": "1.1.0",
"data": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"count": 28,
"first": "https://hubeau.eaufrance.fr/api/v1/qualite_rivieres/station_pc?format=json&code_departement=11&page=2&size=1000",
"last": null,
"prev": null,
"next": null,
"api_version": "1.1.0",
"data": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"first": "https://hubeau.eaufrance.fr/api/v1/temperature/station?format=json&code_commune=25320&code_commune=25415&page=1&size=1000",
"last": null,
"prev": null,
"next": null,
"next": "https://hubeau.eaufrance.fr/api/v1/temperature/station?format=json&code_commune=25320&code_commune=25415&page=3&size=1000",
"api_version": "1.0.0",
"data": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"count": 0,
"first": "https://hubeau.eaufrance.fr/api/v1/temperature/station?format=json&code_commune=25320&code_commune=25415&page=2&size=1000",
"last": null,
"prev": null,
"next": "",
"api_version": "1.0.0",
"data": []
}
50 changes: 48 additions & 2 deletions georiviere/observations/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,43 @@ def requests_get_mock_response(*args, **kwargs):
"""Build mock_response with test file data, according to api_url"""
# Get filename from api_url
filename = TEST_DATA_PATH / 'response_api_hydrometrie_stations.json'
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_hydrometrie_stations_page3.json'
if "urf" in args[0]:
filename = TEST_DATA_PATH / 'response_sandre_urf.json'
if "hydrometrie" in args[0]:
filename = TEST_DATA_PATH / 'response_api_hydrometrie_stations.json'
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_hydrometrie_stations_page3.json'
if "temperature" in args[0]:
filename = TEST_DATA_PATH / 'response_api_temperature_stations.json'
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_temperature_stations_page3.json'
if "station_pc" in args[0]:
filename = TEST_DATA_PATH / 'response_api_pcquality_stations.json'
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_pcquality_stations_page3.json'
if "analyse_pc" in args[0]:
if "sort" in kwargs['params']:
filename = TEST_DATA_PATH / 'response_api_pcquality_analyse_desc.json'
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_pcquality_analyse_page3.json'
else:
filename = TEST_DATA_PATH / 'response_api_pcquality_analyse.json'
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_pcquality_analyse_page3.json'
if 'stations_hydrobio' in args[0]:
filename = TEST_DATA_PATH / 'response_api_hydrobio_stations.json'
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_hydrobio_stations_page3.json'
if 'indices' in args[0]:
filename = TEST_DATA_PATH / 'response_api_hydrobio_indices.json'
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_hydrobio_indices_page3.json'
if 'taxons' in args[0]:
filename = TEST_DATA_PATH / 'response_api_hydrobio_taxons.json'
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_hydrobio_taxons_page3.json'
# Build response
mock_response = mock.Mock()
with open(filename, 'r') as f:
Expand All @@ -57,27 +75,45 @@ def requests_get_mock_response_error(*args, **kwargs):
# Get filename from api_url
mock_response = mock.Mock()
filename = TEST_DATA_PATH / 'response_api_hydrometrie_stations.json'
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_hydrometrie_stations_page3.json'
if "urf" in args[0]:
filename = TEST_DATA_PATH / 'response_sandre_urf.json'
if "hydrometrie" in args[0]:
filename = TEST_DATA_PATH / 'response_api_hydrometrie_stations.json'
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_hydrometrie_stations_page3.json'
if "temperature" in args[0]:
filename = TEST_DATA_PATH / 'response_api_temperature_stations.json'
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_temperature_stations_page3.json'
if "station_pc" in args[0]:
filename = TEST_DATA_PATH / 'response_api_pcquality_stations.json'
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_pcquality_stations_page3.json'
if "analyse_pc" in args[0]:
if "sort" in kwargs['params']:
mock_response.json.side_effect = json.JSONDecodeError('msg', 'doc', 1)
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_pcquality_analyse_page3.json'
else:
filename = TEST_DATA_PATH / 'response_api_pcquality_analyse.json'
if kwargs.get('params', {})["code_station"] == "05137100":
mock_response.json.side_effect = json.JSONDecodeError('msg', 'doc', 1)
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_pcquality_analyse_page3.json'
if 'stations_hydrobio' in args[0]:
filename = TEST_DATA_PATH / 'response_api_hydrobio_stations.json'
if 'page=3' in args[0]:
mock_response.json.side_effect = json.JSONDecodeError('msg', 'doc', 1)
if 'indices' in args[0]:
mock_response.json.side_effect = json.JSONDecodeError('msg', 'doc', 1)
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_hydrobio_indices_page3.json'
if 'taxons' in args[0]:
mock_response.json.side_effect = json.JSONDecodeError('msg', 'doc', 1)
if 'page=3' in args[0]:
mock_response.json.side_effect = json.JSONDecodeError('msg', 'doc', 1)
# Build response

with open(filename, 'r') as f:
Expand Down Expand Up @@ -198,7 +234,7 @@ def test_hydrometric_stations_imported(self, mock_get):
out = StringIO()

# Call command
call_command('import_hydrometric_stations', stdout=out)
call_command('import_hydrometric_stations', stdout=out, size=100, department="49")

# Check stations imported
stations = Station.objects.all()
Expand Down Expand Up @@ -445,7 +481,9 @@ def requests_get_mock_response_fail_connectionerror_asc(*args, **kwargs):
"""Build mock_response with test file data, according to api_url"""
# Get filename from api_url
filename = TEST_DATA_PATH / 'response_api_pcquality_stations.json'
if kwargs.get('params').get('code_station') == "05134550":
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_pcquality_stations_page3.json'
if kwargs.get('params', {}).get('code_station') == "05134550":
raise requests.exceptions.ConnectionError
if "analyse_pc" in args[0]:
filename = TEST_DATA_PATH / 'response_api_pcquality_analyse.json'
Expand All @@ -462,13 +500,19 @@ def requests_get_mock_response_fail_connectionerror_desc(*args, **kwargs):
"""Build mock_response with test file data, according to api_url"""
# Get filename from api_url
filename = TEST_DATA_PATH / 'response_api_pcquality_stations.json'
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_pcquality_stations_page3.json'
if "analyse_pc" in args[0]:
if "sort" in kwargs['params']:
filename = TEST_DATA_PATH / 'response_api_pcquality_analyse_desc.json'
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_pcquality_analyse_page3.json'
if kwargs.get('params').get('code_station') == "05134550":
raise requests.exceptions.ConnectionError
else:
filename = TEST_DATA_PATH / 'response_api_pcquality_analyse.json'
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_pcquality_analyse_page3.json'

# Build response
mock_response = mock.Mock()
Expand Down Expand Up @@ -541,6 +585,8 @@ def requests_get_mock_response_fail_connectionerror_taxons(*args, **kwargs):
"""Build mock_response with test file data, according to api_url"""
# Get filename from api_url
filename = TEST_DATA_PATH / 'response_api_hydrobio_stations.json'
if 'page=3' in args[0]:
filename = TEST_DATA_PATH / 'response_api_hydrobio_stations_page3.json'
if "taxons" in args[0]:
raise requests.exceptions.ConnectionError
if "indices" in args[0]:
Expand Down

0 comments on commit f647551

Please sign in to comment.