Store counts for runs and tests

This commit adds count updates to both the run and test tables.
Before this commit the counts of test status in the run table and
test tables weren't ever used.
This commit is contained in:
Matthew Treinish 2014-06-16 10:48:03 -04:00
parent 595336f599
commit f395a2b213
4 changed files with 52 additions and 4 deletions

View File

@ -3,7 +3,6 @@ Work Items for Subunit2SQL
Short Term
----------
* Store counts to everything in the DB from the shell script
* Add a new metadata table for each existing table (run_metadata,
test_metadata, test_run_metadata) to store extra info from stream like
tags, or attrs and other information about runs like job name.

View File

@ -59,12 +59,20 @@ def create_test(test_id, run_count=0, success=0, failure=0, session=None):
test.run_count = run_count
test.success = success
test.failure = failure
session = get_session()
session = session or get_session()
with session.begin():
session.add(test)
return test
def update_test(values, test_id, session=None):
session = session or get_session()
with session.begin():
test = get_test_by_id(test_id, session)
test.update(values)
return test
def create_run(skips=0, fails=0, passes=0, run_time=0, artifacts=None,
session=None):
"""Create a new run record in the database
@ -87,8 +95,16 @@ def create_run(skips=0, fails=0, passes=0, run_time=0, artifacts=None,
return run
def update_run(values, run_id, session=None):
session = session or get_session()
with session.begin():
run = get_run_by_id(run_id, session)
run.update(values)
return run
def create_test_run(test_id, run_id, status, start_time=None,
end_time=None):
end_time=None, session=None):
"""Create a new test run record in the database
:param test_id: uuid for test that was run
@ -101,7 +117,7 @@ def create_test_run(test_id, run_id, status, start_time=None,
test_run.run_id = run_id
test_run.stop_time = end_time
test_run.start_time = start_time
session = get_session()
session = session or get_session()
with session.begin():
session.add(test_run)
return test_run
@ -136,6 +152,12 @@ def get_test_by_test_id(test_id, session=None):
return test
def get_run_by_id(id, session=None):
session = session or get_session()
run = db_utils.model_query(models.Run, session).filter_by(id=id).first()
return run
def get_test_run_by_id(test_run_id, session=None):
session = session or get_session()
test_run = db_utils.model_query(models.TestRun, session=session).filter_by(

View File

@ -44,3 +44,7 @@ class Subunit2SQLException(Exception):
class InvalidRunCount(Subunit2SQLException):
message = "Invalid Run Count"
class UnknownStatus(Subunit2SQLException):
message = "Unknown test status"

View File

@ -20,6 +20,7 @@ from oslo.config import cfg
from oslo.db import options
from subunit2sql.db import api
from subunit2sql import exceptions
from subunit2sql import read_subunit as subunit
shell_opts = [
@ -49,6 +50,27 @@ def parse_args(argv, default_config_files=None):
default_config_files=default_config_files)
def increment_counts(run, test, status, session=None):
test_values = {'run_count': test.run_count + 1}
run_values = {}
run = api.get_run_by_id(run.id, session)
if status == 'success':
test_values['success'] = test.success + 1
run_values['passes'] = run.passes + 1
elif status == 'fail':
test_values['failure'] = test.failure + 1
run_values['fails'] = run.fails + 1
elif status == 'skip':
test_values = {}
run_values['skips'] = run.skips + 1
else:
msg = "Unknown test status %s" % status
raise exceptions.UnknownStatus(msg)
if test_values:
api.update_test(test_values, test.id)
api.update_run(run_values, run.id)
def process_results(results):
session = api.get_session()
db_run = api.create_run(run_time=results.pop('run_time'))
@ -56,6 +78,7 @@ def process_results(results):
db_test = api.get_test_by_test_id(test, session)
if not db_test:
db_test = api.create_test(test)
increment_counts(db_run, db_test, results[test]['status'], session)
api.create_test_run(db_test.id, db_run.id, results[test]['status'],
results[test]['start_time'],
results[test]['end_time'])