From a840a1b7204fb9b1098d84f65f768b02b41fc486 Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 31 Aug 2023 13:23:26 +0200 Subject: [PATCH] fixes #65 issue when expanding list of multiple relations (#66) --- pocketbase/models/record.py | 2 ++ tests/integration/test_record.py | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/pocketbase/models/record.py b/pocketbase/models/record.py index 78ba51b..acc965f 100644 --- a/pocketbase/models/record.py +++ b/pocketbase/models/record.py @@ -19,6 +19,8 @@ def load(self, data: dict) -> None: @classmethod def parse_expanded(cls, data: dict): + if isinstance(data, list): + return [cls(a) for a in data] return cls(data) def load_expanded(self) -> None: diff --git a/tests/integration/test_record.py b/tests/integration/test_record.py index 7168160..f70d3dc 100644 --- a/tests/integration/test_record.py +++ b/tests/integration/test_record.py @@ -34,6 +34,18 @@ def test_init_collection(self, client: PocketBase, state): }, }, ) + schema.append( + { + "name": "multirel", + "type": "relation", + "required": False, + "options": { + "collectionId": state.coll.id, + "cascadeDelete": False, + "maxSelect": 5, + }, + }, + ) state.coll = srv.update(state.coll.id, {"schema": schema}) def test_create_record(self, client: PocketBase, state): @@ -61,6 +73,22 @@ def test_create_multiple_record(self, client: PocketBase, state): .id ) + def test_create_multi_relation_record(self, client: PocketBase, state): + state.chained_multi_records = [ + state.record.id, + ] + for _ in range(4): + state.chained_multi_records.append( + client.collection(state.coll.id) + .create( + { + "title": uuid4().hex, + "multirel": state.chained_multi_records[:], + } + ) + .id + ) + def test_get_record(self, client: PocketBase, state): state.get_record = client.collection(state.coll.id).get_one(state.record.id) assert state.get_record.title is not None @@ -79,6 +107,18 @@ def test_get_record_expand(self, client: PocketBase, state): break rel = rel.expand["rel"] + def test_get_record_expand_multirel(self, client: PocketBase, state): + rel = client.collection(state.coll.id).get_one( + state.chained_multi_records[-1], + {"expand": "multirel.multirel.multirel.multirel"}, + ) + for i, r in enumerate(reversed(state.chained_multi_records)): + assert rel.id == r + if i >= 4: + break + assert len(rel.expand["multirel"]) == 4 - i + rel = rel.expand["multirel"][-1] + def test_get_record_expand_full_list(self, client: PocketBase, state): rel = client.collection(state.coll.id).get_full_list( query_params={"expand": "rel.rel"}