From b27a61d7c13d4353e8b2acee23beddb1d5539dd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 18 Sep 2024 22:45:53 +0200 Subject: [PATCH] Performance: Keep collections and indexes between GridFS tests (#1421) --- tests/GridFS/BucketFunctionalTest.php | 13 ++++++++++++ tests/GridFS/FunctionalTestCase.php | 30 ++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/tests/GridFS/BucketFunctionalTest.php b/tests/GridFS/BucketFunctionalTest.php index 2f2d126d4..3456951a9 100644 --- a/tests/GridFS/BucketFunctionalTest.php +++ b/tests/GridFS/BucketFunctionalTest.php @@ -814,9 +814,22 @@ public function testUploadingFirstFileCreatesIndexes(): void public function testExistingIndexIsReused(): void { + // The collections may exist from other tests, ensure they are removed + // before and after to avoid potential conflicts. + $this->dropCollection($this->getDatabaseName(), 'fs.chunks'); + $this->dropCollection($this->getDatabaseName(), 'fs.files'); + + // Create indexes with different numeric types before interacting with + // GridFS to assert that the library respects the existing indexes and + // does not attempt to create its own. $this->filesCollection->createIndex(['filename' => 1.0, 'uploadDate' => 1], ['name' => 'test']); $this->chunksCollection->createIndex(['files_id' => 1.0, 'n' => 1], ['name' => 'test', 'unique' => true]); + $this->assertIndexExists('fs.files', 'test'); + $this->assertIndexExists('fs.chunks', 'test', function (IndexInfo $info): void { + $this->assertTrue($info->isUnique()); + }); + $this->bucket->uploadFromStream('filename', self::createStream('foo')); $this->assertIndexNotExists($this->filesCollection->getCollectionName(), 'filename_1_uploadDate_1'); diff --git a/tests/GridFS/FunctionalTestCase.php b/tests/GridFS/FunctionalTestCase.php index d010ca929..785e1c909 100644 --- a/tests/GridFS/FunctionalTestCase.php +++ b/tests/GridFS/FunctionalTestCase.php @@ -4,6 +4,7 @@ use MongoDB\Collection; use MongoDB\GridFS\Bucket; +use MongoDB\Operation\DropCollection; use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase; use function fopen; @@ -28,10 +29,33 @@ 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'); + } + + public function tearDown(): void + { + $this->chunksCollection->deleteMany([]); + $this->filesCollection->deleteMany([]); + + parent::tearDown(); + } + + /** + * The bucket's collections are created by the first test that runs and + * kept for all subsequent tests. This is done to avoid creating the + * collections and their indexes for each test, which would be slow. + * + * @beforeClass + * @afterClass + */ + public static function dropCollectionsBeforeAfterClass(): void + { + $manager = static::createTestManager(); + + (new DropCollection(self::getDatabaseName(), 'fs.chunks'))->execute($manager->selectServer()); + (new DropCollection(self::getDatabaseName(), 'fs.files'))->execute($manager->selectServer()); } /**