"""Tests for ows_assets model.""" from unittest.mock import MagicMock from flexmock import flexmock from owsrequest import request import pytest from analytics.models import ows_assets class TestOWSAssetsGoodResponse: """Test call to ows-assets with good response.""" product_ids = [123, 789, None] resource = '/image/product/cover/location?ids=123,789' response = { '123': 'location123.png', '789': 'location789.png' } @pytest.fixture def mock_response(self): """Return mock response to get image locations.""" mock_response = MagicMock(status_code=200) mock_response.json = MagicMock(return_value=self.response) return mock_response @pytest.fixture def mock_request(self, mock_response): """Mock GET request.""" flexmock(request).should_receive('get').with_args( 'ows-assets', self.resource).and_return( mock_response) def test_success(self, mock_request): """Test 200 response is returned.""" result = ows_assets.get_image_locations(self.product_ids) assert result.status == 200 assert result.message == self.response class TestOWSAssetsChunkedRequest: """Test call to ows-assets with a large number of ids.""" product_ids = range(0, 500) resource = '/image/product/cover/location?ids=' response = { '123': 'location123.png', '789': 'location789.png' } def chunked_resource(self, ids): """Return resource query string with product ids.""" return self.resource + ','.join([str(pid) for pid in ids]) @pytest.fixture def mock_response(self): """Return mock response to get image locations.""" mock_response = MagicMock(status_code=200) mock_response.json = MagicMock(return_value=self.response) return mock_response @pytest.fixture def mock_request(self, mock_response): """Mock GET request.""" for ids in ows_assets._chunks( self.product_ids, ows_assets.MAX_REQUEST_SIZE): flexmock(request).should_receive('get').with_args( 'ows-assets', self.chunked_resource(ids)).and_return( mock_response) def test_success(self, mock_request): """Test 200 response is returned.""" result = ows_assets.get_image_locations(self.product_ids) assert result.status == 200 assert result.message == self.response class TestOWSAssetsBadResponse: """Test call to ows-assets with bad response.""" product_ids = [123, 789] resource = '/image/product/cover/location?ids=123,789' response = { 'code': 'code', 'message': 'error' } @pytest.fixture def mock_response(self): """Return mock response to get image locations.""" mock_response = MagicMock(status_code=404) mock_response.json = MagicMock(return_value=self.response) return mock_response @pytest.fixture def mock_request(self, mock_response): """Mock GET request.""" flexmock(request).should_receive('get').with_args( 'ows-assets', self.resource).and_return( mock_response) def test_failure(self, mock_request): """Test 404 response is returned.""" result = ows_assets.get_image_locations(self.product_ids) assert result.status == 404 assert result.errors == self.response