|
| 1 | +import { |
| 2 | + MongoDBContainer, |
| 3 | + type StartedMongoDBContainer, |
| 4 | +} from '@testcontainers/mongodb'; |
| 5 | +import { |
| 6 | + PostgreSqlContainer, |
| 7 | + type StartedPostgreSqlContainer, |
| 8 | +} from '@testcontainers/postgresql'; |
| 9 | +import assert from 'assert'; |
| 10 | +import { Db as MongoDb, MongoClient as OriginalMongoClient } from 'mongodb'; |
| 11 | +import { after, before, describe, it } from 'node:test'; |
| 12 | +import MongoClient from '../mongo/mongoClient'; |
| 13 | +import type { Db } from '../mongo/mongoDb'; |
| 14 | +import { endAllPools } from '../postgres'; |
| 15 | + |
| 16 | +type User = { name: string; age: number }; |
| 17 | + |
| 18 | +void describe('MongoDB Compatibility Tests', () => { |
| 19 | + let postgres: StartedPostgreSqlContainer; |
| 20 | + let postgresConnectionString: string; |
| 21 | + let pongoClient: MongoClient; |
| 22 | + |
| 23 | + let mongo: StartedMongoDBContainer; |
| 24 | + let mongoConnectionString: string; |
| 25 | + let mongoClient: OriginalMongoClient; |
| 26 | + |
| 27 | + let pongoDb: Db; |
| 28 | + let mongoDb: MongoDb; |
| 29 | + |
| 30 | + before(async () => { |
| 31 | + postgres = await new PostgreSqlContainer().start(); |
| 32 | + postgresConnectionString = postgres.getConnectionUri(); |
| 33 | + pongoClient = new MongoClient(postgresConnectionString); |
| 34 | + await pongoClient.connect(); |
| 35 | + |
| 36 | + mongo = await new MongoDBContainer('mongo:6.0.12').start(); |
| 37 | + mongoConnectionString = mongo.getConnectionString(); |
| 38 | + mongoClient = new OriginalMongoClient(mongoConnectionString, { |
| 39 | + directConnection: true, |
| 40 | + }); |
| 41 | + await mongoClient.connect(); |
| 42 | + |
| 43 | + const dbName = postgres.getDatabase(); |
| 44 | + |
| 45 | + pongoDb = pongoClient.db(dbName); |
| 46 | + mongoDb = mongoClient.db(dbName); |
| 47 | + }); |
| 48 | + |
| 49 | + after(async () => { |
| 50 | + try { |
| 51 | + await endAllPools(); |
| 52 | + await postgres.stop(); |
| 53 | + } catch (error) { |
| 54 | + console.log(error); |
| 55 | + } |
| 56 | + try { |
| 57 | + await mongoClient.close(); |
| 58 | + await mongo.stop(); |
| 59 | + } catch (error) { |
| 60 | + console.log(error); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + void describe('Insert Operations', () => { |
| 65 | + void it('should insert a document into both PostgreSQL and MongoDB', async () => { |
| 66 | + const pongoCollection = pongoDb.collection<User>('testCollection'); |
| 67 | + const mongoCollection = mongoDb.collection<User>('testCollection'); |
| 68 | + |
| 69 | + const doc = { name: 'Alice', age: 25 }; |
| 70 | + |
| 71 | + const pongoInsertResult = await pongoCollection.insertOne(doc); |
| 72 | + const mongoInsertResult = await mongoCollection.insertOne(doc); |
| 73 | + |
| 74 | + assert(pongoInsertResult.insertedId); |
| 75 | + assert(mongoInsertResult.insertedId); |
| 76 | + |
| 77 | + const pongoDoc = await pongoCollection.findOne({ |
| 78 | + _id: pongoInsertResult.insertedId, |
| 79 | + }); |
| 80 | + const mongoDoc = await mongoCollection.findOne({ |
| 81 | + _id: mongoInsertResult.insertedId, |
| 82 | + }); |
| 83 | + |
| 84 | + assert.deepStrictEqual( |
| 85 | + { |
| 86 | + name: pongoDoc!.name, |
| 87 | + age: pongoDoc!.age, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: mongoDoc!.name, |
| 91 | + age: mongoDoc!.age, |
| 92 | + }, |
| 93 | + ); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + void describe('Update Operations', () => { |
| 98 | + void it('should update a document in both PostgreSQL and MongoDB', async () => { |
| 99 | + const pongoCollection = pongoDb.collection<User>('testCollection'); |
| 100 | + const mongoCollection = mongoDb.collection<User>('testCollection'); |
| 101 | + const doc = { name: 'Bob', age: 30 }; |
| 102 | + |
| 103 | + const pongoInsertResult = await pongoCollection.insertOne(doc); |
| 104 | + const mongoInsertResult = await mongoCollection.insertOne(doc); |
| 105 | + |
| 106 | + const update = { $set: { age: 31 } }; |
| 107 | + |
| 108 | + await pongoCollection.updateOne( |
| 109 | + { _id: pongoInsertResult.insertedId }, |
| 110 | + update, |
| 111 | + ); |
| 112 | + await mongoCollection.updateOne( |
| 113 | + { _id: mongoInsertResult.insertedId }, |
| 114 | + update, |
| 115 | + ); |
| 116 | + |
| 117 | + const pongoDoc = await pongoCollection.findOne({ |
| 118 | + _id: pongoInsertResult.insertedId, |
| 119 | + }); |
| 120 | + const mongoDoc = await mongoCollection.findOne({ |
| 121 | + _id: mongoInsertResult.insertedId, |
| 122 | + }); |
| 123 | + |
| 124 | + assert.deepStrictEqual( |
| 125 | + { |
| 126 | + name: pongoDoc!.name, |
| 127 | + age: 31, |
| 128 | + }, |
| 129 | + { |
| 130 | + name: mongoDoc!.name, |
| 131 | + age: 31, |
| 132 | + }, |
| 133 | + ); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + void describe('Delete Operations', () => { |
| 138 | + void it('should delete a document from both PostgreSQL and MongoDB', async () => { |
| 139 | + const pongoCollection = pongoDb.collection<User>('testCollection'); |
| 140 | + const mongoCollection = mongoDb.collection<User>('testCollection'); |
| 141 | + const doc = { name: 'Charlie', age: 35 }; |
| 142 | + |
| 143 | + const pongoInsertResult = await pongoCollection.insertOne(doc); |
| 144 | + const mongoInsertResult = await mongoCollection.insertOne(doc); |
| 145 | + |
| 146 | + await pongoCollection.deleteOne({ _id: pongoInsertResult.insertedId }); |
| 147 | + await mongoCollection.deleteOne({ _id: mongoInsertResult.insertedId }); |
| 148 | + |
| 149 | + const pongoDoc = await pongoCollection.findOne({ |
| 150 | + _id: pongoInsertResult.insertedId, |
| 151 | + }); |
| 152 | + const mongoDoc = await mongoCollection.findOne({ |
| 153 | + _id: mongoInsertResult.insertedId, |
| 154 | + }); |
| 155 | + |
| 156 | + assert.strictEqual(pongoDoc, null); |
| 157 | + assert.strictEqual(mongoDoc, null); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + void describe('Find Operations', () => { |
| 162 | + void it('should find documents with a filter in both PostgreSQL and MongoDB', async () => { |
| 163 | + const pongoCollection = pongoDb.collection<User>('testCollection'); |
| 164 | + const mongoCollection = mongoDb.collection<User>('testCollection'); |
| 165 | + const docs = [ |
| 166 | + { name: 'David', age: 40 }, |
| 167 | + { name: 'Eve', age: 45 }, |
| 168 | + { name: 'Frank', age: 50 }, |
| 169 | + ]; |
| 170 | + |
| 171 | + await pongoCollection.insertOne(docs[0]!); |
| 172 | + await pongoCollection.insertOne(docs[1]!); |
| 173 | + await pongoCollection.insertOne(docs[2]!); |
| 174 | + |
| 175 | + await mongoCollection.insertOne(docs[0]!); |
| 176 | + await mongoCollection.insertOne(docs[1]!); |
| 177 | + await mongoCollection.insertOne(docs[2]!); |
| 178 | + |
| 179 | + const pongoDocs = await pongoCollection |
| 180 | + .find({ age: { $gte: 45 } }) |
| 181 | + .toArray(); |
| 182 | + const mongoDocs = await mongoCollection |
| 183 | + .find({ age: { $gte: 45 } }) |
| 184 | + .toArray(); |
| 185 | + |
| 186 | + assert.strictEqual(pongoDocs.length, 2); |
| 187 | + |
| 188 | + assert.deepStrictEqual( |
| 189 | + pongoDocs.map((d) => ({ name: d.name, age: d.age })), |
| 190 | + mongoDocs.map((d) => ({ name: d.name, age: d.age })), |
| 191 | + ); |
| 192 | + }); |
| 193 | + |
| 194 | + void it('should find one document with a filter in both PostgreSQL and MongoDB', async () => { |
| 195 | + const pongoCollection = pongoDb.collection<User>('testCollection'); |
| 196 | + const mongoCollection = mongoDb.collection<User>('testCollection'); |
| 197 | + const doc = { name: 'Grace', age: 55 }; |
| 198 | + |
| 199 | + await pongoCollection.insertOne(doc); |
| 200 | + await mongoCollection.insertOne(doc); |
| 201 | + |
| 202 | + const pongoDoc = await pongoCollection.findOne({ name: 'Grace' }); |
| 203 | + const mongoDoc = await mongoCollection.findOne({ name: 'Grace' }); |
| 204 | + |
| 205 | + assert.deepStrictEqual( |
| 206 | + { |
| 207 | + name: pongoDoc!.name, |
| 208 | + age: pongoDoc!.age, |
| 209 | + }, |
| 210 | + { |
| 211 | + name: mongoDoc!.name, |
| 212 | + age: mongoDoc!.age, |
| 213 | + }, |
| 214 | + ); |
| 215 | + }); |
| 216 | + }); |
| 217 | +}); |
0 commit comments