Skip to content

Commit d78413f

Browse files
authored
Merge pull request #655 from utopia-php/zoo-relations-tests
Nested relationship zoo tets
2 parents a8ab3a7 + 9514f56 commit d78413f

2 files changed

Lines changed: 187 additions & 18 deletions

File tree

src/Database/Database.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3324,9 +3324,15 @@ private function populateDocumentRelationships(Document $collection, Document $d
33243324
{
33253325
$attributes = $collection->getAttribute('attributes', []);
33263326

3327-
$relationships = \array_filter($attributes, function ($attribute) {
3328-
return $attribute['type'] === Database::VAR_RELATIONSHIP;
3329-
});
3327+
$relationships = [];
3328+
3329+
foreach ($attributes as $attribute) {
3330+
if ($attribute['type'] === Database::VAR_RELATIONSHIP) {
3331+
if (empty($selects) || array_key_exists($attribute['key'], $selects)) {
3332+
$relationships[] = $attribute;
3333+
}
3334+
}
3335+
}
33303336

33313337
foreach ($relationships as $relationship) {
33323338
$key = $relationship['key'];

tests/e2e/Adapter/Scopes/RelationshipTests.php

Lines changed: 178 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function testZoo(): void
9595
twoWayKey: 'animals'
9696
);
9797

98-
$zoo1 = $database->createDocument('zoo', new Document([
98+
$zoo = $database->createDocument('zoo', new Document([
9999
'$id' => 'zoo1',
100100
'$permissions' => [
101101
Permission::read(Role::any()),
@@ -104,7 +104,10 @@ public function testZoo(): void
104104
'name' => 'Bronx Zoo'
105105
]));
106106

107-
$animal1 = $database->createDocument('__animals', new Document([
107+
$this->assertEquals('zoo1', $zoo->getId());
108+
$this->assertArrayHasKey('animals', $zoo);
109+
110+
$iguana = $database->createDocument('__animals', new Document([
108111
'$id' => 'iguana',
109112
'$permissions' => [
110113
Permission::read(Role::any()),
@@ -121,10 +124,10 @@ public function testZoo(): void
121124
'enum' => 'maybe',
122125
'ip' => '127.0.0.1',
123126
'url' => 'https://appwrite.io/',
124-
'zoo' => $zoo1->getId(),
127+
'zoo' => $zoo->getId(),
125128
]));
126129

127-
$animal2 = $database->createDocument('__animals', new Document([
130+
$tiger = $database->createDocument('__animals', new Document([
128131
'$id' => 'tiger',
129132
'$permissions' => [
130133
Permission::read(Role::any()),
@@ -141,10 +144,10 @@ public function testZoo(): void
141144
'enum' => 'yes',
142145
'ip' => '255.0.0.1',
143146
'url' => 'https://appwrite.io/',
144-
'zoo' => $zoo1->getId(),
147+
'zoo' => $zoo->getId(),
145148
]));
146149

147-
$animal3 = $database->createDocument('__animals', new Document([
150+
$lama = $database->createDocument('__animals', new Document([
148151
'$id' => 'lama',
149152
'$permissions' => [
150153
Permission::read(Role::any()),
@@ -183,7 +186,7 @@ public function testZoo(): void
183186
'animals' => ['tiger'],
184187
]));
185188

186-
$president1 = $database->createDocument('presidents', new Document([
189+
$trump = $database->createDocument('presidents', new Document([
187190
'$id' => 'trump',
188191
'$permissions' => [
189192
Permission::read(Role::any()),
@@ -197,7 +200,7 @@ public function testZoo(): void
197200
],
198201
]));
199202

200-
$president2 = $database->createDocument('presidents', new Document([
203+
$bush = $database->createDocument('presidents', new Document([
201204
'$id' => 'bush',
202205
'$permissions' => [
203206
Permission::read(Role::any()),
@@ -208,7 +211,7 @@ public function testZoo(): void
208211
'animal' => 'iguana',
209212
]));
210213

211-
$president3 = $database->createDocument('presidents', new Document([
214+
$biden = $database->createDocument('presidents', new Document([
212215
'$id' => 'biden',
213216
'$permissions' => [
214217
Permission::read(Role::any()),
@@ -219,23 +222,183 @@ public function testZoo(): void
219222
'animal' => 'tiger',
220223
]));
221224

222-
var_dump('=== start === === start === === start === === start === === start === === start === === start === === start === === start ===');
225+
/**
226+
* Check Zoo data
227+
*/
228+
$zoo = $database->getDocument('zoo', 'zoo1');
229+
230+
$this->assertEquals('zoo1', $zoo->getId());
231+
$this->assertEquals('Bronx Zoo', $zoo->getAttribute('name'));
232+
$this->assertArrayHasKey('animals', $zoo);
233+
$this->assertEquals(2, count($zoo->getAttribute('animals')));
234+
$this->assertArrayHasKey('president', $zoo->getAttribute('animals')[0]);
235+
$this->assertArrayHasKey('veterinarian', $zoo->getAttribute('animals')[0]);
236+
237+
$zoo = $database->findOne('zoo');
238+
239+
$this->assertEquals('zoo1', $zoo->getId());
240+
$this->assertEquals('Bronx Zoo', $zoo->getAttribute('name'));
241+
$this->assertArrayHasKey('animals', $zoo);
242+
$this->assertEquals(2, count($zoo->getAttribute('animals')));
243+
$this->assertArrayHasKey('president', $zoo->getAttribute('animals')[0]);
244+
$this->assertArrayHasKey('veterinarian', $zoo->getAttribute('animals')[0]);
245+
246+
/**
247+
* Check Veterinarians data
248+
*/
249+
$veterinarian = $database->getDocument('veterinarians', 'dr.pol');
250+
251+
$this->assertEquals('dr.pol', $veterinarian->getId());
252+
$this->assertArrayHasKey('presidents', $veterinarian);
253+
$this->assertEquals(1, count($veterinarian->getAttribute('presidents')));
254+
$this->assertArrayHasKey('animal', $veterinarian->getAttribute('presidents')[0]);
255+
$this->assertArrayHasKey('animals', $veterinarian);
256+
$this->assertEquals(1, count($veterinarian->getAttribute('animals')));
257+
$this->assertArrayHasKey('zoo', $veterinarian->getAttribute('animals')[0]);
258+
$this->assertArrayHasKey('president', $veterinarian->getAttribute('animals')[0]);
259+
260+
$veterinarian = $database->findOne('veterinarians', [
261+
Query::equal('$id', ['dr.pol'])
262+
]);
263+
264+
$this->assertEquals('dr.pol', $veterinarian->getId());
265+
$this->assertArrayHasKey('presidents', $veterinarian);
266+
$this->assertEquals(1, count($veterinarian->getAttribute('presidents')));
267+
$this->assertArrayHasKey('animal', $veterinarian->getAttribute('presidents')[0]);
268+
$this->assertArrayHasKey('animals', $veterinarian);
269+
$this->assertEquals(1, count($veterinarian->getAttribute('animals')));
270+
$this->assertArrayHasKey('zoo', $veterinarian->getAttribute('animals')[0]);
271+
$this->assertArrayHasKey('president', $veterinarian->getAttribute('animals')[0]);
272+
273+
/**
274+
* Check Animals data
275+
*/
276+
$animal = $database->getDocument('__animals', 'iguana');
277+
278+
$this->assertEquals('iguana', $animal->getId());
279+
$this->assertArrayHasKey('zoo', $animal);
280+
$this->assertEquals('Bronx Zoo', $animal['zoo']->getAttribute('name'));
281+
$this->assertArrayHasKey('veterinarian', $animal);
282+
$this->assertEquals('dr.pol', $animal['veterinarian']->getId());
283+
$this->assertArrayHasKey('presidents', $animal['veterinarian']);
284+
$this->assertArrayHasKey('president', $animal);
285+
$this->assertEquals('bush', $animal['president']->getId());
286+
287+
$animal = $database->findOne('__animals', [
288+
Query::equal('$id', ['tiger'])
289+
]);
290+
291+
$this->assertEquals('tiger', $animal->getId());
292+
$this->assertArrayHasKey('zoo', $animal);
293+
$this->assertEquals('Bronx Zoo', $animal['zoo']->getAttribute('name'));
294+
$this->assertArrayHasKey('veterinarian', $animal);
295+
$this->assertEquals('dr.seuss', $animal['veterinarian']->getId());
296+
$this->assertArrayHasKey('presidents', $animal['veterinarian']);
297+
$this->assertArrayHasKey('president', $animal);
298+
$this->assertEquals('biden', $animal['president']->getId());
299+
300+
/**
301+
* Check President data
302+
*/
303+
$president = $database->getDocument('presidents', 'trump');
304+
305+
$this->assertEquals('trump', $president->getId());
306+
$this->assertArrayHasKey('animal', $president);
307+
$this->assertArrayHasKey('votes', $president);
308+
$this->assertEquals(2, count($president['votes']));
309+
310+
/**
311+
* Check President data
312+
*/
313+
$president = $database->findOne('presidents', [
314+
Query::equal('$id', ['bush'])
315+
]);
316+
317+
$this->assertEquals('bush', $president->getId());
318+
$this->assertArrayHasKey('animal', $president);
319+
$this->assertArrayHasKey('votes', $president);
320+
$this->assertEquals(0, count($president['votes']));
223321

224-
$docs = $database->find(
322+
$president = $database->findOne('presidents', [
323+
Query::select([
324+
'*',
325+
'votes.*',
326+
]),
327+
Query::equal('$id', ['trump'])
328+
]);
329+
330+
$this->assertEquals('trump', $president->getId());
331+
$this->assertArrayHasKey('votes', $president);
332+
$this->assertEquals(2, count($president['votes']));
333+
$this->assertArrayNotHasKey('animals', $president['votes'][0]); // Not exist
334+
335+
$president = $database->findOne('presidents', [
336+
Query::select([
337+
'*',
338+
'votes.*',
339+
'votes.animals.*',
340+
]),
341+
Query::equal('$id', ['trump'])
342+
]);
343+
344+
$this->assertEquals('trump', $president->getId());
345+
$this->assertArrayHasKey('votes', $president);
346+
$this->assertEquals(2, count($president['votes']));
347+
$this->assertArrayHasKey('animals', $president['votes'][0]); // Exist
348+
349+
/**
350+
* Check Selects queries
351+
*/
352+
$veterinarian = $database->findOne('veterinarians', [
353+
Query::select(['*']), // No resolving
354+
Query::equal('$id', ['dr.pol']),
355+
]);
356+
357+
$this->assertEquals('dr.pol', $veterinarian->getId());
358+
$this->assertArrayNotHasKey('presidents', $veterinarian);
359+
$this->assertArrayNotHasKey('animals', $veterinarian);
360+
361+
$veterinarian = $database->findOne(
362+
'veterinarians',
363+
[
364+
Query::select([
365+
'animals.*',
366+
])
367+
]
368+
);
369+
370+
$this->assertEquals('dr.pol', $veterinarian->getId());
371+
$this->assertArrayHasKey('animals', $veterinarian);
372+
$this->assertArrayNotHasKey('presidents', $veterinarian);
373+
374+
$animal = $veterinarian['animals'][0];
375+
376+
$this->assertArrayHasKey('president', $animal);
377+
$this->assertEquals('bush', $animal->getAttribute('president')); // Check president is a value
378+
$this->assertArrayHasKey('zoo', $animal);
379+
$this->assertEquals('zoo1', $animal->getAttribute('zoo')); // Check zoo is a value
380+
381+
$veterinarian = $database->findOne(
225382
'veterinarians',
226383
[
227384
Query::select([
228-
'*',
229385
'animals.*',
230386
'animals.zoo.*',
231-
//'animals.president.*',
387+
'animals.president.*',
232388
])
233389
]
234390
);
235391

236-
var_dump($docs);
392+
$this->assertEquals('dr.pol', $veterinarian->getId());
393+
$this->assertArrayHasKey('animals', $veterinarian);
394+
$this->assertArrayNotHasKey('presidents', $veterinarian);
395+
396+
$animal = $veterinarian['animals'][0];
237397

238-
//$this->assertEquals('shmuel', 'fogel');
398+
$this->assertArrayHasKey('president', $animal);
399+
$this->assertEquals('Bush', $animal->getAttribute('president')->getAttribute('last_name')); // Check president is an object
400+
$this->assertArrayHasKey('zoo', $animal);
401+
$this->assertEquals('Bronx Zoo', $animal->getAttribute('zoo')->getAttribute('name')); // Check zoo is an object
239402
}
240403

241404
public function testDeleteRelatedCollection(): void

0 commit comments

Comments
 (0)