Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{
"extension": ["js"],
"require": ["ts-node/register", "source-map-support/register"],
"$schema": "https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/mocharc.json",
"extension": [
"js",
"ts"
],
"require": [
"ts-node/register",
"source-map-support/register"
],
"file": "test/tools/runner",
"ui": "test/tools/runner/metadata_ui.js",
"recursive": true,
Expand Down
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@
"@microsoft/tsdoc-config": "^0.13.9",
"@types/aws4": "^1.5.1",
"@types/bl": "^2.1.0",
"@types/chai": "^4.2.14",
"@types/chai-subset": "^1.3.3",
"@types/kerberos": "^1.1.0",
"@types/mocha": "^8.2.0",
"@types/node": "^14.6.4",
"@types/saslprep": "^1.0.0",
"@types/semver": "^7.3.4",
"@typescript-eslint/eslint-plugin": "^3.10.0",
"@typescript-eslint/parser": "^3.10.0",
"chai": "^4.2.0",
Expand Down
28 changes: 28 additions & 0 deletions test/functional/unified-spec-runner/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2018
},
"plugins": [
"@typescript-eslint",
"prettier",
"promise",
"eslint-plugin-tsdoc"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier/@typescript-eslint",
"plugin:prettier/recommended"
],
"env": {
"node": true,
"mocha": true,
"es6": true
},
"rules": {
"prettier/prettier": "error",
"tsdoc/syntax": "warn"
}
}
161 changes: 161 additions & 0 deletions test/functional/unified-spec-runner/entities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import { MongoClient, Db, Collection, GridFSBucket, Document } from '../../../src/index';
import { ClientSession } from '../../../src/sessions';
import { ChangeStream } from '../../../src/change_stream';
import type { ClientEntity, EntityDescription } from './schema';
import type {
CommandFailedEvent,
CommandStartedEvent,
CommandSucceededEvent
} from '../../../src/cmap/events';
import { patchCollectionOptions, patchDbOptions } from './unified-utils';
import { TestConfiguration } from './unified.test';

export type CommandEvent = CommandStartedEvent | CommandSucceededEvent | CommandFailedEvent;

export class UnifiedMongoClient extends MongoClient {
events: CommandEvent[];
observedEvents: ('commandStarted' | 'commandSucceeded' | 'commandFailed')[];

static EVENT_NAME_LOOKUP = {
commandStartedEvent: 'commandStarted',
commandSucceededEvent: 'commandSucceeded',
commandFailedEvent: 'commandFailed'
} as const;

constructor(url: string, description: ClientEntity) {
super(url, { monitorCommands: true, ...description.uriOptions });
this.events = [];
// apm
this.observedEvents = (description.observeEvents ?? []).map(
e => UnifiedMongoClient.EVENT_NAME_LOOKUP[e]
);
for (const eventName of this.observedEvents) {
this.on(eventName, this.pushEvent);
}
}

// NOTE: this must be an arrow function for `this` to work.
pushEvent: (e: CommandEvent) => void = e => {
this.events.push(e);
};

/** Disables command monitoring for the client and returns a list of the captured events. */
stopCapturingEvents(): CommandEvent[] {
for (const eventName of this.observedEvents) {
this.off(eventName, this.pushEvent);
}
return this.events;
}
}

export type Entity =
| UnifiedMongoClient
| Db
| Collection
| ClientSession
| ChangeStream
| GridFSBucket
| Document; // Results from operations

export type EntityCtor =
| typeof UnifiedMongoClient
| typeof Db
| typeof Collection
| typeof ClientSession
| typeof ChangeStream
| typeof GridFSBucket;

export type EntityTypeId = 'client' | 'db' | 'collection' | 'session' | 'bucket' | 'stream';

const ENTITY_CTORS = new Map<EntityTypeId, EntityCtor>();
ENTITY_CTORS.set('client', UnifiedMongoClient);
ENTITY_CTORS.set('db', Db);
ENTITY_CTORS.set('collection', Collection);
ENTITY_CTORS.set('session', ClientSession);
ENTITY_CTORS.set('bucket', GridFSBucket);
ENTITY_CTORS.set('stream', ChangeStream);

export class EntitiesMap<E = Entity> extends Map<string, E> {
mapOf(type: 'client'): EntitiesMap<UnifiedMongoClient>;
mapOf(type: 'db'): EntitiesMap<Db>;
mapOf(type: 'collection'): EntitiesMap<Collection>;
mapOf(type: 'session'): EntitiesMap<ClientSession>;
mapOf(type: 'bucket'): EntitiesMap<GridFSBucket>;
mapOf(type: 'stream'): EntitiesMap<ChangeStream>;
mapOf(type: EntityTypeId): EntitiesMap<Entity> {
const ctor = ENTITY_CTORS.get(type);
if (!ctor) {
throw new Error(`Unknown type ${type}`);
}
return new EntitiesMap(Array.from(this.entries()).filter(([, e]) => e instanceof ctor));
}

getEntity(type: 'client', key: string, assertExists?: boolean): UnifiedMongoClient;
getEntity(type: 'db', key: string, assertExists?: boolean): Db;
getEntity(type: 'collection', key: string, assertExists?: boolean): Collection;
getEntity(type: 'session', key: string, assertExists?: boolean): ClientSession;
getEntity(type: 'bucket', key: string, assertExists?: boolean): GridFSBucket;
getEntity(type: 'stream', key: string, assertExists?: boolean): ChangeStream;
getEntity(type: EntityTypeId, key: string, assertExists = true): Entity {
const entity = this.get(key);
if (!entity) {
if (assertExists) throw new Error(`Entity ${key} does not exist`);
return;
}
const ctor = ENTITY_CTORS.get(type);
if (!ctor) {
throw new Error(`Unknown type ${type}`);
}
if (!(entity instanceof ctor)) {
throw new Error(`${key} is not an instance of ${type}`);
}
return entity;
}

async cleanup(): Promise<void> {
for (const [, client] of this.mapOf('client')) {
await client.close();
}
for (const [, session] of this.mapOf('session')) {
await session.endSession();
}
this.clear();
}

static async createEntities(
config: TestConfiguration,
entities?: EntityDescription[]
): Promise<EntitiesMap> {
const map = new EntitiesMap();
for (const entity of entities ?? []) {
if ('client' in entity) {
const client = new UnifiedMongoClient(config.url(), entity.client);
await client.connect();
map.set(entity.client.id, client);
} else if ('database' in entity) {
const client = map.getEntity('client', entity.database.client);
const db = client.db(
entity.database.databaseName,
patchDbOptions(entity.database.databaseOptions)
);
map.set(entity.database.id, db);
} else if ('collection' in entity) {
const db = map.getEntity('db', entity.collection.database);
const collection = db.collection(
entity.collection.collectionName,
patchCollectionOptions(entity.collection.collectionOptions)
);
map.set(entity.collection.id, collection);
} else if ('session' in entity) {
map.set(entity.session.id, null);
} else if ('bucket' in entity) {
map.set(entity.bucket.id, null);
} else if ('stream' in entity) {
map.set(entity.stream.id, null);
} else {
throw new Error(`Unsupported Entity ${JSON.stringify(entity)}`);
}
}
return map;
}
}
Loading