"""Tests for geographics endpoints.""" from unittest.mock import MagicMock from oto import response as oto_response from owsrequest.constants import headers as owsrequest_constants from analytics.logic import geographics from tests.unit.fixtures import fixture_api def test_get_geographic_insights_missing_start_end_dates(headers): """Test /geographic-insights-by-territory with missing dates params. Assert that 400 error should be returned """ url = '/geographic-insights-by-territory/' response = fixture_api.CLIENT.get(url, headers=headers) assert response.status_code == 400 def test_get_geographic_insights_wrong_date_range(headers): """Test /geographic-insights-by-territory with date range > than one year. Assert that 400 error should be returned """ url = ( '/geographic-insights-by-territory/' '?from_date=2017-01-01&to_date=2018-01-31') response = fixture_api.CLIENT.get(url, headers=headers) assert response.status_code == 400 def test_get_geographic_insights_with_wrong_headers( incorrect_headers): """Test /geographic-insights-by-territory with wrong headers. Assert that 403 error should be returned """ url = ( '/geographic-insights-by-territory/' '?from_date=2017-01-01&to_date=2017-01-31') response = fixture_api.CLIENT.get(url, headers=incorrect_headers) assert response.status_code == 400 def test_get_geographic_insights( monkeypatch, headers, geographics_db_result_formatted_result): """Test /geographic-insights-by-territory with correct params. Assert that 200 should be returned """ account_type = owsrequest_constants.GRASS_ACCOUNT_TYPE account_id = owsrequest_constants.GRASS_ACCOUNT_ID url = ( '/geographic-insights-by-territory/' '?from_date=2017-01-01&to_date=2017-01-31&' 'artist_ids=123,456&stores=286&isrcs=321,654') start_date = '2017-01-01' end_date = '2017-01-31' isrcs = ['321', '654'] artist_ids = ['123', '456'] store_ids = ['286'] feed_ids = [1, 2] distributors = ['theorchard'] logic_mock = MagicMock( return_value=oto_response.Response( message=geographics_db_result_formatted_result)) monkeypatch.setattr( geographics, 'get_geographic_insights', logic_mock) response = fixture_api.CLIENT.get(url, headers=headers) geographics.get_geographic_insights.assert_called_with( start_date, end_date, isrcs, artist_ids, store_ids, feed_ids, headers[account_type], headers[account_id], distributors) assert response.status_code == 200 def test_get_geographic_insights_default_storeids( monkeypatch, headers, geographics_db_result_formatted_result): """Test /geographic-insights-by-territory with correct params. Assert that 200 should be returned """ account_type = owsrequest_constants.GRASS_ACCOUNT_TYPE account_id = owsrequest_constants.GRASS_ACCOUNT_ID url = ( '/geographic-insights-by-territory/' '?from_date=2017-01-01&to_date=2017-01-31&' 'artist_ids=123,456&isrcs=321,654') start_date = '2017-01-01' end_date = '2017-01-31' isrcs = ['321', '654'] artist_ids = ['123', '456'] store_ids = ['286'] feed_ids = [1, 2] distributors = ['theorchard'] logic_mock = MagicMock( return_value=oto_response.Response( message=geographics_db_result_formatted_result)) monkeypatch.setattr( geographics, 'get_geographic_insights', logic_mock) response = fixture_api.CLIENT.get(url, headers=headers) geographics.get_geographic_insights.assert_called_with( start_date, end_date, isrcs, artist_ids, store_ids, feed_ids, headers[account_type], headers[account_id], distributors) assert response.status_code == 200 def test_get_geographic_insights_by_region_missing_start_end_dates(headers): """Test /geographic-insights-by-region with missing dates params. Assert that 400 error should be returned """ url = '/geographic-insights-by-region/' response = fixture_api.CLIENT.get(url, headers=headers) assert response.status_code == 400 def test_get_geographic_insights_by_region_wrong_date_range(headers): """Test /geographic-insights-by-region with date range > than one year. Assert that 400 error should be returned """ url = ( '/geographic-insights-by-region/' '?from_date=2017-01-01&to_date=2018-01-31') response = fixture_api.CLIENT.get(url, headers=headers) assert response.status_code == 400 def test_get_geographic_insights_by_region_with_wrong_headers( incorrect_headers): """Test /geographic-insights-by-region with wrong headers. Assert that 400 error should be returned """ url = ( '/geographic-insights-by-region/' '?from_date=2017-01-01&to_date=2017-01-31') response = fixture_api.CLIENT.get(url, headers=incorrect_headers) assert response.status_code == 400 def test_get_geographic_insights_by_region( monkeypatch, headers, geographics_by_region_db_result_formatted_result): """Test /geographics-insights-by-region with correct params. Assert that 200 should be returned """ account_type = owsrequest_constants.GRASS_ACCOUNT_TYPE account_id = owsrequest_constants.GRASS_ACCOUNT_ID url = ( '/geographic-insights-by-region/' '?from_date=2017-01-01&to_date=2017-01-31&' 'artist_ids=123,456&stores=286&isrcs=321,654') start_date = '2017-01-01' end_date = '2017-01-31' isrcs = ['321', '654'] artist_ids = ['123', '456'] store_ids = ['286'] feed_ids = [1, 2] distributors = ['theorchard'] logic_mock = MagicMock( return_value=oto_response.Response( message=geographics_by_region_db_result_formatted_result)) monkeypatch.setattr( geographics, 'get_geographic_insights_by_region', logic_mock) response = fixture_api.CLIENT.get(url, headers=headers) geographics.get_geographic_insights_by_region.assert_called_with( start_date, end_date, isrcs, artist_ids, store_ids, feed_ids, headers[account_type], headers[account_id], distributors) assert response.status_code == 200 def test_get_geographic_insights_by_region_default_storeids( monkeypatch, headers, geographics_by_region_db_result_formatted_result): """Test /geographics-insights-by-region with correct params. Assert that 200 should be returned """ account_type = owsrequest_constants.GRASS_ACCOUNT_TYPE account_id = owsrequest_constants.GRASS_ACCOUNT_ID url = ( '/geographic-insights-by-region/' '?from_date=2017-01-01&to_date=2017-01-31&' 'artist_ids=123,456&isrcs=321,654') start_date = '2017-01-01' end_date = '2017-01-31' isrcs = ['321', '654'] artist_ids = ['123', '456'] store_ids = ['286'] feed_ids = [1, 2] distributors = ['theorchard'] logic_mock = MagicMock( return_value=oto_response.Response( message=geographics_by_region_db_result_formatted_result)) monkeypatch.setattr( geographics, 'get_geographic_insights_by_region', logic_mock) response = fixture_api.CLIENT.get(url, headers=headers) geographics.get_geographic_insights_by_region.assert_called_with( start_date, end_date, isrcs, artist_ids, store_ids, feed_ids, headers[account_type], headers[account_id], distributors) assert response.status_code == 200 def test_get_geographic_orchard_regions_with_wrong_headers(incorrect_headers): """Test /geographic-orchard-regions with wrong headers. Assert that 400 error should be returned """ url = '/geographic-orchard-regions/' response = fixture_api.CLIENT.get(url, headers=incorrect_headers) assert response.status_code == 400 def test_get_geographic_orchard_regions( monkeypatch, headers, geographics_orchard_regions_db_formatted_result): """Test /geographic-orchard-regions. Assert that 200 error should be returned and logic function called """ url = '/geographic-orchard-regions/' logic_mock = MagicMock( return_value=oto_response.Response( message=geographics_orchard_regions_db_formatted_result)) monkeypatch.setattr( geographics, 'get_geographic_orchard_regions', logic_mock) response = fixture_api.CLIENT.get(url, headers=headers) assert geographics.get_geographic_orchard_regions.called assert response.status_code == 200 def test_get_geographic_orchard_date_with_wrong_headers(incorrect_headers): """Test /geographic-orchard-summary-date with wrong headers. Assert that 400 error should be returned """ url = '/geographics-latest-date/' response = fixture_api.CLIENT.get(url, headers=incorrect_headers) assert response.status_code == 400 def test_get_geographic_orchard_date( monkeypatch, headers, get_orchard_dates_result): """Test /geographic-orchard-summary-date. Assert that 200 should be returned and logic function called """ url = '/geographics-latest-date/' logic_mock = MagicMock(return_value=oto_response.Response( message=get_orchard_dates_result)) monkeypatch.setattr(geographics, 'get_geographics_latest_date', logic_mock) response = fixture_api.CLIENT.get(url, headers=headers) assert geographics.get_geographics_latest_date.called assert response.status_code == 200