diff --git a/arkindex_worker/cache.py b/arkindex_worker/cache.py index 3ae761489ed1d0072614443601a3eff06e0a7d77..89014daa77e51f1dbff6c376f06bdd3bad2c9c1d 100644 --- a/arkindex_worker/cache.py +++ b/arkindex_worker/cache.py @@ -1,11 +1,12 @@ # -*- coding: utf-8 -*- import sqlite3 +from collections import namedtuple from arkindex_worker import logger SQL_ELEMENTS_TABLE_CREATION = """CREATE TABLE IF NOT EXISTS elements ( id VARCHAR(32) PRIMARY KEY, - parent_id VARCHAR(32), + parent_id VARCHAR(32) NOT NULL, name TEXT NOT NULL, type TEXT NOT NULL, polygon TEXT, @@ -13,6 +14,11 @@ SQL_ELEMENTS_TABLE_CREATION = """CREATE TABLE IF NOT EXISTS elements ( )""" +CachedElement = namedtuple( + "CachedElement", ["id", "parent_id", "name", "type", "polygon", "worker_version_id"] +) + + class LocalDB(object): def __init__(self, path): self.db = sqlite3.connect(path) @@ -26,9 +32,9 @@ class LocalDB(object): def insert(self, table, lines): if not lines: return - columns = ", ".join(lines[0].keys()) + columns = ", ".join(lines[0]._fields) placeholders = ", ".join("?" * len(lines[0])) - values = [tuple(line.values()) for line in lines] + values = [tuple(line) for line in lines] self.cursor.executemany( f"INSERT INTO {table} ({columns}) VALUES ({placeholders})", values ) diff --git a/arkindex_worker/worker.py b/arkindex_worker/worker.py index 2d337c5f131a38c93e2019568d5c2868d1c2ea08..5d32fff5bd981c2c48a8c390f1db237afe26467f 100644 --- a/arkindex_worker/worker.py +++ b/arkindex_worker/worker.py @@ -17,7 +17,7 @@ from apistar.exceptions import ErrorResponse from arkindex import ArkindexClient, options_from_env from arkindex_worker import logger -from arkindex_worker.cache import LocalDB +from arkindex_worker.cache import CachedElement, LocalDB from arkindex_worker.models import Element from arkindex_worker.reporting import Reporter from arkindex_worker.utils import convert_str_uuid_to_hex @@ -468,14 +468,14 @@ class ElementsWorker(BaseWorker): parent_id_hex = convert_str_uuid_to_hex(parent.id) worker_version_id_hex = convert_str_uuid_to_hex(self.worker_version_id) to_insert = [ - { - "id": convert_str_uuid_to_hex(created_ids[idx]["id"]), - "parent_id": parent_id_hex, - "name": element["name"], - "type": element["type"], - "polygon": json.dumps(element["polygon"]), - "worker_version_id": worker_version_id_hex, - } + CachedElement( + id=convert_str_uuid_to_hex(created_ids[idx]["id"]), + parent_id=parent_id_hex, + name=element["name"], + type=element["type"], + polygon=json.dumps(element["polygon"]), + worker_version_id=worker_version_id_hex, + ) for idx, element in enumerate(elements) ] self.cache.insert("elements", to_insert) diff --git a/tests/data/cache/tables.sqlite b/tests/data/cache/tables.sqlite index 91639e7b9242c4f9cdb046e666f0b3eb1bbc9125..590b560894da841ce30465b76e2e3c11d773820c 100644 Binary files a/tests/data/cache/tables.sqlite and b/tests/data/cache/tables.sqlite differ diff --git a/tests/test_cache.py b/tests/test_cache.py index 26cadfa3cd5dd8f5913fb20dcc2325287f9e1155..07754469378861d41d2954f714dc14970d0877a5 100644 --- a/tests/test_cache.py +++ b/tests/test_cache.py @@ -6,31 +6,31 @@ from pathlib import Path import pytest -from arkindex_worker.cache import LocalDB +from arkindex_worker.cache import CachedElement, LocalDB from arkindex_worker.utils import convert_str_uuid_to_hex FIXTURES = Path(__file__).absolute().parent / "data/cache" ELEMENTS_TO_INSERT = [ - { - "id": convert_str_uuid_to_hex("11111111-1111-1111-1111-111111111111"), - "parent_id": convert_str_uuid_to_hex("12341234-1234-1234-1234-123412341234"), - "name": "0", - "type": "something", - "polygon": json.dumps([[1, 1], [2, 2], [2, 1], [1, 2]]), - "worker_version_id": convert_str_uuid_to_hex( + CachedElement( + id=convert_str_uuid_to_hex("11111111-1111-1111-1111-111111111111"), + parent_id=convert_str_uuid_to_hex("12341234-1234-1234-1234-123412341234"), + name="0", + type="something", + polygon=json.dumps([[1, 1], [2, 2], [2, 1], [1, 2]]), + worker_version_id=convert_str_uuid_to_hex( "56785678-5678-5678-5678-567856785678" ), - }, - { - "id": convert_str_uuid_to_hex("22222222-2222-2222-2222-222222222222"), - "parent_id": convert_str_uuid_to_hex("12341234-1234-1234-1234-123412341234"), - "name": "1", - "type": "something", - "polygon": json.dumps([[1, 1], [2, 2], [2, 1], [1, 2]]), - "worker_version_id": convert_str_uuid_to_hex( + ), + CachedElement( + id=convert_str_uuid_to_hex("22222222-2222-2222-2222-222222222222"), + parent_id=convert_str_uuid_to_hex("12341234-1234-1234-1234-123412341234"), + name="1", + type="something", + polygon=json.dumps([[1, 1], [2, 2], [2, 1], [1, 2]]), + worker_version_id=convert_str_uuid_to_hex( "56785678-5678-5678-5678-567856785678" ), - }, + ), ] @@ -129,4 +129,4 @@ def test_insert(): == expected_cache.cursor.execute("SELECT * FROM elements").fetchall() ) - assert [dict(row) for row in generated_rows] == ELEMENTS_TO_INSERT + assert [CachedElement(**dict(row)) for row in generated_rows] == ELEMENTS_TO_INSERT diff --git a/tests/test_elements_worker/test_elements.py b/tests/test_elements_worker/test_elements.py index 39712a87b3b677dee273ca12bd196e29a5f4c2ee..83a88bc8cc325be5a7754f65896931bf191fe8be 100644 --- a/tests/test_elements_worker/test_elements.py +++ b/tests/test_elements_worker/test_elements.py @@ -8,6 +8,7 @@ from pathlib import Path import pytest from apistar.exceptions import ErrorResponse +from arkindex_worker.cache import CachedElement from arkindex_worker.models import Element from arkindex_worker.utils import convert_str_uuid_to_hex from arkindex_worker.worker import ElementsWorker @@ -686,19 +687,17 @@ def test_create_elements(responses, mock_elements_worker): rows = mock_elements_worker.cache.cursor.execute( "SELECT * FROM elements" ).fetchall() - assert [dict(row) for row in rows] == [ - { - "id": convert_str_uuid_to_hex("497f6eca-6276-4993-bfeb-53cbbbba6f08"), - "parent_id": convert_str_uuid_to_hex( - "12341234-1234-1234-1234-123412341234" - ), - "name": "0", - "type": "something", - "polygon": json.dumps([[1, 1], [2, 2], [2, 1], [1, 2]]), - "worker_version_id": convert_str_uuid_to_hex( + assert [CachedElement(**dict(row)) for row in rows] == [ + CachedElement( + id=convert_str_uuid_to_hex("497f6eca-6276-4993-bfeb-53cbbbba6f08"), + parent_id=convert_str_uuid_to_hex("12341234-1234-1234-1234-123412341234"), + name="0", + type="something", + polygon=json.dumps([[1, 1], [2, 2], [2, 1], [1, 2]]), + worker_version_id=convert_str_uuid_to_hex( "12341234-1234-1234-1234-123412341234" ), - } + ) ]