Skip to content

Commit

Permalink
Minor fixes on 'Collection::addIndex()' and 'Collection::dropIndex()'…
Browse files Browse the repository at this point in the history
… methods
  • Loading branch information
lucassouzavieira committed Oct 25, 2019
1 parent 5714fab commit 1c865bf
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/Collection/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,11 @@ public function dropIndex(IndexInterface $index): bool
} catch (ClientException $exception) {
$response = json_decode((string)$exception->getResponse()->getBody(), true);
$databaseException = new DatabaseException($response['errorMessage'], $exception, $response['errorNum']);

if ($exception->getResponse()->getStatusCode() === 404) {
return false;
}

throw $databaseException;
}
}
Expand Down
233 changes: 226 additions & 7 deletions tests/Collection/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@
use ArangoDB\Document\Document;
use ArangoDB\Collection\Collection;
use GuzzleHttp\Handler\MockHandler;
use ArangoDB\Collection\Index\Index;
use ArangoDB\Cursor\CollectionCursor;
use ArangoDB\Collection\Index\TTLIndex;
use ArangoDB\Collection\Index\HashIndex;
use ArangoDB\Exceptions\DatabaseException;
use ArangoDB\Collection\Index\Index;
use ArangoDB\Collection\Index\FullTextIndex;
use ArangoDB\Collection\Index\SkipListIndex;
use ArangoDB\Collection\Index\PersistentIndex;
use ArangoDB\Collection\Index\GeoSpatialIndex;

class CollectionTest extends TestCase
{
Expand Down Expand Up @@ -148,21 +154,125 @@ public function testGetGloballyUniqueId()
$this->assertTrue($collection->drop());
}

public function testAddIndex()
public function testAddFullTextIndex()
{
$db = new Database($this->getConnectionObject());
$collection = new Collection('test_save_coll', $db);

$this->assertTrue($collection->save());
$this->assertCount(1, $collection->getIndexes());

$index = new Index('fulltext', ['complicated'], 3);
$index = new FullTextIndex(['complicated'], 3);
$this->assertTrue($collection->addIndex($index));

$this->assertCount(2, $collection->getIndexes());
$this->assertEquals('fulltext', $collection->getIndexes()->last()->getType());
}

public function testAddGeoSpatialIndex()
{
$db = new Database($this->getConnectionObject());
$collection = new Collection('test_save_coll', $db);

$this->assertTrue($collection->save());
$this->assertCount(1, $collection->getIndexes());

$index = new GeoSpatialIndex(['complicated'], true);
$this->assertTrue($collection->addIndex($index));

$this->assertCount(2, $collection->getIndexes());
$this->assertEquals('geo', $collection->getIndexes()->last()->getType());
}

public function testAddHashIndex()
{
$db = new Database($this->getConnectionObject());
$collection = new Collection('test_save_coll', $db);

$this->assertTrue($collection->save());
$this->assertCount(1, $collection->getIndexes());

$index = new HashIndex(['complicated']);
$this->assertTrue($collection->addIndex($index));

$this->assertCount(2, $collection->getIndexes());
$this->assertEquals('hash', $collection->getIndexes()->last()->getType());
}

public function testAddPersistentIndex()
{
$db = new Database($this->getConnectionObject());
$collection = new Collection('test_save_coll', $db);

$this->assertTrue($collection->save());
$this->assertCount(1, $collection->getIndexes());

$index = new PersistentIndex(['complicated']);
$this->assertTrue($collection->addIndex($index));

$this->assertCount(2, $collection->getIndexes());
$this->assertEquals('persistent', $collection->getIndexes()->last()->getType());
}

public function testAddSkipListIndex()
{
$db = new Database($this->getConnectionObject());
$collection = new Collection('test_save_coll', $db);

$this->assertTrue($collection->save());
$this->assertCount(1, $collection->getIndexes());

$index = new SkipListIndex(['complicated']);
$this->assertTrue($collection->addIndex($index));

$this->assertCount(2, $collection->getIndexes());
$this->assertEquals('skiplist', $collection->getIndexes()->last()->getType());
}

public function testAddTTLIndex()
{
$db = new Database($this->getConnectionObject());
$collection = new Collection('test_save_coll', $db);

$this->assertTrue($collection->save());
$this->assertCount(1, $collection->getIndexes());

$index = new TTLIndex(['complicated']);
$this->assertTrue($collection->addIndex($index));

$this->assertCount(2, $collection->getIndexes());
$this->assertEquals('ttl', $collection->getIndexes()->last()->getType());
}

public function testAddIndexThrowDatabaseException()
{
$mock = new MockHandler([
new Response(200, [], json_encode(['result' => []])),
new Response(200, [], json_encode(['result' => []])),
new Response(200, [], json_encode(['result' => []])),
new Response(200, [], json_encode(['result' => []])),
new Response(403, [], json_encode($this->mockServerError()))
]);

$db = new Database($this->getConnectionObject($mock));
$collection = new Collection('test_save_coll', $db);

$this->assertTrue($collection->save());

$index = new TTLIndex(['complicated']);
$this->expectException(DatabaseException::class);
$collection->addIndex($index);
}

public function testAddIndexOnNewCollectionReturnFalse()
{
$db = new Database($this->getConnectionObject());
$collection = new Collection('test_save_coll', $db);

$index = new FullTextIndex(['complicated'], 3);
$this->assertFalse($collection->addIndex($index));
}

public function testDropIndex()
{
$db = new Database($this->getConnectionObject());
Expand All @@ -171,7 +281,7 @@ public function testDropIndex()
$this->assertTrue($collection->save());
$this->assertCount(1, $collection->getIndexes());

$index = new Index('fulltext', ['complicated'], 3);
$index = new FullTextIndex(['complicated'], 3);
$this->assertTrue($collection->addIndex($index));

$list = $collection->getIndexes();
Expand All @@ -186,18 +296,127 @@ public function testDropIndex()
$this->assertCount(1, $collection->getIndexes());
}

public function testDropIndexOnNewCollectionReturnFalse()
{
$db = new Database($this->getConnectionObject());
$collection = new Collection('test_save_coll', $db);

$index = new FullTextIndex(['complicated'], 3);
$this->assertFalse($collection->dropIndex($index));
}

public function testDropNewIndexReturnFalse()
{
$db = new Database($this->getConnectionObject());
$collection = new Collection('test_save_coll', $db);

$this->assertTrue($collection->save());
$this->assertCount(1, $collection->getIndexes());

// Index not created before
$index = new FullTextIndex(['complicated'], 3);
$this->assertFalse($collection->dropIndex($index));
}


public function testDropNonExistentIndexReturnFalse()
{
$db = new Database($this->getConnectionObject());
$collection = new Collection('test_save_coll', $db);

$this->assertTrue($collection->save());

$index = new FullTextIndex(['complicated'], 3);
$collection->addIndex($index);

$list = $collection->getIndexes();
// Drop
$fulltext = $list->last();
$this->assertTrue($collection->dropIndex($fulltext));

// Try to drop an non-existent index
$this->assertFalse($collection->dropIndex($fulltext));
}

public function testDropIndexThrowDatabaseException()
{
$index = new FullTextIndex(['complicated'], 3);
$mocked = [
'indexes' => [
[
'id' => 'coll/1',
'name' => 'primary',
'type' => 'primary',
'sparse' => false,
'unique' => true,
'fields' => [
'_key'
]
],
[
'id' => 'coll/2',
'name' => 'idx_1646382074382254082',
'type' => 'fulltext',
'sparse' => true,
'unique' => true,
'fields' => [
'complicated'
]

]
]
];

$mock = new MockHandler([
new Response(200, [], json_encode(['result' => []])),
new Response(200, [], json_encode(['result' => []])),
new Response(200, [], json_encode(['result' => []])),
new Response(200, [], json_encode(['result' => []])),
new Response(200, [], json_encode(['result' => []])),
new Response(200, [], json_encode($mocked)),
new Response(403, [], json_encode($this->mockServerError()))
]);

$db = new Database($this->getConnectionObject($mock));
$collection = new Collection('test_save_coll', $db);

$this->assertTrue($collection->save());

$collection->addIndex($index);

$list = $collection->getIndexes();

// Try to drop an non-existent index
$this->expectException(DatabaseException::class);
$collection->dropIndex($list->last());
}

public function testGetIndexes()
{
$db = new Database($this->getConnectionObject());
$collection = new Collection('test_save_coll', $db);

$this->assertCount(0, $collection->getIndexes());
// On new collections return an empty array list
$list = $collection->getIndexes();
$this->assertCount(0, $list);

// Save collection
$this->assertTrue($collection->save());
$this->assertCount(1, $collection->getIndexes());

$index = new Index('fulltext', ['complicated'], 3);
$this->assertTrue($collection->addIndex($index));

// Check if collection index is created.
$list = $collection->getIndexes();
$this->assertCount(2, $collection->getIndexes());
$this->assertEquals('fulltext', $list->last()->getType());

// Drop
$fulltext = $list->last();
$this->assertTrue($collection->dropIndex($fulltext));

// Must have only 'primary' index
$this->assertCount(1, $collection->getIndexes());
$this->assertInstanceOf(Index::class, $collection->getIndexes()->first());
}

public function testGetIndexesThrowDatabaseException()
Expand Down
9 changes: 8 additions & 1 deletion tests/Collection/Index/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testConstructorThrowInvalidParameterExceptionForInvalidType()
public function testConstructorThrowInvalidParameterExceptionForInvalidKey()
{
$this->expectException(InvalidParameterException::class);
$index = new Index("easy", [new \stdClass()], 3);
$index = new Index("fulltext", [154], 3);
}

public function testToString()
Expand Down Expand Up @@ -92,4 +92,11 @@ public function testGetFields()
$this->assertIsArray($index->getFields());
$this->assertEquals("_key", $index->getFields()[0]);
}

public function testGetCreateData()
{
$index = new Index("skiplist", ['custom_field'], 3);
$this->assertIsArray($index->getCreateData());
$this->assertArrayHasKey("type", $index->getCreateData());
}
}

0 comments on commit 1c865bf

Please sign in to comment.