Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase test coverage for Mongo Seeding CLI #60

Merged
merged 1 commit into from
Jan 5, 2019
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
12 changes: 11 additions & 1 deletion cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { CommandLineArguments, CliSpecificOptions } from './types';
import { DeepPartial } from 'mongo-seeding/dist/common';

class CliSeeder {
readonly DEFAULT_INPUT_PATH = './';

run = async () => {
let options: CommandLineArguments;

Expand All @@ -45,7 +47,7 @@ class CliSeeder {
this.useCliSpecificOptions(config as DeepPartial<CliSpecificOptions>);
const seeder = new Seeder(config as DeepPartial<SeederConfig>);

const collectionsPath = options.data ? options.data : './';
const collectionsPath = this.getCollectionsPath(options);
const collectionReadingConfig = this.getCollectionReadingConfig(options);

try {
Expand All @@ -62,6 +64,14 @@ class CliSeeder {
process.exit(0);
};

private getCollectionsPath(options: CommandLineArguments): string {
if (options.data) {
return options.data;
}

return this.DEFAULT_INPUT_PATH;
}

private getCollectionReadingConfig = (
options: CommandLineArguments,
): SeederCollectionReadingOptions => {
Expand Down
6 changes: 4 additions & 2 deletions cli/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SeederConfig } from 'mongo-seeding';
import { SeederConfig, SeederCollectionReadingOptions } from 'mongo-seeding';
import { DeepPartial } from 'mongo-seeding/dist/common';

export interface CommandLineOption {
Expand Down Expand Up @@ -26,7 +26,9 @@ export interface CommandLineArguments {
'transpile-only'?: boolean;
}

export type PartialCliOptions = DeepPartial<SeederConfig | CliSpecificOptions>;
export type PartialCliOptions = DeepPartial<
SeederConfig | SeederCollectionReadingOptions | CliSpecificOptions
>;
export interface CliSpecificOptions {
transpileOnly: boolean;
}
12 changes: 12 additions & 0 deletions cli/test/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,16 @@ describe('CLI', () => {
expect(exit).toBeCalledWith(0);
}
});

it('should exit with error on command line arguments error', async () => {
const exit = jest.spyOn(process, 'exit').mockImplementation(code => code);

process.argv = ['', '', '--what-is-this-parameter', 'dunno'];
await cliSeeder.run();

expect(console.error).toBeCalledWith(
expect.stringContaining('InvalidParameterError'),
);
expect(exit).toBeCalledWith(0);
});
});
26 changes: 26 additions & 0 deletions cli/test/unit/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { cliSeeder } from '../../src/index';
import { CommandLineArguments } from '../../src/types';

describe('Index', () => {
it('should fallback to default input path', () => {
const testCases: Array<{
options: CommandLineArguments;
expected: string;
}> = [
{
options: {},
expected: cliSeeder.DEFAULT_INPUT_PATH,
},
{
options: { data: '/test/path' },
expected: '/test/path',
},
];

for (const testCase of testCases) {
// @ts-ignore
const result = cliSeeder.getCollectionsPath(testCase.options);
expect(result).toEqual(testCase.expected);
}
});
});
128 changes: 105 additions & 23 deletions cli/test/unit/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
createConfigFromOptions,
convertEmptyObjectToUndefined,
} from '../../src/options';
import { CommandLineArguments } from '../../src/types';
import { CommandLineArguments, PartialCliOptions } from '../../src/types';

describe('Options', () => {
const previousEnvs = process.env;
Expand All @@ -18,34 +18,116 @@ describe('Options', () => {
});

it('should create config from command line arguments', () => {
const cmdArgs: CommandLineArguments = {
'db-uri': 'cmdUri',
'reconnect-timeout': 7000,
};

const result = createConfigFromOptions(cmdArgs);

expect(result).toHaveProperty('database', 'cmdUri');
expect(result).toHaveProperty('databaseReconnectTimeout', 7000);
const testCases: Array<{
input: CommandLineArguments;
expected: PartialCliOptions;
}> = [
{
input: {
'db-uri': 'cmdUri',
'reconnect-timeout': 7000,
'drop-database': true,
'drop-collections': true,
'transpile-only': true,
},
expected: {
database: 'cmdUri',
databaseReconnectTimeout: 7000,
dropDatabase: true,
dropCollections: true,
transpileOnly: true,
},
},
{
input: {
'db-protocol': 'testing://',
'db-host': 'testHost',
'db-port': 3232,
'db-name': 'testName',
'db-username': 'testUserName',
'db-password': 'testPasswd',
},
expected: {
database: {
protocol: 'testing://',
host: 'testHost',
port: 3232,
name: 'testName',
username: 'testUserName',
password: 'testPasswd',
},
dropDatabase: false,
dropCollections: false,
transpileOnly: false,
},
},
];

expect(result).toMatchObject({
database: 'cmdUri',
databaseReconnectTimeout: 7000,
});
for (const testCase of testCases) {
const result = createConfigFromOptions(testCase.input);
expect(result).toMatchObject(testCase.expected);
}
});

it('should read options from environmental variables', () => {
process.env.DB_URI = 'envUri';
process.env.RECONNECT_TIMEOUT = '5000';
process.env.DROP_DATABASE = 'true';
interface Envs {
[key: string]: string;
}
const testCases: Array<{
envs: Envs;
expected: PartialCliOptions;
}> = [
{
envs: {
DB_URI: 'cmdUri',
RECONNECT_TIMEOUT: '7000',
DROP_DATABASE: 'true',
DROP_COLLECTIONS: 'true',
TRANSPILE_ONLY: 'true',
},
expected: {
database: 'cmdUri',
databaseReconnectTimeout: 7000,
dropDatabase: true,
dropCollections: true,
transpileOnly: true,
},
},
{
envs: {
DB_PROTOCOL: 'testing://',
DB_HOST: 'testHost',
DB_PORT: '3232',
DB_NAME: 'testName',
DB_USERNAME: 'testUserName',
DB_PASSWORD: 'testPasswd',
},
expected: {
database: {
protocol: 'testing://',
host: 'testHost',
port: 3232,
name: 'testName',
username: 'testUserName',
password: 'testPasswd',
},
dropDatabase: false,
dropCollections: false,
transpileOnly: false,
},
},
];

for (const testCase of testCases) {
Object.keys(testCase.envs).forEach(key => {
process.env[key] = testCase.envs[key];
});

const result = createConfigFromOptions({});
const result = createConfigFromOptions({});
expect(result).toMatchObject(testCase.expected);

expect(result).toMatchObject({
database: 'envUri',
databaseReconnectTimeout: 5000,
dropDatabase: true,
});
process.env = previousEnvs;
}
});

it('should overwrite environmental variables with command line arguments', () => {
Expand Down