def test_cleanup_job_request_not_stale(mock_requests, mock_mark_as_stale, mock_dt): mock_dt.utcnow = mock.Mock(return_value=datetime(2019, 9, 5)) mock_dt.strptime = mock.Mock(return_value=datetime(2019, 9, 5)) mock_requests.return_value.json.return_value = expected main() assert mock_requests.call_count == 1 assert not mock_mark_as_stale.called
def test_cleanup_job_request_not_stale(mock_requests, mock_mark_as_stale, mock_dt): mock_dt.utcnow = mock.Mock(return_value=datetime(2019, 9, 5)) mock_dt.strptime = mock.Mock(return_value=datetime(2019, 9, 5)) mock_requests.return_value.json.side_effect = [ mock_complete, mock_in_progress, mock_failed ] main() assert mock_requests.call_count == 3 assert not mock_mark_as_stale.called
def test_cleanup_job_success(mock_requests, mock_auth_requests, mock_dt): mock_dt.utcnow = mock.Mock(return_value=datetime(2019, 9, 7)) mock_dt.strptime = mock.Mock(return_value=datetime(2019, 9, 5)) mock_requests.return_value.json.return_value = expected mock_auth_requests.return_value.ok = True main() calls = [ mock.call('http://cachito.domain.local/api/v1/requests/50', json={ 'state': 'stale', 'state_reason': 'The request has expired' }, timeout=60), ] assert mock_requests.call_count == 1 assert mock_auth_requests.call_count == 1 mock_auth_requests.assert_has_calls(calls)
def test_cleanup_job_pagination_behaviour(mock_basic_session, mock_auth_session, mock_dt): """Test that marking requests as stale does not mess with pagination.""" mock_dt.utcnow = mock.Mock(return_value=datetime(2020, 10, 12)) mock_dt.strptime = mock.Mock(return_value=datetime(2019, 10, 12)) # 11 requests will be split into two pages (10 on the first page, 1 on the second page) mock_paginated_session = MockRequestsPagination(total_complete_requests=11) mock_basic_session.get = mock_paginated_session.get mock_auth_session.patch = mock_paginated_session.patch main() # All complete requests should have been marked as stale assert mock_paginated_session.complete_ids == [] assert mock_paginated_session.stale_ids == list(range(1, 12)) # We should be past the last page (last page is 2, we should be on page 3) assert mock_paginated_session.page == 3
def test_cleanup_job_success(mock_requests, mock_auth_requests, mock_dt): mock_dt.utcnow = mock.Mock(return_value=datetime(2019, 9, 7)) mock_dt.strptime = mock.Mock(return_value=datetime(2019, 9, 5)) mock_requests.return_value.json.side_effect = [ mock_complete, mock_in_progress, mock_failed ] mock_auth_requests.return_value.ok = True main() calls = [ mock.call( "http://cachito.domain.local/api/v1/requests/50", json={ "state": "stale", "state_reason": "The request has expired" }, timeout=60, ), mock.call( "http://cachito.domain.local/api/v1/requests/51", json={ "state": "stale", "state_reason": "The request has expired" }, timeout=60, ), mock.call( "http://cachito.domain.local/api/v1/requests/52", json={ "state": "stale", "state_reason": "The request has expired" }, timeout=60, ), ] assert mock_requests.call_count == 3 assert mock_auth_requests.call_count == 3 mock_auth_requests.assert_has_calls(calls)
def test_cleanup_job_request_failed_get(mock_requests): mock_requests.return_value.ok = False expected = "Could not reach the Cachito API to find the requests to be marked as stale" with pytest.raises(CachitoError, match=expected): main() assert mock_requests.call_count == 1
def test_cleanup_job_request_get_timeout(mock_requests): mock_requests.side_effect = requests.ConnectionError() expected = "The connection failed when querying .+" with pytest.raises(CachitoError, match=expected): main() assert mock_requests.call_count == 1