88from sqlalchemy .orm import aliased , Session
99
1010from process_tracker .models .extract import Extract , ExtractProcess , ExtractStatus , Location
11- from process_tracker .models .process import ErrorType , ErrorTracking , Process , ProcessSource , ProcessTarget , ProcessTracking
11+ from process_tracker .models .process import ErrorType , ErrorTracking , Process , ProcessDependency , ProcessSource , ProcessTarget , ProcessTracking
1212
1313from process_tracker .data_store import DataStore
1414from process_tracker .extract_tracker import ExtractTracker
@@ -19,15 +19,16 @@ class TestProcessTracking(unittest.TestCase):
1919
2020 @classmethod
2121 def setUpClass (cls ):
22- data_store = DataStore ()
23- cls .session = data_store .session
24- cls .data_store_type = data_store .data_store_type
22+ cls . data_store = DataStore ()
23+ cls .session = cls . data_store .session
24+ cls .data_store_type = cls . data_store .data_store_type
2525
2626 @classmethod
2727 def tearDownClass (cls ):
2828 cls .session .query (Location ).delete ()
2929 cls .session .query (ProcessSource ).delete ()
3030 cls .session .query (ProcessTarget ).delete ()
31+ cls .session .query (ProcessDependency ).delete ()
3132 cls .session .query (Process ).delete ()
3233 cls .session .commit ()
3334
@@ -400,6 +401,80 @@ def test_register_new_process_run_with_previous_run(self):
400401
401402 self .assertEqual (expected_result , given_result )
402403
404+ def test_register_new_process_run_dependencies_completed (self ):
405+ """
406+ Testing that for a given process, if there are completed dependencies, then the process run is created.
407+ :return:
408+ """
409+ dependent_process = ProcessTracker (process_name = 'Testing Process Tracking Dependency'
410+ , process_type = 'Extract'
411+ , actor_name = 'UnitTesting'
412+ , tool_name = 'Spark'
413+ , sources = 'Unittests'
414+ , targets = 'Unittests' )
415+
416+ dependent_process .change_run_status (new_status = 'completed' )
417+ self .process_tracker .change_run_status (new_status = 'completed' )
418+ self .data_store .get_or_create (model = ProcessDependency
419+ , parent_process_id = dependent_process .process_tracking_run .process_id
420+ , child_process_id = self .process_id )
421+
422+ self .process_tracker .register_new_process_run ()
423+
424+ given_count = self .session .query (ProcessTracking ) \
425+ .filter (ProcessTracking .process_id == self .process_id ) \
426+ .count ()
427+
428+ expected_count = 2
429+
430+ self .assertEqual (expected_count , given_count )
431+
432+ def test_register_new_process_run_dependencies_running (self ):
433+ """
434+ Testing that for a given process, if there are running dependencies, then the process run is prevented from starting.
435+ :return:
436+ """
437+ dependent_process = ProcessTracker (process_name = 'Testing Process Tracking Dependency Running'
438+ , process_type = 'Extract'
439+ , actor_name = 'UnitTesting'
440+ , tool_name = 'Spark'
441+ , sources = 'Unittests'
442+ , targets = 'Unittests' )
443+
444+ dependent_process .change_run_status (new_status = 'running' )
445+ self .process_tracker .change_run_status (new_status = 'completed' )
446+ self .data_store .get_or_create (model = ProcessDependency
447+ , parent_process_id = dependent_process .process_tracking_run .process_id
448+ , child_process_id = self .process_id )
449+
450+ with self .assertRaises (Exception ) as context :
451+ self .process_tracker .register_new_process_run ()
452+
453+ return self .assertTrue ('Processes that this process is dependent on are running or failed.' in str (context .exception ))
454+
455+ def test_register_new_process_run_dependencies_failed (self ):
456+ """
457+ Testing that for a given process, if there are failed dependencies, then the process run is prevented from starting.
458+ :return:
459+ """
460+ dependent_process = ProcessTracker (process_name = 'Testing Process Tracking Dependency Failed'
461+ , process_type = 'Extract'
462+ , actor_name = 'UnitTesting'
463+ , tool_name = 'Spark'
464+ , sources = 'Unittests'
465+ , targets = 'Unittests' )
466+
467+ dependent_process .change_run_status (new_status = 'failed' )
468+ self .process_tracker .change_run_status (new_status = 'completed' )
469+ self .data_store .get_or_create (model = ProcessDependency
470+ , parent_process_id = dependent_process .process_tracking_run .process_id
471+ , child_process_id = self .process_id )
472+
473+ with self .assertRaises (Exception ) as context :
474+ self .process_tracker .register_new_process_run ()
475+
476+ return self .assertTrue ('Processes that this process is dependent on are running or failed.' in str (context .exception ))
477+
403478 def test_register_process_sources_one_source (self ):
404479 """
405480 Testing that when a new process is registered, a source registered as well.
0 commit comments