|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\E2E\Adapter; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Redis; |
| 7 | +use Utopia\Cache\Adapter\Redis as RedisAdapter; |
| 8 | +use Utopia\Cache\Cache; |
| 9 | +use Utopia\Database\Adapter\Mongo; |
| 10 | +use Utopia\Database\Database; |
| 11 | +use Utopia\Mongo\Client; |
| 12 | + |
| 13 | +class MongoDBTest extends Base |
| 14 | +{ |
| 15 | + public static ?Database $database = null; |
| 16 | + protected static string $namespace; |
| 17 | + |
| 18 | + /** |
| 19 | + * Return name of adapter |
| 20 | + * |
| 21 | + * @return string |
| 22 | + */ |
| 23 | + public static function getAdapterName(): string |
| 24 | + { |
| 25 | + return "mongodb"; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @return Database |
| 30 | + * @throws Exception |
| 31 | + */ |
| 32 | + public static function getDatabase(): Database |
| 33 | + { |
| 34 | + if (!is_null(self::$database)) { |
| 35 | + return self::$database; |
| 36 | + } |
| 37 | + |
| 38 | + $redis = new Redis(); |
| 39 | + $redis->connect('redis', 6379); |
| 40 | + $redis->flushAll(); |
| 41 | + $cache = new Cache(new RedisAdapter($redis)); |
| 42 | + |
| 43 | + $schema = 'utopiaTests'; // same as $this->testDatabase |
| 44 | + $client = new Client( |
| 45 | + $schema, |
| 46 | + 'mongo', |
| 47 | + 27017, |
| 48 | + 'root', |
| 49 | + 'password', |
| 50 | + false |
| 51 | + ); |
| 52 | + |
| 53 | + $database = new Database(new Mongo($client), $cache); |
| 54 | + $database->getAdapter()->setSupportForAttributes(false); |
| 55 | + $database |
| 56 | + ->setDatabase($schema) |
| 57 | + ->setNamespace(static::$namespace = 'myapp_' . uniqid()); |
| 58 | + |
| 59 | + if ($database->exists()) { |
| 60 | + $database->delete(); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + $database->create(); |
| 65 | + |
| 66 | + return self::$database = $database; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @throws Exception |
| 71 | + */ |
| 72 | + public function testCreateExistsDelete(): void |
| 73 | + { |
| 74 | + // Mongo creates databases on the fly, so exists would always pass. So we override this test to remove the exists check. |
| 75 | + $this->assertNotNull(static::getDatabase()->create()); |
| 76 | + $this->assertEquals(true, static::getDatabase()->delete($this->testDatabase)); |
| 77 | + $this->assertEquals(true, static::getDatabase()->create()); |
| 78 | + $this->assertEquals(static::getDatabase(), static::getDatabase()->setDatabase($this->testDatabase)); |
| 79 | + } |
| 80 | + |
| 81 | + public function testRenameAttribute(): void |
| 82 | + { |
| 83 | + $this->assertTrue(true); |
| 84 | + } |
| 85 | + |
| 86 | + public function testRenameAttributeExisting(): void |
| 87 | + { |
| 88 | + $this->assertTrue(true); |
| 89 | + } |
| 90 | + |
| 91 | + public function testUpdateAttributeStructure(): void |
| 92 | + { |
| 93 | + $this->assertTrue(true); |
| 94 | + } |
| 95 | + |
| 96 | + public function testKeywords(): void |
| 97 | + { |
| 98 | + $this->assertTrue(true); |
| 99 | + } |
| 100 | + |
| 101 | + protected static function deleteColumn(string $collection, string $column): bool |
| 102 | + { |
| 103 | + return true; |
| 104 | + } |
| 105 | + |
| 106 | + protected static function deleteIndex(string $collection, string $index): bool |
| 107 | + { |
| 108 | + return true; |
| 109 | + } |
| 110 | +} |
0 commit comments