Skip to content

Commit b5ffd31

Browse files
pre-commit-ci[bot]shaypal5
authored andcommitted
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 5b63cf6 commit b5ffd31

File tree

3 files changed

+58
-26
lines changed

3 files changed

+58
-26
lines changed

.github/workflows/ci-test.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ jobs:
3939
CACHIER_TEST_VS_DOCKERIZED_MYSQL: "true"
4040
CACHIER_TEST_PYODBC_CONNECTION_STRING: "DRIVER={MySQL ODBC Driver};SERVER=localhost;PORT=3306;DATABASE=test;USER=root;PASSWORD=password;"
4141

42-
4342
steps:
4443
- uses: actions/checkout@v4
4544

src/cachier/cores/odbc.py

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@
88
# Copyright (c) 2016, Shay Palachy <[email protected]>
99

1010
# standard library imports
11+
import datetime
1112
import pickle
1213
import time
13-
import datetime
1414

1515
pyodbc = None
1616
# third party imports
1717
with suppress(ImportError):
1818
import pyodbc
1919

2020
# local imports
21-
from .base import _BaseCore, RecalculationNeeded
21+
from .base import RecalculationNeeded, _BaseCore
2222

23-
class _OdbcCore(_BaseCore):
2423

24+
class _OdbcCore(_BaseCore):
2525
def __init__(
26-
self,
27-
hash_func,
28-
wait_for_calc_timeout,
29-
connection_string,
30-
table_name,
26+
self,
27+
hash_func,
28+
wait_for_calc_timeout,
29+
connection_string,
30+
table_name,
3131
):
3232
if "pyodbc" not in sys.modules:
3333
warnings.warn(
@@ -43,7 +43,8 @@ def __init__(
4343
def ensure_table_exists(self):
4444
with pyodbc.connect(self.connection_string) as conn:
4545
cursor = conn.cursor()
46-
cursor.execute(f"""
46+
cursor.execute(
47+
f"""
4748
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'{self.table_name}')
4849
BEGIN
4950
CREATE TABLE {self.table_name} (
@@ -54,13 +55,17 @@ def ensure_table_exists(self):
5455
PRIMARY KEY (key)
5556
);
5657
END
57-
""")
58+
"""
59+
)
5860
conn.commit()
5961

6062
def get_entry_by_key(self, key):
6163
with pyodbc.connect(self.connection_string) as conn:
6264
cursor = conn.cursor()
63-
cursor.execute(f"SELECT value, time, being_calculated FROM {self.table_name} WHERE key = ?", key)
65+
cursor.execute(
66+
f"SELECT value, time, being_calculated FROM {self.table_name} WHERE key = ?",
67+
key,
68+
)
6469
row = cursor.fetchone()
6570
if row:
6671
return {
@@ -73,34 +78,48 @@ def get_entry_by_key(self, key):
7378
def set_entry(self, key, func_res):
7479
with pyodbc.connect(self.connection_string) as conn:
7580
cursor = conn.cursor()
76-
cursor.execute(f"""
81+
cursor.execute(
82+
f"""
7783
MERGE INTO {self.table_name} USING (SELECT 1 AS dummy) AS src ON (key = ?)
7884
WHEN MATCHED THEN
7985
UPDATE SET value = ?, time = GETDATE(), being_calculated = 0
8086
WHEN NOT MATCHED THEN
8187
INSERT (key, value, time, being_calculated) VALUES (?, ?, GETDATE(), 0);
82-
""", key, pickle.dumps(func_res), key, pickle.dumps(func_res))
88+
""",
89+
key,
90+
pickle.dumps(func_res),
91+
key,
92+
pickle.dumps(func_res),
93+
)
8394
conn.commit()
8495

8596
def mark_entry_being_calculated(self, key):
8697
with pyodbc.connect(self.connection_string) as conn:
8798
cursor = conn.cursor()
88-
cursor.execute(f"UPDATE {self.table_name} SET being_calculated = 1 WHERE key = ?", key)
99+
cursor.execute(
100+
f"UPDATE {self.table_name} SET being_calculated = 1 WHERE key = ?",
101+
key,
102+
)
89103
conn.commit()
90104

91105
def mark_entry_not_calculated(self, key):
92106
with pyodbc.connect(self.connection_string) as conn:
93107
cursor = conn.cursor()
94-
cursor.execute(f"UPDATE {self.table_name} SET being_calculated = 0 WHERE key = ?", key)
108+
cursor.execute(
109+
f"UPDATE {self.table_name} SET being_calculated = 0 WHERE key = ?",
110+
key,
111+
)
95112
conn.commit()
96113

97114
def wait_on_entry_calc(self, key):
98115
start_time = datetime.datetime.now()
99116
while True:
100117
entry = self.get_entry_by_key(key)
101-
if entry and not entry['being_calculated']:
102-
return entry['value']
103-
if (datetime.datetime.now() - start_time).total_seconds() > self.wait_for_calc_timeout:
118+
if entry and not entry["being_calculated"]:
119+
return entry["value"]
120+
if (
121+
datetime.datetime.now() - start_time
122+
).total_seconds() > self.wait_for_calc_timeout:
104123
raise RecalculationNeeded()
105124
time.sleep(1)
106125

@@ -113,5 +132,7 @@ def clear_cache(self):
113132
def clear_being_calculated(self):
114133
with pyodbc.connect(self.connection_string) as conn:
115134
cursor = conn.cursor()
116-
cursor.execute(f"UPDATE {self.table_name} SET being_calculated = 0")
135+
cursor.execute(
136+
f"UPDATE {self.table_name} SET being_calculated = 0"
137+
)
117138
conn.commit()

tests/test_odbc_core.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010

1111
# local imports
1212
from cachier import cachier
13+
1314
# from cachier.cores.base import RecalculationNeeded
1415
# from cachier.cores.odbc import _OdbcCore
1516

1617

1718
class CfgKey:
1819
"""Configuration keys for testing."""
20+
1921
TEST_VS_DOCKERIZED_MYSQL = "TEST_VS_DOCKERIZED_MYSQL"
2022
TEST_PYODBC_CONNECTION_STRING = "TEST_PYODBC_CONNECTION_STRING"
2123

@@ -34,7 +36,7 @@ class CfgKey:
3436
def test_odbc_entry_creation_and_retrieval(odbc_core):
3537
"""Test inserting and retrieving an entry from ODBC cache."""
3638

37-
@cachier(backend='odbc', odbc_connection_string=CONCT_STR)
39+
@cachier(backend="odbc", odbc_connection_string=CONCT_STR)
3840
def sample_function(arg_1, arg_2):
3941
return arg_1 + arg_2
4042

@@ -48,19 +50,28 @@ def test_odbc_stale_after(odbc_core):
4850
"""Test ODBC core handling stale_after parameter."""
4951
stale_after = datetime.timedelta(seconds=1)
5052

51-
@cachier(backend='odbc', odbc_connection_string=CONCT_STR, stale_after=stale_after)
53+
@cachier(
54+
backend="odbc",
55+
odbc_connection_string=CONCT_STR,
56+
stale_after=stale_after,
57+
)
5258
def stale_test_function(arg_1, arg_2):
53-
return arg_1 + arg_2 + datetime.datetime.now().timestamp() # Add timestamp to ensure unique values
59+
return (
60+
arg_1 + arg_2 + datetime.datetime.now().timestamp()
61+
) # Add timestamp to ensure unique values
5462

5563
initial_value = stale_test_function(5, 10)
5664
sleep(2) # Wait for the entry to become stale
57-
assert stale_test_function(5, 10) != initial_value # Should recompute since stale
65+
assert (
66+
stale_test_function(5, 10) != initial_value
67+
) # Should recompute since stale
5868

5969

6070
@pytest.mark.odbc
6171
def test_odbc_clear_cache(odbc_core):
6272
"""Test clearing the ODBC cache."""
63-
@cachier(backend='odbc', odbc_connection_string=CONCT_STR)
73+
74+
@cachier(backend="odbc", odbc_connection_string=CONCT_STR)
6475
def clearable_function(arg):
6576
return arg
6677

@@ -74,7 +85,8 @@ def clearable_function(arg):
7485
@pytest.mark.odbc
7586
def test_odbc_being_calculated_flag(odbc_core):
7687
"""Test handling of 'being_calculated' flag in ODBC core."""
77-
@cachier(backend='odbc', odbc_connection_string=CONCT_STR)
88+
89+
@cachier(backend="odbc", odbc_connection_string=CONCT_STR)
7890
def slow_function(arg):
7991
sleep(2) # Simulate long computation
8092
return arg * 2

0 commit comments

Comments
 (0)