Skip to content

Commit 1b727a8

Browse files
committed
[IMP] testing.py: use util.format_query
Show example and don't str.format queries. Part-of: #327 Signed-off-by: Christophe Simonis (chs) <chs@odoo.com>
1 parent 60c49c5 commit 1b727a8

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

src/testing.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -224,39 +224,53 @@ def key(self):
224224
def _set_value(self, key, value):
225225
self._init_db()
226226
value = json.dumps(value, sort_keys=True)
227-
query = """
227+
cr = self._data_table_cr
228+
query = util.format_query(
229+
cr,
230+
"""
228231
INSERT INTO {} (key, value) VALUES (%s, %s)
229232
ON CONFLICT (key) DO UPDATE SET value=EXCLUDED.value
230-
""".format(DATA_TABLE)
231-
self._data_table_cr.execute(query, (key, value))
232-
self._data_table_cr._cnx.commit()
233+
""",
234+
DATA_TABLE,
235+
)
236+
cr.execute(query, (key, value))
237+
cr._cnx.commit()
233238

234239
def _get_value(self, key):
235240
self._init_db()
236-
query = "SELECT value FROM {} WHERE key = %s".format(DATA_TABLE)
237-
self._data_table_cr.execute(query, [key])
238-
result = self._data_table_cr.fetchone()
241+
cr = self._data_table_cr
242+
query = util.format_query(cr, "SELECT value FROM {} WHERE key = %s", DATA_TABLE)
243+
cr.execute(query, [key])
244+
result = cr.fetchone()
239245
if not result:
240246
raise KeyError(key)
241247
return result[0]
242248

243249
def _key_exists(self, key):
244250
self._init_db()
245-
query = "SELECT 1 FROM {} WHERE key = %s".format(DATA_TABLE)
246-
self._data_table_cr.execute(query, [key])
247-
return bool(self._data_table_cr.rowcount)
251+
cr = self._data_table_cr
252+
query = util.format_query(cr, "SELECT 1 FROM {} WHERE key = %s", DATA_TABLE)
253+
cr.execute(query, [key])
254+
return bool(cr.rowcount)
248255

249256
def _init_db(self):
250257
if not UpgradeCommon.__initialized:
251-
self._data_table_cr.execute("SELECT 1 FROM pg_class WHERE relname=%s", [DATA_TABLE])
252-
if not self._data_table_cr.rowcount:
258+
cr = self._data_table_cr
259+
cr.execute("SELECT 1 FROM pg_class WHERE relname=%s", [DATA_TABLE])
260+
if not cr.rowcount:
253261
_logger.info("Creating table %s", DATA_TABLE)
254-
query = """ CREATE TABLE {} (
255-
key VARCHAR(255) PRIMARY KEY,
256-
value JSONB NOT NULL
257-
)""".format(DATA_TABLE)
258-
self._data_table_cr.execute(query)
259-
self._data_table_cr._cnx.commit()
262+
query = util.format_query(
263+
cr,
264+
"""
265+
CREATE TABLE {} (
266+
key VARCHAR(255) PRIMARY KEY,
267+
value JSONB NOT NULL
268+
)
269+
""",
270+
DATA_TABLE,
271+
)
272+
cr.execute(query)
273+
cr._cnx.commit()
260274
UpgradeCommon.__initialized = True
261275

262276
def _setup_registry(self):

0 commit comments

Comments
 (0)