-
Notifications
You must be signed in to change notification settings - Fork 55
Fix:one-way relationship twoWayKey handling in createRelationship #842
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1024,26 +1024,37 @@ public function testIdenticalTwoWayKeyRelationship(): void | |
| id: 'child1' | ||
| ); | ||
|
|
||
| $result = $database->createRelationship( | ||
| collection: 'parent', | ||
| relatedCollection: 'child', | ||
| type: Database::RELATION_ONE_TO_MANY, | ||
| id: 'children', | ||
| ); | ||
| $this->assertTrue($result); | ||
|
|
||
| $result = $database->createRelationship( | ||
| collection: 'parent', | ||
| relatedCollection: 'child', | ||
| type: Database::RELATION_ONE_TO_MANY, | ||
| id: 'childrenById', | ||
| twoWayKey: 'parent_id' | ||
| ); | ||
| $this->assertTrue($result); | ||
|
|
||
| try { | ||
| $database->createRelationship( | ||
| collection: 'parent', | ||
| relatedCollection: 'child', | ||
| type: Database::RELATION_ONE_TO_MANY, | ||
| id: 'children', | ||
| twoWay: true, | ||
| id: 'twoWayChildren', | ||
| twoWayKey: 'parent_id' | ||
| ); | ||
| $this->fail('Failed to throw Exception'); | ||
| } catch (Exception $e) { | ||
| $this->assertEquals('Related attribute already exists', $e->getMessage()); | ||
| } | ||
|
Comment on lines
1044
to
1056
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The More importantly, this test does not exercise the core guard: a genuine two-way vs two-way reverse-key collision, where the reverse attribute was actually created in the related collection. A stronger coverage would first create a |
||
|
|
||
| $database->createRelationship( | ||
| collection: 'parent', | ||
| relatedCollection: 'child', | ||
| type: Database::RELATION_ONE_TO_MANY, | ||
| id: 'children', | ||
| twoWayKey: 'parent_id' | ||
| ); | ||
|
|
||
| $collection = $database->getCollection('parent'); | ||
| $attributes = $collection->getAttribute('attributes', []); | ||
| foreach ($attributes as $attribute) { | ||
|
|
@@ -1052,35 +1063,14 @@ public function testIdenticalTwoWayKeyRelationship(): void | |
| } | ||
|
|
||
| if ($attribute['key'] === 'children') { | ||
| $this->assertEquals('parent', $attribute['options']['twoWayKey']); | ||
| } | ||
|
|
||
| if ($attribute['key'] === 'childrenById') { | ||
| $this->assertEquals('parent_id', $attribute['options']['twoWayKey']); | ||
| } | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| $database->createDocument('parent', new Document([ | ||
| '$permissions' => [ | ||
| Permission::read(Role::any()), | ||
| Permission::update(Role::any()), | ||
| Permission::delete(Role::any()), | ||
| ], | ||
| 'child1' => [ | ||
| '$id' => 'foo', | ||
| '$permissions' => [Permission::read(Role::any())], | ||
| ], | ||
| 'children' => [ | ||
| [ | ||
| '$id' => 'bar', | ||
| '$permissions' => [Permission::read(Role::any())], | ||
| ], | ||
| ], | ||
| ])); | ||
|
|
||
| $documents = $database->find('parent', []); | ||
| $document = array_pop($documents); | ||
| $this->assertArrayHasKey('child1', $document); | ||
| $this->assertEquals('foo', $document->getAttribute('child1')->getId()); | ||
| $this->assertArrayHasKey('children', $document); | ||
| $this->assertEquals('bar', $document->getAttribute('children')[0]->getId()); | ||
|
|
||
| try { | ||
| $database->updateRelationship( | ||
| collection: 'parent', | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.