2020-05-29 05:39:39 +00:00
|
|
|
import os
|
2020-05-27 13:47:44 +00:00
|
|
|
|
|
|
|
from tests.base_test import BaseTest
|
|
|
|
|
2020-05-19 20:11:43 +00:00
|
|
|
from crc.services.file_service import FileService
|
2020-05-29 05:39:39 +00:00
|
|
|
from crc.api.common import ApiError
|
|
|
|
from crc import session, app
|
|
|
|
from crc.models.file import FileDataModel, FileModel, LookupFileModel, LookupDataModel, CONTENT_TYPES
|
2020-05-19 20:11:43 +00:00
|
|
|
from crc.services.lookup_service import LookupService
|
2020-05-29 05:39:39 +00:00
|
|
|
from crc.services.workflow_processor import WorkflowProcessor
|
2020-05-19 20:11:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestLookupService(BaseTest):
|
|
|
|
|
2020-05-29 05:39:39 +00:00
|
|
|
def test_lookup_returns_good_error_on_bad_field(self):
|
|
|
|
spec = BaseTest.load_test_spec('enum_options_with_search')
|
|
|
|
workflow = self.create_workflow('enum_options_with_search')
|
2021-06-18 20:41:55 +00:00
|
|
|
file_model = session.query(FileModel).filter(FileModel.name == "customer_list.xlsx").first()
|
2020-05-19 20:11:43 +00:00
|
|
|
file_data_model = session.query(FileDataModel).filter(FileDataModel.file_model == file_model).first()
|
2020-05-29 05:39:39 +00:00
|
|
|
with self.assertRaises(ApiError):
|
2021-03-01 19:54:04 +00:00
|
|
|
LookupService.lookup(workflow, "Task_Enum_Lookup", "not_the_right_field", "sam", limit=10)
|
2020-05-29 05:39:39 +00:00
|
|
|
|
|
|
|
def test_lookup_table_is_not_created_more_than_once(self):
|
|
|
|
spec = BaseTest.load_test_spec('enum_options_with_search')
|
|
|
|
workflow = self.create_workflow('enum_options_with_search')
|
2021-03-01 19:54:04 +00:00
|
|
|
LookupService.lookup(workflow, "Task_Enum_Lookup", "sponsor", "sam", limit=10)
|
|
|
|
LookupService.lookup(workflow, "Task_Enum_Lookup", "sponsor", "something", limit=10)
|
|
|
|
LookupService.lookup(workflow, "Task_Enum_Lookup", "sponsor", "blah", limit=10)
|
2020-05-19 20:11:43 +00:00
|
|
|
lookup_records = session.query(LookupFileModel).all()
|
|
|
|
self.assertIsNotNone(lookup_records)
|
|
|
|
self.assertEqual(1, len(lookup_records))
|
|
|
|
lookup_record = lookup_records[0]
|
|
|
|
lookup_data = session.query(LookupDataModel).filter(LookupDataModel.lookup_file_model == lookup_record).all()
|
2020-06-05 18:08:46 +00:00
|
|
|
self.assertEqual(28, len(lookup_data))
|
2020-05-29 05:39:39 +00:00
|
|
|
|
|
|
|
def test_updates_to_file_cause_lookup_rebuild(self):
|
|
|
|
spec = BaseTest.load_test_spec('enum_options_with_search')
|
|
|
|
workflow = self.create_workflow('enum_options_with_search')
|
2021-06-18 20:41:55 +00:00
|
|
|
file_model = session.query(FileModel).filter(FileModel.name == "sponsors.xlsx").first()
|
2021-03-01 19:54:04 +00:00
|
|
|
LookupService.lookup(workflow, "Task_Enum_Lookup", "sponsor", "sam", limit=10)
|
2020-05-19 20:11:43 +00:00
|
|
|
lookup_records = session.query(LookupFileModel).all()
|
|
|
|
self.assertIsNotNone(lookup_records)
|
2020-05-29 05:39:39 +00:00
|
|
|
self.assertEqual(1, len(lookup_records))
|
|
|
|
lookup_record = lookup_records[0]
|
|
|
|
lookup_data = session.query(LookupDataModel).filter(LookupDataModel.lookup_file_model == lookup_record).all()
|
2020-06-05 18:08:46 +00:00
|
|
|
self.assertEqual(28, len(lookup_data))
|
2020-05-29 05:39:39 +00:00
|
|
|
|
|
|
|
# Update the workflow specification file.
|
|
|
|
file_path = os.path.join(app.root_path, '..', 'tests', 'data',
|
2021-06-18 20:41:55 +00:00
|
|
|
'enum_options_with_search', 'sponsors_modified.xlsx')
|
2020-05-29 05:39:39 +00:00
|
|
|
file = open(file_path, 'rb')
|
2021-06-18 20:41:55 +00:00
|
|
|
FileService.update_file(file_model, file.read(), CONTENT_TYPES['xlsx'])
|
2020-05-29 05:39:39 +00:00
|
|
|
file.close()
|
|
|
|
|
|
|
|
# restart the workflow, so it can pick up the changes.
|
2021-01-20 18:24:53 +00:00
|
|
|
|
2021-03-09 16:52:55 +00:00
|
|
|
processor = WorkflowProcessor.reset(workflow)
|
2021-01-20 18:24:53 +00:00
|
|
|
workflow = processor.workflow_model
|
2020-05-29 05:39:39 +00:00
|
|
|
|
2021-03-01 19:54:04 +00:00
|
|
|
LookupService.lookup(workflow, "Task_Enum_Lookup", "sponsor", "sam", limit=10)
|
2020-05-29 05:39:39 +00:00
|
|
|
lookup_records = session.query(LookupFileModel).all()
|
|
|
|
lookup_record = lookup_records[0]
|
|
|
|
lookup_data = session.query(LookupDataModel).filter(LookupDataModel.lookup_file_model == lookup_record).all()
|
2020-06-05 18:08:46 +00:00
|
|
|
self.assertEqual(4, len(lookup_data))
|
2020-05-29 05:39:39 +00:00
|
|
|
|
2020-06-30 14:00:22 +00:00
|
|
|
def test_lookup_based_on_id(self):
|
|
|
|
spec = BaseTest.load_test_spec('enum_options_from_file')
|
|
|
|
workflow = self.create_workflow('enum_options_from_file')
|
|
|
|
processor = WorkflowProcessor(workflow)
|
|
|
|
processor.do_engine_steps()
|
2021-03-01 19:54:04 +00:00
|
|
|
results = LookupService.lookup(workflow, "TaskEnumLookup", "AllTheNames", "", value="1000", limit=10)
|
2020-06-30 14:00:22 +00:00
|
|
|
self.assertEqual(1, len(results), "It is possible to find an item based on the id, rather than as a search")
|
2021-10-19 14:13:43 +00:00
|
|
|
self.assertIsNotNone(results[0])
|
|
|
|
self.assertIsInstance(results[0], dict)
|
2020-05-29 05:39:39 +00:00
|
|
|
|
2020-05-19 20:11:43 +00:00
|
|
|
|
2021-03-01 19:54:04 +00:00
|
|
|
def test_lookup_with_two_spreadsheets_with_the_same_field_name_in_different_forms(self):
|
|
|
|
spec = BaseTest.load_test_spec('enum_options_competing_files')
|
|
|
|
workflow = self.create_workflow('enum_options_competing_files')
|
|
|
|
processor = WorkflowProcessor(workflow)
|
|
|
|
|
|
|
|
processor.do_engine_steps()
|
|
|
|
task = processor.get_ready_user_tasks()[0]
|
2021-11-16 20:57:37 +00:00
|
|
|
task.data = {"type": "animals"}
|
2021-03-01 19:54:04 +00:00
|
|
|
processor.complete_task(task)
|
|
|
|
processor.do_engine_steps()
|
|
|
|
task = processor.get_ready_user_tasks()[0]
|
|
|
|
results = LookupService.lookup(workflow, task.task_spec.name, "selectedItem", "", value="pigs", limit=10)
|
|
|
|
self.assertEqual(1, len(results), "It is possible to find an item based on the id, rather than as a search")
|
2021-10-19 14:13:43 +00:00
|
|
|
self.assertIsNotNone(results[0])
|
2021-03-02 19:15:29 +00:00
|
|
|
results = LookupService.lookup(workflow, task.task_spec.name, "selectedItem", "", value="apples", limit=10)
|
|
|
|
self.assertEqual(0, len(results), "We shouldn't find our fruits mixed in with our animals.")
|
|
|
|
|
2021-03-09 16:52:55 +00:00
|
|
|
processor = WorkflowProcessor.reset(workflow, clear_data=True)
|
2021-03-01 19:54:04 +00:00
|
|
|
processor.do_engine_steps()
|
|
|
|
task = processor.get_ready_user_tasks()[0]
|
2021-11-16 20:57:37 +00:00
|
|
|
task.data = {"type": "fruits"}
|
2021-03-01 19:54:04 +00:00
|
|
|
processor.complete_task(task)
|
|
|
|
processor.do_engine_steps()
|
|
|
|
task = processor.get_ready_user_tasks()[0]
|
|
|
|
results = LookupService.lookup(workflow, task.task_spec.name, "selectedItem", "", value="apples", limit=10)
|
|
|
|
self.assertEqual(1, len(results), "It is possible to find an item based on the id, rather than as a search")
|
2021-10-19 14:13:43 +00:00
|
|
|
self.assertIsNotNone(results[0])
|
2021-03-02 19:15:29 +00:00
|
|
|
results = LookupService.lookup(workflow, task.task_spec.name, "selectedItem", "", value="pigs", limit=10)
|
|
|
|
self.assertEqual(0, len(results), "We shouldn't find our animals mixed in with our fruits.")
|
2021-03-01 19:54:04 +00:00
|
|
|
|
|
|
|
|
2020-05-19 20:11:43 +00:00
|
|
|
def test_some_full_text_queries(self):
|
2020-05-27 18:36:10 +00:00
|
|
|
spec = BaseTest.load_test_spec('enum_options_from_file')
|
2020-05-29 05:39:39 +00:00
|
|
|
workflow = self.create_workflow('enum_options_from_file')
|
|
|
|
processor = WorkflowProcessor(workflow)
|
|
|
|
processor.do_engine_steps()
|
2020-05-19 20:11:43 +00:00
|
|
|
|
2021-03-01 19:54:04 +00:00
|
|
|
results = LookupService.lookup(workflow, "TaskEnumLookup", "AllTheNames", "", limit=10)
|
2020-06-05 18:08:46 +00:00
|
|
|
self.assertEqual(10, len(results), "Blank queries return everything, to the limit")
|
2020-05-29 05:39:39 +00:00
|
|
|
|
2021-03-01 19:54:04 +00:00
|
|
|
results = LookupService.lookup(workflow, "TaskEnumLookup", "AllTheNames", "medicines", limit=10)
|
2020-06-05 18:08:46 +00:00
|
|
|
self.assertEqual(1, len(results), "words in the middle of label are detected.")
|
2021-10-19 14:13:43 +00:00
|
|
|
self.assertEqual("The Medicines Company", results[0]['CUSTOMER_NAME'])
|
2020-05-19 20:11:43 +00:00
|
|
|
|
2021-03-01 19:54:04 +00:00
|
|
|
results = LookupService.lookup(workflow,"TaskEnumLookup", "AllTheNames", "UVA", limit=10)
|
2020-06-05 18:08:46 +00:00
|
|
|
self.assertEqual(1, len(results), "Beginning of label is found.")
|
2021-10-19 14:13:43 +00:00
|
|
|
self.assertEqual("UVA - INTERNAL - GM USE ONLY", results[0]['CUSTOMER_NAME'])
|
2020-05-19 20:11:43 +00:00
|
|
|
|
2021-03-01 19:54:04 +00:00
|
|
|
results = LookupService.lookup(workflow, "TaskEnumLookup","AllTheNames", "uva", limit=10)
|
2020-06-05 18:08:46 +00:00
|
|
|
self.assertEqual(1, len(results), "case does not matter.")
|
2021-10-19 14:13:43 +00:00
|
|
|
self.assertEqual("UVA - INTERNAL - GM USE ONLY", results[0]['CUSTOMER_NAME'])
|
2020-05-19 20:11:43 +00:00
|
|
|
|
2021-03-01 19:54:04 +00:00
|
|
|
results = LookupService.lookup(workflow, "TaskEnumLookup", "AllTheNames", "medici", limit=10)
|
2020-06-05 18:08:46 +00:00
|
|
|
self.assertEqual(1, len(results), "partial words are picked up.")
|
2021-10-19 14:13:43 +00:00
|
|
|
self.assertEqual("The Medicines Company", results[0]['CUSTOMER_NAME'])
|
2020-05-19 20:11:43 +00:00
|
|
|
|
2021-03-01 19:54:04 +00:00
|
|
|
results = LookupService.lookup(workflow, "TaskEnumLookup", "AllTheNames", "Genetics Savings", limit=10)
|
2020-06-05 18:08:46 +00:00
|
|
|
self.assertEqual(1, len(results), "multiple terms are picked up..")
|
2021-10-19 14:13:43 +00:00
|
|
|
self.assertEqual("Genetics Savings & Clone, Inc.", results[0]['CUSTOMER_NAME'])
|
2020-05-19 20:11:43 +00:00
|
|
|
|
2021-03-01 19:54:04 +00:00
|
|
|
results = LookupService.lookup(workflow, "TaskEnumLookup", "AllTheNames", "Genetics Sav", limit=10)
|
2020-06-05 18:08:46 +00:00
|
|
|
self.assertEqual(1, len(results), "prefix queries still work with partial terms")
|
2021-10-19 14:13:43 +00:00
|
|
|
self.assertEqual("Genetics Savings & Clone, Inc.", results[0]['CUSTOMER_NAME'])
|
2020-05-19 20:11:43 +00:00
|
|
|
|
2021-03-01 19:54:04 +00:00
|
|
|
results = LookupService.lookup(workflow, "TaskEnumLookup", "AllTheNames", "Gen Sav", limit=10)
|
2020-06-05 18:08:46 +00:00
|
|
|
self.assertEqual(1, len(results), "prefix queries still work with ALL the partial terms")
|
2021-10-19 14:13:43 +00:00
|
|
|
self.assertEqual("Genetics Savings & Clone, Inc.", results[0]['CUSTOMER_NAME'])
|
2020-05-19 20:11:43 +00:00
|
|
|
|
2021-03-01 19:54:04 +00:00
|
|
|
results = LookupService.lookup(workflow, "TaskEnumLookup", "AllTheNames", "Inc", limit=10)
|
2020-06-05 18:08:46 +00:00
|
|
|
self.assertEqual(7, len(results), "short terms get multiple correct results.")
|
2021-10-19 14:13:43 +00:00
|
|
|
self.assertEqual("Genetics Savings & Clone, Inc.", results[0]['CUSTOMER_NAME'])
|
2020-05-19 20:11:43 +00:00
|
|
|
|
2021-03-01 19:54:04 +00:00
|
|
|
results = LookupService.lookup(workflow, "TaskEnumLookup", "AllTheNames", "reaction design", limit=10)
|
2020-08-13 22:35:24 +00:00
|
|
|
self.assertEqual(3, len(results), "all results come back for two terms.")
|
2021-10-19 14:13:43 +00:00
|
|
|
self.assertEqual("Reaction Design", results[0]['CUSTOMER_NAME'], "Exact matches come first.")
|
2020-05-27 18:36:10 +00:00
|
|
|
|
2021-03-01 19:54:04 +00:00
|
|
|
results = LookupService.lookup(workflow, "TaskEnumLookup", "AllTheNames", "1 Something", limit=10)
|
2021-10-19 14:13:43 +00:00
|
|
|
self.assertEqual("1 Something", results[0]['CUSTOMER_NAME'], "Exact matches are preferred")
|
2020-05-27 18:36:10 +00:00
|
|
|
|
2021-03-01 19:54:04 +00:00
|
|
|
results = LookupService.lookup(workflow, "TaskEnumLookup", "AllTheNames", "1 (!-Something", limit=10)
|
2021-10-19 14:13:43 +00:00
|
|
|
self.assertEqual("1 Something", results[0]['CUSTOMER_NAME'], "special characters don't flake out")
|
2020-05-29 05:39:39 +00:00
|
|
|
|
2021-03-01 19:54:04 +00:00
|
|
|
results = LookupService.lookup(workflow, "TaskEnumLookup", "AllTheNames", "1 Something", limit=10)
|
2021-10-19 14:13:43 +00:00
|
|
|
self.assertEqual("1 Something", results[0]['CUSTOMER_NAME'], "double spaces should not be an issue.")
|
2020-06-30 14:00:22 +00:00
|
|
|
|
2021-03-01 19:54:04 +00:00
|
|
|
results = LookupService.lookup(workflow, "TaskEnumLookup", "AllTheNames", "in", limit=10)
|
2020-08-13 22:35:24 +00:00
|
|
|
self.assertEqual(10, len(results), "stop words are not removed.")
|
2021-10-19 14:13:43 +00:00
|
|
|
self.assertEqual("Genetics Savings & Clone, Inc.", results[0]['CUSTOMER_NAME'])
|
2020-05-29 19:17:51 +00:00
|
|
|
|
2021-03-01 19:54:04 +00:00
|
|
|
results = LookupService.lookup(workflow, "TaskEnumLookup", "AllTheNames", "other", limit=10)
|
2021-10-19 14:13:43 +00:00
|
|
|
self.assertEqual("Other", results[0]['CUSTOMER_NAME'], "Can't find the word 'other', which is an english stop word")
|
2020-05-27 13:47:44 +00:00
|
|
|
|
2021-10-19 14:13:43 +00:00
|
|
|
def test_find_by_id(self):
|
|
|
|
spec = BaseTest.load_test_spec('enum_options_from_file')
|
|
|
|
workflow = self.create_workflow('enum_options_from_file')
|
|
|
|
processor = WorkflowProcessor(workflow)
|
|
|
|
processor.do_engine_steps()
|
2020-05-27 13:47:44 +00:00
|
|
|
|
2021-10-19 14:13:43 +00:00
|
|
|
result = LookupService.lookup(workflow, "TaskEnumLookup", "AllTheNames", None, value="1000")
|
|
|
|
first_result = result[0]
|
|
|
|
self.assertEquals(1000, first_result['CUSTOMER_NUMBER'])
|
|
|
|
self.assertEquals('UVA - INTERNAL - GM USE ONLY', first_result['CUSTOMER_NAME'])
|
2021-11-16 16:57:24 +00:00
|
|
|
|
|
|
|
def test_lookup_fails_for_xls(self):
|
|
|
|
BaseTest.load_test_spec('enum_options_with_search')
|
|
|
|
|
|
|
|
# Using an old xls file should raise an error
|
|
|
|
file_model_xls = session.query(FileModel).filter(FileModel.name == 'sponsors.xls').first()
|
|
|
|
file_data_model_xls = session.query(FileDataModel).filter(FileDataModel.file_model_id == file_model_xls.id).first()
|
|
|
|
with self.assertRaises(ApiError) as ae:
|
|
|
|
LookupService.build_lookup_table(file_data_model_xls, 'CUSTOMER_NUMBER', 'CUSTOMER_NAME')
|
|
|
|
self.assertIn('Error opening excel file', ae.exception.args[0])
|
|
|
|
|
|
|
|
# Using an xlsx file should work
|
|
|
|
file_model_xlsx = session.query(FileModel).filter(FileModel.name == 'sponsors.xlsx').first()
|
|
|
|
file_data_model_xlsx = session.query(FileDataModel).filter(FileDataModel.file_model_id == file_model_xlsx.id).first()
|
|
|
|
lookup_model = LookupService.build_lookup_table(file_data_model_xlsx, 'CUSTOMER_NUMBER', 'CUSTOMER_NAME')
|
|
|
|
self.assertEqual(28, len(lookup_model.dependencies))
|
|
|
|
self.assertIn('CUSTOMER_NAME', lookup_model.dependencies[0].data.keys())
|
|
|
|
self.assertIn('CUSTOMER_NUMBER', lookup_model.dependencies[0].data.keys())
|