Skip to content

Commit

Permalink
Added tests for handle method
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Jul 12, 2024
1 parent 8636fce commit 4f4e75a
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/packages/pongo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"dist"
],
"peerDependencies": {
"@event-driven-io/dumbo": "^0.1.0",
"@event-driven-io/dumbo": "^0.2.0",
"@types/mongodb": "^4.0.7",
"@types/pg": "^8.11.6",
"@types/pg-format": "^1.0.5",
Expand Down
109 changes: 108 additions & 1 deletion src/packages/pongo/src/e2e/compatibilityTest.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import {
type StartedPostgreSqlContainer,
} from '@testcontainers/postgresql';
import assert from 'assert';
import { Db as MongoDb, MongoClient as OriginalMongoClient } from 'mongodb';
import {
Db as MongoDb,
ObjectId,
MongoClient as OriginalMongoClient,
} from 'mongodb';
import { after, before, describe, it } from 'node:test';
import { v4 as uuid } from 'uuid';
import { MongoClient, type Db } from '../';

type History = { street: string };
Expand All @@ -20,6 +25,7 @@ type Address = {
};

type User = {
_id?: ObjectId;
name: string;
age: number;
address?: Address;
Expand Down Expand Up @@ -904,4 +910,105 @@ void describe('MongoDB Compatibility Tests', () => {
);
});
});

void describe('Handle Operations', () => {
void it('should insert a new document if it does not exist', async () => {
const pongoCollection = pongoDb.collection<User>('handleCollection');
const nonExistingId = uuid() as unknown as ObjectId;

const newDoc: User = { name: 'John', age: 25 };

const handle = (_existing: User | null) => newDoc;

const resultPongo = await pongoCollection.handle(nonExistingId, handle);
assert.deepStrictEqual(resultPongo, { ...newDoc, _id: nonExistingId });

const pongoDoc = await pongoCollection.findOne({
_id: nonExistingId,
});

assert.deepStrictEqual(pongoDoc, { ...newDoc, _id: nonExistingId });
});

void it('should replace an existing document', async () => {
const pongoCollection = pongoDb.collection<User>('handleCollection');

const existingDoc: User = { name: 'John', age: 25 };
const updatedDoc: User = { name: 'John', age: 30 };

const pongoInsertResult = await pongoCollection.insertOne(existingDoc);

const handle = (_existing: User | null) => updatedDoc;

const resultPongo = await pongoCollection.handle(
pongoInsertResult.insertedId,
handle,
);

assert.deepStrictEqual(resultPongo, {
...updatedDoc,
});

const pongoDoc = await pongoCollection.findOne({
_id: pongoInsertResult.insertedId,
});

assert.deepStrictEqual(pongoDoc, {
...updatedDoc,
_id: pongoInsertResult.insertedId,
});
});

void it('should delete an existing document if the handler returns null', async () => {
const pongoCollection = pongoDb.collection<User>('handleCollection');

const existingDoc: User = { name: 'John', age: 25 };

const pongoInsertResult = await pongoCollection.insertOne(existingDoc);

const handle = (_existing: User | null) => null;

const resultPongo = await pongoCollection.handle(
pongoInsertResult.insertedId,
handle,
);

assert.strictEqual(resultPongo, null);

const pongoDoc = await pongoCollection.findOne({
_id: pongoInsertResult.insertedId,
});

assert.strictEqual(pongoDoc, null);
});

void it('should do nothing if the handler returns the existing document unchanged', async () => {
const pongoCollection = pongoDb.collection<User>('handleCollection');

const existingDoc: User = { name: 'John', age: 25 };

const pongoInsertResult = await pongoCollection.insertOne(existingDoc);

const handle = (existing: User | null) => existing;

const resultPongo = await pongoCollection.handle(
pongoInsertResult.insertedId,
handle,
);

assert.deepStrictEqual(resultPongo, {
...existingDoc,
_id: pongoInsertResult.insertedId,
});

const pongoDoc = await pongoCollection.findOne({
_id: pongoInsertResult.insertedId,
});

assert.deepStrictEqual(pongoDoc, {
...existingDoc,
_id: pongoInsertResult.insertedId,
});
});
});
});
3 changes: 2 additions & 1 deletion src/packages/pongo/src/main/typing/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface PongoDb {
collection<T extends PongoDocument>(name: string): PongoCollection<T>;
}

export interface PongoCollection<T> {
export interface PongoCollection<T extends PongoDocument> {
readonly dbName: string;
readonly collectionName: string;
createCollection(): Promise<void>;
Expand All @@ -34,6 +34,7 @@ export interface PongoCollection<T> {
find(filter: PongoFilter<T>): Promise<T[]>;
drop(): Promise<boolean>;
rename(newName: string): Promise<PongoCollection<T>>;
handle(id: string, handle: DocumentHandler<T>): Promise<T | null>;
}

export type HasId = { _id: string };
Expand Down
11 changes: 10 additions & 1 deletion src/packages/pongo/src/mongo/mongoCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import type {
ModifyResult,
Collection as MongoCollection,
FindCursor as MongoFindCursor,
ObjectId,
OperationOptions,
OptionalUnlessRequiredId,
OrderedBulkOperation,
Expand All @@ -58,7 +59,12 @@ import type {
WriteConcern,
} from 'mongodb';
import type { Key } from 'readline';
import type { PongoCollection, PongoFilter, PongoUpdate } from '../main';
import type {
DocumentHandler,
PongoCollection,
PongoFilter,
PongoUpdate,
} from '../main';
import { FindCursor } from './findCursor';

export class Collection<T extends Document> implements MongoCollection<T> {
Expand Down Expand Up @@ -501,4 +507,7 @@ export class Collection<T extends Document> implements MongoCollection<T> {
async createCollection(): Promise<void> {
await this.collection.createCollection();
}
async handle(id: ObjectId, handle: DocumentHandler<T>): Promise<T | null> {
return this.collection.handle(id.toString(), handle);
}
}
14 changes: 11 additions & 3 deletions src/packages/pongo/src/mongo/mongoDb.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { Collection as MongoCollection, type Document } from 'mongodb';
import type { PongoDb } from '../main';
import {
Collection as MongoCollection,
ObjectId,
type Document,
} from 'mongodb';
import type { DocumentHandler, PongoDb } from '../main';
import { Collection } from './mongoCollection';

export class Db {
constructor(private pongoDb: PongoDb) {}

collection<T extends Document>(collectionName: string): MongoCollection<T> {
collection<T extends Document>(
collectionName: string,
): MongoCollection<T> & {
handle(id: ObjectId, handle: DocumentHandler<T>): Promise<T | null>;
} {
return new Collection<T>(this.pongoDb.collection<T>(collectionName));
}
}
15 changes: 12 additions & 3 deletions src/packages/pongo/src/postgres/postgresCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,18 @@ export const postgresCollection = <T extends PongoDocument>(

const result = await handle(existing);

if (!existing && result) await collection.insertOne(result);
else if (existing && result) await collection.replaceOne(byId, result);
else if (existing && !result) await collection.deleteOne(byId);
if (!existing && result) {
const newDoc = { ...result, _id: id };
await collection.insertOne({ ...newDoc, _id: id });
return newDoc;
}

if (existing && !result) {
await collection.deleteOne(byId);
return null;
}

if (existing && result) await collection.replaceOne(byId, result);

return result;
},
Expand Down

0 comments on commit 4f4e75a

Please sign in to comment.