From f395a2b21340557bae669c73127668749a951102 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Mon, 16 Jun 2014 10:48:03 -0400 Subject: [PATCH] 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. --- TODO.rst | 1 - subunit2sql/db/api.py | 28 +++++++++++++++++++++++++--- subunit2sql/exceptions.py | 4 ++++ subunit2sql/shell.py | 23 +++++++++++++++++++++++ 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/TODO.rst b/TODO.rst index 5f652d2..c495750 100644 --- a/TODO.rst +++ b/TODO.rst @@ -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. diff --git a/subunit2sql/db/api.py b/subunit2sql/db/api.py index b8fc6f2..4796035 100644 --- a/subunit2sql/db/api.py +++ b/subunit2sql/db/api.py @@ -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( diff --git a/subunit2sql/exceptions.py b/subunit2sql/exceptions.py index 8456696..6775918 100644 --- a/subunit2sql/exceptions.py +++ b/subunit2sql/exceptions.py @@ -44,3 +44,7 @@ class Subunit2SQLException(Exception): class InvalidRunCount(Subunit2SQLException): message = "Invalid Run Count" + + +class UnknownStatus(Subunit2SQLException): + message = "Unknown test status" diff --git a/subunit2sql/shell.py b/subunit2sql/shell.py index 9fe2b9f..9a8a8d8 100644 --- a/subunit2sql/shell.py +++ b/subunit2sql/shell.py @@ -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'])