"""Test projections model.""" from datetime import date from unittest.mock import Mock from unittest.mock import patch from api.models import projections @patch('api.models.projections.date') def test_fetch_all_time(mock_date, monkeypatch): """Test fetch_all_time function.""" upc = 888812345678 today = date(2012, 12, 21) mock_date.today.return_value = today projections_sql = 'SELECT __PROJECTIONS__' # Mocking # projections.select_all_time function select_all_time_mock = Mock(return_value=projections_sql) monkeypatch.setattr(projections, 'select_all_time', select_all_time_mock) # projections.aurora.context context manager cursor_mock = Mock(fetchall=Mock(return_value='some_result')) connection_mock = Mock() aurora_context_mock = Mock(__exit__=Mock(), __enter__=Mock( return_value=(cursor_mock, connection_mock))) monkeypatch.setattr(projections.aurora, 'context', Mock( return_value=aurora_context_mock)) result = projections.fetch_all_time(upc, 'regular') # Asserts assert select_all_time_mock.called assert cursor_mock.execute.called execute_args = cursor_mock.execute.call_args_list[0][0] assert projections_sql in execute_args[0] assert execute_args[1] == {'upc': upc, 'date_mask': today} assert cursor_mock.fetchall.called assert result == 'some_result' @patch('api.models.projections.constants') @patch('api.models.projections.fetch_all_time') def test_fetch_all_time_buckets( fetch_all_time, constants, projections_all_time, projections_all_time_buckets): """Test fetch function correctly aggregates on buckets.""" constants.SERIES_BUCKETS = {'odds': {1, 3}, 'evens': {2, 4}} constants.ORIGINAL_PROJECTION_SERIES_BUCKETS = { 'one': {1}, 'two': {2}, 'three': {3}, 'four': {4}} constants.PROJECTION_TYPE_SERIES_BUCKETS = { 'regular': constants.SERIES_BUCKETS, 'original': constants.ORIGINAL_PROJECTION_SERIES_BUCKETS} fetch_all_time.return_value = projections_all_time results = projections.fetch_all_time_buckets('123', 'regular') expected = projections_all_time_buckets assert results == expected @patch('api.models.projections.constants') @patch('api.models.projections.fetch_all_time') def test_fetch_all_time_buckets_original_projection( fetch_all_time, constants, projections_all_time, original_projections_all_time_buckets): """Test fetch function correctly aggregates for original_projection.""" constants.SERIES_BUCKETS = {'odds': {1, 3}, 'evens': {2, 4}} constants.ORIGINAL_PROJECTION_SERIES_BUCKETS = { 'one': {1}, 'two': {2}, 'three': {3}, 'four': {4}} constants.PROJECTION_TYPE_SERIES_BUCKETS = { 'regular': constants.SERIES_BUCKETS, 'original': constants.ORIGINAL_PROJECTION_SERIES_BUCKETS} fetch_all_time.return_value = projections_all_time results = projections.fetch_all_time_buckets('123', 'original') expected = original_projections_all_time_buckets assert results == expected