from dogpile.cache.api import NO_VALUE import pytest from unittest.mock import Mock from labelaudit import config from labelaudit.logic import audit_persistence from labelaudit.logic.result import Result from labelaudit.tasks import report_generation_tasks from labelaudit.connectors import cache @pytest.fixture def ok_fetch_all(): return { 'reports': [ {'auditStatus': 'in_progress', 'initiatedById': None, 'reportLocation': None, 'updatedTimestamp': '2015-06-09 14:30:34', 'vendorId': '22294', 'youtubeAuditId': '2'}, {'auditStatus': 'in_progress', 'initiatedById': '888', 'reportLocation': 'some location', 'updatedTimestamp': '2015-06-09 14:30:36', 'vendorId': '5', 'youtubeAuditId': '3'} ], 'total_reports': 3} @pytest.fixture def ok_no_results(): return { 'reports': [], 'total_reports': 0} @pytest.fixture def mock_cache_and_config(monkeypatch): monkeypatch.setattr(config, 'CACHE_ENABLED', Mock(True)) monkeypatch.setattr(config, 'AUDIT_GENERATION_BATCH_SIZE', Mock(50)) monkeypatch.setattr(cache.region, 'get', Mock(return_value=NO_VALUE)) monkeypatch.setattr(cache.region, 'set', Mock(return_value='')) monkeypatch.setattr(cache.region, 'delete', Mock(return_value='')) def test_exception_if_cache_off(): with pytest.raises(Exception): report_generation_tasks.find() @pytest.mark.parametrize('mock_cache_and_config', [('monkeypatch')], indirect=['mock_cache_and_config']) def test_no_reports_to_process(monkeypatch, ok_no_results, mock_cache_and_config): """Testing find() when no reports found.""" success_result = Result(message=ok_no_results, errors={}, status=200) monkeypatch.setattr( audit_persistence, 'fetch_all', Mock(return_value=success_result)) assert report_generation_tasks.find() is None @pytest.mark.parametrize('mock_cache_and_config', [('monkeypatch')], indirect=['mock_cache_and_config']) def test_success_reports_calls_generate_task(monkeypatch, ok_fetch_all, mock_cache_and_config): """Testing find() gets reports and makes generate call.""" success_result = Result(message=ok_fetch_all, errors={}, status=200) monkeypatch.setattr( audit_persistence, 'fetch_all', Mock(return_value=success_result)) monkeypatch.setattr( report_generation_tasks, 'generate', Mock(return_value=5)) monkeypatch.setattr( audit_persistence, 'update_report_status', Mock(return_value=True)) report_generation_tasks.find() assert report_generation_tasks.generate.delay.called assert audit_persistence.update_report_status.called