Skip to content

Commit

Permalink
create_run_period with unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
DraTeots committed Nov 22, 2023
1 parent be58e33 commit 002de60
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
54 changes: 54 additions & 0 deletions python/rcdb/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ def connect(self, connection_string="mysql+pymysql://[email protected]/rcdb", check
:type connection_string: str
"""

if not connection_string:
raise ValueError("Connection string is whitespace or empty. Provide proper connection string for DB")

try:
self.engine = sqlalchemy.create_engine(connection_string)
except ImportError as err:
Expand Down Expand Up @@ -316,6 +319,57 @@ def get_run_periods(self):
except NoResultFound:
return []

# ------------------------------------------------
# Creates run period
# ------------------------------------------------
def create_run_period(self, name, description, run_min, run_max, start_date, end_date):
"""
Creates run period
:param name: Short name or run period e.g. Gluex Spring 2018
:type name: str
:param description: More detailed description if needed
:type description: str
:return: ConditionType object that corresponds to created DB record
:rtype: ConditionType
"""

query = self.session.query(RunPeriod).filter(RunPeriod.run_min == run_min, RunPeriod.run_max == run_max)

if query.count():
# we've found a run period with this run_max and run_min
rp = query.first()
assert isinstance(rp, RunPeriod)

message = f"Run period with run_min={run_min} and run_max={run_max} already exists in DB:" \
f"name={rp.name}, descr.={rp.description}, start_date={rp.start_date}, end_date={rp.end_date}"

raise ValueError(message)
else:
# no such ConditionType found in the database
rp = RunPeriod()
rp.name = name
rp.description = description
rp.start_date = start_date
rp.end_date = end_date
rp.run_min = run_min
rp.run_max = run_max

try:
self.session.add(rp)
self.session.commit()
# clear cache
self._run_periods_cache = None
except:
self.session.rollback()
raise

log_desc = f"RunPeriod created with name='{name}', run_min='{run_min}' run_max='{run_max}'"
self.add_log_record(rp, log_desc, 0)
return rp

# ------------------------------------------------
# Returns condition type
# ------------------------------------------------
Expand Down
51 changes: 50 additions & 1 deletion python/tests/test_get_run_periods.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,66 @@
import rcdb
import rcdb.model
from rcdb.model import RunPeriod
import datetime

"""
Example of run periods table used in this unit tests:
Yet another data example is here:
https://halldweb.jlab.org/wiki/index.php/Run_Periods
run_periods = {
"2017-01": (30000, 39999, "23 Jan 2017 - 13 Mar 2017 12 GeV e-"),
"2016-10": (20000, 29999, "15 Sep 2016 - 21 Dec 2016 12 GeV e-"),
"2016-02": (10000, 19999, "28 Jan 2016 - 24 Apr 2016 Commissioning, 12 GeV e-"),
"2015-12": (3939, 4807, "01 Dec 2015 - 28 Jan 2016 Commissioning, 12 GeV e-, Cosmics"),
"2015-06": (3386, 3938, "29 May 2015 - 01 Dec 2015 Cosmics"),
"2015-03": (2607, 3385, "11 Mar 2015 - 29 May 2015 Commissioning, 5.5 GeV e-"),
"2015-01": (2440, 2606, "06 Feb 2015 - 11 Mar 2015 Cosmics"),
"2014-10": (630, 2439, "28 Oct 2014 - 21 Dec 2014 Commissioning, 10 GeV e-"),
}
"""

class TestRunPeriod(unittest.TestCase):
def setUp(self):
self.db = rcdb.RCDBProvider("sqlite://", check_version=False)
rcdb.provider.destroy_all_create_schema(self.db)


def tearDown(self):
self.db.disconnect()

def test_create_run_periods(self):
"""Test of Run in db function"""

rp = self.db.create_run_period("Gluex 2017-01",
"12 GeV e-",
30000,
39999,
datetime.date(2017,1, 23),
datetime.date(2017, 3, 13))
self.assertEqual(rp.name, "Gluex 2017-01")
self.assertEqual(rp.description, "12 GeV e-")
self.assertEqual(rp.start_date, datetime.date(2017,1, 23))
self.assertEqual(rp.end_date, datetime.date(2017, 3, 13))
self.assertEqual(rp.run_min, 30000)
self.assertEqual(rp.run_max, 39999)

def test_get_run_periods(self):
self.db.create_run_period("Gluex 2017-01",
"12 GeV e-",
30000,
39999,
datetime.date(2017,1, 23),
datetime.date(2017, 3, 13))

self.db.create_run_period("Gluex 2016-02",
"End of GlueX phase",
20000,
29999,
datetime.date(2016,9, 15),
datetime.date(2016, 12, 21))
rps = self.db.get_run_periods()
self.assertEqual(len(rps), 2)


if __name__ == '__main__':
unittest.main()

0 comments on commit 002de60

Please sign in to comment.