def test_archived_after(self): """Test if only those items archived after a date are returned""" manager = ArchiveManager(self.test_path) args = { 'origin': 'http://example.com/', 'category': 'mock_item', 'tag': 'test', 'subtype': 'mocksubtype', 'from-date': str_to_datetime('2015-01-01') } items = fetch(CommandBackend, args, manager=manager) items = [item for item in items] self.assertEqual(len(items), 5) archived_dt = datetime_utcnow() items = fetch(CommandBackend, args, manager=manager) items = [item for item in items] self.assertEqual(len(items), 5) # Fetch items from the archive items = fetch_from_archive(CommandBackend, args, manager, 'mock_item', str_to_datetime('1970-01-01')) items = [item for item in items] self.assertEqual(len(items), 10) # Fetch items archived after the given date items = fetch_from_archive(CommandBackend, args, manager, 'mock_item', archived_dt) items = [item for item in items] self.assertEqual(len(items), 5)
def test_ignore_corrupted_archive(self): """Check if a corrupted archive is ignored while fetching from archive""" def delete_rows(db, table_name): conn = sqlite3.connect(db) cursor = conn.cursor() cursor.execute("DELETE FROM " + table_name) cursor.close() conn.commit() manager = ArchiveManager(self.test_path) args = { 'origin': 'http://example.com/', 'category': 'mock_item', 'tag': 'test', 'subtype': 'mocksubtype', 'from-date': str_to_datetime('2015-01-01') } # First, fetch the items twice to check if several archive # are used items = fetch(CommandBackend, args, manager=manager) items = [item for item in items] self.assertEqual(len(items), 5) items = fetch(CommandBackend, args, manager=manager) items = [item for item in items] self.assertEqual(len(items), 5) # Find archive names to delete the rows of one of them to make it # corrupted filepaths = manager.search('http://example.com/', 'CommandBackend', 'mock_item', str_to_datetime('1970-01-01')) self.assertEqual(len(filepaths), 2) to_remove = filepaths[0] delete_rows(to_remove, 'archive') # Fetch items from the archive items = fetch_from_archive(CommandBackend, args, manager, 'mock_item', str_to_datetime('1970-01-01')) items = [item for item in items] self.assertEqual(len(items), 5) for x in range(5): item = items[x] expected_uuid = uuid('http://example.com/', str(x)) self.assertEqual(item['data']['item'], x) self.assertEqual(item['data']['archive'], True) self.assertEqual(item['origin'], 'http://example.com/') self.assertEqual(item['uuid'], expected_uuid) self.assertEqual(item['tag'], 'test')
def test_items_storing_archive(self): """Test whether items are stored in an archive""" manager = ArchiveManager(self.test_path) args = { 'origin': 'http://example.com/', 'category': 'mock_item', 'tag': 'test', 'subtype': 'mocksubtype', 'from-date': str_to_datetime('2015-01-01') } items = fetch(CommandBackend, args, manager=manager) items = [item for item in items] self.assertEqual(len(items), 5) for x in range(5): item = items[x] expected_uuid = uuid('http://example.com/', str(x)) self.assertEqual(item['data']['item'], x) self.assertEqual(item['origin'], 'http://example.com/') self.assertEqual(item['uuid'], expected_uuid) self.assertEqual(item['tag'], 'test') filepaths = manager.search('http://example.com/', 'CommandBackend', 'mock_item', str_to_datetime('1970-01-01')) self.assertEqual(len(filepaths), 1) archive = Archive(filepaths[0]) self.assertEqual(archive._count_table_rows('archive'), 5)
def test_archive(self): """Test whether a set of items is fetched from the archive""" manager = ArchiveManager(self.test_path) args = { 'origin': 'http://example.com/', 'category': 'mock_item', 'tag': 'test', 'subtype': 'mocksubtype', 'from-date': str_to_datetime('2015-01-01') } # First, fetch the items twice to check if several archive # are used items = fetch(CommandBackend, args, manager=manager) items = [item for item in items] self.assertEqual(len(items), 5) items = fetch(CommandBackend, args, manager=manager) items = [item for item in items] self.assertEqual(len(items), 5) # Fetch items from the archive items = fetch_from_archive(CommandBackend, args, manager, 'mock_item', str_to_datetime('1970-01-01')) items = [item for item in items] self.assertEqual(len(items), 10) for x in range(2): for y in range(5): item = items[y + (x * 5)] expected_uuid = uuid('http://example.com/', str(y)) self.assertEqual(item['data']['item'], y) self.assertEqual(item['data']['archive'], True) self.assertEqual(item['origin'], 'http://example.com/') self.assertEqual(item['uuid'], expected_uuid) self.assertEqual(item['tag'], 'test')
def test_no_archived_items(self): """Test when no archived items are available""" manager = ArchiveManager(self.test_path) args = { 'origin': 'http://example.com/', 'category': 'mock_item', 'tag': 'test', 'subtype': 'mocksubtype', 'from-date': str_to_datetime('2015-01-01') } items = fetch(CommandBackend, args, manager=manager) items = [item for item in items] self.assertEqual(len(items), 5) # There aren't items for this category items = fetch_from_archive(CommandBackend, args, manager, 'alt_item', str_to_datetime('1970-01-01')) items = [item for item in items] self.assertEqual(len(items), 0)
def test_remove_archive_on_error(self): """Test whether an archive is removed when an unhandled exception occurs""" manager = ArchiveManager(self.test_path) args = { 'origin': 'http://example.com/', 'category': 'mock_item', 'tag': 'test', 'subtype': 'mocksubtype', 'from-date': str_to_datetime('2015-01-01') } items = fetch(ErrorCommandBackend, args, manager=manager) with self.assertRaises(BackendError): _ = [item for item in items] filepaths = manager.search('http://example.com/', 'ErrorCommandBackend', 'mock_item', str_to_datetime('1970-01-01')) self.assertEqual(len(filepaths), 0)
def test_items(self): """Test whether a set of items is returned""" args = { 'origin': 'http://example.com/', 'category': 'mock_item', 'tag': 'test', 'subtype': 'mocksubtype', 'from-date': str_to_datetime('2015-01-01') } items = fetch(CommandBackend, args, manager=None) items = [item for item in items] self.assertEqual(len(items), 5) for x in range(5): item = items[x] expected_uuid = uuid('http://example.com/', str(x)) self.assertEqual(item['data']['item'], x) self.assertEqual(item['origin'], 'http://example.com/') self.assertEqual(item['uuid'], expected_uuid) self.assertEqual(item['tag'], 'test')