Skip to content

Commit

Permalink
Added basic tests for collections caching
Browse files Browse the repository at this point in the history
Added also comments for Mongo dummy mocks
  • Loading branch information
oskardudycz committed Dec 18, 2024
1 parent 3259540 commit dacbef5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ void describe('mongoDBEventStoreStorage', () => {
assertEqual(collection.dbName, defaultDBName);
});

void it('resolves the same instance of collection for multiple calls for the same stream type resolution', async () => {
// Given
const storage = mongoDBEventStoreStorage({
getConnectedClient,
});

// When
const firstResolution = await storage.collectionFor(testStreamTypeName);
const nextResolution = await storage.collectionFor(testStreamTypeName);

// Then
assertEqual(firstResolution, nextResolution);
});

void describe('Single Collection storage', () => {
void it('handles SINGLE_COLLECTION passed as string with default collection name and no db', async () => {
// Given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import type {
MongoClient,
} from 'mongodb';

/**
* Creates a dummy MongoDB collection. It should not be used as in-memory version,
* but just a dummy replacement for the basic structure and calls test.
* @param name collection name
* @param options collection setup options
* @returns Dummmy collection that has name, dbName and createIndex method
*/
export const getDummyCollection = <TSchema extends Document = Document>(
name: string,
options?: CollectionOptions & { dbName?: string },
Expand All @@ -20,6 +27,13 @@ export const getDummyCollection = <TSchema extends Document = Document>(
return dummyCollection;
};

/**
* Creates a dummy MongoDB database. It should not be used as in-memory version,
* but just a dummy replacement for the basic structure and calls test.
* @param dbName database name
* @param options database setup options
* @returns Dummmy database that has name and can setup dummy collection
*/
export const getDummyDb = (dbName?: string, options?: DbOptions): Db => {
const dummyDB: Db = {
databaseName: dbName!,
Expand All @@ -30,6 +44,12 @@ export const getDummyDb = (dbName?: string, options?: DbOptions): Db => {
return dummyDB;
};

/**
* Creates a dummy MongoDB connection. It should not be used as in-memory version,
* but just a dummy replacement for the basic structure and calls test.
* @param options setup options allowing to pass the default database name
* @returns Dummmy connection that can setup a dummy database
*/
export const getDummyClient = (options?: {
defaultDBName?: string;
}): MongoClient => {
Expand Down

0 comments on commit dacbef5

Please sign in to comment.