Skip to content

Commit 6a197b5

Browse files
added tests
1 parent 3bc1774 commit 6a197b5

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

src/Database/Validator/Index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public function checkIndexLength(Document $index): bool
199199
break;
200200
}
201201
if ($indexLength < 0) {
202-
$this->message = 'Negative index provided for '.$attribute->getAttribute('key');
202+
$this->message = 'Negative index provided for '.$attributeName;
203203
return false;
204204
}
205205

tests/e2e/Adapter/Scopes/IndexTests.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,49 @@ public function testIndexValidation(): void
243243
} catch (Exception $e) {
244244
$this->assertEquals($errorMessage, $e->getMessage());
245245
}
246+
247+
248+
$indexes = [
249+
new Document([
250+
'$id' => ID::custom('index_negative_length'),
251+
'type' => Database::INDEX_KEY,
252+
'attributes' => ['title1'],
253+
'lengths' => [-1],
254+
'orders' => [],
255+
]),
256+
];
257+
258+
$errorMessage = 'Negative index provided for title1';
259+
$this->assertFalse($validator->isValid($indexes[0]));
260+
$this->assertEquals($errorMessage, $validator->getDescription());
261+
262+
try {
263+
static::getDatabase()->createCollection(ID::unique(), $attributes, $indexes);
264+
$this->fail('Failed to throw exception');
265+
} catch (Exception $e) {
266+
$this->assertEquals($errorMessage, $e->getMessage());
267+
}
268+
269+
$indexes = [
270+
new Document([
271+
'$id' => ID::custom('index_extra_lengths'),
272+
'type' => Database::INDEX_KEY,
273+
'attributes' => ['title1', 'title2'],
274+
'lengths' => [100, 100, 100],
275+
'orders' => [],
276+
]),
277+
];
278+
279+
$errorMessage = 'Invalid index lengths. Count of lengths must be equal or less than the number of attributes.';
280+
$this->assertFalse($validator->isValid($indexes[0]));
281+
$this->assertEquals($errorMessage, $validator->getDescription());
282+
283+
try {
284+
static::getDatabase()->createCollection(ID::unique(), $attributes, $indexes);
285+
$this->fail('Failed to throw exception');
286+
} catch (Exception $e) {
287+
$this->assertEquals($errorMessage, $e->getMessage());
288+
}
246289
}
247290

248291
public function testRenameIndex(): void

0 commit comments

Comments
 (0)