Skip to content

Commit

Permalink
Merge main into stable/4.0.x. Update 20231018
Browse files Browse the repository at this point in the history
* commit '0720dd02b04b5d77a6269b5cd8611b1eab154e9f':
  Open and rename action_worker route also for collection import_preview (#259)
  • Loading branch information
peb-adr committed Oct 18, 2023
2 parents b9ed006 + 0720dd0 commit 11b5874
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 45 deletions.
8 changes: 4 additions & 4 deletions datastore/writer/core/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def delete_history_information(self) -> None:
def truncate_db(self) -> None:
"""Truncate all tables. Only for dev purposes!"""

def write_model_updates_action_worker(self, models: Dict[Fqid, Model]) -> None:
"""For writing directly to models-table used for action_workers"""
def write_model_updates_without_events(self, models: Dict[Fqid, Model]) -> None:
"""For writing directly to models-table used for action_workers and import_previews"""

def write_model_deletes_action_worker(self, fqids: List[Fqid]) -> None:
"""For deleting directly to models-table used for action_workers"""
def write_model_deletes_without_events(self, fqids: List[Fqid]) -> None:
"""For deleting directly to models-table used for action_workers and import_previews"""
4 changes: 2 additions & 2 deletions datastore/writer/core/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def delete_history_information(self) -> None:
def truncate_db(self) -> None:
"""Truncate all tables. Dev mode only"""

def write_action_worker(
def write_without_events(
self,
write_request: WriteRequest,
) -> None:
"""Writes or updates an action_worker-object"""
"""Writes or updates an object without events (action_worker or import_preview)"""
13 changes: 7 additions & 6 deletions datastore/writer/core/writer_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,15 @@ def truncate_db(self) -> None:
logger.info("Database truncated")

@retry_on_db_failure
def write_action_worker(
def write_without_events(
self,
write_request: WriteRequest,
) -> None:
"""Writes or updates an action_worker-object
The action_worker record will be written to
"""Writes or updates an action_worker- or
import_preview-object.
The record will be written to
the models-table only, because there is no history
needed and after the acton is finished and notified,
needed and after the action is finished and notified,
isn't needed anymore.
There is no position available or needed,
for redis notifying the 0 is used therefore.
Expand All @@ -143,13 +144,13 @@ def write_action_worker(
for event in write_request.events:
fqids_to_delete.append(event.fqid)
with self.database.get_context():
self.database.write_model_deletes_action_worker(fqids_to_delete)
self.database.write_model_deletes_without_events(fqids_to_delete)
else:
with self.database.get_context():
for event in write_request.events:
fields_with_delete = copy.deepcopy(event.fields) # type: ignore
fields_with_delete.update({META_DELETED: False})
self.database.write_model_updates_action_worker(
self.database.write_model_updates_without_events(
{event.fqid: fields_with_delete}
)
self.position_to_modified_models[0] = {event.fqid: event.fields} # type: ignore
Expand Down
4 changes: 2 additions & 2 deletions datastore/writer/flask_frontend/json_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ def write(self, data: JSON) -> None:
writer = injector.get(Writer)
writer.write(write_requests)

def write_action_worker(self, data: JSON) -> None:
def write_without_events(self, data: JSON) -> None:
write_request = self.build_write_request(data)

writer = injector.get(Writer)
writer.write_action_worker(write_request)
writer.write_without_events(write_request)

def build_write_request(self, data: JSON) -> WriteRequest:
try:
Expand Down
2 changes: 1 addition & 1 deletion datastore/writer/flask_frontend/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
RESERVE_IDS_URL = unify_urls(URL_PREFIX, "/reserve_ids")
DELETE_HISTORY_INFORMATION_URL = unify_urls(URL_PREFIX, "/delete_history_information")
TRUNCATE_DB_URL = unify_urls(URL_PREFIX, "/truncate_db")
WRITE_ACTION_WORKER_URL = unify_urls(URL_PREFIX, "/write_action_worker")
WRITE_WITHOUT_EVENTS_URL = unify_urls(URL_PREFIX, "/write_without_events")
18 changes: 10 additions & 8 deletions datastore/writer/flask_frontend/routes_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
DELETE_HISTORY_INFORMATION_URL,
RESERVE_IDS_URL,
TRUNCATE_DB_URL,
WRITE_ACTION_WORKER_URL,
WRITE_URL,
WRITE_WITHOUT_EVENTS_URL,
)

from .json_handlers import ReserveIdsHandler, WriteHandler
Expand All @@ -44,23 +44,25 @@ def reserve_ids():


@handle_internal_errors
def write_action_worker():
def write_without_events():
if not request.is_json:
raise InvalidRequest("Data must be json")
if type(request.json) != list:
raise InvalidRequest("write_action_worker data internally must be a list!")
raise InvalidRequest("write_without_events data internally must be a list!")
req_json = cast(List[Dict[str, Any]], request.json)[0]
if len(req_json.get("events", ())) != 1 and any(
event["type"] != "delete" for event in req_json.get("events", ())
):
raise InvalidRequest("write_action_worker may contain only 1 event!")
raise InvalidRequest("write_without_events may contain only 1 event!")
if any(
collection_from_fqid(event["fqid"]) != "action_worker"
collection_from_fqid(event["fqid"]) not in ["action_worker", "import_preview"]
for event in req_json.get("events", ())
):
raise InvalidRequest("Collection for write_action_worker must be action_worker")
raise InvalidRequest(
"Collection for write_without_events must be action_worker or import_preview"
)
write_handler = WriteHandler()
write_handler.write_action_worker(req_json)
write_handler.write_without_events(req_json)
return_code = 200 if req_json.get("events", ())[0]["type"] == "delete" else 201
return ("", return_code)

Expand All @@ -86,7 +88,7 @@ def register_routes(app, url_prefix):
"reserve_ids",
"delete_history_information",
"truncate_db",
"write_action_worker",
"write_without_events",
):
app.add_url_rule(
globals()[f"{route.upper()}_URL"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def write_model_updates(self, models: Dict[Fqid, Model]) -> None:
use_execute_values=True,
)

def write_model_updates_action_worker(self, models: Dict[Fqid, Model]) -> None:
def write_model_updates_without_events(self, models: Dict[Fqid, Model]) -> None:
statement = dedent(
"""\
insert into models (fqid, data, deleted) values %s
Expand All @@ -189,8 +189,8 @@ def write_model_updates_action_worker(self, models: Dict[Fqid, Model]) -> None:
use_execute_values=True,
)

def write_model_deletes_action_worker(self, fqids: List[Fqid]) -> None:
"""Physically delete of action_workers"""
def write_model_deletes_without_events(self, fqids: List[Fqid]) -> None:
"""Physically delete of action_workers or import_previews"""
statement = "delete from models where fqid in %s;"
self.connection.execute(statement, [fqids], use_execute_values=True)

Expand Down
6 changes: 3 additions & 3 deletions tests/writer/system/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from datastore.writer.flask_frontend.routes import (
RESERVE_IDS_URL,
URL_PREFIX,
WRITE_ACTION_WORKER_URL,
WRITE_URL,
WRITE_WITHOUT_EVENTS_URL,
)


Expand All @@ -22,8 +22,8 @@ def test_wrong_method_reserve_ids(client):
assert response.status_code == 405


def test_wrong_method_write_action_worker(client):
response = client.get(WRITE_ACTION_WORKER_URL)
def test_wrong_method_write_without_events(client):
response = client.get(WRITE_WITHOUT_EVENTS_URL)
assert response.status_code == 405


Expand Down
27 changes: 14 additions & 13 deletions tests/writer/system/test_write_action_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from datastore.writer.flask_frontend.routes import WRITE_ACTION_WORKER_URL
from datastore.writer.flask_frontend.routes import WRITE_WITHOUT_EVENTS_URL
from tests.util import assert_response_code


Expand Down Expand Up @@ -34,7 +34,7 @@ def data():

def test_create_update_action_worker(json_client, data, db_cur):
# create action_worker
response = json_client.post(WRITE_ACTION_WORKER_URL, data)
response = json_client.post(WRITE_WITHOUT_EVENTS_URL, data)
assert_response_code(response, 201)

db_cur.execute("select fqid, data from models where fqid = 'action_worker/1'")
Expand All @@ -49,7 +49,7 @@ def test_create_update_action_worker(json_client, data, db_cur):
data_single["events"][0]["fields"] = {
"timestamp": 1658489444,
}
response = json_client.post(WRITE_ACTION_WORKER_URL, data)
response = json_client.post(WRITE_WITHOUT_EVENTS_URL, data)
assert_response_code(response, 201)
db_cur.execute("select fqid, data from models where fqid = 'action_worker/1'")
fqid, result = db_cur.fetchone()
Expand All @@ -62,7 +62,7 @@ def test_create_update_action_worker(json_client, data, db_cur):
"state": "end",
"timestamp": 1658489454,
}
response = json_client.post(WRITE_ACTION_WORKER_URL, data)
response = json_client.post(WRITE_WITHOUT_EVENTS_URL, data)
assert_response_code(response, 201)
db_cur.execute("select fqid, data from models where fqid = 'action_worker/1'")
fqid, result = db_cur.fetchone()
Expand All @@ -86,31 +86,32 @@ def test_create_action_worker_not_single_event(json_client, data, db_cur):
},
}
)
response = json_client.post(WRITE_ACTION_WORKER_URL, data)
response = json_client.post(WRITE_WITHOUT_EVENTS_URL, data)
assert_response_code(response, 400)
assert (
response.json["error"]["msg"] == "write_action_worker may contain only 1 event!"
response.json["error"]["msg"]
== "write_without_events may contain only 1 event!"
)


def test_create_action_worker_data_not_in_list_format(json_client, data, db_cur):
data_single = data[0]
response = json_client.post(WRITE_ACTION_WORKER_URL, data_single)
response = json_client.post(WRITE_WITHOUT_EVENTS_URL, data_single)
assert_response_code(response, 400)
assert (
response.json["error"]["msg"]
== "write_action_worker data internally must be a list!"
== "write_without_events data internally must be a list!"
)


def test_create_action_worker_wrong_collection(json_client, data, db_cur):
data_single = data[0]
data_single["events"][0]["fqid"] = "topic/1"
response = json_client.post(WRITE_ACTION_WORKER_URL, data)
response = json_client.post(WRITE_WITHOUT_EVENTS_URL, data)
assert_response_code(response, 400)
assert (
response.json["error"]["msg"]
== "Collection for write_action_worker must be action_worker"
== "Collection for write_without_events must be action_worker or import_preview"
)


Expand All @@ -123,11 +124,11 @@ def test_delete_action_worker_wrong_collection(json_client, data, db_cur):
}
]

response = json_client.post(WRITE_ACTION_WORKER_URL, data)
response = json_client.post(WRITE_WITHOUT_EVENTS_URL, data)
assert_response_code(response, 400)
assert (
response.json["error"]["msg"]
== "Collection for write_action_worker must be action_worker"
== "Collection for write_without_events must be action_worker or import_preview"
)


Expand Down Expand Up @@ -156,7 +157,7 @@ def test_delete_action_worker_with_2_events(json_client, data, db_cur):
}
]

response = json_client.post(WRITE_ACTION_WORKER_URL, data)
response = json_client.post(WRITE_WITHOUT_EVENTS_URL, data)
assert_response_code(response, 200)
db_cur.execute(
"select fqid from models where fqid in ('action_worker/1', 'action_worker/2')"
Expand Down
6 changes: 3 additions & 3 deletions tests/writer/system/write/test_invalid_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from datastore.shared.flask_frontend import ERROR_CODES
from datastore.shared.util import META_FIELD_PREFIX
from datastore.writer.flask_frontend.routes import WRITE_ACTION_WORKER_URL, WRITE_URL
from datastore.writer.flask_frontend.routes import WRITE_URL, WRITE_WITHOUT_EVENTS_URL
from tests.util import assert_error_response
from tests.writer.system.util import assert_no_db_entry

Expand Down Expand Up @@ -38,8 +38,8 @@ def test_no_json_write(client):
assert_error_response(response, ERROR_CODES.INVALID_REQUEST)


def test_no_json_write_action_worker(client):
response = client.post(WRITE_ACTION_WORKER_URL, data={"some": "data"})
def test_no_json_write_without_events(client):
response = client.post(WRITE_WITHOUT_EVENTS_URL, data={"some": "data"})
assert response.is_json
assert_error_response(response, ERROR_CODES.INVALID_REQUEST)

Expand Down

0 comments on commit 11b5874

Please sign in to comment.