data setup on app boot

This commit is contained in:
burnettk 2022-10-12 13:26:04 -04:00
parent 0b21a5d4c5
commit d29621aef9
5 changed files with 30 additions and 9 deletions

View File

@ -7,7 +7,7 @@ def main() -> None:
"""Main."""
app = get_hacked_up_app_for_script()
with app.app_context():
failing_process_models = DataSetupService.save_all()
failing_process_models = DataSetupService.save_all_process_models()
for bpmn_errors in failing_process_models:
print(bpmn_errors)
if len(failing_process_models) > 0:

View File

@ -2,6 +2,7 @@
import json
import time
from flask import current_app
from flask_bpmn.models.db import db
from tests.spiffworkflow_backend.helpers.base_test import BaseTest
@ -9,8 +10,9 @@ from spiffworkflow_backend.models.process_instance import ProcessInstanceModel
from spiffworkflow_backend.models.process_instance import ProcessInstanceStatus
def load_fixtures() -> list[ProcessInstanceModel]:
def load_acceptance_test_fixtures() -> list[ProcessInstanceModel]:
"""Load_fixtures."""
current_app.logger.debug("load_acceptance_test_fixtures() start")
test_process_group_id = "acceptance-tests-group-one"
test_process_model_id = "acceptance-tests-model-1"
user = BaseTest.find_or_create_user()
@ -40,4 +42,5 @@ def load_fixtures() -> list[ProcessInstanceModel]:
process_instances.append(process_instance)
db.session.commit()
current_app.logger.debug("load_acceptance_test_fixtures() end")
return process_instances

View File

@ -1,4 +1,6 @@
"""Data_setup_service."""
from flask import current_app
from spiffworkflow_backend.services.process_model_service import ProcessModelService
from spiffworkflow_backend.services.spec_file_service import SpecFileService
@ -6,9 +8,15 @@ from spiffworkflow_backend.services.spec_file_service import SpecFileService
class DataSetupService:
"""DataSetupService."""
@staticmethod
def save_all() -> list:
@classmethod
def run_setup(cls) -> list:
"""Run_setup."""
return cls.save_all_process_models()
@classmethod
def save_all_process_models(cls) -> list:
"""Save_all."""
current_app.logger.debug("DataSetupService.save_all_process_models() start")
failing_process_models = []
process_models = ProcessModelService().get_process_models()
for process_model in process_models:
@ -26,7 +34,7 @@ class DataSetupService:
]
if process_model.primary_file_name in bad_files:
continue
print(f"primary_file_name: {process_model.primary_file_name}")
# print(f"primary_file_name: {process_model.primary_file_name}")
try:
SpecFileService.update_file(
process_model,
@ -79,4 +87,5 @@ class DataSetupService:
"primary_file_name not set",
)
)
current_app.logger.debug("DataSetupService.save_all_process_models() end")
return failing_process_models

View File

@ -1,12 +1,14 @@
"""Test_acceptance_test_fixtures."""
from flask.app import Flask
from spiffworkflow_backend.services.acceptance_test_fixtures import load_fixtures
from spiffworkflow_backend.services.acceptance_test_fixtures import (
load_acceptance_test_fixtures,
)
def test_start_dates_are_one_hour_apart(app: Flask) -> None:
"""Test_start_dates_are_one_hour_apart."""
process_instances = load_fixtures()
process_instances = load_acceptance_test_fixtures()
assert len(process_instances) > 2
assert process_instances[0].start_in_seconds is not None

11
wsgi.py
View File

@ -2,7 +2,10 @@
import os
from spiffworkflow_backend import create_app
from spiffworkflow_backend.services.acceptance_test_fixtures import load_fixtures
from spiffworkflow_backend.services.acceptance_test_fixtures import (
load_acceptance_test_fixtures,
)
from spiffworkflow_backend.services.data_setup_service import DataSetupService
app = create_app()
@ -10,4 +13,8 @@ app = create_app()
# it also loaded when we were running migrations, which resulted in a chicken/egg thing.
if os.environ.get("SPIFFWORKFLOW_BACKEND_LOAD_FIXTURE_DATA") == "true":
with app.app_context():
load_fixtures()
load_acceptance_test_fixtures()
if os.environ.get("SPIFFWORKFLOW_BACKEND_RUN_DATA_SETUP") != "false":
with app.app_context():
DataSetupService.run_setup()