Merge pull request #364 from sartography/bug/447-library-issues

A little cleanup to assure that when we sync we correctly pick up and…
This commit is contained in:
Dan Funk 2021-09-03 14:30:18 -04:00 committed by GitHub
commit 52b86a4182
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 29 deletions

View File

@ -5,7 +5,8 @@ from pandas._libs.missing import NA
from crc import session, app
from crc.api.common import ApiError
from crc.models.file import FileModel, FileDataModel
from crc.models.workflow import WorkflowSpecModel, WorkflowSpecCategoryModel
from crc.models.workflow import WorkflowSpecModel, WorkflowSpecCategoryModel, WorkflowSpecCategoryModelSchema, \
WorkflowSpecModelSchema, WorkflowLibraryModel, WorkflowLibraryModelSchema
from crc.services.file_service import FileService
from crc.services.workflow_sync import WorkflowSyncService
from crc.api.workflow import get_workflow_specification
@ -134,40 +135,39 @@ def file_get(workflow_spec_id,filename):
FileModel.name == filename).first()
return currentfile
def create_or_update_local_spec(remote,workflow_spec_id):
specdict = WorkflowSyncService.get_remote_workflow_spec(remote, workflow_spec_id)
# if we are updating from a master spec, then we want to make sure it is the only
# master spec in our local system.
# master spec in our local system, turn all other master_specs off
if specdict['is_master_spec']:
masterspecs = session.query(WorkflowSpecModel).filter(WorkflowSpecModel.is_master_spec == True).all()
for masterspec in masterspecs:
masterspec.is_master_spec = False
session.add(masterspec)
master_specs = session.query(WorkflowSpecModel).filter(WorkflowSpecModel.is_master_spec == True).all()
for master_spec in master_specs:
master_spec.is_master_spec = False
session.add(master_spec)
localspec = session.query(WorkflowSpecModel).filter(WorkflowSpecModel.id == workflow_spec_id).first()
if localspec is None:
localspec = WorkflowSpecModel()
localspec.id = workflow_spec_id
if specdict['category'] == None:
localspec.category = None
else:
localcategory = session.query(WorkflowSpecCategoryModel).filter(WorkflowSpecCategoryModel.name
== specdict['category']['name']).first()
if localcategory == None:
# category doesn't exist - lets make it
localcategory = WorkflowSpecCategoryModel()
localcategory.name = specdict['category']['name']
localcategory.display_name = specdict['category']['display_name']
localcategory.display_order = specdict['category']['display_order']
session.add(localcategory)
localspec.category = localcategory
# Update local_spec, or create a new one if one does not exist.
local_spec = session.query(WorkflowSpecModel).filter(WorkflowSpecModel.id == workflow_spec_id).first()
local_spec = WorkflowSpecModelSchema().load(specdict, session=session, instance=local_spec)
localspec.display_order = specdict['display_order']
localspec.display_name = specdict['display_name']
localspec.name = specdict['name']
localspec.is_master_spec = specdict['is_master_spec']
localspec.description = specdict['description']
session.add(localspec)
# Set the category
if specdict['category'] is not None:
local_category = session.query(WorkflowSpecCategoryModel).\
filter(WorkflowSpecCategoryModel.name == specdict['category']['name']).first()
local_category = WorkflowSpecCategoryModelSchema().load(specdict['category'], session=session,
instance=local_category)
session.add(local_category)
local_spec.category = local_category
# Set the libraries
session.query(WorkflowLibraryModel).filter(WorkflowLibraryModel.workflow_spec_id == local_spec.id).delete()
for library in specdict['libraries']:
# Assure refernced libraries are local, and link them.
create_or_update_local_spec(remote, library['id'])
local_lib = WorkflowLibraryModel(workflow_spec_id=local_spec.id,
library_spec_id=library['id'])
session.add(local_lib)
session.add(local_spec)
def update_or_create_current_file(remote,workflow_spec_id,updatefile):
currentfile = file_get(workflow_spec_id, updatefile['filename'])

View File

@ -1,4 +1,6 @@
from unittest import mock
from unittest.mock import patch
from tests.base_test import BaseTest
from crc import db
@ -11,6 +13,7 @@ from crc.api.workflow_sync import get_all_spec_state, \
from crc.models.workflow import WorkflowSpecModel
from datetime import datetime
from crc.services.file_service import FileService
from crc.services.workflow_sync import WorkflowSyncService
def get_random_fact_pos(othersys):
"""
@ -141,6 +144,7 @@ class TestWorkflowSync(BaseTest):
self.assertEqual(remote_workflow['display_name'],'Random Fact')
remote_workflow['description'] = 'This Workflow came from Remote'
remote_workflow['display_name'] = 'Remote Workflow'
remote_workflow['library'] = True
workflow_mock.return_value = remote_workflow
# change the remote file date and hash
othersys = get_workflow_spec_files('random_fact')
@ -160,7 +164,45 @@ class TestWorkflowSync(BaseTest):
self.assertEqual('21bb6f9e-0af7-0ab2-0fc7-ec0f94787e58' in md5sums, True)
new_local_workflow = get_workflow_specification('random_fact')
self.assertEqual(new_local_workflow['display_name'],'Remote Workflow')
self.assertTrue(new_local_workflow['library'])
@patch('crc.services.workflow_sync.WorkflowSyncService.get_remote_file_by_hash')
@patch('crc.services.workflow_sync.WorkflowSyncService.get_remote_workflow_spec_files')
def test_workflow_sync_with_libraries(self, get_remote_workflow_spec_files_mock, get_remote_file_by_hash_mock):
self.load_example_data()
# make a remote workflow that is slightly different from local, and add a library to it.
remote_workflow = get_workflow_specification('random_fact')
remote_library = self.load_test_spec('two_forms')
remote_workflow['description'] = 'This Workflow came from Remote'
remote_workflow['libraries'] = [{'id': remote_library.id, 'name': 'two_forms', 'display_name': "Two Forms"}]
random_workflow_remote_files = get_workflow_spec_files('random_fact')
rf2pos = get_random_fact_2_pos(random_workflow_remote_files)
random_workflow_remote_files[rf2pos]['date_created'] = str(datetime.utcnow())
random_workflow_remote_files[rf2pos]['md5_hash'] = '12345'
get_remote_workflow_spec_files_mock.return_value = random_workflow_remote_files
get_remote_file_by_hash_mock.return_value = self.workflow_sync_response('random_fact2.bpmn')
# more mock stuff, but we need to return different things depending on what is asked, so we use the side
# effect pattern rather than setting a single return_value through a patch.
def mock_workflow_spec(*args):
if args[1] == 'random_fact':
return remote_workflow
else:
return get_workflow_specification(args[1])
with mock.patch.object(WorkflowSyncService, 'get_remote_workflow_spec', side_effect=mock_workflow_spec):
response = sync_changed_files('localhost:0000','random_fact') # endpoint not used due to mock
self.assertIsNotNone(response)
self.assertEqual(len(response),1)
self.assertEqual(response[0], 'random_fact2.bpmn')
files = FileService.get_spec_data_files('random_fact')
md5sums = [str(f.md5_hash) for f in files]
self.assertEqual('21bb6f9e-0af7-0ab2-0fc7-ec0f94787e58' in md5sums, True)
new_local_workflow = get_workflow_specification('random_fact')
self.assertEqual(new_local_workflow['display_name'],'Random Fact')
self.assertEqual(1, len(new_local_workflow['libraries']))
@patch('crc.services.workflow_sync.WorkflowSyncService.get_remote_file_by_hash')