Skip to content

Commit 78fbc7a

Browse files
committed
process_tracker_python-30 Extract Lookups should return Extract objects
All the extract file finders now return extract objects so they can be modified as they are used. A helper method was also added that provides the full filename of a given extract (called full_filepath). Also realized that I missed adding string representations of a bunch of the data models. Closes #30
1 parent efda527 commit 78fbc7a

File tree

4 files changed

+63
-27
lines changed

4 files changed

+63
-27
lines changed

process_tracker/models/actor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ class Actor(Base):
1111

1212
actor_id = Column(Integer, Sequence('actor_lkup_actor_id_seq'), primary_key=True)
1313
actor_name = Column(String(250), nullable=False, unique=True)
14+
15+
def __repr__(self):
16+
return "<Actor id=%s, name=%s>" % (self.actor_id
17+
, self.actor_name)

process_tracker/models/extract.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Models for Extract (Data) entities
33

44
from datetime import datetime
5+
from os.path import join
56

67
from sqlalchemy import Column, DateTime, ForeignKey, Integer, Sequence, String
78
from sqlalchemy.orm import relationship
@@ -18,6 +19,11 @@ class ExtractStatus(Base):
1819

1920
extracts = relationship("ExtractProcess")
2021

22+
def __repr__(self):
23+
24+
return "<Extract Status id=%s, name=%s>" % (self.extract_status_id
25+
, self.extract_status_name)
26+
2127

2228
class Extract(Base):
2329

@@ -32,6 +38,17 @@ class Extract(Base):
3238
extract_process = relationship("ExtractProcess", back_populates='process_extracts')
3339
locations = relationship("Location", foreign_keys=[extract_location_id])
3440

41+
def __repr__(self):
42+
43+
return "<Extract id=%s, filename=%s, location=%s, status=%s>" % (self.extract_id
44+
, self.extract_filename
45+
, self.extract_location_id
46+
, self.extract_status_id)
47+
48+
def full_filepath(self):
49+
50+
return join(self.locations.location_path, self.extract_filename)
51+
3552

3653
class ExtractProcess(Base):
3754

@@ -45,6 +62,12 @@ class ExtractProcess(Base):
4562
process_extracts = relationship('Extract', foreign_keys=[extract_tracking_id])
4663
extract_processes = relationship('ProcessTracking', foreign_keys=[process_tracking_id])
4764

65+
def __repr__(self):
66+
67+
return "<ExtractProcess extract=%s, process_run=%s, extract_status=%s>" % (self.extract_tracking_id
68+
, self.process_tracking_id
69+
, self.extract_process_status_id)
70+
4871

4972
class LocationType(Base):
5073

@@ -54,7 +77,12 @@ class LocationType(Base):
5477
location_type_name = Column(String(25), unique=True, nullable=False)
5578

5679
locations = relationship('Location', back_populates='location_types')
57-
80+
81+
def __repr__(self):
82+
83+
return "<LocationType id=%s, name=%s>" % (self.location_type_id
84+
, self.location_type_name)
85+
5886

5987
class Location(Base):
6088

@@ -68,3 +96,9 @@ class Location(Base):
6896
extracts = relationship("Extract")
6997

7098
location_types = relationship('LocationType', foreign_keys=[location_type])
99+
100+
def __repr__(self):
101+
102+
return "<Location id=%s, name=%s, type=%s>" % (self.location_id
103+
, self.location_name
104+
, self.location_path)

process_tracker/process_tracker.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from datetime import datetime
55
import logging
66
import os
7-
from os.path import join
87

98
from sqlalchemy.orm import aliased
109

@@ -110,62 +109,54 @@ def find_ready_extracts_by_filename(self, filename):
110109
:param filename:
111110
:return:
112111
"""
113-
extract_files = []
114112

115-
process_files = self.session.query(Extract.extract_filename, Location.location_path)\
116-
.join(Location)\
113+
process_files = self.session.query(Extract)\
117114
.join(ExtractStatus)\
118115
.filter(Extract.extract_filename.like("%" + filename + "%"))\
119116
.filter(ExtractStatus.extract_status_name == 'ready') \
120117
.order_by(Extract.extract_registration_date_time)\
121-
.order_by(Extract.extract_id)
118+
.order_by(Extract.extract_id)\
119+
.all()
122120

123-
for record in process_files:
124-
extract_files.append(join(record.location_path, record.extract_filename))
125-
126-
return extract_files
121+
return process_files
127122

128123
def find_ready_extracts_by_location(self, location):
129124
"""
130125
For the given location name, find all matching extracts that are ready for processing
131126
:param location:
132127
:return:
133128
"""
134-
extract_files = []
135129

136-
process_files = self.session.query(Extract.extract_filename, Location.location_path)\
130+
process_files = self.session.query(Extract)\
137131
.join(Location)\
138132
.join(ExtractStatus)\
139133
.filter(ExtractStatus.extract_status_name == 'ready')\
140134
.filter(Location.location_name == location) \
141-
.order_by(Extract.extract_registration_date_time)
142-
143-
for record in process_files:
144-
extract_files.append(join(record.location_path, record.extract_filename))
135+
.order_by(Extract.extract_registration_date_time)\
136+
.all()
145137

146-
return extract_files
138+
return process_files
147139

148140
def find_ready_extracts_by_process(self, extract_process_name):
149141
"""
150142
For the given named process, find the extracts that are ready for processing.
151143
:return: List of OS specific filepaths with filenames.
152144
"""
153-
extract_files = []
154145

155-
process_files = self.session.query(Extract.extract_filename, Location.location_path) \
146+
process_files = self.session.query(Extract) \
156147
.join(ExtractStatus, Extract.extract_status_id == ExtractStatus.extract_status_id) \
157148
.join(Location, Extract.extract_location_id == Location.location_id) \
158149
.join(ExtractProcess, Extract.extract_id == ExtractProcess.extract_tracking_id) \
159150
.join(ProcessTracking) \
160151
.join(Process) \
161152
.filter(Process.process_name == extract_process_name
162153
, ExtractStatus.extract_status_name == 'ready') \
163-
.order_by(Extract.extract_registration_date_time)
154+
.order_by(Extract.extract_registration_date_time)\
155+
.all()
164156

165-
for record in process_files:
166-
extract_files.append(join(record.location_path, record.extract_filename))
157+
self.logger.info('Returning extract files by process.')
167158

168-
return extract_files
159+
return process_files
169160

170161
def get_latest_tracking_record(self, process):
171162
"""

tests/test_process_tracker.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ def test_find_ready_extracts_by_filename_full(self):
129129
expected_result = ['/home/test/extract_dir/test_extract_filename2.csv']
130130

131131
given_result = self.process_tracker.find_ready_extracts_by_filename('test_extract_filename2.csv')
132+
given_result = [record.full_filepath() for record in given_result]
132133

133-
self.assertEqual(expected_result, given_result)
134+
self.assertCountEqual(expected_result, given_result)
134135

135136
def test_find_ready_extracts_by_filename_partial(self):
136137
"""
@@ -161,8 +162,9 @@ def test_find_ready_extracts_by_filename_partial(self):
161162
, '/home/test/extract_dir/test_extract_filename3-2.csv']
162163

163164
given_result = self.process_tracker.find_ready_extracts_by_filename('test_extract_filename')
165+
given_result = [record.full_filepath() for record in given_result]
164166

165-
self.assertEqual(expected_result, given_result)
167+
self.assertCountEqual(expected_result, given_result)
166168

167169
def test_find_ready_extracts_by_filename_partial_not_descending(self):
168170
"""
@@ -193,6 +195,7 @@ def test_find_ready_extracts_by_filename_partial_not_descending(self):
193195
, '/home/test/extract_dir/test_extract_filename3-1.csv']
194196

195197
given_result = self.process_tracker.find_ready_extracts_by_filename('test_extract_filename')
198+
given_result = [record.full_filepath() for record in given_result]
196199

197200
self.assertNotEqual(expected_result, given_result)
198201

@@ -225,8 +228,9 @@ def test_find_ready_extracts_by_location(self):
225228
, '/home/test/extract_dir/test_extract_filename4-2.csv']
226229

227230
given_result = self.process_tracker.find_ready_extracts_by_location('Test Location')
231+
given_result = [record.full_filepath() for record in given_result]
228232

229-
self.assertEqual(expected_result, given_result)
233+
self.assertCountEqual(expected_result, given_result)
230234

231235
def test_find_ready_extracts_by_location_not_descending(self):
232236
"""
@@ -257,6 +261,7 @@ def test_find_ready_extracts_by_location_not_descending(self):
257261
, '/home/test/extract_dir/test_extract_filename4-1.csv']
258262

259263
given_result = self.process_tracker.find_ready_extracts_by_location('Test Location')
264+
given_result = [record.full_filepath() for record in given_result]
260265

261266
self.assertNotEqual(expected_result, given_result)
262267

@@ -288,8 +293,9 @@ def test_find_ready_extracts_by_process(self):
288293
, '/home/test/extract_dir/test_extract_filename5-2.csv']
289294

290295
given_result = self.process_tracker.find_ready_extracts_by_process('Testing Process Tracking Initialization')
296+
given_result = [record.full_filepath() for record in given_result]
291297

292-
self.assertEqual(sorted(expected_result), sorted(given_result))
298+
self.assertCountEqual(expected_result, given_result)
293299

294300
def test_find_ready_extracts_by_process_not_descending(self):
295301
"""
@@ -323,6 +329,7 @@ def test_find_ready_extracts_by_process_not_descending(self):
323329
, '/home/test/extract_dir/test_extract_filename5-1.csv']
324330

325331
given_result = self.process_tracker.find_ready_extracts_by_process('Testing Process Tracking Initialization')
332+
given_result = [record.full_filepath() for record in given_result]
326333

327334
self.assertNotEqual(expected_result, given_result)
328335

0 commit comments

Comments
 (0)