Skip to content

Commit

Permalink
Don't recreate collections and indexes on each test for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Sep 18, 2024
1 parent 4689540 commit 3a2f35d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 4 additions & 0 deletions tests/GridFS/BucketFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,10 @@ public function testUploadingFirstFileCreatesIndexes(): void

public function testExistingIndexIsReused(): void
{
// The collections may exist from other tests, ensure they are removed before and after the test
$this->dropCollection($this->getDatabaseName(), 'fs.chunks');
$this->dropCollection($this->getDatabaseName(), 'fs.files');

$this->filesCollection->createIndex(['filename' => 1.0, 'uploadDate' => 1], ['name' => 'test']);
$this->chunksCollection->createIndex(['files_id' => 1.0, 'n' => 1], ['name' => 'test', 'unique' => true]);

Expand Down
25 changes: 22 additions & 3 deletions tests/GridFS/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MongoDB\Tests\GridFS;

use MongoDB\Collection;
use MongoDB\Driver\Command;
use MongoDB\GridFS\Bucket;
use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;

Expand All @@ -28,10 +29,28 @@ public function setUp(): void
parent::setUp();

$this->bucket = new Bucket($this->manager, $this->getDatabaseName());
$this->bucket->drop();

$this->chunksCollection = $this->createCollection($this->getDatabaseName(), 'fs.chunks');
$this->filesCollection = $this->createCollection($this->getDatabaseName(), 'fs.files');
$this->chunksCollection = new Collection($this->manager, $this->getDatabaseName(), 'fs.chunks');
$this->filesCollection = new Collection($this->manager, $this->getDatabaseName(), 'fs.files');
$this->chunksCollection->deleteMany([]);
$this->filesCollection->deleteMany([]);
}

public function tearDown(): void
{
$this->chunksCollection->deleteMany([]);
$this->filesCollection->deleteMany([]);

parent::tearDown();
}

public static function tearDownAfterClass(): void
{
$manager = static::createTestManager();
$manager->executeCommand(self::getDatabaseName(), new Command(['drop' => 'fs.chunks']));
$manager->executeCommand(self::getDatabaseName(), new Command(['drop' => 'fs.files']));

parent::tearDownAfterClass();
}

/**
Expand Down

0 comments on commit 3a2f35d

Please sign in to comment.