Skip to content

Commit b010790

Browse files
authored
MySQL8 compliance (#189)
* pytest running unittests correctly * new local location for creds * changed 'system' tbl syntax in SQL queries to conform to MySQL8 keywords
1 parent 4a091a9 commit b010790

File tree

6 files changed

+105
-72
lines changed

6 files changed

+105
-72
lines changed

babel/data/datastore_worker.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def construct_report_query_stmn(
171171
system_id: int, library_id: int, user_ids: list[int], start_date: str, end_date: str
172172
):
173173
"""
174-
Creates SQL query statemanet to select datastore records matching
174+
Creates SQL query statement to select datastore records matching
175175
report criteria
176176
177177
args:
@@ -190,7 +190,7 @@ def construct_report_query_stmn(
190190
cart.created as cart_date,
191191
status.name as cart_status,
192192
user.name as user,
193-
system.name as system,
193+
`system`.name as system_name,
194194
library.name as library,
195195
`order`.did as order_id,
196196
lang.name as lang_name,
@@ -206,7 +206,7 @@ def construct_report_query_stmn(
206206
FROM cart
207207
JOIN status ON cart.status_id = status.did
208208
JOIN user ON cart.user_id = user.did
209-
JOIN system ON cart.system_id = system.did
209+
JOIN `system` ON cart.system_id = `system`.did
210210
JOIN library ON cart.library_id = library.did
211211
JOIN `order` ON cart.did = `order`.cart_id
212212
JOIN lang ON `order`.lang_id = lang.did

babel/data/transactions_search.py

Lines changed: 97 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,50 @@
77
from sqlalchemy.sql import text
88

99

10-
from data.datastore import (session_scope, Order, Cart, Resource, System,
11-
Library, User, Lang, Audn, MatType, Vendor,
12-
OrderLocation, Fund, Branch, ShelfCode, Wlos)
10+
from data.datastore import (
11+
session_scope,
12+
Order,
13+
Cart,
14+
Resource,
15+
System,
16+
Library,
17+
User,
18+
Lang,
19+
Audn,
20+
MatType,
21+
Vendor,
22+
OrderLocation,
23+
Fund,
24+
Branch,
25+
ShelfCode,
26+
Wlos,
27+
)
1328
from data.datastore_worker import retrieve_record
1429
from errors import BabelError
1530
from logging_settings import format_traceback
1631

1732

18-
mlogger = logging.getLogger('babel')
33+
mlogger = logging.getLogger("babel")
1934

2035

2136
@lru_cache(maxsize=2)
2237
def get_shelfcode(session, shelfcode_id, audn_code):
23-
shelfcode = ''
38+
shelfcode = ""
2439
if shelfcode_id is not None:
25-
stmn = text("""
40+
stmn = text(
41+
"""
2642
SELECT code, includes_audn FROM shelfcode
2743
WHERE did=:shelfcode_id
28-
""")
44+
"""
45+
)
2946
stmn = stmn.bindparams(shelfcode_id=shelfcode_id)
3047
instances = session.execute(stmn)
31-
shelfcode = ''
48+
shelfcode = ""
3249
for i in instances:
3350
if i.includes_audn:
34-
shelfcode = f'{audn_code}{i.code}'
51+
shelfcode = f"{audn_code}{i.code}"
3552
else:
36-
shelfcode = f'{i.code}'
53+
shelfcode = f"{i.code}"
3754
break
3855
return shelfcode
3956

@@ -42,10 +59,12 @@ def get_shelfcode(session, shelfcode_id, audn_code):
4259
def get_fund_code(session, fund_id):
4360
code = None
4461
if fund_id is not None:
45-
stmn = text("""
62+
stmn = text(
63+
"""
4664
SELECT code FROM fund
4765
WHERE did=:fund_id
48-
""")
66+
"""
67+
)
4968
stmn = stmn.bindparams(fund_id=fund_id)
5069
instances = session.execute(stmn)
5170
for i in instances:
@@ -58,10 +77,12 @@ def get_fund_code(session, fund_id):
5877
def get_branch_code(session, branch_id):
5978
code = None
6079
if branch_id is not None:
61-
stmn = text("""
80+
stmn = text(
81+
"""
6282
SELECT code FROM branch
6383
WHERE did=:branch_id
64-
""")
84+
"""
85+
)
6586
stmn = stmn.bindparams(branch_id=branch_id)
6687
instances = session.execute(stmn)
6788
for i in instances:
@@ -74,22 +95,26 @@ def get_branch_code(session, branch_id):
7495
def get_vendor_code(session, vendor_id, system):
7596
code = None
7697
if vendor_id is not None:
77-
if system == 'BPL':
78-
stmn = text("""
98+
if system == "BPL":
99+
stmn = text(
100+
"""
79101
SELECT bpl_code FROM vendor
80102
WHERE did=:vendor_id
81-
""")
82-
elif system == 'NYP':
83-
stmn = text("""
103+
"""
104+
)
105+
elif system == "NYP":
106+
stmn = text(
107+
"""
84108
SELECT nyp_code FROM vendor
85109
WHERE did=:vendor_id
86-
""")
110+
"""
111+
)
87112
stmn = stmn.bindparams(vendor_id=vendor_id)
88113
instances = session.execute(stmn)
89114
for i in instances:
90-
if system == 'BPL':
115+
if system == "BPL":
91116
code = i.bpl_code
92-
elif system == 'NYP':
117+
elif system == "NYP":
93118
code = i.nyp_code
94119
break
95120
return code
@@ -99,10 +124,12 @@ def get_vendor_code(session, vendor_id, system):
99124
def get_lang_name(session, lang_id):
100125
name = None
101126
if lang_id is not None:
102-
stmn = text("""
127+
stmn = text(
128+
"""
103129
SELECT name FROM lang
104130
WHERE did=:lang_id
105-
""")
131+
"""
132+
)
106133
stmn = stmn.bindparams(lang_id=lang_id)
107134
instances = session.execute(stmn)
108135
for i in instances:
@@ -116,10 +143,12 @@ def get_audn_name_and_code(session, audn_id):
116143
name = None
117144
code = None
118145
if audn_id is not None:
119-
stmn = text("""
146+
stmn = text(
147+
"""
120148
SELECT name, code FROM audn
121149
WHERE did=:audn_id
122-
""")
150+
"""
151+
)
123152
stmn = stmn.bindparams(audn_id=audn_id)
124153
instances = session.execute(stmn)
125154
for i in instances:
@@ -133,10 +162,12 @@ def get_audn_name_and_code(session, audn_id):
133162
def get_mattype_name(session, mattype_id):
134163
name = None
135164
if mattype_id is not None:
136-
stmn = text("""
165+
stmn = text(
166+
"""
137167
SELECT name FROM mattype
138168
WHERE did=:mattype_id
139-
""")
169+
"""
170+
)
140171
stmn = stmn.bindparams(mattype_id=mattype_id)
141172
instances = session.execute(stmn)
142173
for i in instances:
@@ -147,10 +178,12 @@ def get_mattype_name(session, mattype_id):
147178

148179
@lru_cache(maxsize=4)
149180
def get_status_name(session, status_id):
150-
stmn = text("""
181+
stmn = text(
182+
"""
151183
SELECT name FROM status
152184
WHERE did=:status_id
153-
""")
185+
"""
186+
)
154187
stmn = stmn.bindparams(status_id=status_id)
155188
instances = session.execute(stmn)
156189
for i in instances:
@@ -161,10 +194,12 @@ def get_status_name(session, status_id):
161194

162195
@lru_cache(maxsize=2)
163196
def get_library_name(session, library_id):
164-
stmn = text("""
197+
stmn = text(
198+
"""
165199
SELECT name FROM library
166200
WHERE did=:library_id
167-
""")
201+
"""
202+
)
168203
stmn = stmn.bindparams(library_id=library_id)
169204
instances = session.execute(stmn)
170205

@@ -179,10 +214,12 @@ def get_library_name(session, library_id):
179214
@lru_cache(maxsize=2)
180215
def get_system_name(session, system_id):
181216
name = None
182-
stmn = text("""
183-
SELECT name FROM system
217+
stmn = text(
218+
"""
219+
SELECT name FROM `system`
184220
WHERE did=:system_id
185-
""")
221+
"""
222+
)
186223
stmn = stmn.bindparams(system_id=system_id)
187224
instances = session.execute(stmn)
188225
for i in instances:
@@ -194,10 +231,12 @@ def get_system_name(session, system_id):
194231
@lru_cache(maxsize=4)
195232
def get_owner(session, user_id):
196233
owner = None
197-
stmn = text("""
198-
SELECT name FROM user
234+
stmn = text(
235+
"""
236+
SELECT name FROM `user`
199237
WHERE did=:user_id
200-
""")
238+
"""
239+
)
201240
stmn = stmn.bindparams(user_id=user_id)
202241
instances = session.execute(stmn)
203242
for i in instances:
@@ -207,35 +246,33 @@ def get_owner(session, user_id):
207246

208247

209248
def get_data_by_identifier(keyword, keyword_type):
210-
if keyword_type == 'bib #':
249+
if keyword_type == "bib #":
211250
param = Order.bid
212-
elif keyword_type == 'order #':
251+
elif keyword_type == "order #":
213252
param = Order.oid
214-
elif keyword_type == 'wlo #':
253+
elif keyword_type == "wlo #":
215254
param = Order.wlo
216-
elif keyword_type == 'ISBN':
255+
elif keyword_type == "ISBN":
217256
param = Resource.isbn
218-
elif keyword_type == 'UPC':
257+
elif keyword_type == "UPC":
219258
param = Resource.upc
220-
elif keyword_type == 'other #':
259+
elif keyword_type == "other #":
221260
param = Resource.other_no
222-
elif keyword_type == 'blanketPO':
261+
elif keyword_type == "blanketPO":
223262
param = Cart.blanketPO
224263
else:
225-
raise AttributeError('Invalid keyword_type passed')
226-
mlogger.debug(f'Basic search params: {keyword}, type {param}')
264+
raise AttributeError("Invalid keyword_type passed")
265+
mlogger.debug(f"Basic search params: {keyword}, type {param}")
227266

228267
try:
229268
with session_scope() as session:
230269
recs = (
231-
session.query(
232-
Cart,
233-
Order,
234-
Resource)
270+
session.query(Cart, Order, Resource)
235271
.join(Order, Cart.did == Order.cart_id)
236272
.join(Resource, Order.did == Resource.order_id)
237273
.filter(param == keyword)
238-
.all())
274+
.all()
275+
)
239276

240277
results = []
241278
for cart_rec, ord_rec, res_rec in recs:
@@ -255,20 +292,17 @@ def get_data_by_identifier(keyword, keyword_type):
255292
po = ord_rec.poPerLine
256293

257294
lang = get_lang_name(session, ord_rec.lang_id)
258-
audn_name, audn_code = get_audn_name_and_code(
259-
session, ord_rec.audn_id)
295+
audn_name, audn_code = get_audn_name_and_code(session, ord_rec.audn_id)
260296
vendor = get_vendor_code(session, ord_rec.vendor_id, system)
261297
mattype = get_mattype_name(session, ord_rec.matType_id)
262298

263299
locs = []
264300
for loc in ord_rec.locations:
265301
branch = get_branch_code(session, loc.branch_id)
266-
shelfcode = get_shelfcode(
267-
session, loc.shelfcode_id, audn_code)
302+
shelfcode = get_shelfcode(session, loc.shelfcode_id, audn_code)
268303
qty = loc.qty
269304
fund = get_fund_code(session, loc.fund_id)
270-
locs.append(
271-
f'{branch}{shelfcode}({qty})/{fund}')
305+
locs.append(f"{branch}{shelfcode}({qty})/{fund}")
272306

273307
# resouce
274308
title = res_rec.title
@@ -297,7 +331,8 @@ def get_data_by_identifier(keyword, keyword_type):
297331
audn_name=audn_name,
298332
mattype=mattype,
299333
po=po,
300-
locs=', '.join(locs))
334+
locs=", ".join(locs),
335+
)
301336
results.append(unit)
302337
session.expunge_all()
303338

@@ -306,16 +341,14 @@ def get_data_by_identifier(keyword, keyword_type):
306341
except Exception as exc:
307342
_, _, exc_traceback = sys.exc_info()
308343
tb = format_traceback(exc, exc_traceback)
309-
mlogger.error(
310-
'Unhandled error during Basic search.'
311-
f'Traceback: {tb}')
312-
raise BabelError('Unable to retrieve records.')
344+
mlogger.error("Unhandled error during Basic search." f"Traceback: {tb}")
345+
raise BabelError("Unable to retrieve records.")
313346

314347

315348
def complex_search(conditions):
316349
"""
317350
args:
318-
condictions: dict, key=element name, value=tuple(con, value)
351+
conditions: dict, key=element name, value=tuple(con, value)
319352
"""
320353

321354
# will tackle in next iteration

babel/reports/reports.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ def generate_basic_stats(df: DataFrame, start_date: str, end_date: str) -> dict:
104104
data["end_date"] = end_date
105105

106106
fdf = df[df["cart_status"] != "in-works"]
107-
ndf = fdf[fdf["system"] == "NYP"]
108-
bdf = fdf[fdf["system"] == "BPL"]
107+
ndf = fdf[fdf["system_name"] == "NYP"]
108+
bdf = fdf[fdf["system_name"] == "BPL"]
109109

110110
# total Babel orders
111111
rows = []

tests/integration/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ def dev_user_data(dummy_user_data_handle):
2929
with open(os.path.join(user_fh, ".babel/new-babel-prod-db.json"), "r") as f:
3030
db = json.load(f)
3131

32-
with open(os.path.join(user_fh, ".platform/tomasz_platform.json"), "r") as f:
32+
with open(os.path.join(user_fh, ".cred/.platform/tomasz_platform.json"), "r") as f:
3333
pl = json.load(f)
3434

35-
with open(os.path.join(user_fh, ".bpl-solr/bpl-solr-prod.json"), "r") as f:
35+
with open(os.path.join(user_fh, ".cred/.solr/bpl-solr-prod.json"), "r") as f:
3636
sl = json.load(f)
3737

3838
user_data = shelve.open(dummy_user_data_handle)
File renamed without changes.

tests/test_xlsx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class TestOrderDataReader(unittest.TestCase):
1010
"""Test parsing vendor sheet with title/order info"""
1111

1212
def setUp(self):
13-
self.fh_eng = "./test_sheets/eng.xlsx"
13+
self.fh_eng = "./tests/test_sheets/eng.xlsx"
1414

1515
def test_no_header_provided(self):
1616
with self.assertRaises(AttributeError):

0 commit comments

Comments
 (0)