Browse Source

Factorize test code.

master
Damien Goutte-Gattat 5 months ago
parent
commit
ae88830522
  1. 39
      tests/test_usa.py

39
tests/test_usa.py

@ -93,24 +93,27 @@ class TestDatabaseUSA(unittest.TestCase):
db_provider.cfg.set('db1', 'subdb', 'tests')
self.db_provider = db_provider
def _parse(self, u):
return usa.parse_usa(u, databases=self.db_provider)
def test_defaulting_to_file(self):
"""Check that database-based USAs are parsed as file-based
USAs if the database is not recognized."""
usas = usa.parse_usa('dbname', databases=self.db_provider)
usas = self._parse('dbname')
self.assertIsInstance(usas, usa.FileUSA)
def test_recognize_database_usa(self):
"""Check that database-based USAs are recognized as such."""
usas = usa.parse_usa('db1', databases=self.db_provider)
usas = self._parse('db1')
self.assertIsInstance(usas, usa.DatabaseUSA)
self.assertEqual(usas.database, self.db_provider['db1'])
def test_parse_database_identifier(self):
"""Check that database-based USAs with identifier are parsed correctly."""
usas = usa.parse_usa('db1:id', databases=self.db_provider)
usas = self._parse('db1:id')
self.assertIsInstance(usas, usa.DatabaseUSA)
self.assertEqual(usas.database, self.db_provider['db1'])
self.assertEqual(usas.identifier, 'id')
@ -118,7 +121,7 @@ class TestDatabaseUSA(unittest.TestCase):
def test_parse_database_keyword(self):
"""Check that database-based USAs with keyword search are parsed correctly."""
usas = usa.parse_usa('db1-acc:word', databases=self.db_provider)
usas = self._parse('db1-acc:word')
self.assertIsInstance(usas, usa.DatabaseUSA)
self.assertEqual(usas.database, self.db_provider['db1'])
self.assertEqual(usas.field, 'acc')
@ -128,15 +131,15 @@ class TestDatabaseUSA(unittest.TestCase):
"""Check that invalid database-based USAs are recognized as such."""
with self.assertRaisesRegex(usa.Error, "Invalid database USA: keyword required"):
usa.parse_usa('db1-acc', databases=self.db_provider)
self._parse('db1-acc')
with self.assertRaisesRegex(usa.Error, "Invalid database USA: too many parts"):
usa.parse_usa('db1:id:extra', databases=self.db_provider)
self._parse('db1:id:extra')
def test_read_database_usa(self):
"""Check that we can read sequences from a database-based USA."""
usas = usa.parse_usa('db1', databases=self.db_provider)
usas = self._parse('db1')
records = usas.read()
self.assertEqual(len(records), 10)
self.assertEqual(records[0].name, 'EMBOSS_001')
@ -144,26 +147,26 @@ class TestDatabaseUSA(unittest.TestCase):
def test_read_database_usa_with_identifiers(self):
"""Check that we can filter by identifier."""
usas = usa.parse_usa('db1:EMBOSS_001', databases=self.db_provider)
usas = self._parse('db1:EMBOSS_001')
records = usas.read()
self.assertEqual(len(records), 1)
self.assertEqual(records[0].name, 'EMBOSS_001')
usas = usa.parse_usa('db1:EMBOSS_099', databases=self.db_provider)
usas = self._parse('db1:EMBOSS_099')
records = usas.read()
self.assertEqual(len(records), 0)
usas = usa.parse_usa('db1:EMBOSS_00?', databases=self.db_provider)
usas = self._parse('db1:EMBOSS_00?')
records = usas.read()
self.assertEqual(len(records), 9)
self.assertEqual(records[0].name, 'EMBOSS_001')
usas = usa.parse_usa('db1:EMBOSS_*', databases=self.db_provider)
usas = self._parse('db1:EMBOSS_*')
records = usas.read()
self.assertEqual(len(records), 10)
self.assertEqual(records[0].name, 'EMBOSS_001')
usas = usa.parse_usa('db1:TES_000002', databases=self.db_provider)
usas = self._parse('db1:TES_000002')
records = usas.read()
self.assertEqual(len(records), 1)
self.assertEqual(records[0].name, 'EMBOSS_002')
@ -171,31 +174,31 @@ class TestDatabaseUSA(unittest.TestCase):
def test_read_database_usa_with_fields(self):
"""Check that we can filter by search fields."""
usas = usa.parse_usa('db1-acc:TES_000003', databases=self.db_provider)
usas = self._parse('db1-acc:TES_000003')
records = usas.read()
self.assertEqual(len(records), 1)
self.assertEqual(records[0].name, 'EMBOSS_003')
usas = usa.parse_usa('db1-acc:TES_0000?0', databases=self.db_provider)
usas = self._parse('db1-acc:TES_0000?0')
records = usas.read()
self.assertEqual(len(records), 1)
self.assertEqual(records[0].name, 'EMBOSS_010')
usas = usa.parse_usa('db1-acc:TES_0000*', databases=self.db_provider)
usas = self._parse('db1-acc:TES_0000*')
records = usas.read()
self.assertEqual(len(records), 10)
usas = usa.parse_usa('db1-sv:TES_000009.?', databases=self.db_provider)
usas = self._parse('db1-sv:TES_000009.?')
records = usas.read()
self.assertEqual(len(records), 1)
self.assertEqual(records[0].name, 'EMBOSS_009')
usas = usa.parse_usa('db1-sv:10', databases=self.db_provider)
usas = self._parse('db1-sv:10')
records = usas.read()
self.assertEqual(len(records), 1)
self.assertEqual(records[0].name, 'EMBOSS_010')
usas = usa.parse_usa('db1-id:EMBOSS_002', databases=self.db_provider)
usas = self._parse('db1-id:EMBOSS_002')
records = usas.read()
self.assertEqual(len(records), 1)
self.assertEqual(records[0].name, 'EMBOSS_002')

Loading…
Cancel
Save