def test_success(shelve, IOLoop): """Test recording successful fetch""" shelf = {'repo1': {}} shelve.open.return_value = shelf repo_dict = {} store = repositories.RepositoryStore(repo_dict, api_client=MagicMock()) now = datetime.now() store.success('repo1', now) expected = { 'next': now, 'last': datetime(2000, 01, 01), 'errors': 0, 'successful_queries': 1 } assert shelf['repo1'] == expected assert repo_dict['repo1'] == expected later = datetime.now() store.success('repo1', later) expected = { 'next': later, 'last': datetime(2000, 01, 01), 'errors': 0, 'successful_queries': 2 } assert shelf['repo1'] == expected assert repo_dict['repo1'] == expected
def mock_manager(use_clock=False): """ Returns an instance of repositories.Manager with a mock database, an empty shelf (using a dict instead of an actual shelf instance), and an example repository """ repostore = repositories.RepositoryStore({}) repostore._shelf = { 'repo_a': { 'id': 'repo_a', 'service': { 'location': 'http://a.test' } } } queue = Queue.Queue() notification_q = repositories.Notification(queue) scheduler = repositories.Scheduler() scheduler._use_clock = use_clock if use_clock: scheduler._time = iter(frange(0, 100000, 0.5)).next notification_q.connect_with(scheduler) manager = repositories.Manager(Mock(), repostore, scheduler) manager.db.add_entities.return_value = make_future({'errors': []}) return scheduler, repostore, manager
def test_get_repository(shelve): shelf = {'repo1': {'id': 1}} shelve.open.return_value = shelf repo_store = repositories.RepositoryStore({}, api_client=Mock()) result = yield repo_store.get_repository('repo1') assert result == shelf['repo1']
def test_fail_without_reason(shelve, logging): """Test recording failed fetch without specifying a reason""" shelf = {'repo1': {}} shelve.open.return_value = shelf store = repositories.RepositoryStore({}, api_client=MagicMock()) store.fail('repo1') assert shelf['repo1'] == {'errors': 1} assert logging.warning.call_count == 1
def test_get_repositories_swallow_exceptions(koi, API, logging, shelve): """Log but don't raise exceptions""" API().accounts.repositories.get.side_effect = Exception('Test') schedule_fetch = Mock() schedule_fetch.return_value = make_future([]) repostore = repositories.RepositoryStore({}) repostore.on_new_repo = schedule_fetch yield repostore._fetch_repositories() assert logging.exception.call_count == 1 assert not schedule_fetch.called
def test_fail(shelve, logging): """Test recording failed fetch""" shelf = {'repo1': {}} shelve.open.return_value = shelf store = repositories.RepositoryStore({}, api_client=MagicMock()) store.fail('repo1', 'An error') assert shelf['repo1'] == {'errors': 1} store.fail('repo1', 'An error') assert shelf['repo1'] == {'errors': 2} logging.warning.assert_has_calls([call('An error'), call('An error')])
def test_fail_unknown_(shelve): """ Test KeyError raised if unknown repository We don't want to keep a record of unknown repositories """ shelf = {'repo1': {}} shelve.open.return_value = shelf store = repositories.RepositoryStore({}, api_client=MagicMock()) with pytest.raises(KeyError): yield store.fail('repo0') assert shelf['repo1'] == {}
def test_get_unknown_repository_does_not_exist_in_accounts(shelve): """Query for a service that doesn't exist in the accounts service""" shelf = {} shelve.open.return_value = shelf api_client = MagicMock() endpoint = api_client.accounts.repositories.__getitem__ endpoint().get.side_effect = HTTPError(404, 'Unknown resource') repo_store = repositories.RepositoryStore({}, api_client=api_client) with pytest.raises(KeyError): yield repo_store.get_repository('repo1') assert endpoint.called assert endpoint().get.called
def test_success_replace_next_with_none(shelve, IOLoop): """Test recording successful fetch resets errors""" shelf = {'repo1': {'next': 'something'}} shelve.open.return_value = shelf repo_dict = {} store = repositories.RepositoryStore(repo_dict, api_client=MagicMock()) store.success('repo1') expected = { 'next': None, 'last': datetime(2000, 01, 01), 'errors': 0, 'successful_queries': 1 } assert shelf['repo1'] == expected assert repo_dict['repo1'] == expected
def test_get_unknown_repository_closed_service(shelve, options): """When closed service don't fetch unknown repository""" shelf = {} shelve.open.return_value = shelf options.open_service = False api_client = MagicMock() endpoint = api_client.accounts.repositories.__getitem__() endpoint().get.return_value = make_future({'data': {'id': 1}}) endpoint.reset_mock() repo_store = repositories.RepositoryStore({}, api_client=api_client) with pytest.raises(KeyError): yield repo_store.get_repository('repo1') assert not endpoint.called assert not endpoint().get.called
def test_get_unknown_repository_exists_in_accounts(shelve): """Query for a service that exists in the accounts service""" shelf = {} shelve.open.return_value = shelf repo_id = 'repo1' repo = {'id': repo_id} api_client = MagicMock() endpoint = api_client.accounts.repositories.__getitem__ endpoint().get.return_value = make_future({'data': repo}) endpoint.reset_mock() repo_store = repositories.RepositoryStore({}, api_client=api_client) result = yield repo_store.get_repository(repo_id) endpoint.assert_called_once_with(repo_id) assert endpoint().get.called assert result == repo
def test_get_repositories(koi, API, shelve): API().accounts.repositories.get.return_value = make_future({ 'data': [{'id': 'a', 'location': 'http://a.test'}, {'id': 'b', 'location': 'http://b.test'}] }) schedule_fetch = Mock() schedule_fetch.return_value = make_future([]) repostore = repositories.RepositoryStore({}) repostore.on_new_repo = schedule_fetch repostore._shelf = {} yield repostore._fetch_repositories() assert API().accounts.repositories.get.called schedule_fetch.assert_has_calls([call('a'), call('b')]) assert repostore._shelf == { 'a': {'id': 'a', 'location': 'http://a.test'}, 'b': {'id': 'b', 'location': 'http://b.test'} }
def test_get_repositories_are_registered_once(koi, API, shelve): API().accounts.repositories.get.return_value = make_future({ 'data': [{'id': 'a', 'location': 'http://a.test'}] }) schedule_fetch = Mock() schedule_fetch.return_value = make_future([]) repostore = repositories.RepositoryStore({}) repostore.on_new_repo = schedule_fetch repostore._shelf = { 'a': {'id': 'a', 'location': 'http://a.test'}, 'b': {'id': 'b', 'location': 'http://b.test'} } repostore._fetch_repositories() assert not schedule_fetch.called assert repostore._shelf == { 'a': {'id': 'a', 'location': 'http://a.test'}, 'b': {'id': 'b', 'location': 'http://b.test'} }
def test_unknown_repository(API): """ If a repository is not in the RepositoryStore then the manager should not try to fetch the repository, not add it to the repository store and not reschedule it. We don't want to keep trying to fetch unknown repositories. This scenario can arise because a notification is always scheduled to be fetched on the assumption that we will be able to find an unknown repository in the accounts service. """ scheduler = MagicMock() scheduler.get.return_value = make_future('repo1') store = repositories.RepositoryStore({}) store._shelf = {} # get_repository raises a KeyError if repo is unknown store.get_repository = MagicMock(side_effect=KeyError) manager = repositories.Manager(MagicMock(), store, scheduler) yield manager.fetch('repo1') assert not API().repository.repositories[''].assets.identifiers.called assert not scheduler.schedule.called assert store._shelf == {}