def test_catalogue_of_life(tmpdir): data = fixtures('data_providers', 'catalogueoflife') id_ = '9249d9473aac5c8e99fb9d758ced91ec' repos = create_repos(tmpdir) with patch('pytsammalex.util.requests', MockRequests(content=data['identify'])): prov = CatalogueOfLife(Path(repos)) assert (prov.identify('x') == id_) with patch('pytsammalex.util.requests', MockRequests(content=data['metadata'])): prov = CatalogueOfLife(Path(repos)) md = prov.cached_metadata('test', id_) taxon = {} prov.update(taxon, md) assert ( taxon == { 'catalogueoflife_url': 'http://www.catalogueoflife.org/col/' 'browse/tree/id/' '9249d9473aac5c8e99fb9d758ced91ec', 'class': 'Mammalia', 'family': 'Felidae', 'kingdom': 'Animalia', 'order': 'Carnivora', 'phylum': 'Chordata'})
def test_Eol(self): from pytsammalex.data_providers.eol import EOL data = fixtures('data_providers', 'eol') with patch( 'pytsammalex.util.requests', MockRequests(json=[ data['identify'], data['metadata'], data['hierarchy'] ])): prov = EOL(create_repos(self.tmp_path())) taxon = {'id': 'pantheraleo', 'name': 'Panthera leo'} prov.update_taxon(taxon) self.assertEqual( taxon, { 'eol_id': 328672, 'english_name': 'Asian lion', 'id': 'pantheraleo', 'name': 'Panthera leo', 'class': 'Mammalia', 'family': 'Felidae', 'genus': 'Panthera', 'kingdom': 'Animalia', 'order': 'Carnivora', 'phylum': 'Chordata', 'taxonRank': 'species' })
def test_catalogue_of_life(tmpdir): data = fixtures('data_providers', 'catalogueoflife') id_ = '9249d9473aac5c8e99fb9d758ced91ec' repos = create_repos(tmpdir) with patch('pytsammalex.util.requests', MockRequests(content=data['identify'])): prov = CatalogueOfLife(Path(repos)) assert (prov.identify('x') == id_) with patch('pytsammalex.util.requests', MockRequests(content=data['metadata'])): prov = CatalogueOfLife(Path(repos)) md = prov.cached_metadata('test', id_) taxon = {} prov.update(taxon, md) assert (taxon == { 'catalogueoflife_url': 'http://www.catalogueoflife.org/col/' 'browse/tree/id/' '9249d9473aac5c8e99fb9d758ced91ec', 'class': 'Mammalia', 'family': 'Felidae', 'kingdom': 'Animalia', 'order': 'Carnivora', 'phylum': 'Chordata' })
def test_ImageProvider_retrieve(self): from pytsammalex.image_providers.base import ImageProvider repos = create_repos(self.tmp_path()) fname = self.tmp_path('test') media = self.tmp_path(('media.json')) with fname.open('w', encoding='utf8') as fp: fp.write('test') class P(ImageProvider): def __init__(self, md): self._md = md ImageProvider.__init__(self, repos) def metadata(self, item): return self._md self.assertIsNone(P({}).retrieve(None, None, None, None)) staged_image = Staged_images.fromdict({'id': 'abc', 'taxa__id': 'abc'}) prov = P({'source_url': fname}) with self.assertRaises(ValueError): prov.retrieve(staged_image, None, [md5(fname)], None) cdstar = MagicMock( create=MagicMock(return_value=[(None, None, MOCK_CDSTAR_OBJECT)])) prov = P({'source_url': 'x'}) with patch('pytsammalex.util.requests', MockRequests()): with MediaCatalog(media.name, repos=repos) as mcat: prov.retrieve(staged_image, cdstar, [], mcat) self.assertTrue(cdstar.create.called) self.assertEqual(len(MediaCatalog(media.name, repos=repos)), 1)
def test_Catalogueoflife(self): from pytsammalex.data_providers.catalogueoflife import CatalogueOfLife data = fixtures('data_providers', 'catalogueoflife') id_ = '9249d9473aac5c8e99fb9d758ced91ec' repos = create_repos(self.tmp_path()) with patch('pytsammalex.util.requests', MockRequests(content=data['identify'])): prov = CatalogueOfLife(repos) self.assertEqual(prov.identify('x'), id_) with patch('pytsammalex.util.requests', MockRequests(content=data['metadata'])): prov = CatalogueOfLife(repos) md = prov.cached_metadata('test', id_) taxon = {} prov.update(taxon, md) self.assertEqual( taxon, { 'catalogueoflife_url': 'http://www.catalogueoflife.org/col/browse/tree/id/9249d9473aac5c8e99fb9d758ced91ec', 'class': 'Mammalia', 'family': 'Felidae', 'kingdom': 'Animalia', 'order': 'Carnivora', 'phylum': 'Chordata' })
def test_TaxaData(self): from pytsammalex.taxa import TaxaData repos = create_repos(self.tmp_path()) with TaxaData(repos) as taxa: taxa.add(0, Taxa.fromdict({'id': 'abc'})) self.assertIn('abc', TaxaData(repos))
def test_taxa_data(tmpdir): repos = Path(create_repos(tmpdir)) with TaxaData(repos) as taxa: taxa.add(0, Taxa.fromdict({'id': 'abc'})) assert 'abc' in TaxaData(repos)
def test_ImageProvider(self): from pytsammalex.image_providers.base import ImageProvider repos = create_repos(self.tmp_path()) prov = ImageProvider(repos) with self.assertRaises(NotImplementedError): 'x' in prov self.assertEqual(len(prov.url_parts({'id': '/path'})), 3)
def test_image_provider(tmpdir): repos = create_repos(tmpdir) prov = ImageProvider(repos) with pytest.raises(NotImplementedError): 'x' in prov assert (len(prov.url_parts({'id': '/path'})) == 3)
def test_JsonData(self): from pytsammalex.util import JsonData, data_file tmpdir = create_repos(self.tmp_path()) with JsonData('test.json', repos=tmpdir) as jdat: jdat['a'] = 1 self.assertTrue(data_file('test.json', repos=tmpdir).exists()) with JsonData('test.json', repos=tmpdir) as jdat: self.assertEqual(len(jdat), 1) self.assertEqual(jdat['a'], 1)
def test_json_data(tmpdir): tmp_ = create_repos(tmpdir) with JsonData('test.json', repos=Path(tmp_)) as jdat: jdat['a'] = 1 assert (data_file('test.json', repos=Path(tmp_)).exists() is True) with JsonData('test.json', repos=Path(tmp_)) as jdat: assert (len(jdat) == 1) assert (jdat['a'] == 1)
def test_update(tmpdir): repos = create_repos(tmpdir) with patch.multiple('pytsammalex.distribution', shape=Mock(return_value=Mock(return_value=True)), Point=Mock()): update(Path(repos), log=Mock()) data = CsvData('distribution', repos=Path(repos)) assert (len(data) == 1) assert (data.items[0].ecoregions__ids == [ 'AT0110', 'AT0111', 'AT0112', 'AT0113', 'AT0114', 'AT0115', 'AT0116', 'AT0117', 'AT0118', 'AT0119' ])
def test_update(self): from pytsammalex.distribution import update repos = create_repos(self.tmp_path()) with patch.multiple('pytsammalex.distribution', shape=Mock(return_value=Mock(return_value=True)), Point=Mock()): update(repos, verbose=False) data = CsvData('distribution', repos=repos) self.assertEqual(len(data), 1) self.assertEqual(data.items[0].ecoregions__ids, [ 'AT0110', 'AT0111', 'AT0112', 'AT0113', 'AT0114', 'AT0115', 'AT0116', 'AT0117', 'AT0118', 'AT0119' ])
def test_gbif(tmpdir): data = fixtures('data_providers', 'gbif') with patch('pytsammalex.util.requests', MockRequests(json=[data['identify'], data['metadata']])): prov = GBIF(Path(create_repos(tmpdir))) taxon = {'id': 'pantheraleo', 'name': 'Panthera leo'} prov.update_taxon(taxon) assert (taxon == { 'gbif_id': 5219404, 'id': 'pantheraleo', 'name': 'Panthera leo', 'class': 'Mammalia', 'family': 'Felidae', 'genus': 'Panthera', 'kingdom': 'Animalia', 'order': 'Carnivora', 'phylum': 'Chordata', 'taxonRank': 'species' })
def test_gbif(tmpdir): data = fixtures('data_providers', 'gbif') with patch( 'pytsammalex.util.requests', MockRequests(json=[data['identify'], data['metadata']])): prov = GBIF(Path(create_repos(tmpdir))) taxon = {'id': 'pantheraleo', 'name': 'Panthera leo'} prov.update_taxon(taxon) assert ( taxon == { 'gbif_id': 5219404, 'id': 'pantheraleo', 'name': 'Panthera leo', 'class': 'Mammalia', 'family': 'Felidae', 'genus': 'Panthera', 'kingdom': 'Animalia', 'order': 'Carnivora', 'phylum': 'Chordata', 'taxonRank': 'species'})
def test_Gbif(self): from pytsammalex.data_providers.gbif import GBIF data = fixtures('data_providers', 'gbif') with patch('pytsammalex.util.requests', MockRequests(json=[data['identify'], data['metadata']])): prov = GBIF(create_repos(self.tmp_path())) taxon = {'id': 'pantheraleo', 'name': 'Panthera leo'} prov.update_taxon(taxon) self.assertEqual( taxon, { 'gbif_id': 5219404, 'id': 'pantheraleo', 'name': 'Panthera leo', 'class': 'Mammalia', 'family': 'Felidae', 'genus': 'Panthera', 'kingdom': 'Animalia', 'order': 'Carnivora', 'phylum': 'Chordata', 'taxonRank': 'species' })
def test_image_provider_retrieve(tmpdir): repos = create_repos(tmpdir) fname = tmpdir.join('test') with fname.open('w', encoding='utf8') as fp: fp.write('test') class TestProvider(ImageProvider): def identify(self, name): pass def __init__(self, md): self._md = md ImageProvider.__init__(self, repos) def metadata(self, item): return self._md assert (TestProvider({}).retrieve(None, None, None, None) is None) staged_image = Staged_images.fromdict({'id': 'abc', 'taxa__id': 'abc'}) prov = TestProvider({'source_url': fname}) with pytest.raises(ValueError): prov.retrieve(staged_image, None, [md5(fname)], None) cdstar = MagicMock( create=MagicMock(return_value=[(None, None, MOCK_CDSTAR_OBJECT)])) prov = TestProvider({'source_url': 'x'}) with patch('pytsammalex.util.requests', MockRequests()): with MediaCatalog('media.json', repos=Path(repos)) as mcat: prov.retrieve(staged_image, cdstar, [], mcat) assert (cdstar.create.called is True) assert (len(MediaCatalog('media.json', repos=Path(repos))) == 1)
def test_image_provider_retrieve(tmpdir): repos = create_repos(tmpdir) fname = tmpdir.join('test') with fname.open('w', encoding='utf8') as fp: fp.write('test') class TestProvider(ImageProvider): def identify(self, name): pass def __init__(self, md): self._md = md ImageProvider.__init__(self, repos) def metadata(self, item): return self._md assert (TestProvider({}).retrieve(None, None, None, None) is None) staged_image = Staged_images.fromdict({'id': 'abc', 'taxa__id': 'abc'}) prov = TestProvider({'source_url': fname}) with pytest.raises(ValueError): prov.retrieve(staged_image, None, [md5(fname)], None) cdstar = MagicMock(create=MagicMock(return_value=[(None, None, MOCK_CDSTAR_OBJECT)])) prov = TestProvider({'source_url': 'x'}) with patch('pytsammalex.util.requests', MockRequests()): with MediaCatalog('media.json', repos=Path(repos)) as mcat: prov.retrieve(staged_image, cdstar, [], mcat) assert (cdstar.create.called is True) assert (len(MediaCatalog('media.json', repos=Path(repos))) == 1)
def test_eol(tmpdir): data = fixtures('data_providers', 'eol') with patch( 'pytsammalex.util.requests', MockRequests( json=[data['identify'], data['metadata'], data['hierarchy']]) ): prov = EOL(Path(create_repos(tmpdir))) taxon = {'id': 'pantheraleo', 'name': 'Panthera leo'} prov.update_taxon(taxon) assert ( taxon == { 'eol_id': 328672, 'english_name': 'Asian lion', 'id': 'pantheraleo', 'name': 'Panthera leo', 'class': 'Mammalia', 'family': 'Felidae', 'genus': 'Panthera', 'kingdom': 'Animalia', 'order': 'Carnivora', 'phylum': 'Chordata', 'taxonRank': 'species'})