Browse Source

Add get_coverage()

Checking the unexpected records.

Change-Id: I22aa3aa528259ede949df935993a6d1ce24ca7fd
changes/99/396499/5
Masayuki Igawa 5 years ago
parent
commit
6390a0086d
  1. 24
      coverage2sql/db/api.py
  2. 2
      coverage2sql/migrations/env.py
  3. 4
      coverage2sql/tests/coverage2sql_fixtures.py
  4. 18
      coverage2sql/tests/db/test_api.py

24
coverage2sql/db/api.py

@ -29,6 +29,7 @@ CONF.register_cli_opt(cfg.BoolOpt('verbose', short='v', default=False,
DAY_SECONDS = 60 * 60 * 24
Session = None
engine = None
def setup():
@ -55,8 +56,7 @@ def get_session(autocommit=True, expire_on_commit=False):
:param bool expire_on_commit: Expire the session on commit defaults False.
"""
global Session
if not Session:
setup()
setup()
session = Session(autocommit=autocommit,
expire_on_commit=expire_on_commit)
@ -101,3 +101,23 @@ def create_coverage(project_name, coverage_rate=0.0, report_time=None,
with session.begin():
session.add(coverage)
return coverage
def get_coverage(project_name=None, test_type=None, session=None):
"""Get new coverage records in the database.
This method is used to get coverage records in the database. The records
are order by report_time.
:param str project_name: project_name e.g. openstack/tempest
:param str test_type: test_type e.g. py35
:return: The coverage objects from the DB
:rtype: list
"""
session = session or get_session()
covs = session.query(models.Coverage)
if project_name:
covs = covs.filter_by(project_name=project_name)
if test_type:
covs = covs.filter_by(test_type=test_type)
return covs.order_by(models.Coverage.report_time).all()

2
coverage2sql/migrations/env.py

@ -74,7 +74,7 @@ def run_migrations_online():
and associate a connection with the context.
"""
connectable = db_api.get_session().get_bind()
connectable = db_api.engine
with connectable.connect() as connection:
context.configure(

4
coverage2sql/tests/coverage2sql_fixtures.py

@ -46,13 +46,13 @@ class Database(fix.Fixture):
db_test_utils.run_migration("head")
def cleanup(self):
engine = db_api.get_session().get_bind()
engine = db_api.engine
engine.dispose()
pass
def reset(self):
self._cache_schema()
engine = db_api.get_session().get_bind()
engine = db_api.engine
engine.dispose()
engine.connect()

18
coverage2sql/tests/db/test_api.py

@ -47,3 +47,21 @@ class TestDatabaseAPI(base.TestCase):
cov = api.create_coverage('foo_project')
self.assertTrue(cov is not None)
self.assertEqual(cov.project_name, 'foo_project')
def test_get_coverage_all(self):
api.create_coverage('foo1_project')
api.create_coverage('foo2_project')
covs = api.get_coverage()
self.assertTrue(covs is not None)
self.assertEqual(len(covs), 2)
names = [n.project_name for n in covs]
self.assertIn(needle='foo1_project', haystack=names)
self.assertIn(needle='foo2_project', haystack=names)
def test_get_coverage_with_projenct_name(self):
api.create_coverage('foo1_project')
api.create_coverage('foo2_project')
covs = api.get_coverage(project_name='foo1_project')
self.assertTrue(covs is not None)
self.assertEqual(len(covs), 1)
self.assertEqual(covs[0].project_name, 'foo1_project')
Loading…
Cancel
Save