Skip to content
Snippets Groups Projects
Commit 19e78c1a authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

TMSS-139: changed import order and environment settings to make tests run again

parent 337304d0
No related branches found
No related tags found
1 merge request!96Resolve TMSS-139
......@@ -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
......
......@@ -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,
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)
......
......@@ -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.
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.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment