Skip to content

Commit 61f3766

Browse files
committed
Wrote tests for ProcessTracking find extracts functions
✅ Wrote tests for ProcessTracking Wrote tests for helper functions that are used by ProcessTracking to find extracts by process name, location name, or full/partial filename. They are not necessarily part of the core functionality of the ProcessTracking class, but by having them as part of it, users will have an easier time using the framework.
1 parent 695f5e4 commit 61f3766

File tree

4 files changed

+104
-12
lines changed

4 files changed

+104
-12
lines changed

models/extract.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Extract(Base):
3434
# extract_archive_date_time = Column(DateTime, nullable=False, default=default_date)
3535

3636
extract_process = relationship("ExtractProcess", back_populates='process_extracts')
37+
locations = relationship("Location", foreign_keys=[extract_location_id])
3738

3839

3940
class ExtractProcess(Base):

process_tracker/extract_tracking.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ def __init__(self, process_run, filename, location_path, location_name=None):
5858

5959
self.extract_process = self.retrieve_extract_process()
6060

61+
self.extract.extract_status_id = self.extract_status_initializing
62+
session.commit()
63+
6164
def change_extract_status(self, new_status):
6265
"""
6366
Change an extract record status.

process_tracker/process_tracking.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from process_tracker.data_store import DataStore
1010

1111
from models.actor import Actor
12-
from models.extract import Extract, ExtractStatus, Location
12+
from models.extract import Extract, ExtractProcess, ExtractStatus, Location
1313
from models.process import ErrorTracking, ErrorType, Process, ProcessTracking, ProcessStatus, ProcessType
1414
from models.source import Source
1515
from models.tool import Tool
@@ -97,14 +97,14 @@ def find_ready_extracts_by_filename(filename):
9797
"""
9898
extract_files = []
9999

100-
process_files = session.query(Extract, Location)\
100+
process_files = session.query(Extract.extract_filename, Location.location_path)\
101101
.join(Location)\
102102
.join(ExtractStatus)\
103103
.filter(Extract.extract_filename.like("%" + filename + "%"))\
104104
.filter(ExtractStatus.extract_status_name == 'ready')
105105

106106
for record in process_files:
107-
extract_files.append(join(record.location_path, record.filename))
107+
extract_files.append(join(record.location_path, record.extract_filename))
108108

109109
return extract_files
110110

@@ -117,35 +117,36 @@ def find_ready_extracts_by_location(location):
117117
"""
118118
extract_files = []
119119

120-
process_files = session.query(Extract, Location)\
120+
process_files = session.query(Extract.extract_filename, Location.location_path)\
121121
.join(Location)\
122122
.join(ExtractStatus)\
123123
.filter(ExtractStatus.extract_status_name == 'ready')\
124124
.filter(Location.location_name == location)
125125

126126
for record in process_files:
127-
extract_files.append(join(record.location_path, record.filename))
127+
extract_files.append(join(record.location_path, record.extract_filename))
128128

129129
return extract_files
130130

131131
@staticmethod
132-
def find_ready_extracts_by_process(self, extract_process_name):
132+
def find_ready_extracts_by_process(extract_process_name):
133133
"""
134134
For the given named process, find the extracts that are ready for processing.
135135
:return: List of OS specific filepaths with filenames.
136136
"""
137137
extract_files = []
138138

139-
process_files = session.query(Extract, Location) \
140-
.join(Location) \
139+
process_files = session.query(Extract.extract_filename, Location.location_path) \
140+
.join(ExtractStatus, Extract.extract_status_id == ExtractStatus.extract_status_id) \
141+
.join(Location, Extract.extract_location_id == Location.location_id) \
142+
.join(ExtractProcess, Extract.extract_id == ExtractProcess.extract_tracking_id) \
141143
.join(ProcessTracking) \
142144
.join(Process) \
143-
.join(ExtractStatus)\
144145
.filter(Process.process_name == extract_process_name
145146
, ExtractStatus.extract_status_name == 'ready')
146147

147148
for record in process_files:
148-
extract_files.append(join(record.location_path, record.filename))
149+
extract_files.append(join(record.location_path, record.extract_filename))
149150

150151
return extract_files
151152

tests/test_process_tracking.py

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
from datetime import datetime
44
import unittest
55

6+
from models.extract import Extract, ExtractProcess
67
from models.process import ErrorType, ErrorTracking, Process, ProcessTracking
78

8-
from process_tracker import data_store_type
9-
from process_tracker import session
9+
from process_tracker import data_store_type, session
10+
from process_tracker.extract_tracking import ExtractTracker
1011
from process_tracker.process_tracking import ProcessTracker
1112

1213

@@ -36,7 +37,9 @@ def tearDown(self):
3637
Need to clean up tables to return them to pristine state for other tests.
3738
:return:
3839
"""
40+
session.query(ExtractProcess).delete()
3941
session.query(ProcessTracking).delete()
42+
session.query(Extract).delete()
4043
session.query(ErrorTracking).delete()
4144
session.query(ErrorType).delete()
4245
session.commit()
@@ -52,6 +55,90 @@ def test_verify_session_variables_postgresql(self):
5255

5356
self.assertEqual(expected_result, given_result)
5457

58+
def test_find_ready_extracts_by_filename_full(self):
59+
"""
60+
Testing that for the given full filename, find the extract, provided it's in 'ready' state.
61+
:return:
62+
"""
63+
extract = ExtractTracker(process_run=self.process_tracker
64+
, filename='test_extract_filename2.csv'
65+
, location_name='Test Location'
66+
, location_path='/home/test/extract_dir')
67+
68+
# Need to manually change the status, because this would normally be done while the process was processing data
69+
extract.extract.extract_status_id = extract.extract_status_ready
70+
71+
session.commit()
72+
73+
expected_result = ['/home/test/extract_dir/test_extract_filename2.csv']
74+
75+
given_result = self.process_tracker.find_ready_extracts_by_filename('test_extract_filename2.csv')
76+
77+
self.assertEqual(expected_result, given_result)
78+
79+
def test_find_ready_extracts_by_filename_partial(self):
80+
"""
81+
Testing that for the given partial filename, find the extracts, provided they are in 'ready' state.
82+
:return:
83+
"""
84+
extract = ExtractTracker(process_run=self.process_tracker
85+
, filename='test_extract_filename3.csv'
86+
, location_name='Test Location'
87+
, location_path='/home/test/extract_dir')
88+
89+
# Need to manually change the status, because this would normally be done while the process was processing data
90+
extract.extract.extract_status_id = extract.extract_status_ready
91+
92+
session.commit()
93+
94+
expected_result = ['/home/test/extract_dir/test_extract_filename3.csv']
95+
96+
given_result = self.process_tracker.find_ready_extracts_by_filename('test_extract_filename')
97+
98+
self.assertEqual(expected_result, given_result)
99+
100+
def test_find_ready_extracts_by_location(self):
101+
"""
102+
Testing that for the given location name, find the extracts, provided they are in 'ready' state.
103+
:return:
104+
"""
105+
extract = ExtractTracker(process_run=self.process_tracker
106+
, filename='test_extract_filename4.csv'
107+
, location_name='Test Location'
108+
, location_path='/home/test/extract_dir')
109+
110+
# Need to manually change the status, because this would normally be done while the process was processing data
111+
extract.extract.extract_status_id = extract.extract_status_ready
112+
113+
session.commit()
114+
115+
expected_result = ['/home/test/extract_dir/test_extract_filename4.csv']
116+
117+
given_result = self.process_tracker.find_ready_extracts_by_location('Test Location')
118+
119+
self.assertEqual(expected_result, given_result)
120+
121+
def test_find_ready_extracts_by_process(self):
122+
"""
123+
Testing that for the given process name, find the extracts, provided they are in 'ready' state.
124+
:return:
125+
"""
126+
extract = ExtractTracker(process_run=self.process_tracker
127+
, filename='test_extract_filename5.csv'
128+
, location_name='Test Location'
129+
, location_path='/home/test/extract_dir')
130+
131+
# Need to manually change the status, because this would normally be done while the process was processing data
132+
extract.extract.extract_status_id = extract.extract_status_ready
133+
134+
session.commit()
135+
136+
expected_result = ['/home/test/extract_dir/test_extract_filename5.csv']
137+
138+
given_result = self.process_tracker.find_ready_extracts_by_process('Testing Process Tracking Initialization')
139+
140+
self.assertEqual(expected_result, given_result)
141+
55142
def test_initializing_process_tracking(self):
56143
"""
57144
Testing that when ProcessTracking is initialized, the necessary objects are created.

0 commit comments

Comments
 (0)