2019-12-18 19:02:17 +00:00
|
|
|
# Set environment variable to testing before loading.
|
|
|
|
# IMPORTANT - Environment must be loaded before app, models, etc....
|
|
|
|
import json
|
|
|
|
import os
|
2020-01-24 14:35:14 +00:00
|
|
|
import unittest
|
|
|
|
|
2020-01-24 16:52:52 +00:00
|
|
|
from crc.models.file import FileModel, FileDataModel
|
|
|
|
from crc.models.workflow import WorkflowSpecModel
|
2020-01-24 14:35:14 +00:00
|
|
|
|
2019-12-18 19:02:17 +00:00
|
|
|
os.environ["TESTING"] = "true"
|
|
|
|
|
2020-01-14 16:45:12 +00:00
|
|
|
from crc import app, db, session
|
2019-12-30 18:03:57 +00:00
|
|
|
from example_data import ExampleDataLoader
|
2019-12-18 19:02:17 +00:00
|
|
|
|
2019-12-27 18:50:03 +00:00
|
|
|
# UNCOMMENT THIS FOR DEBUGGING SQL ALCHEMY QUERIES
|
|
|
|
# import logging
|
|
|
|
# logging.basicConfig()
|
|
|
|
# logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
|
|
|
|
|
|
|
|
|
2019-12-30 18:03:57 +00:00
|
|
|
|
2019-12-18 19:02:17 +00:00
|
|
|
|
2019-11-21 14:22:42 +00:00
|
|
|
|
|
|
|
# Great class to inherit from, as it sets up and tears down
|
|
|
|
# classes efficiently when we have a database in place.
|
2020-01-24 14:35:14 +00:00
|
|
|
class BaseTest(unittest.TestCase):
|
2019-11-21 14:22:42 +00:00
|
|
|
|
|
|
|
auths = {}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
app.config.from_object('config.testing')
|
|
|
|
cls.ctx = app.test_request_context()
|
|
|
|
cls.app = app.test_client()
|
2019-12-18 19:02:17 +00:00
|
|
|
db.create_all()
|
2019-11-21 14:22:42 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2019-12-18 19:02:17 +00:00
|
|
|
db.drop_all()
|
2020-01-14 16:45:12 +00:00
|
|
|
session.remove()
|
2019-11-21 14:22:42 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.ctx.push()
|
|
|
|
|
|
|
|
def tearDown(self):
|
2019-12-30 18:03:57 +00:00
|
|
|
ExampleDataLoader.clean_db() # This does not seem to work, some colision of sessions.
|
2019-11-21 14:22:42 +00:00
|
|
|
self.ctx.pop()
|
|
|
|
self.auths = {}
|
|
|
|
|
2019-12-18 19:02:17 +00:00
|
|
|
def load_example_data(self):
|
|
|
|
from example_data import ExampleDataLoader
|
2019-12-30 18:15:39 +00:00
|
|
|
ExampleDataLoader.clean_db()
|
2019-12-18 19:02:17 +00:00
|
|
|
ExampleDataLoader().load_all()
|
|
|
|
|
2020-01-24 14:35:14 +00:00
|
|
|
specs = session.query(WorkflowSpecModel).all()
|
|
|
|
self.assertIsNotNone(specs)
|
|
|
|
|
|
|
|
for spec in specs:
|
|
|
|
files = session.query(FileModel).filter_by(workflow_spec_id=spec.id).all()
|
|
|
|
self.assertIsNotNone(files)
|
|
|
|
self.assertGreater(len(files), 0)
|
|
|
|
|
|
|
|
for file in files:
|
|
|
|
file_data = session.query(FileDataModel).filter_by(file_model_id=file.id).all()
|
|
|
|
self.assertIsNotNone(file_data)
|
|
|
|
self.assertGreater(len(file_data), 0)
|
|
|
|
|
2019-12-18 19:02:17 +00:00
|
|
|
def assert_success(self, rv, msg=""):
|
|
|
|
try:
|
|
|
|
data = json.loads(rv.get_data(as_text=True))
|
|
|
|
self.assertTrue(rv.status_code >= 200 and rv.status_code < 300,
|
|
|
|
"BAD Response: %i. \n %s" %
|
|
|
|
(rv.status_code, json.dumps(data)) + ". " + msg)
|
|
|
|
except:
|
|
|
|
self.assertTrue(rv.status_code >= 200 and rv.status_code < 300,
|
|
|
|
"BAD Response: %i." % rv.status_code + ". " + msg)
|