diff --git a/SAS/TMSS/test/t_tmssapp_scheduling_django.py b/SAS/TMSS/test/t_tmssapp_scheduling_django.py index ce25ece5ddc34ca6302da39e3503ef1ceb01e6b0..494bdc7359c87c9abb1cc25afe9e3c632dab04af 100755 --- a/SAS/TMSS/test/t_tmssapp_scheduling_django.py +++ b/SAS/TMSS/test/t_tmssapp_scheduling_django.py @@ -32,11 +32,21 @@ logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=loggin # todo: Tags? -> Decide how to deal with them first. # todo: Immutability of Blueprints on db level? +# Do Mandatory setup steps: +# Setup step 1: # use setup/teardown magic for tmss test database +# (ignore pycharm unused import statement, python unittests does use at RunTime the tmss_database_unittest_setup module) from lofar.sas.tmss.test.tmss_database_unittest_setup import * -# now it's safe to import django modules +# Setup step 2: +# now it's safe to import django modules, and mandatory to set env DJANGO_SETTINGS_MODULE and do django.setup() +import django +django.setup() + +# Setup step 3: +# Do the actual tmss imports from lofar.sas.tmss.tmss.tmssapp import models + from django.db.utils import IntegrityError from lofar.sas.tmss.test.t_tmssapp_specification_django import TaskBlueprint_test_data, TaskRelationBlueprint_test_data diff --git a/SAS/TMSS/test/t_tmssapp_specification_django.py b/SAS/TMSS/test/t_tmssapp_specification_django.py index 46e7d80c193cbeb9d665d9a90d235c011cfa02bb..345ebeb57f17d75083a06cc70c48e67a056059d9 100755 --- a/SAS/TMSS/test/t_tmssapp_specification_django.py +++ b/SAS/TMSS/test/t_tmssapp_specification_django.py @@ -31,11 +31,22 @@ logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=loggin # todo: Tags? -> Decide how to deal with them first. # todo: Immutability of Blueprints on db level? +# Do Mandatory setup steps: +# Setup step 1: # use setup/teardown magic for tmss test database +# (ignore pycharm unused import statement, python unittests does use at RunTime the tmss_database_unittest_setup module) from lofar.sas.tmss.test.tmss_database_unittest_setup import * -# now it's safe to import django modules +# Setup step 2: +# now it's safe to import django modules, and mandatory to set env DJANGO_SETTINGS_MODULE and do django.setup() +import django +django.setup() + +# Setup step 3: +# Do the actual tmss imports from lofar.sas.tmss.tmss.tmssapp import models + + from django.db.utils import IntegrityError from django.contrib.auth.models import User @@ -44,8 +55,8 @@ from django.contrib.auth.models import User # client = rest_framework.test.APIClient() # from lofar.sas.tmss.test.test_utils import assertDataWithUrls, assertUrlList -def GeneratorTemplate_test_data(): - return {"name": "observation", +def GeneratorTemplate_test_data(name="my_GeneratorTemplate"): + return {"name": name, "description": 'My one observation', "version": 'v0.314159265359', "schema": {"mykey": "my value"}, @@ -107,27 +118,21 @@ class GeneratorTemplateTest(unittest.TestCase): # self.assertIn(item, response2.data.items()) -def DefaultGeneratorTemplate_test_data(): - return {'name': 'default', - 'template': None, - 'tags':[]} +def DefaultGeneratorTemplate_test_data(name=None, template=None): + return {'name': name if name is not None else "DefaultGeneratorTemplate_"+str(uuid.uuid4()), + 'template': template, + 'tags':[]} class DefaultGeneratorTemplateTest(unittest.TestCase): def test_DefaultGeneratorTemplate_prevents_same_name(self): + common_forbidden_name = "my_name" + template = models.GeneratorTemplate.objects.create(**GeneratorTemplate_test_data()) - templ1 = models.GeneratorTemplate.objects.create(**GeneratorTemplate_test_data()) - templ2 = models.GeneratorTemplate.objects.create(**GeneratorTemplate_test_data()) - - # setup - test_data_1 = dict(DefaultGeneratorTemplate_test_data()) - test_data_2 = dict(DefaultGeneratorTemplate_test_data()) - test_data_1['template'] = templ1 - test_data_2['template'] = templ2 - + test_data_1 = DefaultGeneratorTemplate_test_data(common_forbidden_name, template) models.DefaultGeneratorTemplate.objects.create(**test_data_1) - # assert + test_data_2 = DefaultGeneratorTemplate_test_data(common_forbidden_name, template) with self.assertRaises(IntegrityError): models.DefaultGeneratorTemplate.objects.create(**test_data_2) diff --git a/SAS/TMSS/test/test_utils.py b/SAS/TMSS/test/test_utils.py index ee5c81e1f21efe5a149910e7f93b000fe8386e13..30f8f664e924688d8a50d7a90eec4c444b004ee8 100644 --- a/SAS/TMSS/test/test_utils.py +++ b/SAS/TMSS/test/test_utils.py @@ -76,23 +76,25 @@ class TMSSTestDatabaseInstance(PostgresTestDatabaseInstance): def apply_database_schema(self): logger.info('applying TMSS sql schema to %s', self.dbcreds) - def _migrate_helper(): - # a TMSSTestDatabaseInstance needs to run in a clean env, - # with these variables set to the current test values. - os.environ["TMSS_DBCREDENTIALS"] = self.dbcreds_id - os.environ["DJANGO_SETTINGS_MODULE"] = "lofar.sas.tmss.tmss.settings" + # a TMSSTestDatabaseInstance needs to run in a clean env, + # with these variables set to the current test values. + import os + os.environ["TMSS_DBCREDENTIALS"] = self.dbcreds_id + os.environ["DJANGO_SETTINGS_MODULE"] = "lofar.sas.tmss.tmss.settings" + # run migrate in a seperate process so the needed django setup does not pollute our current apps environment + def _migrate_helper(): # use django management modules to apply database schema via initial migration import django django.setup() django.core.management.call_command('migrate') - # self._server_process = Process(target=_migrate_helper, daemon=True) self._server_process.start() self._server_process.join() + class TMSSPostgresTestMixin(PostgresTestMixin): ''' A common test mixin class from which you can derive to get a freshly setup postgres testing instance with the latest TMSS sql schema.