-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathIndexTests.php
More file actions
647 lines (542 loc) · 26.5 KB
/
Copy pathIndexTests.php
File metadata and controls
647 lines (542 loc) · 26.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
<?php
namespace Tests\E2E\Adapter\Scopes;
use Exception;
use Throwable;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\Limit as LimitException;
use Utopia\Database\Exception\Query as QueryException;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role;
use Utopia\Database\Query;
use Utopia\Database\Validator\Index;
trait IndexTests
{
public function testCreateIndex(): void
{
$database = $this->getDatabase();
$database->createCollection('indexes');
/**
* Check ticks sounding cast index for reserved words
*/
$database->createAttribute('indexes', 'int', Database::VAR_INTEGER, 8, false, array:true);
if ($database->getAdapter()->getSupportForIndexArray()) {
$database->createIndex('indexes', 'indx8711', Database::INDEX_KEY, ['int'], [255]);
}
$database->createAttribute('indexes', 'name', Database::VAR_STRING, 10, false);
$database->createIndex('indexes', 'index_1', Database::INDEX_KEY, ['name']);
try {
$database->createIndex('indexes', 'index3', Database::INDEX_KEY, ['$id', '$id']);
} catch (Throwable $e) {
self::assertTrue($e instanceof DatabaseException);
self::assertEquals($e->getMessage(), 'Duplicate attributes provided');
}
try {
$database->createIndex('indexes', 'index4', Database::INDEX_KEY, ['name', 'Name']);
} catch (Throwable $e) {
self::assertTrue($e instanceof DatabaseException);
self::assertEquals($e->getMessage(), 'Duplicate attributes provided');
}
$database->deleteCollection('indexes');
}
public function testCreateDeleteIndex(): void
{
/** @var Database $database */
$database = static::getDatabase();
$database->createCollection('indexes');
$this->assertEquals(true, $database->createAttribute('indexes', 'string', Database::VAR_STRING, 128, true));
$this->assertEquals(true, $database->createAttribute('indexes', 'order', Database::VAR_STRING, 128, true));
$this->assertEquals(true, $database->createAttribute('indexes', 'integer', Database::VAR_INTEGER, 0, true));
$this->assertEquals(true, $database->createAttribute('indexes', 'float', Database::VAR_FLOAT, 0, true));
$this->assertEquals(true, $database->createAttribute('indexes', 'boolean', Database::VAR_BOOLEAN, 0, true));
// Indexes
$this->assertEquals(true, $database->createIndex('indexes', 'index1', Database::INDEX_KEY, ['string', 'integer'], [128], [Database::ORDER_ASC]));
$this->assertEquals(true, $database->createIndex('indexes', 'index2', Database::INDEX_KEY, ['float', 'integer'], [], [Database::ORDER_ASC, Database::ORDER_DESC]));
$this->assertEquals(true, $database->createIndex('indexes', 'index3', Database::INDEX_KEY, ['integer', 'boolean'], [], [Database::ORDER_ASC, Database::ORDER_DESC, Database::ORDER_DESC]));
$this->assertEquals(true, $database->createIndex('indexes', 'index4', Database::INDEX_UNIQUE, ['string'], [128], [Database::ORDER_ASC]));
$this->assertEquals(true, $database->createIndex('indexes', 'index5', Database::INDEX_UNIQUE, ['$id', 'string'], [128], [Database::ORDER_ASC]));
$this->assertEquals(true, $database->createIndex('indexes', 'order', Database::INDEX_UNIQUE, ['order'], [128], [Database::ORDER_ASC]));
$collection = $database->getCollection('indexes');
$this->assertCount(6, $collection->getAttribute('indexes'));
// Delete Indexes
$this->assertEquals(true, $database->deleteIndex('indexes', 'index1'));
$this->assertEquals(true, $database->deleteIndex('indexes', 'index2'));
$this->assertEquals(true, $database->deleteIndex('indexes', 'index3'));
$this->assertEquals(true, $database->deleteIndex('indexes', 'index4'));
$this->assertEquals(true, $database->deleteIndex('indexes', 'index5'));
$this->assertEquals(true, $database->deleteIndex('indexes', 'order'));
$collection = $database->getCollection('indexes');
$this->assertCount(0, $collection->getAttribute('indexes'));
// Test non-shared tables duplicates throw duplicate
$database->createIndex('indexes', 'duplicate', Database::INDEX_KEY, ['string', 'boolean'], [128], [Database::ORDER_ASC]);
try {
$database->createIndex('indexes', 'duplicate', Database::INDEX_KEY, ['string', 'boolean'], [128], [Database::ORDER_ASC]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(DuplicateException::class, $e);
}
// Test delete index when index does not exist
$this->assertEquals(true, $database->createIndex('indexes', 'index1', Database::INDEX_KEY, ['string', 'integer'], [128], [Database::ORDER_ASC]));
$this->assertEquals(true, static::deleteIndex('indexes', 'index1'));
$this->assertEquals(true, $database->deleteIndex('indexes', 'index1'));
// Test delete index when attribute does not exist
$this->assertEquals(true, $database->createIndex('indexes', 'index1', Database::INDEX_KEY, ['string', 'integer'], [128], [Database::ORDER_ASC]));
$this->assertEquals(true, $database->deleteAttribute('indexes', 'string'));
$this->assertEquals(true, $database->deleteIndex('indexes', 'index1'));
$database->deleteCollection('indexes');
}
/**
* @throws Exception|Throwable
*/
public function testIndexValidation(): void
{
$attributes = [
new Document([
'$id' => ID::custom('title1'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => 700,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
]),
new Document([
'$id' => ID::custom('title2'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => 500,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
]),
];
$indexes = [
new Document([
'$id' => ID::custom('index1'),
'type' => Database::INDEX_KEY,
'attributes' => ['title1', 'title2'],
'lengths' => [701,50],
'orders' => [],
]),
];
$collection = new Document([
'$id' => ID::custom('index_length'),
'name' => 'test',
'attributes' => $attributes,
'indexes' => $indexes
]);
/** @var Database $database */
$database = static::getDatabase();
$validator = new Index(
$attributes,
$indexes,
$database->getAdapter()->getMaxIndexLength(),
$database->getAdapter()->getInternalIndexesKeys(),
$database->getAdapter()->getSupportForIndexArray(),
$database->getAdapter()->getSupportForSpatialAttributes(),
$database->getAdapter()->getSupportForSpatialIndexNull(),
$database->getAdapter()->getSupportForSpatialIndexOrder(),
$database->getAdapter()->getSupportForAttributes(),
$database->getAdapter()->getSupportForMultipleFulltextIndexes(),
$database->getAdapter()->getSupportForIdenticalIndexes()
);
if ($database->getAdapter()->getSupportForIdenticalIndexes()) {
$errorMessage = 'Index length 701 is larger than the size for title1: 700"';
$this->assertFalse($validator->isValid($indexes[0]));
$this->assertEquals($errorMessage, $validator->getDescription());
try {
$database->createCollection($collection->getId(), $attributes, $indexes, [
Permission::read(Role::any()),
Permission::create(Role::any()),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertEquals($errorMessage, $e->getMessage());
}
}
$indexes = [
new Document([
'$id' => ID::custom('index1'),
'type' => Database::INDEX_KEY,
'attributes' => ['title1', 'title2'],
'lengths' => [700], // 700, 500 (length(title2))
'orders' => [],
]),
];
$collection->setAttribute('indexes', $indexes);
if ($database->getAdapter()->getSupportForAttributes() && $database->getAdapter()->getMaxIndexLength() > 0) {
$errorMessage = 'Index length is longer than the maximum: ' . $database->getAdapter()->getMaxIndexLength();
$this->assertFalse($validator->isValid($indexes[0]));
$this->assertEquals($errorMessage, $validator->getDescription());
try {
$database->createCollection($collection->getId(), $attributes, $indexes);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertEquals($errorMessage, $e->getMessage());
}
}
$attributes[] = new Document([
'$id' => ID::custom('integer'),
'type' => Database::VAR_INTEGER,
'format' => '',
'size' => 10000,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
]);
$indexes = [
new Document([
'$id' => ID::custom('index1'),
'type' => Database::INDEX_FULLTEXT,
'attributes' => ['title1', 'integer'],
'lengths' => [],
'orders' => [],
]),
];
$collection = new Document([
'$id' => ID::custom('index_length'),
'name' => 'test',
'attributes' => $attributes,
'indexes' => $indexes
]);
// not using $indexes[0] as the index validator skips indexes with same id
$newIndex = new Document([
'$id' => ID::custom('newIndex1'),
'type' => Database::INDEX_FULLTEXT,
'attributes' => ['title1', 'integer'],
'lengths' => [],
'orders' => [],
]);
$validator = new Index(
$attributes,
$indexes,
$database->getAdapter()->getMaxIndexLength(),
$database->getAdapter()->getInternalIndexesKeys(),
$database->getAdapter()->getSupportForIndexArray(),
$database->getAdapter()->getSupportForSpatialAttributes(),
$database->getAdapter()->getSupportForSpatialIndexNull(),
$database->getAdapter()->getSupportForSpatialIndexOrder(),
$database->getAdapter()->getSupportForAttributes(),
$database->getAdapter()->getSupportForMultipleFulltextIndexes(),
$database->getAdapter()->getSupportForIdenticalIndexes()
);
$this->assertFalse($validator->isValid($newIndex));
if (!$database->getAdapter()->getSupportForMultipleFulltextIndexes()) {
$this->assertEquals('There is already a fulltext index in the collection', $validator->getDescription());
} elseif ($database->getAdapter()->getSupportForAttributes()) {
$this->assertEquals('Attribute "integer" cannot be part of a fulltext index, must be of type string', $validator->getDescription());
}
try {
$database->createCollection($collection->getId(), $attributes, $indexes);
if ($database->getAdapter()->getSupportForAttributes()) {
$this->fail('Failed to throw exception');
}
} catch (Exception $e) {
$this->assertEquals('Attribute "integer" cannot be part of a fulltext index, must be of type string', $e->getMessage());
}
$indexes = [
new Document([
'$id' => ID::custom('index_negative_length'),
'type' => Database::INDEX_KEY,
'attributes' => ['title1'],
'lengths' => [-1],
'orders' => [],
]),
];
if ($database->getAdapter()->getSupportForAttributes()) {
$errorMessage = 'Negative index length provided for title1';
$this->assertFalse($validator->isValid($indexes[0]));
$this->assertEquals($errorMessage, $validator->getDescription());
try {
$database->createCollection(ID::unique(), $attributes, $indexes);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertEquals($errorMessage, $e->getMessage());
}
$indexes = [
new Document([
'$id' => ID::custom('index_extra_lengths'),
'type' => Database::INDEX_KEY,
'attributes' => ['title1', 'title2'],
'lengths' => [100, 100, 100],
'orders' => [],
]),
];
$errorMessage = 'Invalid index lengths. Count of lengths must be equal or less than the number of attributes.';
$this->assertFalse($validator->isValid($indexes[0]));
$this->assertEquals($errorMessage, $validator->getDescription());
try {
$database->createCollection(ID::unique(), $attributes, $indexes);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertEquals($errorMessage, $e->getMessage());
}
}
}
public function testIndexLengthZero(): void
{
/** @var Database $database */
$database = static::getDatabase();
if (!$database->getAdapter()->getSupportForAttributes()) {
$this->expectNotToPerformAssertions();
return;
}
$database->createCollection(__FUNCTION__);
$database->createAttribute(__FUNCTION__, 'title1', Database::VAR_STRING, $database->getAdapter()->getMaxIndexLength() + 300, true);
try {
$database->createIndex(__FUNCTION__, 'index_title1', Database::INDEX_KEY, ['title1'], [0]);
$this->fail('Failed to throw exception');
} catch (Throwable $e) {
$this->assertEquals('Index length is longer than the maximum: '.$database->getAdapter()->getMaxIndexLength(), $e->getMessage());
}
$database->createAttribute(__FUNCTION__, 'title2', Database::VAR_STRING, 100, true);
$database->createIndex(__FUNCTION__, 'index_title2', Database::INDEX_KEY, ['title2'], [0]);
try {
$database->updateAttribute(__FUNCTION__, 'title2', Database::VAR_STRING, $database->getAdapter()->getMaxIndexLength() + 300, true);
$this->fail('Failed to throw exception');
} catch (Throwable $e) {
$this->assertEquals('Index length is longer than the maximum: '.$database->getAdapter()->getMaxIndexLength(), $e->getMessage());
}
}
public function testRenameIndex(): void
{
$database = static::getDatabase();
$numbers = $database->createCollection('numbers');
$database->createAttribute('numbers', 'verbose', Database::VAR_STRING, 128, true);
$database->createAttribute('numbers', 'symbol', Database::VAR_INTEGER, 0, true);
$database->createIndex('numbers', 'index1', Database::INDEX_KEY, ['verbose'], [128], [Database::ORDER_ASC]);
$database->createIndex('numbers', 'index2', Database::INDEX_KEY, ['symbol'], [0], [Database::ORDER_ASC]);
$index = $database->renameIndex('numbers', 'index1', 'index3');
$this->assertTrue($index);
$numbers = $database->getCollection('numbers');
$this->assertEquals('index2', $numbers->getAttribute('indexes')[1]['$id']);
$this->assertEquals('index3', $numbers->getAttribute('indexes')[0]['$id']);
$this->assertCount(2, $numbers->getAttribute('indexes'));
}
/**
* @depends testRenameIndex
* @expectedException Exception
*/
public function testRenameIndexMissing(): void
{
$database = static::getDatabase();
$this->expectExceptionMessage('Index not found');
$index = $database->renameIndex('numbers', 'index1', 'index4');
}
/**
* @depends testRenameIndex
* @expectedException Exception
*/
public function testRenameIndexExisting(): void
{
$database = static::getDatabase();
$this->expectExceptionMessage('Index name already used');
$index = $database->renameIndex('numbers', 'index3', 'index2');
}
public function testExceptionIndexLimit(): void
{
/** @var Database $database */
$database = static::getDatabase();
$database->createCollection('indexLimit');
// add unique attributes for indexing
for ($i = 0; $i < 64; $i++) {
$this->assertEquals(true, $database->createAttribute('indexLimit', "test{$i}", Database::VAR_STRING, 16, true));
}
// Testing for indexLimit
// Add up to the limit, then check if the next index throws IndexLimitException
for ($i = 0; $i < ($this->getDatabase()->getLimitForIndexes()); $i++) {
$this->assertEquals(true, $database->createIndex('indexLimit', "index{$i}", Database::INDEX_KEY, ["test{$i}"], [16]));
}
$this->expectException(LimitException::class);
$this->assertEquals(false, $database->createIndex('indexLimit', "index64", Database::INDEX_KEY, ["test64"], [16]));
$database->deleteCollection('indexLimit');
}
public function testListDocumentSearch(): void
{
$fulltextSupport = $this->getDatabase()->getAdapter()->getSupportForFulltextIndex();
if (!$fulltextSupport) {
$this->expectNotToPerformAssertions();
return;
}
/** @var Database $database */
$database = static::getDatabase();
$database->createIndex('documents', 'string', Database::INDEX_FULLTEXT, ['string']);
$database->createDocument('documents', new Document([
'$permissions' => [
Permission::read(Role::any()),
Permission::create(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
'string' => '*test+alias@email-provider.com',
'integer_signed' => 0,
'integer_unsigned' => 0,
'bigint_signed' => 0,
'bigint_unsigned' => 0,
'float_signed' => -5.55,
'float_unsigned' => 5.55,
'boolean' => true,
'colors' => ['pink', 'green', 'blue'],
'empty' => [],
]));
/**
* Allow reserved keywords for search
*/
$documents = $database->find('documents', [
Query::search('string', '*test+alias@email-provider.com'),
]);
$this->assertEquals(1, count($documents));
}
public function testMaxQueriesValues(): void
{
/** @var Database $database */
$database = static::getDatabase();
$max = $database->getMaxQueryValues();
$database->setMaxQueryValues(5);
try {
$database->find(
'documents',
[Query::equal('$id', [1, 2, 3, 4, 5, 6])]
);
$this->fail('Failed to throw exception');
} catch (Throwable $e) {
$this->assertTrue($e instanceof QueryException);
$this->assertEquals('Invalid query: Query on attribute has greater than 5 values: $id', $e->getMessage());
}
$database->setMaxQueryValues($max);
}
public function testEmptySearch(): void
{
$fulltextSupport = $this->getDatabase()->getAdapter()->getSupportForFulltextIndex();
if (!$fulltextSupport) {
$this->expectNotToPerformAssertions();
return;
}
/** @var Database $database */
$database = static::getDatabase();
$documents = $database->find('documents', [
Query::search('string', ''),
]);
$this->assertEquals(0, count($documents));
$documents = $database->find('documents', [
Query::search('string', '*'),
]);
$this->assertEquals(0, count($documents));
$documents = $database->find('documents', [
Query::search('string', '<>'),
]);
$this->assertEquals(0, count($documents));
}
public function testMultipleFulltextIndexValidation(): void
{
$fulltextSupport = $this->getDatabase()->getAdapter()->getSupportForFulltextIndex();
if (!$fulltextSupport) {
$this->expectNotToPerformAssertions();
return;
}
/** @var Database $database */
$database = static::getDatabase();
$collectionId = 'multiple_fulltext_test';
try {
$database->createCollection($collectionId);
$database->createAttribute($collectionId, 'title', Database::VAR_STRING, 256, false);
$database->createAttribute($collectionId, 'content', Database::VAR_STRING, 256, false);
$database->createIndex($collectionId, 'fulltext_title', Database::INDEX_FULLTEXT, ['title']);
$supportsMultipleFulltext = $database->getAdapter()->getSupportForMultipleFulltextIndexes();
// Try to add second fulltext index
try {
$database->createIndex($collectionId, 'fulltext_content', Database::INDEX_FULLTEXT, ['content']);
if ($supportsMultipleFulltext) {
$this->assertTrue(true, 'Multiple fulltext indexes are supported and second index was created successfully');
} else {
$this->fail('Expected exception when creating second fulltext index, but none was thrown');
}
} catch (Throwable $e) {
if (!$supportsMultipleFulltext) {
$this->assertTrue(true, 'Multiple fulltext indexes are not supported and exception was thrown as expected');
} else {
$this->fail('Unexpected exception when creating second fulltext index: ' . $e->getMessage());
}
}
} finally {
// Clean up
$database->deleteCollection($collectionId);
}
}
public function testIdenticalIndexValidation(): void
{
/** @var Database $database */
$database = static::getDatabase();
$collectionId = 'identical_index_test';
try {
$database->createCollection($collectionId);
$database->createAttribute($collectionId, 'name', Database::VAR_STRING, 256, false);
$database->createAttribute($collectionId, 'age', Database::VAR_INTEGER, 8, false);
$database->createIndex($collectionId, 'index1', Database::INDEX_KEY, ['name', 'age'], [], [Database::ORDER_ASC, Database::ORDER_DESC]);
$supportsIdenticalIndexes = $database->getAdapter()->getSupportForIdenticalIndexes();
// Try to add identical index (failure)
try {
$database->createIndex($collectionId, 'index2', Database::INDEX_KEY, ['name', 'age'], [], [Database::ORDER_ASC, Database::ORDER_DESC]);
if ($supportsIdenticalIndexes) {
$this->assertTrue(true, 'Identical indexes are supported and second index was created successfully');
} else {
$this->fail('Expected exception but got none');
}
} catch (Throwable $e) {
if (!$supportsIdenticalIndexes) {
$this->assertTrue(true, 'Identical indexes are not supported and exception was thrown as expected');
} else {
$this->fail('Unexpected exception when creating identical index: ' . $e->getMessage());
}
}
// Test with different attributes order - faliure
try {
$database->createIndex($collectionId, 'index3', Database::INDEX_KEY, ['age', 'name'], [], [ Database::ORDER_ASC, Database::ORDER_DESC]);
$this->assertTrue(true, 'Index with different attributes was created successfully');
} catch (Throwable $e) {
if (!$supportsIdenticalIndexes) {
$this->assertTrue(true, 'Identical indexes are not supported and exception was thrown as expected');
} else {
$this->fail('Unexpected exception when creating identical index: ' . $e->getMessage());
}
}
// Test with different orders order - faliure
try {
$database->createIndex($collectionId, 'index4', Database::INDEX_KEY, ['age', 'name'], [], [ Database::ORDER_DESC, Database::ORDER_ASC]);
$this->assertTrue(true, 'Index with different attributes was created successfully');
} catch (Throwable $e) {
if (!$supportsIdenticalIndexes) {
$this->assertTrue(true, 'Identical indexes are not supported and exception was thrown as expected');
} else {
$this->fail('Unexpected exception when creating identical index: ' . $e->getMessage());
}
}
// Test with different attributes - success
try {
$database->createIndex($collectionId, 'index5', Database::INDEX_KEY, ['name'], [], [Database::ORDER_ASC]);
$this->assertTrue(true, 'Index with different attributes was created successfully');
} catch (Throwable $e) {
$this->fail('Unexpected exception when creating index with different attributes: ' . $e->getMessage());
}
// Test with different orders - success
try {
$database->createIndex($collectionId, 'index6', Database::INDEX_KEY, ['name', 'age'], [], [Database::ORDER_ASC]);
$this->assertTrue(true, 'Index with different orders was created successfully');
} catch (Throwable $e) {
$this->fail('Unexpected exception when creating index with different orders: ' . $e->getMessage());
}
} finally {
// Clean up
$database->deleteCollection($collectionId);
}
}
}