def test_fetch_from_date(self): """Test when return from date""" setup_http_server() backend = Crates() from_date = datetime.datetime(2016, 1, 1) items = [items for items in backend.fetch(from_date=from_date)] item = items[0] self.assertEqual(len(item['data']['owner_team_data']['teams']), 1) self.assertEqual(len(item['data']['owner_user_data']['users']), 2) self.assertEqual(len(item['data']['version_downloads_data']), 0) self.assertEqual(len(item['data']['versions_data']['versions']), 5) item = items[1] self.assertEqual(len(item['data']['owner_team_data']['teams']), 1) self.assertEqual(len(item['data']['owner_user_data']['users']), 2) self.assertEqual(len(item['data']['version_downloads_data']), 0) self.assertEqual(len(item['data']['versions_data']['versions']), 1) item = items[2] self.assertEqual(len(item['data']['owner_team_data']['teams']), 1) self.assertEqual(len(item['data']['owner_user_data']['users']), 3) self.assertEqual(len(item['data']['version_downloads_data']), 0) self.assertEqual(len(item['data']['versions_data']['versions']), 7)
def test_fetch_empty(self): """Test when return empty""" setup_http_server(empty=True) backend = Crates() items = [items for items in backend.fetch()] self.assertEqual(len(items), 0)
def test_summary_search_fields(self): """Test whether the search_fields is properly set""" setup_http_server() backend = Crates() items = [items for items in backend.fetch(category=CATEGORY_SUMMARY)] for item in items: self.assertEqual(backend.metadata_id(item['data']), item['search_fields']['item_id'])
def test_crates_search_fields(self): """Test whether the search_fields is properly set""" setup_http_server() backend = Crates() items = [items for items in backend.fetch(from_date=None)] for item in items: self.assertEqual(backend.metadata_id(item['data']), item['search_fields']['item_id'])
def test_fetch_summary(self): """Test whether a summary is returned""" setup_http_server() backend = Crates() items = [items for items in backend.fetch(category=SUMMARY_CATEGORY)] self.assertEqual(len(items), 1) self.assertEqual(items[0]['category'], SUMMARY_CATEGORY) self.assertEqual(items[0]['data']['num_crates'], 10000) self.assertEqual(items[0]['data']['num_downloads'], 2000000000)
def test_fetch_summary_from_date(self): """Test when return from date""" setup_http_server() backend = Crates() from_date = datetime.datetime(2016, 1, 1) items = [items for items in backend.fetch(category=CATEGORY_SUMMARY, from_date=from_date)] self.assertEqual(len(items), 1) self.assertEqual(items[0]['category'], CATEGORY_SUMMARY) self.assertEqual(items[0]['data']['num_crates'], 10000) self.assertEqual(items[0]['data']['num_downloads'], 2000000000)
def test_initialization(self): """Test whether attributes are initializated""" crates = Crates(tag='test') self.assertEqual(crates.origin, 'https://crates.io/') self.assertEqual(crates.tag, 'test') # When tag is empty or None it will be set to # the value in origin crates = Crates() self.assertEqual(crates.origin, 'https://crates.io/') self.assertEqual(crates.tag, 'https://crates.io/') crates = Crates(tag='') self.assertEqual(crates.origin, 'https://crates.io/') self.assertEqual(crates.tag, 'https://crates.io/')
def test_initialization(self): """Test whether attributes are initializated""" crates = Crates(tag='test') self.assertEqual(crates.origin, 'https://crates.io/') self.assertEqual(crates.tag, 'test') self.assertIsNone(crates.client) self.assertTrue(crates.ssl_verify) # When tag is empty or None it will be set to # the value in origin crates = Crates(ssl_verify=False) self.assertEqual(crates.origin, 'https://crates.io/') self.assertEqual(crates.tag, 'https://crates.io/') self.assertFalse(crates.ssl_verify) crates = Crates(tag='') self.assertEqual(crates.origin, 'https://crates.io/') self.assertEqual(crates.tag, 'https://crates.io/') self.assertTrue(crates.ssl_verify)
def test_fetch_crates(self): """Test whether a list of crates is returned""" setup_http_server() backend = Crates() items = [items for items in backend.fetch()] self.assertEqual(len(items), 4) item = items[0] self.assertEqual(item['category'], CRATES_CATEGORY) self.assertEqual(len(item['data']['owner_team_data']['teams']), 0) self.assertEqual(len(item['data']['owner_user_data']['users']), 1) self.assertEqual( len(item['data']['version_downloads_data']['version_downloads']), 2) self.assertEqual(len(item['data']['versions_data']['versions']), 1) item = items[1] self.assertEqual(item['category'], CRATES_CATEGORY) self.assertEqual(len(item['data']['owner_team_data']['teams']), 1) self.assertEqual(len(item['data']['owner_user_data']['users']), 2) self.assertEqual(len(item['data']['version_downloads_data']), 0) self.assertEqual(len(item['data']['versions_data']['versions']), 5) item = items[2] self.assertEqual(item['category'], CRATES_CATEGORY) self.assertEqual(len(item['data']['owner_team_data']['teams']), 1) self.assertEqual(len(item['data']['owner_user_data']['users']), 2) self.assertEqual(len(item['data']['version_downloads_data']), 0) self.assertEqual(len(item['data']['versions_data']['versions']), 1) item = items[3] self.assertEqual(item['category'], CRATES_CATEGORY) self.assertEqual(len(item['data']['owner_team_data']['teams']), 1) self.assertEqual(len(item['data']['owner_user_data']['users']), 3) self.assertEqual(len(item['data']['version_downloads_data']), 0) self.assertEqual(len(item['data']['versions_data']['versions']), 7)
def test_has_resuming(self): """Test if it returns True when has_resuming is called""" self.assertEqual(Crates.has_resuming(), False)
def setUp(self): super().setUp() self.backend_write_archive = Crates(archive=self.archive) self.backend_read_archive = Crates(archive=self.archive)
class TestCratesBackendArchive(TestCaseBackendArchive): """Crates backend tests using an archive""" def setUp(self): super().setUp() self.backend_write_archive = Crates(archive=self.archive) self.backend_read_archive = Crates(archive=self.archive) @httpretty.activate def test_fetch_crates_from_archive(self): """Test whether a list of crates is returned from archive""" setup_http_server() self._test_fetch_from_archive() @httpretty.activate def test_fetch_summary_from_archive(self): """Test whether a summary is returned from archive""" setup_http_server() items = [ items for items in self.backend_write_archive.fetch( category=CATEGORY_SUMMARY) ] items_archived = [ items for items in self.backend_read_archive.fetch_from_archive() ] self.assertEqual(len(items), len(items_archived)) item = items[0] item_archived = items_archived[0] del item['data']['fetched_on'] del item_archived['data']['fetched_on'] self.assertEqual(item['data'], item_archived['data']) @httpretty.activate def test_fetch_from_date_from_archive(self): """Test when return from date from archive""" setup_http_server() from_date = datetime.datetime(2016, 1, 1) self._test_fetch_from_archive(from_date=from_date) @httpretty.activate def test_fetch_summary_from_date_from_archive(self): """Test when return from date from archive""" setup_http_server() from_date = datetime.datetime(2016, 1, 1) items = [ items for items in self.backend_write_archive.fetch( category=CATEGORY_SUMMARY, from_date=from_date) ] items_archived = [ items for items in self.backend_read_archive.fetch_from_archive() ] self.assertEqual(len(items), len(items_archived)) item = items[0] item_archived = items_archived[0] del item['data']['fetched_on'] del item_archived['data']['fetched_on'] self.assertEqual(item['data'], item_archived['data']) @httpretty.activate def test_fetch_empty_from_archive(self): """Test when fetch from archive return nothing when the archive is empty""" setup_http_server(empty=True) self._test_fetch_from_archive()
def test_has_archiving(self): """Test if it returns True when has_archiving is called""" self.assertEqual(Crates.has_archiving(), True)