|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit\Indexes; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Utopia\Database\Attribute; |
| 7 | +use Utopia\Database\Collection; |
| 8 | +use Utopia\Database\Document; |
| 9 | +use Utopia\Database\Exception\Index as IndexException; |
| 10 | +use Utopia\Database\Index; |
| 11 | +use Utopia\Query\OrderDirection; |
| 12 | + |
| 13 | +final class LegacyDroppedAttributeIndexTest extends TestCase |
| 14 | +{ |
| 15 | + public function testAFulltextIndexWhoseOnlyAttributeWasDeletedIsReadWithNoAttributes(): void |
| 16 | + { |
| 17 | + $index = Index::fromDocument(new Document(['$id' => 'category', 'key' => 'category', 'type' => 'fulltext', 'attributes' => [null], 'orders' => ['ASC'], 'lengths' => [200]])); |
| 18 | + |
| 19 | + $this->assertSame('category', $index->key); |
| 20 | + $this->assertSame([], $index->attributes); |
| 21 | + $this->assertSame([], $index->lengths); |
| 22 | + $this->assertSame([], $index->orders); |
| 23 | + } |
| 24 | + |
| 25 | + public function testAUniqueIndexKeepsTheAttributesItStillCoversWithTheirLengthsAndOrders(): void |
| 26 | + { |
| 27 | + $index = Index::fromDocument(new Document(['$id' => 'index_2', 'key' => 'index_2', 'type' => 'unique', 'attributes' => ['playId', null], 'orders' => ['ASC', 'ASC'], 'lengths' => [null, null]])); |
| 28 | + |
| 29 | + $this->assertSame(['playId'], $index->attributes); |
| 30 | + $this->assertSame([null], $index->lengths); |
| 31 | + $this->assertSame([OrderDirection::Asc], $index->orders); |
| 32 | + } |
| 33 | + |
| 34 | + public function testADroppedAttributeBetweenOthersKeepsEachLengthAndOrderAtItsAttribute(): void |
| 35 | + { |
| 36 | + $index = Index::fromDocument(new Document(['$id' => 'mixed', 'key' => 'mixed', 'type' => 'key', 'attributes' => ['a', null, 'c'], 'orders' => ['ASC', 'DESC', 'DESC'], 'lengths' => [10, 20, 30]])); |
| 37 | + |
| 38 | + $this->assertSame(['a', 'c'], $index->attributes); |
| 39 | + $this->assertSame([10, 30], $index->lengths); |
| 40 | + $this->assertSame([OrderDirection::Asc, OrderDirection::Desc], $index->orders); |
| 41 | + } |
| 42 | + |
| 43 | + public function testACollectionHoldingSuchAnIndexIsRead(): void |
| 44 | + { |
| 45 | + $collection = Collection::fromDocument(new Document([ |
| 46 | + '$id' => 'plays', |
| 47 | + 'name' => 'plays', |
| 48 | + 'attributes' => [Attribute::string(key: 'playId', size: 36)->toDocument()], |
| 49 | + 'indexes' => [new Document(['$id' => 'index_2', 'key' => 'index_2', 'type' => 'unique', 'attributes' => ['playId', null], 'orders' => ['ASC', 'ASC'], 'lengths' => [null, null]])], |
| 50 | + ])); |
| 51 | + |
| 52 | + $this->assertSame(['playId'], $collection->indexes()[0]->attributes); |
| 53 | + } |
| 54 | + |
| 55 | + public function testAnIndexDefinedByACallerStillRefusesAnAttributeThatIsNotAString(): void |
| 56 | + { |
| 57 | + $this->expectException(IndexException::class); |
| 58 | + $this->expectExceptionMessage('Index attributes must be strings'); |
| 59 | + |
| 60 | + Index::fromArray(['$id' => 'bad', 'type' => 'key', 'attributes' => ['playId', null]]); |
| 61 | + } |
| 62 | +} |
0 commit comments