def test_sets_cache(db, oaisets): """Test caching for OAISets.""" cache = {} rec = { '_oai': { 'sets': [ 'user-c1', 'extra', ], } } update_oaisets_cache(cache, rec) assert cache['user-c1'].search_pattern is None assert cache['extra'].search_pattern == 'title:extra' # see in conftest with patch.object(OAISet, 'query') as query_mock: # Mock the sqlalchemy query API q_result_mock = MagicMock() q_result_mock.count = 1 q_result_mock.one().search_pattern = None query_mock.filter_by = MagicMock(return_value=q_result_mock) r = { 'communities': ['c1', 'c2'], '_oai': { 'id': 'some_id_1234', 'updated': 'timestamp', 'sets': ['user-c1', 'extra', 'user-c2'] } } assert not requires_sync(r, cache=cache) # Should not require sync # Should be only called once for the item not in cache query_mock.filter_by.assert_called_once_with(spec='user-c2')
def test_sets_cache(mocker, db, oaisets): """Test caching for OAISets.""" cache = {} rec = { '_oai': { 'sets': ['user-c1', 'extra', ], } } update_oaisets_cache(cache, rec) assert cache['user-c1'].search_pattern is None assert cache['extra'].search_pattern == 'title:extra' # see in conftest query_mock = mocker.patch.object(OAISet, 'query') # Mock the sqlalchemy query API q_result_mock = MagicMock() q_result_mock.count = 1 q_result_mock.one().search_pattern = None query_mock.filter_by = MagicMock(return_value=q_result_mock) r = { 'communities': ['c1', 'c2'], '_oai': { 'id': 'some_id_1234', 'updated': 'timestamp', 'sets': ['user-c1', 'extra', 'user-c2'] } } assert not requires_sync(r, cache=cache) # Should not require sync # Should be only called once for the item not in cache query_mock.filter_by.assert_called_once_with(spec='user-c2')
def test_syncing_required(db, oaisets): """Test OAI syncing requirement criterion.""" assert requires_sync({}) r = { 'communities': [ 'c1', ], '_oai': { 'id': 'some_id_1234', 'updated': 'timestamp', 'sets': [ 'user-c1', 'extra', ] } } assert not requires_sync(r) # should not update it r = { 'communities': [ 'c1', ], '_oai': { # 'id' is missing 'updated': 'timestamp', 'sets': [ 'user-c1', 'extra', ] } } assert requires_sync(r) r = { 'communities': [ 'c1', ], '_oai': { 'id': '', # 'id' empty 'updated': 'timestamp', 'sets': [ 'user-c1', 'extra', ] } } assert requires_sync(r) r = { 'communities': [ 'c1', ], '_oai': { 'id': 'some_id_1234', # update is missing 'sets': [ 'user-c1', 'extra', ] } } assert requires_sync(r) r = { 'communities': [ 'c1', ], '_oai': { 'id': 'some_id_1234', 'updated': 'timestamp', 'sets': [ 'extra', 'user-c2', ] # additional 'user-c2' } } assert requires_sync(r) r = { 'communities': ['c1', 'c2'], '_oai': { 'id': 'some_id_1234', 'updated': 'timestamp', 'sets': [ 'extra', 'user-c1', ] # 'user-c2' missing } } assert requires_sync(r) # should not update it r = { 'communities': [ 'c1', ], '_oai': { 'id': 'some_id_1234', 'updated': 'timestamp', # sets missing } } assert requires_sync(r) # should not update it r = { 'communities': [ 'c1', ], # _oai is missing completely } assert requires_sync(r) # should not update it
def test_syncing_required(db, oaisets): """Test OAI syncing requirement criterion.""" assert requires_sync({}) r = { 'communities': ['c1', ], '_oai': { 'id': 'some_id_1234', 'updated': 'timestamp', 'sets': ['user-c1', 'extra', ] } } assert not requires_sync(r) # should not update it r = { 'communities': ['c1', ], '_oai': { # 'id' is missing 'updated': 'timestamp', 'sets': ['user-c1', 'extra', ] } } assert requires_sync(r) r = { 'communities': ['c1', ], '_oai': { 'id': '', # 'id' empty 'updated': 'timestamp', 'sets': ['user-c1', 'extra', ] } } assert requires_sync(r) r = { 'communities': ['c1', ], '_oai': { 'id': 'some_id_1234', # update is missing 'sets': ['user-c1', 'extra', ] } } assert requires_sync(r) r = { 'communities': ['c1', ], '_oai': { 'id': 'some_id_1234', 'updated': 'timestamp', 'sets': ['extra', 'user-c2', ] # additional 'user-c2' } } assert requires_sync(r) r = { 'communities': ['c1', 'c2'], '_oai': { 'id': 'some_id_1234', 'updated': 'timestamp', 'sets': ['extra', 'user-c1', ] # 'user-c2' missing } } assert requires_sync(r) # should not update it r = { 'communities': ['c1', ], '_oai': { 'id': 'some_id_1234', 'updated': 'timestamp', # sets missing } } assert requires_sync(r) # should not update it r = { 'communities': ['c1', ], # _oai is missing completely } assert requires_sync(r) # should not update it