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

Phase 1 - Configure, initialize, connect and disconnect clients #139

Open
wants to merge 30 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
24d0dff
decoplued argument parsing from run command, and added base test
SkeloGH Nov 8, 2022
8b11f2b
fixed export
SkeloGH Nov 13, 2022
e1e68c3
test jest config
SkeloGH Nov 13, 2022
b518641
test jest config
SkeloGH Nov 13, 2022
0a194a1
test jest config
SkeloGH Nov 13, 2022
0e34a5c
fix test
SkeloGH Nov 13, 2022
d4de2c8
code style fixes
SkeloGH Nov 13, 2022
1c4b182
allow console logs to display in test-dev
SkeloGH Nov 13, 2022
7cfdb08
decoupled and enhanced config validation
SkeloGH Nov 13, 2022
5c7f106
de-coupled run command functionalities
SkeloGH Nov 13, 2022
88c2c34
dded tests for run command parsing and validations
SkeloGH Nov 13, 2022
1355f96
added uuid as dependency
SkeloGH Nov 14, 2022
5be73f3
mongodb and postgres tests to validate and parse cli options
SkeloGH Nov 14, 2022
c796f2a
query/client validation and parsing
SkeloGH Nov 14, 2022
f233964
using explicit corejs version to be compliant with usage guide
SkeloGH Nov 15, 2022
9b15f76
added jest-light-runner
SkeloGH Nov 15, 2022
1f74dae
config for runner, and resolve uuid module
SkeloGH Nov 15, 2022
59dbe63
decoupled db initialization for mongodb
SkeloGH Nov 15, 2022
c36d977
tests cleanup
SkeloGH Nov 15, 2022
3a15eec
validation fixes call to undefined when callback is missing
SkeloGH Nov 15, 2022
2c8c90a
added tests for run command
SkeloGH Nov 15, 2022
db1802f
removed unused ref
SkeloGH Nov 15, 2022
2a80bde
restructured tests and source code for extensibility
SkeloGH Nov 16, 2022
8595e6f
base postgres support estructure
SkeloGH Nov 16, 2022
1356bbd
added WeaverPostgresClient connection support for postgres, includes …
SkeloGH Nov 17, 2022
2e7b59c
upgraded to uuid3
SkeloGH Jan 24, 2023
ce7ed37
upgraded to uuid3
SkeloGH Jan 24, 2023
3b34d17
upgraded to uuid3
SkeloGH Jan 24, 2023
1077018
prep work for postgres integration
SkeloGH Jan 24, 2023
4ff9bec
unit test progress
SkeloGH Aug 2, 2023
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
81 changes: 79 additions & 2 deletions bin/__tests__/commands/run.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,84 @@
const ObjectId = require('bson-objectid');

const WeaverMongoClient = require('../../../src/clients/mongodb');
const run = require('../../commands/run');
const {
_isValidConfig,
_parseClients,
_parseQueries,
parse,
} = require('../../commands/run/index');

describe('weaver run command tests', () => {
test('Base behavior', () => {
describe('weaver-cli run command integrity', () => {
test('Command has the expected properties', () => {
expect(run).not.toBe(undefined);
expect(run.name).not.toBe(undefined);
expect(run.description).not.toBe(undefined);
expect(run.setup).toBeInstanceOf(Function);
expect(run.parse).toBeInstanceOf(Function);
});

test('Command options parsing', () => {
expect(parse).toBeInstanceOf(Function);
expect(parse({})).toMatchObject({});
});
});

describe('weaver-cli MongoDB options and client parsing', () => {
const validType = 'source';
const validDbURL = 'test://';
const validDbName = 'test';
const validDataClient = {
family: 'mongodb',
type: validType,
db: {
url: validDbURL,
name: validDbName,
options: {},
},
};
const validDataClients = [validDataClient];
const validQuery = '507f1f77bcf86cd799439011';
const validQueries = [validQuery];
const validConfig = {
queries: validQueries,
dataClients: validDataClients,
};

test('weaver-cli MongoDB client parsing', () => {
const parsedClients = _parseClients(validDataClients);

expect(_parseClients).toBeInstanceOf(Function);
expect(parsedClients).toBeInstanceOf(Array);

parsedClients.forEach((client) => {
expect(client).toBeInstanceOf(WeaverMongoClient);
});
});

test('weaver-cli MongoDB query parsing', () => {
const parsedQueries = _parseQueries(validQueries);

expect(_parseQueries).toBeInstanceOf(Function);
expect(parsedQueries).toBeInstanceOf(Array);
parsedQueries.forEach((query) => {
expect(query._id).toBeInstanceOf(ObjectId);
});
});

test('weaver-cli MongoDB options validation', () => {
const invalidConfig1 = { queries: [], dataClients: [] };
const invalidConfig2 = { queries: {}, dataClients: {} };
const invalidConfig3 = { queries: 'any', dataClients: 'any' };
const invalidConfig4 = { other: 'value' };
const expectedWhenValid = { validConfig: true, hasQueries: true, hasDataClients: true };
const expectedWhenInvalid = { validConfig: false, hasQueries: false, hasDataClients: false };

expect(_isValidConfig).toBeInstanceOf(Function);
expect(_isValidConfig(invalidConfig1)).toMatchObject(expectedWhenInvalid);
expect(_isValidConfig(invalidConfig2)).toMatchObject(expectedWhenInvalid);
expect(_isValidConfig(invalidConfig3)).toMatchObject(expectedWhenInvalid);
expect(_isValidConfig(invalidConfig4)).toMatchObject(expectedWhenInvalid);
expect(_isValidConfig(validConfig)).toMatchObject(expectedWhenValid);
});
});
56 changes: 2 additions & 54 deletions bin/commands/run.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,8 @@
const Debug = require('debug');
const shell = require('shelljs');
const ObjectId = require('bson-objectid');
const ldLang = require('lodash');

const {
getConfig,
} = require('../lib/config');
const Weaver = require('../../src');
const WeaverMongoClient = require('../../src/clients/mongodb');

const logging = Debug('Weaver:bin:commands:run');
const clientsByFamily = {
mongodb: WeaverMongoClient,
};
const { parse } = require('./run/');

module.exports = {
name: 'run',
description: 'Runs the app with the loaded configuration',
setup: (yargs) => yargs,
parse: (_argv) => {
logging(`getting config from argv ${_argv}`);
const config = getConfig(_argv);
const configStr = JSON.stringify(config, null, 2);
let message = `Running with the current configuration"\n ${configStr}`;
const hasQueries = !ldLang.isEmpty(config.queries);
const hasDataClients = !ldLang.isEmpty(config.dataClients);
const validConfig = hasDataClients && hasQueries;

if (!hasDataClients) {
message = `Error: dataClients not set, try:
weaver add [client|query|ignore]
`;
}
if (!hasQueries) {
message = `Error: queries not set, try:
weaver run --queries <a document id>
`;
}
shell.echo(message);
if (!validConfig) return _argv;

// TODO: need to delegate this conversion to each client
// once starting to add new client families
config.dataClients = config.dataClients.map((c) => {
const ClientConstructor = clientsByFamily[c.family];
if (ClientConstructor) { return new ClientConstructor(c); }
return c;
});
config.queries = config.queries.map((q) => ({ _id: ObjectId(q) }));

const weaver = new Weaver(config);
weaver.run((result) => {
logging('Result', result);
shell.echo('Done');
weaver.disconnect(process.exit);
});
return _argv;
},
parse,
};
114 changes: 114 additions & 0 deletions bin/commands/run/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
const Debug = require('debug');
const shell = require('shelljs');
const ObjectId = require('bson-objectid');
const ldLang = require('lodash/lang');
const ldObject = require('lodash/object');
const ldColl = require('lodash/collection');

const {
getConfig,
} = require('../../lib/config');
const Weaver = require('../../../src');
const WeaverMongoClient = require('../../../src/clients/mongodb');

const logging = Debug('Weaver:bin:commands:run');
const clientsByFamily = {
mongodb: WeaverMongoClient,
};

// TODO: once starting to add new client families,
// add `family` as required second param, as it will be used to define clients and queries
const _hasValidQueries = (queries) => {
let validQueries = ldLang.isArray(queries);
validQueries = validQueries && !ldLang.isEmpty(queries);
validQueries = validQueries && !ldLang.isEmpty(queries[0]);
validQueries = validQueries && ldColl.every(queries, ObjectId.isValid);
return validQueries;
};

// TODO: once starting to add new client families,
// add `family` as required second param, as it will be used to define clients and queries
const _hasValidDataClient = (dataClient) => {
let validDataClient = ldLang.isObject(dataClient);
validDataClient = validDataClient && ldObject.hasIn(dataClient, 'type');
validDataClient = validDataClient && (dataClient.type === 'target' || dataClient.type === 'source');
validDataClient = validDataClient && ldObject.hasIn(dataClient, 'db');
validDataClient = validDataClient && ldObject.hasIn(dataClient, 'db.url');
validDataClient = validDataClient && ldObject.hasIn(dataClient, 'db.name');
validDataClient = validDataClient && ldObject.hasIn(dataClient, 'db.options');
validDataClient = validDataClient && ldLang.isObject(dataClient.db.options);
return validDataClient;
};

// TODO: once starting to add new client families,
// add `family` as required second param, as it will be used to define clients and queries
const _hasValidDataClientss = (dataClients) => {
let validDataClients = ldLang.isArray(dataClients);
validDataClients = validDataClients && !ldLang.isEmpty(dataClients);
validDataClients = validDataClients && ldColl.every(dataClients, _hasValidDataClient);
return validDataClients;
};

// TODO: once starting to add new client families,
// add `family` as required second param, as it will be used to define clients and queries
const _parseClients = (dataClients) => dataClients.map((c) => {
SkeloGH marked this conversation as resolved.
Show resolved Hide resolved
const ClientConstructor = clientsByFamily[c.family];
if (ClientConstructor) { return new ClientConstructor(c); }
return c;
});

// TODO: once starting to add new client families,
// add `family` as required second param, as it will be used to define clients and queries
const _parseQueries = (queries) => {
const parsedQueries = queries.map((q) => ({ _id: ObjectId(q) }));
return parsedQueries;
};

const _isValidConfig = (config) => {
const hasQueries = _hasValidQueries(config.queries);
const hasDataClients = _hasValidDataClientss(config.dataClients);
const validConfig = hasDataClients && hasQueries;
return { hasQueries, hasDataClients, validConfig };
};

const parse = (_argv) => {
logging(`getting config from argv ${_argv}`);
const config = getConfig(_argv);
const configStr = JSON.stringify(config, null, 2);
let message = `Running with the current configuration"\n ${configStr}`;
const { validConfig, hasQueries, hasDataClients } = _isValidConfig(config);

if (!hasDataClients) {
message = `Error: dataClients not set, try:
weaver add client
`;
}
if (!hasQueries) {
message = `Error: queries not set, try:
weaver run --queries <a document id>
`;
}
shell.echo(message);
if (!validConfig) return _argv;

config.dataClients = _parseClients(config.dataClients);
config.queries = _parseQueries(config.queries);

const weaver = new Weaver(config);
weaver.run((result) => {
logging('Result', result);
shell.echo('Done');
weaver.disconnect(process.exit);
});
return _argv;
};

module.exports = {
_hasValidQueries,
_hasValidDataClient,
_hasValidDataClientss,
_isValidConfig,
_parseClients,
_parseQueries,
parse,
};
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"dev": "DEBUG=* nodemon --config ./nodemon.json ./src/index.js",
"debug-install": "MONGOMS_DEBUG=1 npm install --foreground-scripts",
"test": "jest --runInBand",
"test-debug": "jest --clearCache && MONGOMS_DEBUG=1 DEBUG=Weaver* jest --runInBand --detectOpenHandles --debug",
"test-dev": "MONGOMS_DEBUG=1 jest --runInBand --detectOpenHandles --onlyChanged",
"test-coverage": "jest --runInBand --detectOpenHandles --coverage --coverageReporters=text-lcov | coveralls"
"test-debug": "jest --clearCache && MONGOMS_DEBUG=1 DEBUG=Weaver* jest --runInBand --detectOpenHandles --silent=false --debug",
"test-dev": "jest --runInBand --detectOpenHandles --onlyChanged --silent=false",
"test-coverage": "jest --runInBand --detectOpenHandles --coverage --coverageReporters=text-lcov | coveralls"
},
"bin": {
"weaver": "dist/bin/cli.js"
Expand Down