Skip to content
Snippets Groups Projects
Commit 4874c3d8 authored by Eva Bardou's avatar Eva Bardou
Browse files

Fix some review related code snippets

parent 3ad7a80c
No related branches found
No related tags found
No related merge requests found
Pipeline #78323 passed
......@@ -21,6 +21,13 @@ CachedElement = namedtuple(
)
def convert_table_tuple(table):
if table == "elements":
return CachedElement
else:
raise NotImplementedError
class LocalDB(object):
def __init__(self, path):
self.db = sqlite3.connect(path)
......@@ -43,11 +50,27 @@ class LocalDB(object):
self.db.commit()
def fetch(self, table, where=[]):
"""
where parameter is a list containing 3-values tuples defining an SQL WHERE condition.
e.g: where=[("id", "LIKE", "%0000%"), ("id", "NOT LIKE", "%1111%")]
stands for "WHERE id LIKE '%0000%' AND id NOT LIKE '%1111%'" in SQL.
This method only supports 'AND' SQL conditions.
"""
sql = f"SELECT * FROM {table}"
if where:
assert isinstance(where, list), "where should be a list"
assert all(
isinstance(condition, tuple) and len(condition) == 3
for condition in where
), "where conditions should be tuples of 3 values"
sql += " WHERE "
sql += " AND ".join(
[f"{field} {operator} (?)" for field, operator, _ in where]
)
self.cursor.execute(sql, [value for _, _, value in where])
return self.cursor.fetchall()
tuple_type = convert_table_tuple(table)
return [tuple_type(**dict(row)) for row in self.cursor.fetchall()]
......@@ -897,18 +897,17 @@ class ElementsWorker(BaseWorker):
}, "When using the local cache, you can only filter by 'type' and/or 'worker_version'"
conditions = [("parent_id", "=", convert_str_uuid_to_hex(element.id))]
conditions += [("type", "=", type)] if type else []
conditions += (
[("worker_version_id", "=", convert_str_uuid_to_hex(worker_version))]
if worker_version
else []
)
if type:
conditions.append(("type", "=", type))
if worker_version:
conditions.append(
("worker_version_id", "=", convert_str_uuid_to_hex(worker_version))
)
children = self.cache.fetch(
"elements",
where=conditions,
)
children = [CachedElement(**dict(child)) for child in children]
else:
children = self.api_client.paginate(
"ListElementChildren", id=element.id, **query_params
......
......@@ -134,15 +134,15 @@ def test_fetch_all():
db_path = f"{FIXTURES}/lines.sqlite"
cache = LocalDB(db_path)
cache.create_tables()
rows = cache.fetch("elements")
assert [CachedElement(**dict(row)) for row in rows] == ELEMENTS_TO_INSERT
children = cache.fetch("elements")
assert children == ELEMENTS_TO_INSERT
def test_fetch_with_where():
db_path = f"{FIXTURES}/lines.sqlite"
cache = LocalDB(db_path)
cache.create_tables()
rows = cache.fetch(
children = cache.fetch(
"elements",
where=[
(
......@@ -153,4 +153,4 @@ def test_fetch_with_where():
("id", "LIKE", "%1111%"),
],
)
assert [CachedElement(**dict(row)) for row in rows] == [ELEMENTS_TO_INSERT[0]]
assert children == [ELEMENTS_TO_INSERT[0]]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment