From cf945c56472e3089f15a518f4823c50a4dfec0ab Mon Sep 17 00:00:00 2001 From: Jorrit Schaap <schaap@astron.nl> Date: Fri, 24 Jan 2020 16:23:09 +0100 Subject: [PATCH] TMSS-139: use setup/teardown magic for tmss test database --- SAS/TMSS/test/CMakeLists.txt | 1 + SAS/TMSS/test/t_tmssapp_scheduling_django.py | 13 ++----- .../test/t_tmssapp_specification_django.py | 13 ++----- SAS/TMSS/test/tmss_database_unittest_setup.py | 34 +++++++++++++++++++ 4 files changed, 39 insertions(+), 22 deletions(-) create mode 100644 SAS/TMSS/test/tmss_database_unittest_setup.py diff --git a/SAS/TMSS/test/CMakeLists.txt b/SAS/TMSS/test/CMakeLists.txt index e89812178a0..d323c2d52be 100644 --- a/SAS/TMSS/test/CMakeLists.txt +++ b/SAS/TMSS/test/CMakeLists.txt @@ -12,6 +12,7 @@ if(BUILD_TESTING) include(PythonInstall) python_install(test_utils.py + tmss_database_unittest_setup.py t_tmssapp_specification_django.py t_tmssapp_scheduling_django.py DESTINATION lofar/sas/tmss/test) diff --git a/SAS/TMSS/test/t_tmssapp_scheduling_django.py b/SAS/TMSS/test/t_tmssapp_scheduling_django.py index b33f42acb84..9d01c50e5c8 100755 --- a/SAS/TMSS/test/t_tmssapp_scheduling_django.py +++ b/SAS/TMSS/test/t_tmssapp_scheduling_django.py @@ -26,17 +26,8 @@ from datetime import datetime # todo: Tags? -> Decide how to deal with them first. # todo: Immutability of Blueprints on db level? - -# before we import any django modules the DJANGO_SETTINGS_MODULE and TMSS_DBCREDENTIALS need to be known/set. -# import and start an isolated TMSSTestDatabaseInstance (with fresh database) -# this automagically sets the required DJANGO_SETTINGS_MODULE and TMSS_DBCREDENTIALS envvars. -from lofar.sas.tmss.test.test_utils import TMSSTestDatabaseInstance -_tmss_test_db_instance = TMSSTestDatabaseInstance() -_tmss_test_db_instance.create() - -# tell unittest to stop (and automagically cleanup) the test database once all testing is done. -def tearDownModule(): - _tmss_test_db_instance.destroy() +# use setup/teardown magic for tmss test database +from lofar.sas.tmss.test.tmss_database_unittest_setup import * # now it's safe to import django modules from lofar.sas.tmss.tmss.tmssapp import models diff --git a/SAS/TMSS/test/t_tmssapp_specification_django.py b/SAS/TMSS/test/t_tmssapp_specification_django.py index 7221f426d5c..caf1c591afd 100755 --- a/SAS/TMSS/test/t_tmssapp_specification_django.py +++ b/SAS/TMSS/test/t_tmssapp_specification_django.py @@ -26,17 +26,8 @@ import uuid # todo: Tags? -> Decide how to deal with them first. # todo: Immutability of Blueprints on db level? - -# before we import any django modules the DJANGO_SETTINGS_MODULE and TMSS_DBCREDENTIALS need to be known/set. -# import and start an isolated TMSSTestDjangoServerWithPostgresInstance (with fresh database and django server) -# this automagically sets the required DJANGO_SETTINGS_MODULE and TMSS_DBCREDENTIALS envvars. -from lofar.sas.tmss.test.test_utils import TMSSTestDjangoServerWithPostgresInstance -_tmss_test_server = TMSSTestDjangoServerWithPostgresInstance() -_tmss_test_server.start() - -# tell unittest to stop (and automagically cleanup) the test database and django server once all testing is done. -def tearDownModule(): - _tmss_test_server.stop() +# use setup/teardown magic for tmss test database +from lofar.sas.tmss.test.tmss_database_unittest_setup import * # now it's safe to import django modules from lofar.sas.tmss.tmss.tmssapp import models diff --git a/SAS/TMSS/test/tmss_database_unittest_setup.py b/SAS/TMSS/test/tmss_database_unittest_setup.py new file mode 100644 index 00000000000..1eddd590c1c --- /dev/null +++ b/SAS/TMSS/test/tmss_database_unittest_setup.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2018 ASTRON (Netherlands Institute for Radio Astronomy) +# P.O. Box 2, 7990 AA Dwingeloo, The Netherlands +# +# This file is part of the LOFAR software suite. +# The LOFAR software suite is free software: you can redistribute it and/or +# modify it under the terms of the GNU General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# The LOFAR software suite is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>. + +''' +By importing this helper module in your unittest module you get a TMSSTestDatabaseInstance +which is automatically destroyed at the end of the unittest session. +''' + +# before we import any django modules the DJANGO_SETTINGS_MODULE and TMSS_DBCREDENTIALS need to be known/set. +# import and start an isolated TMSSTestDatabaseInstance (with fresh database) +# this automagically sets the required DJANGO_SETTINGS_MODULE and TMSS_DBCREDENTIALS envvars. +from lofar.sas.tmss.test.test_utils import TMSSTestDatabaseInstance +_tmss_test_db_instance = TMSSTestDatabaseInstance() +_tmss_test_db_instance.create() + +# tell unittest to stop (and automagically cleanup) the test database once all testing is done. +def tearDownModule(): + _tmss_test_db_instance.destroy() -- GitLab