Skip to content

Commit

Permalink
Changed model format (#150)
Browse files Browse the repository at this point in the history
Use the double-dict format for model im-/export, create_initial_data,
get_everything request.
  • Loading branch information
FinnStutzenstein committed Aug 31, 2021
1 parent 2523306 commit 365ceb5
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 21 deletions.
4 changes: 2 additions & 2 deletions cli/create_initial_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def main():
migration_index = models
continue

for model in models:
fqid = fqid_from_collection_and_id(collection, model["id"])
for id, model in models.items():
fqid = fqid_from_collection_and_id(collection, id)
event = RequestCreateEvent(fqid, model)
events.append(event)

Expand Down
2 changes: 1 addition & 1 deletion cli/export_data_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def main():

# strip meta fields
for collection, models in response.items():
for model in models:
for model in models.values():
for field in list(model.keys()):
if is_reserved_field(field):
del model[field]
Expand Down
4 changes: 2 additions & 2 deletions cli/import_data_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def main():
migration_index = models
continue

for model in models:
fqid = fqid_from_collection_and_id(collection, model["id"])
for id, model in models.items():
fqid = fqid_from_collection_and_id(collection, id)
event = RequestCreateEvent(fqid, model)
events.append(event)

Expand Down
2 changes: 1 addition & 1 deletion datastore/reader/core/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def get_all(self, request: GetAllRequest) -> Dict[Id, Model]:

def get_everything(
self, request: GetEverythingRequest
) -> Dict[Collection, List[Model]]:
) -> Dict[Collection, Dict[Id, Model]]:
"""
Returns all models In the form of the example data: Collections mapped to
lists of models.
Expand Down
2 changes: 1 addition & 1 deletion datastore/reader/core/reader_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def get_all(self, request: GetAllRequest) -> Dict[Id, Model]:
@retry_on_db_failure
def get_everything(
self, request: GetEverythingRequest
) -> Dict[Collection, List[Model]]:
) -> Dict[Collection, Dict[Id, Model]]:
return self.database.get_everything(request.get_deleted_models)

@retry_on_db_failure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,26 +112,22 @@ def get_all(
def get_everything(
self,
get_deleted_models: DeletedModelsBehaviour = DeletedModelsBehaviour.NO_DELETED,
) -> Dict[Collection, List[Model]]:
) -> Dict[Collection, Dict[Id, Model]]:
del_cond = self.query_helper.get_deleted_condition(
get_deleted_models, prepend_and=False
)
query = f"""
select fqid as __fqid__, data from models
{"where " + del_cond if del_cond else ""}"""

result = self.connection.query(query, [], [])
unsorted_data = defaultdict(list)

data: Dict[Collection, Dict[Id, Model]] = defaultdict(dict)
for row in result:
collection, id = collection_and_id_from_fqid(row["__fqid__"])
model = row["data"]
model["id"] = id
unsorted_data[collection].append(model)
data[collection][id] = model

data = {}
for collection, models in unsorted_data.items():
data[collection] = sorted(models, key=lambda model: model["id"])
return data

def filter(
Expand Down
2 changes: 1 addition & 1 deletion datastore/shared/services/read_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def get_all(
def get_everything(
self,
get_deleted_models: DeletedModelsBehaviour = DeletedModelsBehaviour.NO_DELETED,
) -> Dict[Collection, List[Model]]:
) -> Dict[Collection, Dict[Id, Model]]:
"""
Returns all models of the given collection. WARNING: May result in a huge
amount of data. Use with caution!
Expand Down
10 changes: 5 additions & 5 deletions tests/reader/system/get_everything/test_get_everything.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def test_simple(json_client, db_connection, db_cur):
response = json_client.post(Route.GET_EVERYTHING.URL, {})
assert_success_response(response)
assert response.json == {
"a": [get_data_with_id("a/1")],
"b": [get_data_with_id("b/1")],
"a": {"1": get_data_with_id("a/1")},
"b": {"1": get_data_with_id("b/1")},
}


Expand All @@ -65,7 +65,7 @@ def test_only_deleted(json_client, db_connection, db_cur):
{"get_deleted_models": DeletedModelsBehaviour.ONLY_DELETED},
)
assert_success_response(response)
assert response.json == {"a": [get_data_with_id("a/2")]}
assert response.json == {"a": {"2": get_data_with_id("a/2")}}


def test_deleted_all_models(json_client, db_connection, db_cur):
Expand All @@ -76,8 +76,8 @@ def test_deleted_all_models(json_client, db_connection, db_cur):
)
assert_success_response(response)
assert response.json == {
"a": [get_data_with_id("a/1"), get_data_with_id("a/2")],
"b": [get_data_with_id("b/1")],
"a": {"1": get_data_with_id("a/1"), "2": get_data_with_id("a/2")},
"b": {"1": get_data_with_id("b/1")},
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_get_everything(read_database: ReadDatabase, connection: ConnectionHandl
)

f.assert_called()
assert models == {"a": [{"id": 1}, {"id": 2}]}
assert models == {"a": {1: {"id": 1}, 2: {"id": 2}}}


def test_filter(read_database: ReadDatabase):
Expand Down

0 comments on commit 365ceb5

Please sign in to comment.