Skip to content

Commit de7841c

Browse files
authored
Merge pull request #39 from OpenDataAlex/process_tracker_python-32
process_tracker_python-32 Extract Location Lookup Fix
2 parents 564fd7d + 5ce7451 commit de7841c

File tree

2 files changed

+92
-16
lines changed

2 files changed

+92
-16
lines changed

process_tracker/process_tracker.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,22 +157,43 @@ def find_ready_extracts_by_filename(self, filename):
157157

158158
return process_files
159159

160-
def find_ready_extracts_by_location(self, location):
160+
def find_ready_extracts_by_location(self, location_name=None, location_path=None):
161161
"""
162-
For the given location name, find all matching extracts that are ready for processing
163-
:param location:
164-
:return:
162+
For the given location path or location name, find all matching extracts that are ready for processing
163+
:param location_name: The name of the location
164+
:type location_name: str
165+
:param location_path: The path of the location
166+
:type location_path: str
167+
:return: List of extract files that are in 'ready' state'.
165168
"""
166169

167-
process_files = (
168-
self.session.query(Extract)
169-
.join(Location)
170-
.join(ExtractStatus)
171-
.filter(ExtractStatus.extract_status_name == "ready")
172-
.filter(Location.location_name == location)
173-
.order_by(Extract.extract_registration_date_time)
174-
.all()
175-
)
170+
if location_path is not None:
171+
process_files = (
172+
self.session.query(Extract)
173+
.join(Location)
174+
.join(ExtractStatus)
175+
.filter(ExtractStatus.extract_status_name == "ready")
176+
.filter(Location.location_path == location_path)
177+
.order_by(Extract.extract_registration_date_time)
178+
.all()
179+
)
180+
elif location_name is not None:
181+
process_files = (
182+
self.session.query(Extract)
183+
.join(Location)
184+
.join(ExtractStatus)
185+
.filter(ExtractStatus.extract_status_name == "ready")
186+
.filter(Location.location_name == location_name)
187+
.order_by(Extract.extract_registration_date_time)
188+
.all()
189+
)
190+
else:
191+
self.logger.error(
192+
"A location name or path must be provided. Please try again."
193+
)
194+
raise Exception(
195+
"A location name or path must be provided. Please try again."
196+
)
176197

177198
return process_files
178199

tests/test_process_tracker.py

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def test_find_ready_extracts_by_filename_partial_not_descending(self):
231231

232232
self.assertNotEqual(expected_result, given_result)
233233

234-
def test_find_ready_extracts_by_location(self):
234+
def test_find_ready_extracts_by_location_name(self):
235235
"""
236236
Testing that for the given location name, find the extracts, provided they are in 'ready' state. Should return
237237
them in ascending order by registration datettime.
@@ -266,7 +266,7 @@ def test_find_ready_extracts_by_location(self):
266266
]
267267

268268
given_result = self.process_tracker.find_ready_extracts_by_location(
269-
"Test Location"
269+
location_name="Test Location"
270270
)
271271
given_result = [record.full_filepath() for record in given_result]
272272

@@ -307,12 +307,67 @@ def test_find_ready_extracts_by_location_not_descending(self):
307307
]
308308

309309
given_result = self.process_tracker.find_ready_extracts_by_location(
310-
"Test Location"
310+
location_name="Test Location"
311311
)
312312
given_result = [record.full_filepath() for record in given_result]
313313

314314
self.assertNotEqual(expected_result, given_result)
315315

316+
def test_find_ready_extracts_by_location_path(self):
317+
"""
318+
Testing that for the given location path, find the extracts, provided they are in 'ready' state. Should return
319+
them in ascending order by registration datettime.
320+
:return:
321+
"""
322+
extract = ExtractTracker(
323+
process_run=self.process_tracker,
324+
filename="test_extract_filename4-1.csv",
325+
location_name="Test Location",
326+
location_path="/home/test/extract_dir",
327+
)
328+
329+
extract2 = ExtractTracker(
330+
process_run=self.process_tracker,
331+
filename="test_extract_filename4-2.csv",
332+
location_name="Test Location",
333+
location_path="/home/test/extract_dir",
334+
)
335+
336+
# Need to manually change the status, because this would normally be done while the process was processing data
337+
extract.extract.extract_status_id = extract.extract_status_ready
338+
session = Session.object_session(extract.extract)
339+
session.commit()
340+
341+
extract2.extract.extract_status_id = extract2.extract_status_ready
342+
session = Session.object_session(extract2.extract)
343+
session.commit()
344+
345+
expected_result = [
346+
"/home/test/extract_dir/test_extract_filename4-1.csv",
347+
"/home/test/extract_dir/test_extract_filename4-2.csv",
348+
]
349+
350+
given_result = self.process_tracker.find_ready_extracts_by_location(
351+
location_path="/home/test/extract_dir"
352+
)
353+
given_result = [record.full_filepath() for record in given_result]
354+
355+
self.assertCountEqual(expected_result, given_result)
356+
357+
def test_find_ready_extracts_by_location_unset(self):
358+
"""
359+
Testing that if both the location path and location name are not set, find_ready_extracts_by_location
360+
will throw an exception.
361+
:return:
362+
"""
363+
with self.assertRaises(Exception) as context:
364+
self.process_tracker.find_ready_extracts_by_location()
365+
366+
return self.assertTrue(
367+
"A location name or path must be provided. Please try again."
368+
in str(context.exception)
369+
)
370+
316371
def test_find_ready_extracts_by_process(self):
317372
"""
318373
Testing that for the given process name, find the extracts, provided they are in 'ready' state.

0 commit comments

Comments
 (0)