Skip to content

Commit

Permalink
fixes #65 issue when expanding list of multiple relations (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
m29h authored Aug 31, 2023
1 parent 4fc9b3f commit a840a1b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pocketbase/models/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
40 changes: 40 additions & 0 deletions tests/integration/test_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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"}
Expand Down

0 comments on commit a840a1b

Please sign in to comment.