diff --git a/Makefile b/Makefile index 34cf31f..a53a99a 100644 --- a/Makefile +++ b/Makefile @@ -50,7 +50,7 @@ watch: # lint test files lint: - eslint 'app' 'test' + command -v eslint >/dev/null 2>&1 && eslint 'app' 'test' || ./node_modules/lint-rules/node_modules/.bin/eslint 'app' 'test'; # run unit tests test: @@ -73,7 +73,7 @@ post-coverage: # The command the ci server runs ci: - make lint build-source-maps -i + make lint || exit 1 # if the tests fail then it will exit with an error make coverage || exit 1 # show the coverage report diff --git a/app/documents.js b/app/documents.js index 34090f7..fd96498 100644 --- a/app/documents.js +++ b/app/documents.js @@ -1,3 +1,9 @@ +let exit = false; +process.on('SIGINT', () => { + exit = true; + process.exit(2); +}); + import faker from 'faker'; import Chance from 'chance'; import Base from './base'; @@ -6,13 +12,112 @@ import { set, get } from 'lodash'; import to from 'to-js'; //// -/// @name Document -/// @page api/document +/// @name Documents +/// @page api/documents //// +/// @name Documents +/// @description This class is used to generate all the documents for the passed models +export default class Documents extends Base { + constructor(options = {}, documents = {}, globals = {}, inputs = {}) { + super(options); + this.options = to.extend({ count: 0 }, this.options); + this.documents = documents; + this.globals = globals; + this.inputs = inputs; + this.total = 0; + } + + ///# @name build + ///# @description + ///# This takes an array of models and builds them + ///# @arg {array} models - Array of models + ///# @returns {array} of documents + ///# @async + build(models) { + models = to.clone(models); + const todo = models.map((model) => model.file); + const result = []; + + function finished(dependencies) { + for (let file of dependencies) { + for (let model of models) { + if (model.file === file && !model.complete) { + return false; + } + } + } + + return true; + } + + this.on('run', () => { + // don't run again if the program should exit + if (exit) return; + for (let model of models) { + const index = todo.indexOf(model.file); + if (index > -1 && finished(model.data.dependencies)) { + todo.splice(index, 1); + const document = this.document(model); + + if (!model.is_dependency) { + result.push(document); + } + } + + // if the model is complete and it's dependants are also completed is no longer needed then remove it + if (model.complete && finished(model.dependants)) { + delete this.documents[model.name]; + } + } + + if (!todo.length) { + this.emit('finished'); + } + }); + + this.emit('run'); + + return new Promise((resolve, reject) => { + this.once('error', (err) => { + reject(err); + }); + this.once('finished', () => { + Promise.all(result).then((res) => { + this.inputs = {}; + resolve(res); + }); + }); + }); + } + + document(model) { + const document = new Document(this.options, this.documents, this.globals, this.inputs); + return document.build(model) + .then(() => { + let result; + if (!model.is_dependency) { + result = this.emit('data', this.documents[model.name], model); + } + // update the total documents number + this.total += this.documents[model.name].length; + model.complete = true; + this.emit('run'); + return result; + }) + .catch((err) => { + exit = true; + process.exit(2); + this.emit('error', err); + }); + } +} + + + /// @name Document /// @description This is used to generate documents based off a model -export default class Document extends Base { +export class Document extends Base { constructor(options = {}, documents = {}, globals = {}, inputs = {}) { super(options); this.options = to.extend({ count: 0 }, this.options); @@ -59,12 +164,19 @@ export default class Document extends Base { spinner.start(); const delay = (duration) => new Promise((resolve) => setTimeout(resolve, duration)); await pool(model.data.count, async (i) => { // loop over each model and execute in order of dependency + // don't anymore if the program should exit + if (exit) return; + + update(); // this allows the spinner to actually update the count and doesn't affect performance much const doc = await this.buildDocument(model, i); update(); await delay(0); this.documents[model.name].push(doc); - }, this.options.spinners ? 75 : 1000); + }, this.options.spinners ? 75 : 1000) + .catch((err) => { + spinner.fail(err); + }); this.runData(model.data.post_run, model); diff --git a/app/index.js b/app/index.js index b76b22e..df9d586 100644 --- a/app/index.js +++ b/app/index.js @@ -1,10 +1,9 @@ import Models from './models'; -import { map } from 'async-array-methods'; import Output from './output/index'; import Base from './base'; import to from 'to-js'; import { uniqueId } from 'lodash'; -import Document from './documents'; +import Documents from './documents'; import { success } from 'log-symbols'; /// @name Fakeit @@ -53,27 +52,17 @@ export default class Fakeit extends Base { output.prepare(); await model.registerModels(models); - - const document = new Document(this.options, this.documents, this.globals, model.inputs); - - let result = []; - - for (let obj of to.flatten(model.models)) { - const value = await document.build(obj); - if (!obj.is_dependency) { - result.push(value); - } - } - - result = await Promise.all(result); - await output.preparing; - result = await map(result, (data) => output.output(data)); + + const documents = new Documents(this.options, this.documents, this.globals, model.inputs); + delete model.inputs; + let result = documents.build(model.models); + documents.on('data', (data) => output.output(data)); + result = await result; await output.finalize(); const time = this.timeEnd(label); - const total = to.reduce(document.documents, (prev, { value }) => prev + value.length, 0); if (this.options.verbose) { - console.log(`${success} Finished generating ${total} documents in ${time}`); + console.log(`${success} Finished generating ${documents.total} documents in ${time}`); } return result; diff --git a/app/logger.js b/app/logger.js index f2191b8..7922d53 100644 --- a/app/logger.js +++ b/app/logger.js @@ -6,14 +6,18 @@ symbols.warn = symbols.warning; symbols.ok = symbols.okay = symbols.success; import ora from 'ora'; import formatSeconds from 'format-seconds'; +import Emitter from 'events-async'; + /// @name Logger /// @description /// This is the main logger for the application -export default class Logger { +export default class Logger extends Emitter { ///# @name constructor ///# @arg {object} options [{ log: true, verbose: false, timestamp: true }] constructor(options = {}) { + super(); + this.setMaxListeners(50); this.options = to.extend({ log: true, verbose: false, diff --git a/app/models.js b/app/models.js index 5134427..ec7f3ee 100644 --- a/app/models.js +++ b/app/models.js @@ -30,8 +30,6 @@ export default class Models extends Base { this.registered_models = []; // holds the paths that have already been added this.prepared = false; - - this.progress = this.spinner('Models'); } ///# @name prepare @@ -109,7 +107,7 @@ export default class Models extends Base { } async registerModels(models, dependency = false) { - this.progress.start(); + this.progress = this.progress || this.spinner('Models').start(); // if models weren't passed in then don't do anything if (!models) { return; @@ -189,6 +187,7 @@ export default class Models extends Base { // update the models order this.models = resolveDependenciesOrder(this.models); + this.models = resolveDependants(this.models); if (this.models.length === this.registered_models.length) { this.progress.stop(); @@ -216,7 +215,7 @@ export default class Models extends Base { parseModelSeed(model, this.options.seed); // add this models inputs to the main inputs object - this.inputs = to.extend(this.inputs || {}, await inputs); + this.inputs = Object.assign(this.inputs || {}, await inputs); await dependencies; return model; } @@ -236,7 +235,7 @@ export default class Models extends Base { } // get list of files, flatten the array of files and filter files for valid input formats: yaml - const files = this.filterModelFiles(await utils.findFiles(model.data.dependencies)); + const files = to.flatten(await utils.findFiles(model.data.dependencies)); if (!files.length) { model.data.dependencies = []; @@ -260,7 +259,7 @@ export async function parseModelInputs(model) { !model.data.inputs || !model.data.inputs.length ) { - model.data.inputs = {}; + model.data.inputs = []; return {}; } @@ -286,7 +285,6 @@ export async function parseModelInputs(model) { return file; }); - model.data.inputs = inputs; return inputs; } @@ -481,3 +479,27 @@ export function resolveDependenciesOrder(models = []) { return resolver.sort().map((file) => models[order[file]]); } + + +/// @name resolveDependenciesOf +/// @description Figures out which models use the model as a dependency +/// @arg {array} models [[]] - The models to loop over +/// @returns {array} - The models are returned with the `dependants` +export function resolveDependants(models = []) { + return models.map((model) => { + // loop over each model and find out which other models depend on the current model + model.dependants = models.reduce((prev, next) => { + if ( + // the next file in the loop doesn't matche the current models file + model.file !== next.file && + // the next models dependencies includes the current models files + next.data.dependencies.includes(model.file) + ) { + prev.push(next.file); + } + return prev; + }, []); + + return model; + }); +} diff --git a/package.json b/package.json index 49bfe5f..42e7d23 100644 --- a/package.json +++ b/package.json @@ -46,20 +46,21 @@ "dependencies": { "adm-zip": "^0.4.7", "async-array-methods": "^2.1.0", - "babel-core": "~6.20.0", - "babel-helpers": "~6.16.0", - "babel-runtime": "^6.18.0", + "babel-core": "~6.22.1", + "babel-helpers": "~6.22.0", + "babel-runtime": "~6.22.0", "chalk": "^1.1.3", "chance": "^1.0.4", "cli-table": "~0.3.1", "commander": "^2.9.0", - "couchbase-promises": "~2.2.0", + "couchbase-promises": "~3.0.1", "cson": "^4.0.0", "csv-parse": "^1.1.7", "csv-stringify": "^1.0.4", "dependency-resolver": "~2.0.1", "es6-promise-pool": "^2.4.4", "es6-promisify": "^5.0.0", + "events-async": "~1.1.0", "faker": "^3.1.0", "find-root": "~1.0.0", "format-seconds": "~0.3.1", @@ -71,7 +72,7 @@ "ora": "~0.4.1", "perfy": "~1.1.2", "request": "^2.78.0", - "set-cookie-parser": "^1.0.2", + "set-cookie-parser": "~2.0.0", "to-js": "^0.0.6", "update-notifier": "^1.0.2", "yamljs": "^0.2.8" @@ -79,21 +80,21 @@ "devDependencies": { "ava": "^0.16.0", "ava-spec": "github:tjbenton/ava-spec", - "babel-cli": "^6.18.0", + "babel-cli": "~6.22.2", "babel-plugin-array-includes": "~2.0.3", - "babel-plugin-external-helpers": "~6.18.0", - "babel-plugin-transform-runtime": "~6.15.0", - "babel-preset-latest": "^6.16.0", - "babel-preset-stage-0": "^6.16.0", + "babel-plugin-external-helpers": "~6.22.0", + "babel-plugin-transform-runtime": "~6.22.0", + "babel-preset-latest": "~6.22.0", + "babel-preset-stage-0": "~6.22.0", "coveralls": "^2.11.15", "docs-core": "0.0.0-alpha-6.1", "docs-theme-default": "0.0.0-alpha-6.1", "get-stream": "~3.0.0", - "joi": "~10.0.5", + "joi": "~10.2.0", "jsondiffpatch": "~0.2.4", "lint-rules": "github:ma-shop/lint-rules", "nixt": "~0.5.0", - "nyc": "~9.0.1", + "nyc": "~10.1.2", "proxyquire": "~1.7.10", "test-console": "~1.0.0" }, @@ -118,7 +119,7 @@ "test/utils.js" ], "failFast": true, - "concurrency": 5, + "concurrency": 1, "babel": "inherit" }, "nyc": { diff --git a/test/base.test.js b/test/base.test.js index e8ebc1e..a7adfc8 100644 --- a/test/base.test.js +++ b/test/base.test.js @@ -9,6 +9,12 @@ const test = ava.group('base:'); test('without args', (t) => { const base = new Base(); t.deepEqual(base, { + // inherited from events-async + domain: null, + _events: {}, + _eventsCount: 0, + _maxListeners: 50, + options: { root: process.cwd(), log: true, @@ -32,6 +38,12 @@ test('without args', (t) => { test('with args', (t) => { const base = new Base({ log: false }); t.deepEqual(base, { + // inherited from events-async + domain: null, + _events: {}, + _eventsCount: 0, + _maxListeners: 50, + options: { root: process.cwd(), log: false, diff --git a/test/documents.test.js b/test/documents.test.js index f444dbd..3bd6c50 100644 --- a/test/documents.test.js +++ b/test/documents.test.js @@ -1,9 +1,10 @@ /* eslint-disable no-undefined */ -import Document, { +import { transformValueToType, getPaths, typeToValue, + Document, } from '../dist/documents.js'; /* istanbul ignore next : needed to test models */ const Model = require('../dist/models.js').default; diff --git a/test/fixtures/models/contacts/validation/contacts.model.js b/test/fixtures/models/contacts/validation/contacts.model.js index d6ff215..86c75e9 100644 --- a/test/fixtures/models/contacts/validation/contacts.model.js +++ b/test/fixtures/models/contacts/validation/contacts.model.js @@ -6,6 +6,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), type: 'object', key: '_id', seed: 0, @@ -13,7 +14,7 @@ module.exports = is.object({ min: 200, max: 400, count: is.number().min(200).max(400), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), dependencies: is.array().length(0), }, properties: { diff --git a/test/fixtures/models/ecommerce/validation/orders.model.js b/test/fixtures/models/ecommerce/validation/orders.model.js index 49d0863..6fe7dba 100644 --- a/test/fixtures/models/ecommerce/validation/orders.model.js +++ b/test/fixtures/models/ecommerce/validation/orders.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 600, count: is.number().min(300).max(600), dependencies: is.array().items(is.string()).length(2), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), }, properties: { _id: utils.check('string', 'The document id', { post_build: is.func(), }), diff --git a/test/fixtures/models/ecommerce/validation/products.model.js b/test/fixtures/models/ecommerce/validation/products.model.js index 746fc5a..67f0d2b 100644 --- a/test/fixtures/models/ecommerce/validation/products.model.js +++ b/test/fixtures/models/ecommerce/validation/products.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 500, count: is.number().min(100).max(500), dependencies: is.array().length(0), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), }, properties: { _id: utils.check('string', 'The document id', { post_build: is.func(), }), diff --git a/test/fixtures/models/ecommerce/validation/reviews.model.js b/test/fixtures/models/ecommerce/validation/reviews.model.js index bc5bb4e..30226b1 100644 --- a/test/fixtures/models/ecommerce/validation/reviews.model.js +++ b/test/fixtures/models/ecommerce/validation/reviews.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 1000, count: is.number().min(500).max(1000), dependencies: is.array().items(is.string()).length(2), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), }, properties: { _id: utils.check('string', 'The document id', { post_build: is.func(), }), diff --git a/test/fixtures/models/ecommerce/validation/users.model.js b/test/fixtures/models/ecommerce/validation/users.model.js index 40cafeb..557efd0 100644 --- a/test/fixtures/models/ecommerce/validation/users.model.js +++ b/test/fixtures/models/ecommerce/validation/users.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 500, count: is.number().min(200).max(500), dependencies: is.array().length(0), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/flat/validation/products.model.js b/test/fixtures/models/flat/validation/products.model.js index 9d2dd21..b619245 100644 --- a/test/fixtures/models/flat/validation/products.model.js +++ b/test/fixtures/models/flat/validation/products.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 500, count: is.number().min(100).max(500), dependencies: is.array().length(0), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), }, properties: { _id: utils.check('string', 'The document id', { post_build: is.func(), }), diff --git a/test/fixtures/models/flat/validation/users.model.js b/test/fixtures/models/flat/validation/users.model.js index 9938b19..aba998d 100644 --- a/test/fixtures/models/flat/validation/users.model.js +++ b/test/fixtures/models/flat/validation/users.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: 'user_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 400, count: is.number().min(100).max(400), dependencies: is.array().length(0), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), }, properties: { user_id: utils.check('integer', 'The users id', { build: is.func(), }), diff --git a/test/fixtures/models/flight-data/validation/airline_reviews.model.js b/test/fixtures/models/flight-data/validation/airline_reviews.model.js index 2b287d0..2095819 100644 --- a/test/fixtures/models/flight-data/validation/airline_reviews.model.js +++ b/test/fixtures/models/flight-data/validation/airline_reviews.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(2), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), pre_build: is.func(), }, diff --git a/test/fixtures/models/flight-data/validation/airlines.model.js b/test/fixtures/models/flight-data/validation/airlines.model.js index 4f25ca7..ec64121 100644 --- a/test/fixtures/models/flight-data/validation/airlines.model.js +++ b/test/fixtures/models/flight-data/validation/airlines.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(1), - inputs: is.object().length(1), + inputs: is.array().items(is.string()).length(1), pre_run: is.func(), post_build: is.func(), }, diff --git a/test/fixtures/models/flight-data/validation/airport_airlines.model.js b/test/fixtures/models/flight-data/validation/airport_airlines.model.js index 707f0c3..4c7e643 100644 --- a/test/fixtures/models/flight-data/validation/airport_airlines.model.js +++ b/test/fixtures/models/flight-data/validation/airport_airlines.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(2), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/flight-data/validation/airport_frequencies.model.js b/test/fixtures/models/flight-data/validation/airport_frequencies.model.js index acc600f..03802db 100644 --- a/test/fixtures/models/flight-data/validation/airport_frequencies.model.js +++ b/test/fixtures/models/flight-data/validation/airport_frequencies.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(2), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/flight-data/validation/airport_navaids.model.js b/test/fixtures/models/flight-data/validation/airport_navaids.model.js index ce3177a..f25b36c 100644 --- a/test/fixtures/models/flight-data/validation/airport_navaids.model.js +++ b/test/fixtures/models/flight-data/validation/airport_navaids.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(2), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/flight-data/validation/airport_reviews.model.js b/test/fixtures/models/flight-data/validation/airport_reviews.model.js index ac19c3c..9c2f35f 100644 --- a/test/fixtures/models/flight-data/validation/airport_reviews.model.js +++ b/test/fixtures/models/flight-data/validation/airport_reviews.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(2), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), pre_build: is.func(), }, diff --git a/test/fixtures/models/flight-data/validation/airport_runways.model.js b/test/fixtures/models/flight-data/validation/airport_runways.model.js index b5e1934..c015052 100644 --- a/test/fixtures/models/flight-data/validation/airport_runways.model.js +++ b/test/fixtures/models/flight-data/validation/airport_runways.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(2), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/flight-data/validation/airports.model.js b/test/fixtures/models/flight-data/validation/airports.model.js index 9b0579b..389ae41 100644 --- a/test/fixtures/models/flight-data/validation/airports.model.js +++ b/test/fixtures/models/flight-data/validation/airports.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(0), - inputs: is.object().length(1), + inputs: is.array().items(is.string()).length(1), pre_run: is.func(), post_build: is.func(), }, diff --git a/test/fixtures/models/flight-data/validation/codes.model.js b/test/fixtures/models/flight-data/validation/codes.model.js index a6cee52..25b1192 100644 --- a/test/fixtures/models/flight-data/validation/codes.model.js +++ b/test/fixtures/models/flight-data/validation/codes.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(2), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/flight-data/validation/continents.model.js b/test/fixtures/models/flight-data/validation/continents.model.js index d1656bf..cc415d7 100644 --- a/test/fixtures/models/flight-data/validation/continents.model.js +++ b/test/fixtures/models/flight-data/validation/continents.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(0), - inputs: is.object().length(1), + inputs: is.array().items(is.string()).length(1), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/flight-data/validation/countries.model.js b/test/fixtures/models/flight-data/validation/countries.model.js index 13ae226..5bd01c7 100644 --- a/test/fixtures/models/flight-data/validation/countries.model.js +++ b/test/fixtures/models/flight-data/validation/countries.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(0), - inputs: is.object().length(1), + inputs: is.array().items(is.string()).length(1), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/flight-data/validation/frequencies.model.js b/test/fixtures/models/flight-data/validation/frequencies.model.js index cf44e2c..fe7df55 100644 --- a/test/fixtures/models/flight-data/validation/frequencies.model.js +++ b/test/fixtures/models/flight-data/validation/frequencies.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(0), - inputs: is.object().length(1), + inputs: is.array().items(is.string()).length(1), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/flight-data/validation/navaids.model.js b/test/fixtures/models/flight-data/validation/navaids.model.js index 2e4329f..8227e17 100644 --- a/test/fixtures/models/flight-data/validation/navaids.model.js +++ b/test/fixtures/models/flight-data/validation/navaids.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(0), - inputs: is.object().length(1), + inputs: is.array().items(is.string()).length(1), pre_run: is.func(), post_build: is.func(), }, diff --git a/test/fixtures/models/flight-data/validation/regions.model.js b/test/fixtures/models/flight-data/validation/regions.model.js index 21c104c..b787688 100644 --- a/test/fixtures/models/flight-data/validation/regions.model.js +++ b/test/fixtures/models/flight-data/validation/regions.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(0), - inputs: is.object().length(1), + inputs: is.array().items(is.string()).length(1), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/flight-data/validation/routes.model.js b/test/fixtures/models/flight-data/validation/routes.model.js index 6ea00f1..5657373 100644 --- a/test/fixtures/models/flight-data/validation/routes.model.js +++ b/test/fixtures/models/flight-data/validation/routes.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(0), - inputs: is.object().length(1), + inputs: is.array().items(is.string()).length(1), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/flight-data/validation/runways.model.js b/test/fixtures/models/flight-data/validation/runways.model.js index f8c8edd..f8e1e20 100644 --- a/test/fixtures/models/flight-data/validation/runways.model.js +++ b/test/fixtures/models/flight-data/validation/runways.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(0), - inputs: is.object().length(1), + inputs: is.array().items(is.string()).length(1), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/flight-data/validation/users.model.js b/test/fixtures/models/flight-data/validation/users.model.js index 74d9ab5..47ed704 100644 --- a/test/fixtures/models/flight-data/validation/users.model.js +++ b/test/fixtures/models/flight-data/validation/users.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 10000, dependencies: is.array().items(is.string()).length(1), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), pre_build: is.func(), post_build: is.func(), diff --git a/test/fixtures/models/music/validation/countries.model.js b/test/fixtures/models/music/validation/countries.model.js index a027be1..4440ac9 100644 --- a/test/fixtures/models/music/validation/countries.model.js +++ b/test/fixtures/models/music/validation/countries.model.js @@ -7,22 +7,14 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { min: 0, max: 0, count: 1, - inputs: { - countries: is.array() - .items({ - name: is.string(), - iso_2: is.string().uppercase().length(2), - iso_3: is.string().uppercase().length(3), - iso_m49: is.number(), - }) - .length(247), - }, + inputs: is.array().items(is.string()).length(1), dependencies: is.array().length(0), pre_run: is.func(), pre_build: is.func(), diff --git a/test/fixtures/models/music/validation/playlists.model.js b/test/fixtures/models/music/validation/playlists.model.js index 62b2863..1cd6301 100644 --- a/test/fixtures/models/music/validation/playlists.model.js +++ b/test/fixtures/models/music/validation/playlists.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 1000, count: is.number().min(500).max(1000), dependencies: is.array().items(is.string()).length(2), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), }, properties: { _id: utils.check('string', 'The document id', { post_build: is.func(), }), diff --git a/test/fixtures/models/music/validation/tracks.model.js b/test/fixtures/models/music/validation/tracks.model.js index 98f7932..b5a04f0 100644 --- a/test/fixtures/models/music/validation/tracks.model.js +++ b/test/fixtures/models/music/validation/tracks.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 800, count: is.number().min(500).max(800), dependencies: is.array().items(is.string()).length(1), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), }, properties: { _id: utils.check('string', 'The document id', { post_build: is.func(), }), diff --git a/test/fixtures/models/music/validation/users.model.js b/test/fixtures/models/music/validation/users.model.js index 428a041..4147ed7 100644 --- a/test/fixtures/models/music/validation/users.model.js +++ b/test/fixtures/models/music/validation/users.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 600, count: is.number().min(400).max(600), dependencies: is.array().length(0), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), }, properties: { _id: utils.check('string', 'The document id', { post_build: is.func(), }), diff --git a/test/fixtures/models/simple/validation/users.model.js b/test/fixtures/models/simple/validation/users.model.js index 9ef1a5f..4950ab6 100644 --- a/test/fixtures/models/simple/validation/users.model.js +++ b/test/fixtures/models/simple/validation/users.model.js @@ -6,6 +6,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: 'id', seed: 0, data: { @@ -13,7 +14,7 @@ module.exports = is.object({ max: 100, count: is.number().min(50).max(100), dependencies: is.array().length(0), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), }, properties: { id: { diff --git a/test/fixtures/models/social/validation/addresses.model.js b/test/fixtures/models/social/validation/addresses.model.js index 2ad54e4..b02fa1e 100644 --- a/test/fixtures/models/social/validation/addresses.model.js +++ b/test/fixtures/models/social/validation/addresses.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(1), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/social/validation/auth.model.js b/test/fixtures/models/social/validation/auth.model.js index b04cdac..9ba3d89 100644 --- a/test/fixtures/models/social/validation/auth.model.js +++ b/test/fixtures/models/social/validation/auth.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(1), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/social/validation/chat_messages.model.js b/test/fixtures/models/social/validation/chat_messages.model.js index abe12e7..3fec34e 100644 --- a/test/fixtures/models/social/validation/chat_messages.model.js +++ b/test/fixtures/models/social/validation/chat_messages.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(2), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), pre_build: is.func(), }, diff --git a/test/fixtures/models/social/validation/chats.model.js b/test/fixtures/models/social/validation/chats.model.js index 8781c9e..01e1805 100644 --- a/test/fixtures/models/social/validation/chats.model.js +++ b/test/fixtures/models/social/validation/chats.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(1), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/social/validation/emails.model.js b/test/fixtures/models/social/validation/emails.model.js index 6ba7782..5974d76 100644 --- a/test/fixtures/models/social/validation/emails.model.js +++ b/test/fixtures/models/social/validation/emails.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(1), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/social/validation/friends.model.js b/test/fixtures/models/social/validation/friends.model.js index 1a78718..c6315c2 100644 --- a/test/fixtures/models/social/validation/friends.model.js +++ b/test/fixtures/models/social/validation/friends.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(1), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/social/validation/likes.model.js b/test/fixtures/models/social/validation/likes.model.js index 4926035..fce8f0f 100644 --- a/test/fixtures/models/social/validation/likes.model.js +++ b/test/fixtures/models/social/validation/likes.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(2), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/social/validation/phones.model.js b/test/fixtures/models/social/validation/phones.model.js index 3756614..4dfeffe 100644 --- a/test/fixtures/models/social/validation/phones.model.js +++ b/test/fixtures/models/social/validation/phones.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(1), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/social/validation/posts.model.js b/test/fixtures/models/social/validation/posts.model.js index 5ffa8e8..6ba02b6 100644 --- a/test/fixtures/models/social/validation/posts.model.js +++ b/test/fixtures/models/social/validation/posts.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(1), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/social/validation/user_addresses.model.js b/test/fixtures/models/social/validation/user_addresses.model.js index 5857e89..f74afe2 100644 --- a/test/fixtures/models/social/validation/user_addresses.model.js +++ b/test/fixtures/models/social/validation/user_addresses.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(2), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/social/validation/user_emails.model.js b/test/fixtures/models/social/validation/user_emails.model.js index cb450db..0fd102a 100644 --- a/test/fixtures/models/social/validation/user_emails.model.js +++ b/test/fixtures/models/social/validation/user_emails.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(2), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/social/validation/user_phones.model.js b/test/fixtures/models/social/validation/user_phones.model.js index b471937..0f4c52c 100644 --- a/test/fixtures/models/social/validation/user_phones.model.js +++ b/test/fixtures/models/social/validation/user_phones.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1, dependencies: is.array().items(is.string()).length(2), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/social/validation/users.model.js b/test/fixtures/models/social/validation/users.model.js index 8f57d13..d32c5db 100644 --- a/test/fixtures/models/social/validation/users.model.js +++ b/test/fixtures/models/social/validation/users.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,7 +15,7 @@ module.exports = is.object({ max: 0, count: 1000, dependencies: is.array().length(0), - inputs: is.object().length(0), + inputs: is.array().items(is.string()).length(0), pre_run: is.func(), }, properties: { diff --git a/test/fixtures/models/zip-input/validation/users.model.js b/test/fixtures/models/zip-input/validation/users.model.js index f7d692e..6c280aa 100644 --- a/test/fixtures/models/zip-input/validation/users.model.js +++ b/test/fixtures/models/zip-input/validation/users.model.js @@ -7,6 +7,7 @@ module.exports = is.object({ file: is.string(), root: is.string(), is_dependency: is.boolean(), + dependants: is.array(), key: '_id', seed: 0, data: { @@ -14,29 +15,7 @@ module.exports = is.object({ max: 500, count: is.number().min(100).max(500), dependencies: is.array().length(0), - inputs: { - address: { - types: is.array().items(is.string()).length(3) - }, - email: { - types: is.array().items(is.string()).length(3) - }, - phone: { - types: is.array().items(is.string()).length(8) - }, - countries: is.array() - .items({ - code: is.string().uppercase(), - name: is.string().regex(/[A-Z].+/), - }).length(247), - regions: is.array() - .items({ - code: is.string().regex(/[A-Z0-9-]{4,}/), - name: is.string(), - iso_country: is.string().uppercase(), - }) - .length(3999), - }, + inputs: is.array().items(is.string()).length(1), pre_run: is.func(), pre_build: is.func(), }, diff --git a/test/index.test.js b/test/index.test.js index e31c17e..c0a5bf8 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -96,21 +96,17 @@ generate.group('supports globs', (test) => { const actual = await t.context.fakeit.generate('ecommerce/**/*.yaml', t.context.defaults); t.is(actual.length, 4); - - const doc_types = actual.map((item) => to.object(item)[0].doc_type); - const documents = [ 'Products', 'Users', 'Reviews', 'Orders' ]; - t.deepEqual(to.keys(t.context.fakeit.documents), documents); - t.deepEqual(doc_types, documents.map((doc) => doc.toLowerCase().slice(0, -1))); + const doc_types = actual.map((item) => to.object(item)[0].doc_type).sort(); + t.deepEqual(doc_types, [ 'product', 'user', 'review', 'order' ].sort()); }); test('ecommerce/**/@(o|p)*.yaml', async (t) => { t.context.defaults.output = 'return'; const actual = await t.context.fakeit.generate('ecommerce/**/@(orders|products).yaml', t.context.defaults); - // expect 3 documents because orders requires `products.yaml`, and `users.yaml` - t.deepEqual(to.keys(t.context.fakeit.documents), [ 'Products', 'Users', 'Orders' ]); + // only the files that were passed should be returned which should be `products`, and `orders` - const doc_types = actual.map((item) => to.object(item)[0].doc_type); - t.deepEqual(doc_types, [ 'product', 'order' ]); + const doc_types = actual.map((item) => to.object(item)[0].doc_type).sort(); + t.deepEqual(doc_types, [ 'order', 'product' ]); }); }); diff --git a/test/models.test.js b/test/models.test.js index 4c63299..360336f 100644 --- a/test/models.test.js +++ b/test/models.test.js @@ -49,9 +49,15 @@ test.beforeEach((t) => { }); -test('without args', async (t) => { +test('without args', (t) => { t.context.options.log = true; const expected = { + // inherited from events-async + domain: null, + _events: {}, + _eventsCount: 0, + _maxListeners: 50, + options: { root: models_root, log: true, @@ -68,7 +74,6 @@ test('without args', async (t) => { prepared: is.boolean(), registered_models: is.array().length(0), spinners: is.object().required(), - progress: is.object().required(), }; const { error } = is.validate(t.context, expected); if (error) { @@ -258,7 +263,6 @@ test.group('parseModelDependencies', models(async (t, file) => { t.plan(0); } else { const length = t.context.models.length; - t.plan(length * 2 + 1); t.is(length, to.unique(t.context.registered_models).length); } @@ -278,11 +282,7 @@ test.group('parseModelDependencies', models(async (t, file) => { } else { t.truthy(dependency.is_dependency); } - if ( - dependency.data && - dependency.data.dependencies && - dependency.data.dependencies.length - ) { + if (dependency.data.dependencies.length) { check(dependency.data.dependencies); } } @@ -315,7 +315,9 @@ test.group('parseModelInputs', models(async (t, file) => { const actual = await parseModelInputs(model); - const tests = [ t.context.inputs, actual, model.data.inputs ]; + t.is(to.type(model.data.inputs), 'array'); + + const tests = [ t.context.inputs, actual ]; for (let item of tests) { const { error } = is.object(expected).validate(item); diff --git a/test/utils.js b/test/utils.js index c18b7f8..4e662da 100644 --- a/test/utils.js +++ b/test/utils.js @@ -125,6 +125,11 @@ module.exports.models = function(settings) { // eslint-disable-next-line no-underscore-dangle const schema_keys = schema.isJoi ? _.map(schema._inner.children, 'key') : to.keys(schema); + + if (model.indexOf('flight-data') >= 0) { + test = test.serial; + } + test(model, function(t) { // eslint-disable-line // run the call back with the `t` assertion object and the current model var result = cb(t, model);