From 32f56f28f9525bdf6f5020cd8e2f38f47757de64 Mon Sep 17 00:00:00 2001 From: vortx Date: Thu, 30 Sep 2021 02:15:33 -0300 Subject: [PATCH 01/13] feat(generating dynamic tests): generating dynamic tests --- src/generators/index.js | 32 +++++----- src/generators/new/domain/useCasesTests.js | 58 ------------------ .../{new => src}/domain/entities.js | 0 .../src/domain/usecases/unitTests.js | 60 +++++++++++++++++++ .../domain/usecases}/useCases.js | 6 +- src/generators/{new => src}/index.js | 0 .../{new => src}/infra/api/graphql.js | 0 src/generators/{new => src}/infra/api/rest.js | 0 .../{new => src}/infra/api/server.js | 0 .../{new => src}/infra/config/index.js | 0 .../{new => src}/infra/database/connection.js | 0 .../infra/database/migrations/migrations.js | 0 .../database/repositories/repositories.js | 0 src/generators/{new => src}/infra/graphql.js | 0 src/generators/{new => src}/infra/rest.js | 0 src/generators/{new => src}/infra/server.js | 0 src/generators/{new => src}/packagejson.js | 0 .../domain/useCases/tests/create.test.ejs | 18 +++--- .../domain/useCases/tests/delete.test.ejs | 18 ++---- .../domain/useCases/tests/getById.test.ejs | 20 +++---- .../domain/useCases/tests/update.test.ejs | 18 ++---- 21 files changed, 105 insertions(+), 125 deletions(-) delete mode 100644 src/generators/new/domain/useCasesTests.js rename src/generators/{new => src}/domain/entities.js (100%) create mode 100644 src/generators/src/domain/usecases/unitTests.js rename src/generators/{new/domain => src/domain/usecases}/useCases.js (91%) rename src/generators/{new => src}/index.js (100%) rename src/generators/{new => src}/infra/api/graphql.js (100%) rename src/generators/{new => src}/infra/api/rest.js (100%) rename src/generators/{new => src}/infra/api/server.js (100%) rename src/generators/{new => src}/infra/config/index.js (100%) rename src/generators/{new => src}/infra/database/connection.js (100%) rename src/generators/{new => src}/infra/database/migrations/migrations.js (100%) rename src/generators/{new => src}/infra/database/repositories/repositories.js (100%) rename src/generators/{new => src}/infra/graphql.js (100%) rename src/generators/{new => src}/infra/rest.js (100%) rename src/generators/{new => src}/infra/server.js (100%) rename src/generators/{new => src}/packagejson.js (100%) diff --git a/src/generators/index.js b/src/generators/index.js index 3e519e0..5a8bc68 100644 --- a/src/generators/index.js +++ b/src/generators/index.js @@ -8,24 +8,24 @@ module.exports = async (tools) => { return { new: { - packageJson: await require('./new/packagejson')(tools), - entities: await require('./new/domain/entities')(tools), - connection: await require('./new/infra/database/connection')(tools), - repositories: await require('./new/infra/database/repositories/repositories')(tools), - migrations: options.postgres ? await require('./new/infra/database/migrations/migrations')(tools) : ignore, - useCases: await require('./new/domain/useCases')(tools), - useCasesTests: (options.entities === '') ? await require('./new/domain/useCasesTests')(tools) : ignore, - graphql: options.graphql ? await require('./new/infra/graphql')(tools) : ignore, - rest: options.rest ? await require('./new/infra/rest')(tools) : ignore, - config: infra ? await require('./new/infra/config')(tools) : ignore, - server: infra ? await require('./new/infra/server')(tools) : ignore, - index: await require('./new/index')(tools) + packageJson: await require('./src/packagejson')(tools), + entities: await require('./src/domain/entities')(tools), + connection: await require('./src/infra/database/connection')(tools), + repositories: await require('./src/infra/database/repositories/repositories')(tools), + migrations: options.postgres ? await require('./src/infra/database/migrations/migrations')(tools) : ignore, + useCases: await require('./src/domain/usecases/useCases')(tools), + useCasesTests: (options.entities === '') ? await require('./src/domain/usecases/unitTests')(tools) : ignore, + graphql: options.graphql ? await require('./src/infra/graphql')(tools) : ignore, + rest: options.rest ? await require('./src/infra/rest')(tools) : ignore, + config: infra ? await require('./src/infra/config')(tools) : ignore, + server: infra ? await require('./src/infra/server')(tools) : ignore, + index: await require('./src/index')(tools) }, update: { - entities: await require('./new/domain/entities')(tools, true), - useCases: await require('./new/domain/useCases')(tools, true), - migrations: updateMigration ? await require('./new/infra/database/migrations/migrations')(tools) : ignore, - repositories: await require('./new/infra/database/repositories/repositories')(tools, true) + entities: await require('./src/domain/entities')(tools, true), + useCases: await require('./src/domain/usecases/useCases')(tools, true), + migrations: updateMigration ? await require('./src/infra/database/migrations/migrations')(tools) : ignore, + repositories: await require('./src/infra/database/repositories/repositories')(tools, true) } } } diff --git a/src/generators/new/domain/useCasesTests.js b/src/generators/new/domain/useCasesTests.js deleted file mode 100644 index d783f00..0000000 --- a/src/generators/new/domain/useCasesTests.js +++ /dev/null @@ -1,58 +0,0 @@ -const useCases = ['create', 'update', 'delete', 'getById'] -const camelCase = require('lodash.camelcase') -const fs = require('fs') - -async function generateRequest (schema) { - // schema to plain JSON - const obj = Object.keys(schema).reduce((obj, key) => { - const { name, type } = schema[key] - if (name === 'id') return obj - obj[name] = type.name || type.constructor.name - return obj - }, {}) - - // convert plain JSON and remove quotation marks(") - const str = JSON.stringify(obj, null, 8) - .replace(/"/g, '') - .split('\n') - - // remove first and last lines - str.shift() - str.pop() - - return str.join('\n').trim() -} - -module.exports = async ({ template: { generate }, filesystem }) => async () => { - const entities = require(`${filesystem.cwd()}/src/domain/entities`) - const requires = [] - - for (const entity of Object.keys(entities)) { - const { name, schema } = entities[entity].prototype.meta - for (const action of useCases) { - const useCaseName = `${action}${name}` - const ucPath = `${filesystem.cwd()}/src/domain/usecases/${camelCase(name)}/${useCaseName}.test.js` - - let type = 'read' - for (const t of ['create', 'update', 'delete']) { - if (useCaseName.includes(t)) type = t - } - - requires.push(`{ usecase: require('./${camelCase(name)}/${useCaseName}'), tags: { group: '${name}s', type: '${type}'} }`) - - if (fs.existsSync(ucPath)) continue - - await generate({ - template: `domain/useCases/tests/${action}.test.ejs`, - target: ucPath, - props: { - name: { - pascalCase: name, - camelCase: camelCase(name) - }, - request: await generateRequest(schema) - } - }) - } - } -} diff --git a/src/generators/new/domain/entities.js b/src/generators/src/domain/entities.js similarity index 100% rename from src/generators/new/domain/entities.js rename to src/generators/src/domain/entities.js diff --git a/src/generators/src/domain/usecases/unitTests.js b/src/generators/src/domain/usecases/unitTests.js new file mode 100644 index 0000000..a30f607 --- /dev/null +++ b/src/generators/src/domain/usecases/unitTests.js @@ -0,0 +1,60 @@ + +const camelCase = require('lodash.camelcase') +const { objToString } = require('../../../utils') +const fs = require('fs') + +const useCaseRequests = { + create: (obj) => { + delete obj.id + return obj + }, + update: (obj) => obj, + delete: (obj) => { return { id: obj.id } }, + getById: (obj) => { return { id: obj.id} } +} + +const useCases = Object.keys(useCaseRequests) + +const value4type = { + String: 'string', + Number: 99, + Boolean: true, + Array: [] +} + +function generateRequestObject(scheema, action, valid){ + const obj = {} + for(const key of Object.keys(scheema)){ + obj[key] = value4type[scheema[key].type.name] + } + return useCaseRequests[action](obj) +} + +module.exports = async ({ template: { generate }, filesystem }) => async () => { + const entities = require(`${filesystem.cwd()}/src/domain/entities`) + + for (const entity of Object.keys(entities)) { + const { name, schema } = entities[entity].prototype.meta + for (const action of useCases) { + const useCaseName = `${action}${name}` + const ucPath = `${filesystem.cwd()}/src/domain/usecases/${camelCase(name)}/${useCaseName}.test.js` + + if (fs.existsSync(ucPath)) continue + + await generate({ + template: `domain/useCases/tests/${action}.test.ejs`, + target: ucPath, + props: { + name: { + pascalCase: name, + camelCase: camelCase(name) + }, + request: { + valid: objToString(generateRequestObject(schema, action, true)), + invalid: objToString(generateRequestObject(schema, action, false)) + } + } + }) + } + } +} diff --git a/src/generators/new/domain/useCases.js b/src/generators/src/domain/usecases/useCases.js similarity index 91% rename from src/generators/new/domain/useCases.js rename to src/generators/src/domain/usecases/useCases.js index 9a8b771..a8488e8 100644 --- a/src/generators/new/domain/useCases.js +++ b/src/generators/src/domain/usecases/useCases.js @@ -1,9 +1,9 @@ const useCases = ['create', 'update', 'delete', 'getById'] -const { objToString } = require('../../utils') +const { objToString } = require('../../../utils') const camelCase = require('lodash.camelcase') const fs = require('fs') -async function generateRequest (schema) { +async function generateRequestScheema (schema) { // schema to plain JSON const obj = Object.keys(schema).reduce((obj, key) => { const { name, type } = schema[key] @@ -51,7 +51,7 @@ module.exports = async ({ template: { generate }, filesystem }) => async () => { pascalCase: name, camelCase: camelCase(name) }, - request: await generateRequest(schema) + request: await generateRequestScheema(schema) } }) } diff --git a/src/generators/new/index.js b/src/generators/src/index.js similarity index 100% rename from src/generators/new/index.js rename to src/generators/src/index.js diff --git a/src/generators/new/infra/api/graphql.js b/src/generators/src/infra/api/graphql.js similarity index 100% rename from src/generators/new/infra/api/graphql.js rename to src/generators/src/infra/api/graphql.js diff --git a/src/generators/new/infra/api/rest.js b/src/generators/src/infra/api/rest.js similarity index 100% rename from src/generators/new/infra/api/rest.js rename to src/generators/src/infra/api/rest.js diff --git a/src/generators/new/infra/api/server.js b/src/generators/src/infra/api/server.js similarity index 100% rename from src/generators/new/infra/api/server.js rename to src/generators/src/infra/api/server.js diff --git a/src/generators/new/infra/config/index.js b/src/generators/src/infra/config/index.js similarity index 100% rename from src/generators/new/infra/config/index.js rename to src/generators/src/infra/config/index.js diff --git a/src/generators/new/infra/database/connection.js b/src/generators/src/infra/database/connection.js similarity index 100% rename from src/generators/new/infra/database/connection.js rename to src/generators/src/infra/database/connection.js diff --git a/src/generators/new/infra/database/migrations/migrations.js b/src/generators/src/infra/database/migrations/migrations.js similarity index 100% rename from src/generators/new/infra/database/migrations/migrations.js rename to src/generators/src/infra/database/migrations/migrations.js diff --git a/src/generators/new/infra/database/repositories/repositories.js b/src/generators/src/infra/database/repositories/repositories.js similarity index 100% rename from src/generators/new/infra/database/repositories/repositories.js rename to src/generators/src/infra/database/repositories/repositories.js diff --git a/src/generators/new/infra/graphql.js b/src/generators/src/infra/graphql.js similarity index 100% rename from src/generators/new/infra/graphql.js rename to src/generators/src/infra/graphql.js diff --git a/src/generators/new/infra/rest.js b/src/generators/src/infra/rest.js similarity index 100% rename from src/generators/new/infra/rest.js rename to src/generators/src/infra/rest.js diff --git a/src/generators/new/infra/server.js b/src/generators/src/infra/server.js similarity index 100% rename from src/generators/new/infra/server.js rename to src/generators/src/infra/server.js diff --git a/src/generators/new/packagejson.js b/src/generators/src/packagejson.js similarity index 100% rename from src/generators/new/packagejson.js rename to src/generators/src/packagejson.js diff --git a/src/templates/domain/useCases/tests/create.test.ejs b/src/templates/domain/useCases/tests/create.test.ejs index 6da1267..c3b4b7f 100644 --- a/src/templates/domain/useCases/tests/create.test.ejs +++ b/src/templates/domain/useCases/tests/create.test.ejs @@ -3,9 +3,7 @@ const assert = require('assert') describe('Create <%= props.name.pascalCase %>', () => { - function aUser({ hasAccess }) { - return { hasAccess } - } + const authorizedUser = aUser describe('Valid <%= props.name.pascalCase %>', () => { @@ -16,13 +14,12 @@ describe('Create <%= props.name.pascalCase %>', () => { async insert(<%= props.name.camelCase %>) { return (<%= props.name.camelCase %>) } }) } - const user = aUser({ hasAccess: true }) - /*{ <%= props.request %> }*/ - const req = { nickname: 'herbsUser', password: 'V&eryStr0ngP@$$'} + + const req = <%= props.request.valid %> // When const uc = create<%= props.name.pascalCase %>(injection)() - await uc.authorize(<%= props.name.camelCase %>) + await uc.authorize(authorizedUser) const ret = await uc.run(req) // Then @@ -37,13 +34,12 @@ describe('Create <%= props.name.pascalCase %>', () => { it('should not create invalid <%= props.name.pascalCase %>', async () => { // Given const injection = {} - const user = aUser({ hasAccess: true }) - /*{ <%= props.request %> }*/ - const req = { nickname: 'herbsUser', password: 96587422 } + + const req = <%= props.request.invalid %> // When const uc = create<%= props.name.pascalCase %>(injection)() - await uc.authorize(<%= props.name.camelCase %>) + await uc.authorize(authorizedUser) const ret = await uc.run(req) // Then diff --git a/src/templates/domain/useCases/tests/delete.test.ejs b/src/templates/domain/useCases/tests/delete.test.ejs index a97772d..97ae900 100644 --- a/src/templates/domain/useCases/tests/delete.test.ejs +++ b/src/templates/domain/useCases/tests/delete.test.ejs @@ -3,9 +3,7 @@ const assert = require('assert') describe('Delete <%= props.name.pascalCase %>', () => { - function aUser({ hasAccess }) { - return { hasAccess } - } + const authorizedUser = { hasAccess = true } describe('Valid <%= props.name.pascalCase %>', () => { @@ -16,13 +14,12 @@ describe('Delete <%= props.name.pascalCase %>', () => { async deleteByID(id) { return true } }) } - const user = aUser({ hasAccess: true }) - /*{ <%= props.request %> }*/ - const req = { id : 5 } + + const req = <%= props.request.valid %> // When const uc = delete<%= props.name.pascalCase %>(injection)() - await uc.authorize(<%= props.name.camelCase %>) + await uc.authorize(authorizedUser) const ret = await uc.run(req) // Then @@ -36,18 +33,15 @@ describe('Delete <%= props.name.pascalCase %>', () => { it('should not delete invalid <%= props.name.pascalCase %>', async () => { // Given const injection = {} - const user = aUser({ hasAccess: true }) - /*{ <%= props.request %> }*/ - const req = { id : '5' } + const req = <%= props.request.invalid %> // When const uc = delete<%= props.name.pascalCase %>(injection)() - await uc.authorize(<%= props.name.camelCase %>) + await uc.authorize(authorizedUser) const ret = await uc.run(req) // Then assert.ok(ret.isErr) - assert.deepStrictEqual(ret.err, {request :[{id:[{wrongType:"Number"}]}]}) }) }) }) diff --git a/src/templates/domain/useCases/tests/getById.test.ejs b/src/templates/domain/useCases/tests/getById.test.ejs index 6e90e09..b1cbd23 100644 --- a/src/templates/domain/useCases/tests/getById.test.ejs +++ b/src/templates/domain/useCases/tests/getById.test.ejs @@ -3,9 +3,7 @@ const assert = require('assert') const { <%= props.name.pascalCase %> } = require('../../entities') describe('Find one <%= props.name.pascalCase %>', () => { - function aUser({ hasAccess }) { - return { hasAccess } - } + const authorizedUser = { hasAccess = true } describe('Valid <%= props.name.pascalCase %>', () => { @@ -18,18 +16,16 @@ describe('Find one <%= props.name.pascalCase %>', () => { async findByID(id) { return ([retInjection]) } }) } - const user = aUser({ hasAccess: true }) - /*{ <%= props.request %> }*/ - const req = { id: 5 } + + const req = <%= props.request.valid %> // When const uc = getById<%= props.name.pascalCase %>(injection)() - await uc.authorize(<%= props.name.camelCase %>) + await uc.authorize(authorizedUser) const ret = await uc.run(req) // Then assert.ok(ret.isOk) - assert.strictEqual(ret.ok.nickname, 'herbsUser') assert.strictEqual(ret.ok.isValid(), true) }) @@ -40,18 +36,16 @@ describe('Find one <%= props.name.pascalCase %>', () => { it('should not return if input is invalid', async () => { // Given const injection = {} - const user = aUser({ hasAccess: true }) - /*{ <%= props.request %> }*/ - const req = { id : '5' } + + const req = <%= props.request.invalid %> // When const uc = getById<%= props.name.pascalCase %>(injection)() - await uc.authorize(<%= props.name.camelCase %>) + await uc.authorize(authorizedUser) const ret = await uc.run(req) // Then assert.ok(ret.isErr) - assert.deepStrictEqual(ret.err, {request :[{id:[{wrongType:"Number"}]}]}) }) }) }) diff --git a/src/templates/domain/useCases/tests/update.test.ejs b/src/templates/domain/useCases/tests/update.test.ejs index 1b264df..8450b96 100644 --- a/src/templates/domain/useCases/tests/update.test.ejs +++ b/src/templates/domain/useCases/tests/update.test.ejs @@ -4,9 +4,7 @@ const { <%= props.name.pascalCase %> } = require('../../entities') describe('Update <%= props.name.pascalCase %>', () => { - function aUser({ hasAccess }) { - return { hasAccess } - } + const authorizedUser = { hasAccess = true } describe('Valid <%= props.name.pascalCase %>', () => { @@ -19,13 +17,12 @@ describe('Update <%= props.name.pascalCase %>', () => { async update(id) { return true } }) } - const user = aUser({ hasAccess: true }) - /*{ <%= props.request %> }*/ - const req = { id: 5, nickname: 'herbsUser', password: 'V&eryStr0ngP@$$'} + + const req = <%= props.request.valid %> // When const uc = update<%= props.name.pascalCase %>(injection)() - await uc.authorize(<%= props.name.camelCase %>) + await uc.authorize(authorizedUser) const ret = await uc.run(req) // Then @@ -39,18 +36,15 @@ describe('Update <%= props.name.pascalCase %>', () => { it('should not update invalid <%= props.name.pascalCase %>', async () => { // Given const injection = {} - const user = aUser({ hasAccess: true }) - /*{ <%= props.request %> }*/ - const req = { nickname: 'herbsUser', password: 96587422 } + const req = <%= props.request.invalid %> // When const uc = update<%= props.name.pascalCase %>(injection)() - await uc.authorize(<%= props.name.camelCase %>) + await uc.authorize(authorizedUser) const ret = await uc.run(req) // Then assert.ok(ret.isErr) - assert.deepStrictEqual(ret.err, {request :[{password:[{wrongType:"String"}]}]}) }) }) }) From 4e166fdbd15b56465be7a3462bb189fe6668989a Mon Sep 17 00:00:00 2001 From: vortx Date: Thu, 30 Sep 2021 02:37:35 -0300 Subject: [PATCH 02/13] test: generating valid and invalid usecase request objects while i'm drunk --- .../src/domain/usecases/unitTests.js | 44 +++++++++++++++---- .../domain/useCases/tests/create.test.ejs | 3 +- .../domain/useCases/tests/delete.test.ejs | 2 +- .../domain/useCases/tests/getById.test.ejs | 2 +- .../domain/useCases/tests/update.test.ejs | 2 +- 5 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/generators/src/domain/usecases/unitTests.js b/src/generators/src/domain/usecases/unitTests.js index a30f607..27c898a 100644 --- a/src/generators/src/domain/usecases/unitTests.js +++ b/src/generators/src/domain/usecases/unitTests.js @@ -3,17 +3,44 @@ const camelCase = require('lodash.camelcase') const { objToString } = require('../../../utils') const fs = require('fs') -const useCaseRequests = { +function invertObjValues(obj){ + for(const key of Object.keys(obj)){ + switch (typeof obj[key]) { + case 'String': + obj[key] = 123 + break; + case 'Number': + case 'Boolean': + obj[key] = '123' + break; + default: + obj[key] = true + } + } + return obj +} + +const validUseCaseRequests = { create: (obj) => { - delete obj.id + delete obj.id return obj }, update: (obj) => obj, delete: (obj) => { return { id: obj.id } }, - getById: (obj) => { return { id: obj.id} } + getById: (obj) => { return { id: obj.id } } +} +const invalidUseCaseRequests = { + create: (obj) => { + delete obj.id + obj = invertObjValues(obj) + return obj + }, + update: (obj) => invertObjValues(obj), + delete: () => { return { id: null } }, + getById: () => { return { id: null } } } -const useCases = Object.keys(useCaseRequests) +const useCases = Object.keys(validUseCaseRequests) const value4type = { String: 'string', @@ -22,12 +49,13 @@ const value4type = { Array: [] } -function generateRequestObject(scheema, action, valid){ +function generateRequestObject (scheema, action, validReq) { const obj = {} - for(const key of Object.keys(scheema)){ - obj[key] = value4type[scheema[key].type.name] + for (const key of Object.keys(scheema)) { + obj[key] = value4type[scheema[key].type.name] } - return useCaseRequests[action](obj) + if(validReq) return validUseCaseRequests[action](obj) + return invalidUseCaseRequests[action](obj) } module.exports = async ({ template: { generate }, filesystem }) => async () => { diff --git a/src/templates/domain/useCases/tests/create.test.ejs b/src/templates/domain/useCases/tests/create.test.ejs index c3b4b7f..91a724d 100644 --- a/src/templates/domain/useCases/tests/create.test.ejs +++ b/src/templates/domain/useCases/tests/create.test.ejs @@ -3,7 +3,7 @@ const assert = require('assert') describe('Create <%= props.name.pascalCase %>', () => { - const authorizedUser = aUser + const authorizedUser = { hasAccess: true } describe('Valid <%= props.name.pascalCase %>', () => { @@ -44,7 +44,6 @@ describe('Create <%= props.name.pascalCase %>', () => { // Then assert.ok(ret.isErr) - assert.deepStrictEqual(ret.err, {request :[{password:[{wrongType:"String"}]}]}) }) }) }) diff --git a/src/templates/domain/useCases/tests/delete.test.ejs b/src/templates/domain/useCases/tests/delete.test.ejs index 97ae900..f2a413c 100644 --- a/src/templates/domain/useCases/tests/delete.test.ejs +++ b/src/templates/domain/useCases/tests/delete.test.ejs @@ -3,7 +3,7 @@ const assert = require('assert') describe('Delete <%= props.name.pascalCase %>', () => { - const authorizedUser = { hasAccess = true } + const authorizedUser = { hasAccess: true } describe('Valid <%= props.name.pascalCase %>', () => { diff --git a/src/templates/domain/useCases/tests/getById.test.ejs b/src/templates/domain/useCases/tests/getById.test.ejs index b1cbd23..617396a 100644 --- a/src/templates/domain/useCases/tests/getById.test.ejs +++ b/src/templates/domain/useCases/tests/getById.test.ejs @@ -3,7 +3,7 @@ const assert = require('assert') const { <%= props.name.pascalCase %> } = require('../../entities') describe('Find one <%= props.name.pascalCase %>', () => { - const authorizedUser = { hasAccess = true } + const authorizedUser = { hasAccess: true } describe('Valid <%= props.name.pascalCase %>', () => { diff --git a/src/templates/domain/useCases/tests/update.test.ejs b/src/templates/domain/useCases/tests/update.test.ejs index 8450b96..749c894 100644 --- a/src/templates/domain/useCases/tests/update.test.ejs +++ b/src/templates/domain/useCases/tests/update.test.ejs @@ -4,7 +4,7 @@ const { <%= props.name.pascalCase %> } = require('../../entities') describe('Update <%= props.name.pascalCase %>', () => { - const authorizedUser = { hasAccess = true } + const authorizedUser = { hasAccess: true } describe('Valid <%= props.name.pascalCase %>', () => { From a157c979b98159dedfb32d14133f12f6ee795a7b Mon Sep 17 00:00:00 2001 From: vortx Date: Thu, 30 Sep 2021 03:01:29 -0300 Subject: [PATCH 03/13] test: added dynamiclly return mock int ogetbyid and update tests files --- .../src/domain/usecases/unitTests.js | 24 ++++++++++++------- .../domain/useCases/tests/create.test.ejs | 4 ++-- .../domain/useCases/tests/delete.test.ejs | 4 ++-- .../domain/useCases/tests/getById.test.ejs | 4 ++-- .../domain/useCases/tests/update.test.ejs | 6 ++--- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/generators/src/domain/usecases/unitTests.js b/src/generators/src/domain/usecases/unitTests.js index 27c898a..4b5f925 100644 --- a/src/generators/src/domain/usecases/unitTests.js +++ b/src/generators/src/domain/usecases/unitTests.js @@ -3,16 +3,16 @@ const camelCase = require('lodash.camelcase') const { objToString } = require('../../../utils') const fs = require('fs') -function invertObjValues(obj){ - for(const key of Object.keys(obj)){ +function invertObjValues (obj) { + for (const key of Object.keys(obj)) { switch (typeof obj[key]) { case 'String': - obj[key] = 123 - break; + obj[key] = 123 + break case 'Number': case 'Boolean': obj[key] = '123' - break; + break default: obj[key] = true } @@ -43,18 +43,23 @@ const invalidUseCaseRequests = { const useCases = Object.keys(validUseCaseRequests) const value4type = { - String: 'string', + String: "'string'", Number: 99, Boolean: true, Array: [] } -function generateRequestObject (scheema, action, validReq) { +function generateMockObj(scheema){ const obj = {} for (const key of Object.keys(scheema)) { obj[key] = value4type[scheema[key].type.name] } - if(validReq) return validUseCaseRequests[action](obj) + return obj +} + +function generateRequestObject (scheema, action, validReq) { + const obj = generateMockObj(scheema) + if (validReq) return validUseCaseRequests[action](obj) return invalidUseCaseRequests[action](obj) } @@ -80,7 +85,8 @@ module.exports = async ({ template: { generate }, filesystem }) => async () => { request: { valid: objToString(generateRequestObject(schema, action, true)), invalid: objToString(generateRequestObject(schema, action, false)) - } + }, + mock: objToString(generateRequestObject(schema, action)), } }) } diff --git a/src/templates/domain/useCases/tests/create.test.ejs b/src/templates/domain/useCases/tests/create.test.ejs index 91a724d..d57549c 100644 --- a/src/templates/domain/useCases/tests/create.test.ejs +++ b/src/templates/domain/useCases/tests/create.test.ejs @@ -15,7 +15,7 @@ describe('Create <%= props.name.pascalCase %>', () => { }) } - const req = <%= props.request.valid %> + const req = <%- props.request.valid %> // When const uc = create<%= props.name.pascalCase %>(injection)() @@ -35,7 +35,7 @@ describe('Create <%= props.name.pascalCase %>', () => { // Given const injection = {} - const req = <%= props.request.invalid %> + const req = <%- props.request.invalid %> // When const uc = create<%= props.name.pascalCase %>(injection)() diff --git a/src/templates/domain/useCases/tests/delete.test.ejs b/src/templates/domain/useCases/tests/delete.test.ejs index f2a413c..e44c734 100644 --- a/src/templates/domain/useCases/tests/delete.test.ejs +++ b/src/templates/domain/useCases/tests/delete.test.ejs @@ -15,7 +15,7 @@ describe('Delete <%= props.name.pascalCase %>', () => { }) } - const req = <%= props.request.valid %> + const req = <%- props.request.valid %> // When const uc = delete<%= props.name.pascalCase %>(injection)() @@ -33,7 +33,7 @@ describe('Delete <%= props.name.pascalCase %>', () => { it('should not delete invalid <%= props.name.pascalCase %>', async () => { // Given const injection = {} - const req = <%= props.request.invalid %> + const req = <%- props.request.invalid %> // When const uc = delete<%= props.name.pascalCase %>(injection)() diff --git a/src/templates/domain/useCases/tests/getById.test.ejs b/src/templates/domain/useCases/tests/getById.test.ejs index 617396a..08005b5 100644 --- a/src/templates/domain/useCases/tests/getById.test.ejs +++ b/src/templates/domain/useCases/tests/getById.test.ejs @@ -9,7 +9,7 @@ describe('Find one <%= props.name.pascalCase %>', () => { it('should return <%= props.name.pascalCase %> if is valid', async () => { // Given - const retInjection = <%= props.name.pascalCase %>.fromJSON('{ "id": 5, "nickname": "herbsUser", "password": "V&eryStr0ngP@$$"}') + const retInjection = <%= props.name.pascalCase %>.fromJSON(<%= props.mock %>) const injection = { <%= props.name.camelCase %>Repository: new ( class <%= props.name.camelCase %>Repository { @@ -37,7 +37,7 @@ describe('Find one <%= props.name.pascalCase %>', () => { // Given const injection = {} - const req = <%= props.request.invalid %> + const req = <%- props.request.invalid %> // When const uc = getById<%= props.name.pascalCase %>(injection)() diff --git a/src/templates/domain/useCases/tests/update.test.ejs b/src/templates/domain/useCases/tests/update.test.ejs index 749c894..4eb3c7b 100644 --- a/src/templates/domain/useCases/tests/update.test.ejs +++ b/src/templates/domain/useCases/tests/update.test.ejs @@ -10,7 +10,7 @@ describe('Update <%= props.name.pascalCase %>', () => { it('should update <%= props.name.pascalCase %> if is valid', async () => { // Given - const retInjection = <%= props.name.pascalCase %>.fromJSON('{ "id": 5, "nickname": "herbsUser", "password": "V&eryStr0ngP@$$"}') + const retInjection = <%= props.name.pascalCase %>.fromJSON(<%= props.mock %>) const injection = { <%= props.name.camelCase %>Repository: new ( class <%= props.name.camelCase %>Repository { async findByID(id) { return ([retInjection]) } @@ -18,7 +18,7 @@ describe('Update <%= props.name.pascalCase %>', () => { }) } - const req = <%= props.request.valid %> + const req = <%- props.request.valid %> // When const uc = update<%= props.name.pascalCase %>(injection)() @@ -36,7 +36,7 @@ describe('Update <%= props.name.pascalCase %>', () => { it('should not update invalid <%= props.name.pascalCase %>', async () => { // Given const injection = {} - const req = <%= props.request.invalid %> + const req = <%- props.request.invalid %> // When const uc = update<%= props.name.pascalCase %>(injection)() From 572aa484702463e92cc940f2ac82c21a8b14b63f Mon Sep 17 00:00:00 2001 From: vortx Date: Mon, 4 Oct 2021 21:53:04 -0300 Subject: [PATCH 04/13] =?UTF-8?q?feat(=F0=9F=A7=AA=20generating=20test=20f?= =?UTF-8?q?or=20all=20entities):=20=F0=9F=A7=AA=20Generating=20test=20for?= =?UTF-8?q?=20all=20entities=20dynamically?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/generators/index.js | 2 +- src/generators/src/domain/usecases/unitTests.js | 4 ++-- .../domain/useCases/tests/delete.test.ejs | 17 ----------------- .../domain/useCases/tests/getById.test.ejs | 6 +++++- 4 files changed, 8 insertions(+), 21 deletions(-) diff --git a/src/generators/index.js b/src/generators/index.js index 5a8bc68..d2211f2 100644 --- a/src/generators/index.js +++ b/src/generators/index.js @@ -14,7 +14,7 @@ module.exports = async (tools) => { repositories: await require('./src/infra/database/repositories/repositories')(tools), migrations: options.postgres ? await require('./src/infra/database/migrations/migrations')(tools) : ignore, useCases: await require('./src/domain/usecases/useCases')(tools), - useCasesTests: (options.entities === '') ? await require('./src/domain/usecases/unitTests')(tools) : ignore, + useCasesTests: await require('./src/domain/usecases/unitTests')(tools), graphql: options.graphql ? await require('./src/infra/graphql')(tools) : ignore, rest: options.rest ? await require('./src/infra/rest')(tools) : ignore, config: infra ? await require('./src/infra/config')(tools) : ignore, diff --git a/src/generators/src/domain/usecases/unitTests.js b/src/generators/src/domain/usecases/unitTests.js index 4b5f925..8759748 100644 --- a/src/generators/src/domain/usecases/unitTests.js +++ b/src/generators/src/domain/usecases/unitTests.js @@ -49,7 +49,7 @@ const value4type = { Array: [] } -function generateMockObj(scheema){ +function generateMockObj (scheema) { const obj = {} for (const key of Object.keys(scheema)) { obj[key] = value4type[scheema[key].type.name] @@ -86,7 +86,7 @@ module.exports = async ({ template: { generate }, filesystem }) => async () => { valid: objToString(generateRequestObject(schema, action, true)), invalid: objToString(generateRequestObject(schema, action, false)) }, - mock: objToString(generateRequestObject(schema, action)), + mock: objToString(generateRequestObject(schema, action)) } }) } diff --git a/src/templates/domain/useCases/tests/delete.test.ejs b/src/templates/domain/useCases/tests/delete.test.ejs index e44c734..cae5bc3 100644 --- a/src/templates/domain/useCases/tests/delete.test.ejs +++ b/src/templates/domain/useCases/tests/delete.test.ejs @@ -27,21 +27,4 @@ describe('Delete <%= props.name.pascalCase %>', () => { }) }) - - describe('Invalid <%= props.name.pascalCase %>', () => { - - it('should not delete invalid <%= props.name.pascalCase %>', async () => { - // Given - const injection = {} - const req = <%- props.request.invalid %> - - // When - const uc = delete<%= props.name.pascalCase %>(injection)() - await uc.authorize(authorizedUser) - const ret = await uc.run(req) - - // Then - assert.ok(ret.isErr) - }) - }) }) diff --git a/src/templates/domain/useCases/tests/getById.test.ejs b/src/templates/domain/useCases/tests/getById.test.ejs index 08005b5..1c528c4 100644 --- a/src/templates/domain/useCases/tests/getById.test.ejs +++ b/src/templates/domain/useCases/tests/getById.test.ejs @@ -35,7 +35,11 @@ describe('Find one <%= props.name.pascalCase %>', () => { it('should not return if input is invalid', async () => { // Given - const injection = {} + const injection = { + <%= props.name.camelCase %>Repository: new ( class <%= props.name.camelCase %>Repository { + async findByID(id) { return ([]) } + }) + } const req = <%- props.request.invalid %> From ead78ad1d7576f2ef82c9c562e2bf997d967858f Mon Sep 17 00:00:00 2001 From: vortx Date: Mon, 4 Oct 2021 22:09:22 -0300 Subject: [PATCH 05/13] feat: generating two unit test for all usecases --- readme.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/readme.md b/readme.md index bb26404..934b9e7 100644 --- a/readme.md +++ b/readme.md @@ -24,14 +24,9 @@ herbs new ```bash . -├── node_modules ├── package-lock.json ├── package.json └── src - ├── config - │ ├── api.js - │ ├── index.js - │ └── mongo.js ├── domain │ ├── entities │ │ ├── index.js @@ -40,13 +35,13 @@ herbs new │ ├── index.js │ └── user │ ├── createUser.js - │ ├── createUser.test.js │ ├── deleteUser.js - │ ├── deleteUser.test.js │ ├── getByIdUser.js - │ ├── getByIdUser.test.js - │ └── updateUser.js - │ └── updateUser.test.js + │ ├── updateUser.js + │ ├── createUser.test.js + │ ├── deleteUser.test.js + │ ├── getByIdUser.test.js + │ └── updateUser.test.js ├── index.js └── infra ├── api @@ -60,11 +55,16 @@ herbs new │ ├── rest │ │ └── index.js │ └── server.js + ├── config + │ ├── api.js + │ ├── index.js + │ └── postgres.js └── data ├── database - │ └── index.js + │ ├── index.js + │ └── migrations + │ └── 20211005010714_users.js └── repositories - ├── baseRepository.js ├── index.js └── userRepository.js ``` From af4ff2b1c798899bb9bd3bb583fe8f1e436c4913 Mon Sep 17 00:00:00 2001 From: vortx Date: Sun, 10 Oct 2021 13:35:39 -0300 Subject: [PATCH 06/13] style: fix typo fix schema variable name typo #57 --- src/generators/src/domain/usecases/unitTests.js | 10 +++++----- src/generators/src/domain/usecases/useCases.js | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/generators/src/domain/usecases/unitTests.js b/src/generators/src/domain/usecases/unitTests.js index 8759748..bebb696 100644 --- a/src/generators/src/domain/usecases/unitTests.js +++ b/src/generators/src/domain/usecases/unitTests.js @@ -49,16 +49,16 @@ const value4type = { Array: [] } -function generateMockObj (scheema) { +function generateMockObj (schema) { const obj = {} - for (const key of Object.keys(scheema)) { - obj[key] = value4type[scheema[key].type.name] + for (const key of Object.keys(schema)) { + obj[key] = value4type[schema[key].type.name] } return obj } -function generateRequestObject (scheema, action, validReq) { - const obj = generateMockObj(scheema) +function generateRequestObject (schema, action, validReq) { + const obj = generateMockObj(schema) if (validReq) return validUseCaseRequests[action](obj) return invalidUseCaseRequests[action](obj) } diff --git a/src/generators/src/domain/usecases/useCases.js b/src/generators/src/domain/usecases/useCases.js index a8488e8..7b59214 100644 --- a/src/generators/src/domain/usecases/useCases.js +++ b/src/generators/src/domain/usecases/useCases.js @@ -3,7 +3,7 @@ const { objToString } = require('../../../utils') const camelCase = require('lodash.camelcase') const fs = require('fs') -async function generateRequestScheema (schema) { +async function generateRequestschema (schema) { // schema to plain JSON const obj = Object.keys(schema).reduce((obj, key) => { const { name, type } = schema[key] @@ -51,7 +51,7 @@ module.exports = async ({ template: { generate }, filesystem }) => async () => { pascalCase: name, camelCase: camelCase(name) }, - request: await generateRequestScheema(schema) + request: await generateRequestschema(schema) } }) } From 9691b48f0935d2296653f843932b4252790ff9c7 Mon Sep 17 00:00:00 2001 From: vortx Date: Tue, 12 Oct 2021 12:06:06 -0300 Subject: [PATCH 07/13] style: update variable names and folder structure --- example-entities/{profile => }/profile.js | 0 example-entities/{room => }/room.js | 0 src/generators/src/domain/usecases/unitTests.js | 4 ++-- 3 files changed, 2 insertions(+), 2 deletions(-) rename example-entities/{profile => }/profile.js (100%) rename example-entities/{room => }/room.js (100%) diff --git a/example-entities/profile/profile.js b/example-entities/profile.js similarity index 100% rename from example-entities/profile/profile.js rename to example-entities/profile.js diff --git a/example-entities/room/room.js b/example-entities/room.js similarity index 100% rename from example-entities/room/room.js rename to example-entities/room.js diff --git a/src/generators/src/domain/usecases/unitTests.js b/src/generators/src/domain/usecases/unitTests.js index bebb696..a049722 100644 --- a/src/generators/src/domain/usecases/unitTests.js +++ b/src/generators/src/domain/usecases/unitTests.js @@ -42,7 +42,7 @@ const invalidUseCaseRequests = { const useCases = Object.keys(validUseCaseRequests) -const value4type = { +const valueType = { String: "'string'", Number: 99, Boolean: true, @@ -52,7 +52,7 @@ const value4type = { function generateMockObj (schema) { const obj = {} for (const key of Object.keys(schema)) { - obj[key] = value4type[schema[key].type.name] + obj[key] = valueType[schema[key].type.name] } return obj } From 2b89c0886e9a8edf1829813a2d511d2c4d5efdd4 Mon Sep 17 00:00:00 2001 From: vortx Date: Sat, 20 Nov 2021 18:42:17 -0300 Subject: [PATCH 08/13] fix: fix unit tests --- src/generators/src/domain/usecases/unitTests.js | 6 +++--- src/generators/utils.js | 8 ++++++-- src/templates/domain/useCases/tests/create.test.ejs | 3 ++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/generators/src/domain/usecases/unitTests.js b/src/generators/src/domain/usecases/unitTests.js index 1a4d4f8..74925d0 100644 --- a/src/generators/src/domain/usecases/unitTests.js +++ b/src/generators/src/domain/usecases/unitTests.js @@ -45,7 +45,7 @@ const invalidUseCaseRequests = { const useCases = Object.keys(validUseCaseRequests) const valueType = { - String: "'string'", + String: "'a text'", Number: 99, Boolean: true, Array: [] @@ -86,8 +86,8 @@ module.exports = async ({ template: { generate }, filesystem }) => async () => { raw: camelCase(name).replace(/([a-z0-9])([A-Z])/g, '$1 $2') }, request: { - valid: objToString(generateRequestObject(schema, action, true)), - invalid: objToString(generateRequestObject(schema, action, false)) + valid: objToString(generateRequestObject(schema, action, true, true)), + invalid: objToString(generateRequestObject(schema, action, false, true)) }, mock: objToString(generateRequestObject(schema, action)) } diff --git a/src/generators/utils.js b/src/generators/utils.js index 38591fc..b39be30 100644 --- a/src/generators/utils.js +++ b/src/generators/utils.js @@ -2,7 +2,12 @@ const camelCase = require('lodash.camelcase') const startCase = require('lodash.startCase') module.exports = { - objToString: (obj, spaces = 4) => JSON.stringify(obj, null, spaces).replace(/"/g, ''), + objToString: (obj, spaces = 4, removeFirstLine = false) => { + let json = JSON.stringify(obj, null, spaces).replace(/"/g, '') + if(removeFirstLine) + return json.replace('{\n', '').split('\n').shift().join('\n') + return json + }, arrayToStringList: (arr, spaces = 1) => { const list = JSON.stringify(arr, null, spaces) .replace(/"/g, '') @@ -13,5 +18,4 @@ module.exports = { return list.join('\n') }, pascalCase: (str) => startCase(camelCase(str)).replace(/ /g, ''), - } diff --git a/src/templates/domain/useCases/tests/create.test.ejs b/src/templates/domain/useCases/tests/create.test.ejs index b31aee8..9c71d40 100644 --- a/src/templates/domain/useCases/tests/create.test.ejs +++ b/src/templates/domain/useCases/tests/create.test.ejs @@ -15,7 +15,8 @@ describe('Create <%= props.name.raw %>', () => { }) } - const req = <%- props.request.valid %> + const req = { + <%- props.request.valid %> // When const uc = create<%= props.name.pascalCase %>(injection)() From 4fc074ba0d7bd959894c5117f038c22bec7fa46a Mon Sep 17 00:00:00 2001 From: vortx Date: Sat, 20 Nov 2021 19:53:10 -0300 Subject: [PATCH 09/13] wip: saving the code --- .../src/domain/usecases/unitTests.js | 15 ++++----- .../domain/useCases/tests/create.test.ejs | 3 +- .../domain/useCases/tests/getById.test.ejs | 31 +++++++++++++------ 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/generators/src/domain/usecases/unitTests.js b/src/generators/src/domain/usecases/unitTests.js index 74925d0..5ba6ad5 100644 --- a/src/generators/src/domain/usecases/unitTests.js +++ b/src/generators/src/domain/usecases/unitTests.js @@ -19,15 +19,16 @@ function invertObjValues (obj) { } return obj } +const removeID = (obj) => { + delete obj.id + return obj +} const validUseCaseRequests = { - create: (obj) => { - delete obj.id - return obj - }, + create: removeID, update: (obj) => obj, delete: (obj) => { return { id: obj.id } }, - getById: (obj) => { return { id: obj.id } }, + getById: removeID, getAll: (obj) => { return [ obj, obj, obj ] } } const invalidUseCaseRequests = { @@ -86,8 +87,8 @@ module.exports = async ({ template: { generate }, filesystem }) => async () => { raw: camelCase(name).replace(/([a-z0-9])([A-Z])/g, '$1 $2') }, request: { - valid: objToString(generateRequestObject(schema, action, true, true)), - invalid: objToString(generateRequestObject(schema, action, false, true)) + valid: objToString(generateRequestObject(schema, action, true)), + invalid: objToString(generateRequestObject(schema, action, false)) }, mock: objToString(generateRequestObject(schema, action)) } diff --git a/src/templates/domain/useCases/tests/create.test.ejs b/src/templates/domain/useCases/tests/create.test.ejs index 9c71d40..b31aee8 100644 --- a/src/templates/domain/useCases/tests/create.test.ejs +++ b/src/templates/domain/useCases/tests/create.test.ejs @@ -15,8 +15,7 @@ describe('Create <%= props.name.raw %>', () => { }) } - const req = { - <%- props.request.valid %> + const req = <%- props.request.valid %> // When const uc = create<%= props.name.pascalCase %>(injection)() diff --git a/src/templates/domain/useCases/tests/getById.test.ejs b/src/templates/domain/useCases/tests/getById.test.ejs index 9bae98f..ff62147 100644 --- a/src/templates/domain/useCases/tests/getById.test.ejs +++ b/src/templates/domain/useCases/tests/getById.test.ejs @@ -1,13 +1,13 @@ -const getById<%= props.name.pascalCase %> = require('./getById<%= props.name.pascalCase %>') +const get<%= props.name.pascalCase %>ById = require('./getById<%= props.name.pascalCase %>') const assert = require('assert') const { <%= props.name.pascalCase %> } = require('../../entities') describe('Find one <%= props.name.raw %>', () => { const authorizedUser = { hasAccess: true } - describe('Valid <%= props.name.raw %>', () => { + describe('Valid scenarios', () => { - it('should return <%= props.name.raw %> if is valid', async () => { + it('should return <%= props.name.raw %>', async () => { // Given const retInjection = <%= props.name.pascalCase %>.fromJSON(<%= props.mock %>) @@ -17,23 +17,36 @@ describe('Find one <%= props.name.raw %>', () => { }) } - const req = <%= props.request.valid %> + const req = <%- props.request.valid %> // When - const uc = getById<%= props.name.pascalCase %>(injection)() + const uc = get<%= props.name.pascalCase %>ById(injection)() await uc.authorize(authorizedUser) const ret = await uc.run(req) // Then assert.ok(ret.isOk) assert.strictEqual(ret.ok.isValid(), true) - + assert.strictEqual(JSON.stringify(ret.ok), JSON.stringify({id: ret.ok.id,...req})) }) }) - describe('Invalid <%= props.name.raw %>', () => { + describe('Error scenarios', () => { + + it('should not return for invalid input', async () => { + // Given + const req = <%- props.request.invalid %> + + // When + const uc = get<%= props.name.pascalCase %>ById({})() + await uc.authorize(authorizedUser) + const ret = await uc.run(req) + + // Then + assert.ok(ret.isErr) + }) - it('should not return if input is invalid', async () => { + it('return notFoundError', async () => { // Given const injection = { <%= props.name.camelCase %>Repository: new ( class <%= props.name.camelCase %>Repository { @@ -44,7 +57,7 @@ describe('Find one <%= props.name.raw %>', () => { const req = <%- props.request.invalid %> // When - const uc = getById<%= props.name.pascalCase %>(injection)() + const uc = get<%= props.name.pascalCase %>ById(injection)() await uc.authorize(authorizedUser) const ret = await uc.run(req) From 66b36edc1a38d1da00e56fc26908fba42d6a7f32 Mon Sep 17 00:00:00 2001 From: vortx Date: Sat, 20 Nov 2021 21:27:47 -0300 Subject: [PATCH 10/13] feat(not found error test): added not found error test --- package.json | 4 -- .../src/domain/usecases/unitTests.js | 12 +++--- .../data/repository/mongo/repository.ejs | 4 +- src/templates/domain/useCases/create.ejs | 30 +++++++-------- src/templates/domain/useCases/delete.ejs | 14 +++---- src/templates/domain/useCases/getAll.ejs | 14 +++---- src/templates/domain/useCases/getById.ejs | 20 +++++----- .../domain/useCases/tests/create.test.ejs | 20 +++++----- .../domain/useCases/tests/delete.test.ejs | 20 +++++----- .../domain/useCases/tests/getAll.test.ejs | 10 ++--- .../domain/useCases/tests/getById.test.ejs | 38 +++++++------------ .../domain/useCases/tests/update.test.ejs | 22 +++++------ src/templates/domain/useCases/update.ejs | 32 ++++++++-------- .../data/repository/mongo/repository.ejs | 8 ++-- .../data/repository/postgres/repository.ejs | 6 +-- .../data/repository/sqlserver/repository.ejs | 6 +-- src/templates/package.json.ejs | 8 ++-- 17 files changed, 125 insertions(+), 143 deletions(-) diff --git a/package.json b/package.json index 24bd779..9e55bc8 100644 --- a/package.json +++ b/package.json @@ -10,9 +10,6 @@ "lint": "npx eslint \"**/*.{js,jsx}\" --quiet", "lint:fix": "npx eslint \"**/*.{js,jsx}\" --fix", "test": "mocha __tests__ --timeout 90000", - "watch": "jest --watch", - "snapupdate": "jest --updateSnapshot", - "coverage": "jest --coverage", "semantic-release": "semantic-release", "commit": "npm run lint && cz" }, @@ -27,7 +24,6 @@ "dependencies": { "-": "0.0.1", "@herbsjs/herbs": "^1.2.0", - "D": "^1.0.0", "glob": "^7.1.7", "gluegun": "^4.6.1", "inquirer": "^8.1.1", diff --git a/src/generators/src/domain/usecases/unitTests.js b/src/generators/src/domain/usecases/unitTests.js index 5ba6ad5..3847874 100644 --- a/src/generators/src/domain/usecases/unitTests.js +++ b/src/generators/src/domain/usecases/unitTests.js @@ -28,7 +28,7 @@ const validUseCaseRequests = { create: removeID, update: (obj) => obj, delete: (obj) => { return { id: obj.id } }, - getById: removeID, + getById: (obj) => { return { id: obj.id } }, getAll: (obj) => { return [ obj, obj, obj ] } } const invalidUseCaseRequests = { @@ -60,7 +60,7 @@ function generateMockObj (schema) { return obj } -function generateRequestObject (schema, action, validReq) { +function generateRequestObject (schema, action, validReq = true) { const obj = generateMockObj(schema) if (validReq) return validUseCaseRequests[action](obj) return invalidUseCaseRequests[action](obj) @@ -72,11 +72,9 @@ module.exports = async ({ template: { generate }, filesystem }) => async () => { for (const entity of Object.keys(entities)) { const { name, schema } = entities[entity].prototype.meta for (const action of useCases) { - const useCaseName = `${action}${name}` - const ucPath = `${filesystem.cwd()}/src/domain/usecases/${camelCase(name)}/${useCaseName}.test.js` + const ucPath = `${filesystem.cwd()}/src/domain/usecases/${camelCase(name)}/${action}.test.js` if (fs.existsSync(ucPath)) continue - await generate({ template: `domain/useCases/tests/${action}.test.ejs`, target: ucPath, @@ -87,10 +85,10 @@ module.exports = async ({ template: { generate }, filesystem }) => async () => { raw: camelCase(name).replace(/([a-z0-9])([A-Z])/g, '$1 $2') }, request: { - valid: objToString(generateRequestObject(schema, action, true)), + valid: objToString(generateRequestObject(schema, action)), invalid: objToString(generateRequestObject(schema, action, false)) }, - mock: objToString(generateRequestObject(schema, action)) + mock: objToString(generateMockObj(schema)) } }) } diff --git a/src/templates/data/repository/mongo/repository.ejs b/src/templates/data/repository/mongo/repository.ejs index f13cb06..9c29b56 100644 --- a/src/templates/data/repository/mongo/repository.ejs +++ b/src/templates/data/repository/mongo/repository.ejs @@ -1,9 +1,9 @@ const Repository = require('./baseRepository') -module.exports = class <%= props.name.pascalCase %>Repository extends Repository { +module.exports = class <%- props.name.pascalCase %>Repository extends Repository { constructor(mongoInstance){ super({ - collection: '<%= props.name.camelCase %>', + collection: '<%- props.name.camelCase %>', mongo: mongoInstance }) } diff --git a/src/templates/domain/useCases/create.ejs b/src/templates/domain/useCases/create.ejs index fab650b..f1d7767 100644 --- a/src/templates/domain/useCases/create.ejs +++ b/src/templates/domain/useCases/create.ejs @@ -1,39 +1,39 @@ const { usecase, step, Ok, Err } = require('@herbsjs/herbs') -const { <%= props.name.pascalCase %> } = require('../../entities') +const { <%- props.name.pascalCase %> } = require('../../entities') -const useCase = ({ <%= props.name.camelCase %>Repository }) => () => - usecase('Create <%= props.name.pascalCase %>', { +const useCase = ({ <%- props.name.camelCase %>Repository }) => () => + usecase('Create <%- props.name.pascalCase %>', { // Input/Request metadata and validation request: { - <%= props.request %> + <%- props.request %> }, // Output/Response metadata - response: <%= props.name.pascalCase %>, + response: <%- props.name.pascalCase %>, //Authorization with Audit - // authorize: (user) => (user.canCreate<%= props.name.pascalCase %> ? Ok() : Err()), + // authorize: (user) => (user.canCreate<%- props.name.pascalCase %> ? Ok() : Err()), authorize: () => Ok(), //Step description and function - 'Check if the <%= props.name.raw %> is valid': step(ctx => { - ctx.<%= props.name.camelCase %> = <%= props.name.pascalCase %>.fromJSON(ctx.req) - ctx.<%= props.name.camelCase %>.id = Math.floor(Math.random() * 100000) + 'Check if the <%- props.name.raw %> is valid': step(ctx => { + ctx.<%- props.name.camelCase %> = <%- props.name.pascalCase %>.fromJSON(ctx.req) + ctx.<%- props.name.camelCase %>.id = Math.floor(Math.random() * 100000) - if (!ctx.<%= props.name.camelCase %>.isValid()) + if (!ctx.<%- props.name.camelCase %>.isValid()) return Err.invalidEntity({ - message: 'The <%= props.name.raw %> entity is invalid', - payload: { entity: '<%= props.name.raw %>' }, - cause: ctx.<%= props.name.camelCase %>.errors + message: 'The <%- props.name.raw %> entity is invalid', + payload: { entity: '<%- props.name.raw %>' }, + cause: ctx.<%- props.name.camelCase %>.errors }) // returning Ok continues to the next step. Err stops the use case execution. return Ok() }), - 'Save the <%= props.name.raw %>': step(async ctx => { + 'Save the <%- props.name.raw %>': step(async ctx => { // ctx.ret is the return value of a use case - return (ctx.ret = await <%= props.name.camelCase %>Repository.insert(ctx.<%= props.name.camelCase %>)) + return (ctx.ret = await <%- props.name.camelCase %>Repository.insert(ctx.<%- props.name.camelCase %>)) }) }) diff --git a/src/templates/domain/useCases/delete.ejs b/src/templates/domain/useCases/delete.ejs index 8c19493..66a3b02 100644 --- a/src/templates/domain/useCases/delete.ejs +++ b/src/templates/domain/useCases/delete.ejs @@ -1,8 +1,8 @@ const { usecase, step, Ok } = require('@herbsjs/herbs') -const { <%= props.name.pascalCase %> } = require('../../entities') +const { <%- props.name.pascalCase %> } = require('../../entities') -const useCase = ({ <%= props.name.camelCase %>Repository }) => () => - usecase('Delete the <%= props.name.pascalCase %>', { +const useCase = ({ <%- props.name.camelCase %>Repository }) => () => + usecase('Delete the <%- props.name.pascalCase %>', { // Input/Request metadata and validation request: { id: Number @@ -12,12 +12,12 @@ const useCase = ({ <%= props.name.camelCase %>Repository }) => () => response: Boolean, //Authorization with Audit - // authorize: (user) => (user.canDelete<%= props.name.pascalCase %> ? Ok() : Err()), + // authorize: (user) => (user.canDelete<%- props.name.pascalCase %> ? Ok() : Err()), authorize: () => Ok(), - 'Delete the <%= props.name.raw %>': step(async ctx => { - const <%= props.name.camelCase %> = <%= props.name.pascalCase %>.fromJSON({ id: ctx.req.id }) - await <%= props.name.camelCase %>Repository.delete(<%= props.name.camelCase %>) + 'Delete the <%- props.name.raw %>': step(async ctx => { + const <%- props.name.camelCase %> = <%- props.name.pascalCase %>.fromJSON({ id: ctx.req.id }) + await <%- props.name.camelCase %>Repository.delete(<%- props.name.camelCase %>) return Ok() }) }) diff --git a/src/templates/domain/useCases/getAll.ejs b/src/templates/domain/useCases/getAll.ejs index ecee6c0..055b6cd 100644 --- a/src/templates/domain/useCases/getAll.ejs +++ b/src/templates/domain/useCases/getAll.ejs @@ -1,8 +1,8 @@ const { usecase, step, Ok } = require('@herbsjs/herbs') -const { <%= props.name.pascalCase %> } = require('../../entities') +const { <%- props.name.pascalCase %> } = require('../../entities') -const useCase = ({ <%= props.name.camelCase %>Repository }) => () => - usecase('Get all <%= props.name.pascalCase %>s', { +const useCase = ({ <%- props.name.camelCase %>Repository }) => () => + usecase('Get all <%- props.name.pascalCase %>s', { // Input/Request metadata and validation request: { limit: Number, @@ -10,16 +10,16 @@ const useCase = ({ <%= props.name.camelCase %>Repository }) => () => }, // Output/Response metadata - response: [<%= props.name.pascalCase %>], + response: [<%- props.name.pascalCase %>], //Authorization with Audit authorize: () => Ok(), - 'Return all the <%= props.name.raw %>s': step(async ctx => { - const result = await <%= props.name.camelCase %>Repository.findAll(ctx.req) + 'Return all the <%- props.name.raw %>s': step(async ctx => { + const result = await <%- props.name.camelCase %>Repository.findAll(ctx.req) // ctx.ret is the return value of a use case - return (ctx.ret = result.map(<%= props.name.camelCase %> => <%= props.name.pascalCase %>.fromJSON(<%= props.name.camelCase %>))) + return (ctx.ret = result.map(<%- props.name.camelCase %> => <%- props.name.pascalCase %>.fromJSON(<%- props.name.camelCase %>))) }) }) diff --git a/src/templates/domain/useCases/getById.ejs b/src/templates/domain/useCases/getById.ejs index 522b7f7..7dbfe0f 100644 --- a/src/templates/domain/useCases/getById.ejs +++ b/src/templates/domain/useCases/getById.ejs @@ -1,30 +1,30 @@ const { usecase, step, Ok, Err } = require('@herbsjs/herbs') -const { <%= props.name.pascalCase %> } = require('../../entities') +const { <%- props.name.pascalCase %> } = require('../../entities') -const useCase = ({ <%= props.name.camelCase %>Repository }) => () => - usecase('Find one <%= props.name.pascalCase %>', { +const useCase = ({ <%- props.name.camelCase %>Repository }) => () => + usecase('Find one <%- props.name.pascalCase %>', { // Input/Request metadata and validation request: { id: Number, }, // Output/Response metadata - response: <%= props.name.pascalCase %>, + response: <%- props.name.pascalCase %>, //Authorization with Audit - // authorize: (user) => (user.canFindOne<%= props.name.pascalCase %> ? Ok() : Err()), + // authorize: (user) => (user.canFindOne<%- props.name.pascalCase %> ? Ok() : Err()), authorize: () => Ok(), - 'Find the <%= props.name.raw %>': step(async ctx => { + 'Find the <%- props.name.raw %>': step(async ctx => { const id = ctx.req.id - const [result] = await <%= props.name.camelCase %>Repository.findByID(id) + const [result] = await <%- props.name.camelCase %>Repository.findByID(id) if (!result) return Err.notFound({ - message: `<%= props.name.raw %> entity not found by ID: ${id}`, - payload: { entity: '<%= props.name.raw %>', id } + message: `<%- props.name.raw %> entity not found by ID: ${id}`, + payload: { entity: '<%- props.name.raw %>', id } }) // ctx.ret is the return value of a use case - return (ctx.ret = <%= props.name.pascalCase %>.fromJSON(result)) + return (ctx.ret = <%- props.name.pascalCase %>.fromJSON(result)) }) }) diff --git a/src/templates/domain/useCases/tests/create.test.ejs b/src/templates/domain/useCases/tests/create.test.ejs index b31aee8..a2e09fd 100644 --- a/src/templates/domain/useCases/tests/create.test.ejs +++ b/src/templates/domain/useCases/tests/create.test.ejs @@ -1,24 +1,24 @@ -const create<%= props.name.pascalCase %> = require('./create<%= props.name.pascalCase %>') +const create<%- props.name.pascalCase %> = require('./create<%- props.name.pascalCase %>') const assert = require('assert') -describe('Create <%= props.name.raw %>', () => { +describe('Create <%- props.name.raw %>', () => { const authorizedUser = { hasAccess: true } - describe('Valid <%= props.name.raw %>', () => { + describe('Valid <%- props.name.raw %>', () => { - it('should add <%= props.name.raw %> if is valid', async () => { + it('should add <%- props.name.raw %> if is valid', async () => { // Given const injection = { - <%= props.name.camelCase %>Repository: new ( class <%= props.name.camelCase %>Repository { - async insert(<%= props.name.camelCase %>) { return (<%= props.name.camelCase %>) } + <%- props.name.camelCase %>Repository: new ( class <%- props.name.camelCase %>Repository { + async insert(<%- props.name.camelCase %>) { return (<%- props.name.camelCase %>) } }) } const req = <%- props.request.valid %> // When - const uc = create<%= props.name.pascalCase %>(injection)() + const uc = create<%- props.name.pascalCase %>(injection)() await uc.authorize(authorizedUser) const ret = await uc.run(req) @@ -29,16 +29,16 @@ describe('Create <%= props.name.raw %>', () => { }) }) - describe('Invalid <%= props.name.raw %>', () => { + describe('Invalid <%- props.name.raw %>', () => { - it('should not create invalid <%= props.name.raw %>', async () => { + it('should not create invalid <%- props.name.raw %>', async () => { // Given const injection = {} const req = <%- props.request.invalid %> // When - const uc = create<%= props.name.pascalCase %>(injection)() + const uc = create<%- props.name.pascalCase %>(injection)() await uc.authorize(authorizedUser) const ret = await uc.run(req) diff --git a/src/templates/domain/useCases/tests/delete.test.ejs b/src/templates/domain/useCases/tests/delete.test.ejs index f2e8412..4fb7981 100644 --- a/src/templates/domain/useCases/tests/delete.test.ejs +++ b/src/templates/domain/useCases/tests/delete.test.ejs @@ -1,16 +1,16 @@ -const delete<%= props.name.pascalCase %> = require('./delete<%= props.name.pascalCase %>') +const delete<%- props.name.pascalCase %> = require('./delete<%- props.name.pascalCase %>') const assert = require('assert') -describe('Delete the <%= props.name.raw %>', () => { +describe('Delete the <%- props.name.raw %>', () => { const authorizedUser = { hasAccess: true } - describe('Valid <%= props.name.raw %>', () => { + describe('Valid <%- props.name.raw %>', () => { - it('should delete the <%= props.name.raw %> if is valid', async () => { + it('should delete the <%- props.name.raw %> if is valid', async () => { // Given const injection = { - <%= props.name.camelCase %>Repository: new ( class <%= props.name.camelCase %>Repository { + <%- props.name.camelCase %>Repository: new ( class <%- props.name.camelCase %>Repository { async delete(entity) { return true } }) } @@ -18,7 +18,7 @@ describe('Delete the <%= props.name.raw %>', () => { const req = <%- props.request.valid %> // When - const uc = delete<%= props.name.pascalCase %>(injection)() + const uc = delete<%- props.name.pascalCase %>(injection)() await uc.authorize(authorizedUser) const ret = await uc.run(req) @@ -28,15 +28,15 @@ describe('Delete the <%= props.name.raw %>', () => { }) }) - describe('Invalid <%= props.name.raw %>', () => { + describe('Invalid <%- props.name.raw %>', () => { - it('should not delete the invalid <%= props.name.pascalCase %>', async () => { + it('should not delete the invalid <%- props.name.pascalCase %>', async () => { // Given - /*{ <%= props.request %> }*/ + /*{ <%- props.request %> }*/ const req = { id : '5' } // When - const uc = delete<%= props.name.pascalCase %>({})() + const uc = delete<%- props.name.pascalCase %>({})() await uc.authorize(authorizedUser) const ret = await uc.run(req) diff --git a/src/templates/domain/useCases/tests/getAll.test.ejs b/src/templates/domain/useCases/tests/getAll.test.ejs index 2be01e0..f726901 100644 --- a/src/templates/domain/useCases/tests/getAll.test.ejs +++ b/src/templates/domain/useCases/tests/getAll.test.ejs @@ -1,20 +1,20 @@ -const getAll<%= props.name.pascalCase %> = require('./getAll<%= props.name.pascalCase %>') +const getAll<%- props.name.pascalCase %> = require('./getAll<%- props.name.pascalCase %>') const assert = require('assert') -describe('Find <%= props.name.raw %>s', () => { +describe('Find <%- props.name.raw %>s', () => { const authorizedUser = { hasAccess: true } - it('should return all <%= props.name.raw %>s', async () => { + it('should return all <%- props.name.raw %>s', async () => { // Given const req = { limit:0, offset:0} const injection = { - <%= props.name.camelCase %>Repository: new ( class <%= props.name.camelCase %>Repository { + <%- props.name.camelCase %>Repository: new ( class <%- props.name.camelCase %>Repository { async findAll() { return [] } }) } // When - const uc = getAll<%= props.name.pascalCase %>(injection)() + const uc = getAll<%- props.name.pascalCase %>(injection)() await uc.authorize(authorizedUser) const ret = await uc.run(req) diff --git a/src/templates/domain/useCases/tests/getById.test.ejs b/src/templates/domain/useCases/tests/getById.test.ejs index ff62147..05822c7 100644 --- a/src/templates/domain/useCases/tests/getById.test.ejs +++ b/src/templates/domain/useCases/tests/getById.test.ejs @@ -1,55 +1,42 @@ -const get<%= props.name.pascalCase %>ById = require('./getById<%= props.name.pascalCase %>') +const get<%- props.name.pascalCase %>ById = require('./getById<%- props.name.pascalCase %>') const assert = require('assert') -const { <%= props.name.pascalCase %> } = require('../../entities') +const { <%- props.name.pascalCase %> } = require('../../entities') -describe('Find one <%= props.name.raw %>', () => { +describe('Find one <%- props.name.raw %>', () => { const authorizedUser = { hasAccess: true } describe('Valid scenarios', () => { - it('should return <%= props.name.raw %>', async () => { - // Given - const retInjection = <%= props.name.pascalCase %>.fromJSON(<%= props.mock %>) + it('should return <%- props.name.raw %>', async () => { + // Given + const mock = <%- props.mock %> const injection = { - <%= props.name.camelCase %>Repository: new ( class <%= props.name.camelCase %>Repository { - async findByID(id) { return ([retInjection]) } + <%- props.name.camelCase %>Repository: new ( class <%- props.name.camelCase %>Repository { + async findByID(id) { return ([<%- props.name.pascalCase %>.fromJSON(mock)]) } }) } const req = <%- props.request.valid %> // When - const uc = get<%= props.name.pascalCase %>ById(injection)() + const uc = get<%- props.name.pascalCase %>ById(injection)() await uc.authorize(authorizedUser) const ret = await uc.run(req) // Then assert.ok(ret.isOk) assert.strictEqual(ret.ok.isValid(), true) - assert.strictEqual(JSON.stringify(ret.ok), JSON.stringify({id: ret.ok.id,...req})) + assert.strictEqual(JSON.stringify(ret.ok), JSON.stringify({id: ret.ok.id,...mock})) }) }) describe('Error scenarios', () => { - it('should not return for invalid input', async () => { - // Given - const req = <%- props.request.invalid %> - - // When - const uc = get<%= props.name.pascalCase %>ById({})() - await uc.authorize(authorizedUser) - const ret = await uc.run(req) - - // Then - assert.ok(ret.isErr) - }) - it('return notFoundError', async () => { // Given const injection = { - <%= props.name.camelCase %>Repository: new ( class <%= props.name.camelCase %>Repository { + <%- props.name.camelCase %>Repository: new ( class <%- props.name.camelCase %>Repository { async findByID(id) { return ([]) } }) } @@ -57,12 +44,13 @@ describe('Find one <%= props.name.raw %>', () => { const req = <%- props.request.invalid %> // When - const uc = get<%= props.name.pascalCase %>ById(injection)() + const uc = get<%- props.name.pascalCase %>ById(injection)() await uc.authorize(authorizedUser) const ret = await uc.run(req) // Then assert.ok(ret.isErr) + assert.ok(ret.err.isNotFoundError) }) }) }) diff --git a/src/templates/domain/useCases/tests/update.test.ejs b/src/templates/domain/useCases/tests/update.test.ejs index 45555a8..a6d51a6 100644 --- a/src/templates/domain/useCases/tests/update.test.ejs +++ b/src/templates/domain/useCases/tests/update.test.ejs @@ -1,18 +1,18 @@ -const update<%= props.name.pascalCase %> = require('./update<%= props.name.pascalCase %>') +const update<%- props.name.pascalCase %> = require('./update<%- props.name.pascalCase %>') const assert = require('assert') -const { <%= props.name.pascalCase %> } = require('../../entities') +const { <%- props.name.pascalCase %> } = require('../../entities') -describe('Update <%= props.name.raw %>', () => { +describe('Update <%- props.name.raw %>', () => { const authorizedUser = { hasAccess: true } - describe('Valid <%= props.name.raw %>', () => { + describe('Valid <%- props.name.raw %>', () => { - it('should update <%= props.name.raw %> if is valid', async () => { + it('should update <%- props.name.raw %> if is valid', async () => { // Given - const retInjection = <%= props.name.pascalCase %>.fromJSON(<%= props.mock %>) + const retInjection = <%- props.name.pascalCase %>.fromJSON(<%- props.mock %>) const injection = { - <%= props.name.camelCase %>Repository: new ( class <%= props.name.camelCase %>Repository { + <%- props.name.camelCase %>Repository: new ( class <%- props.name.camelCase %>Repository { async findByID(id) { return ([retInjection]) } async update(id) { return true } }) @@ -21,7 +21,7 @@ describe('Update <%= props.name.raw %>', () => { const req = <%- props.request.valid %> // When - const uc = update<%= props.name.pascalCase %>(injection)() + const uc = update<%- props.name.pascalCase %>(injection)() await uc.authorize(authorizedUser) const ret = await uc.run(req) @@ -31,15 +31,15 @@ describe('Update <%= props.name.raw %>', () => { }) }) - describe('Invalid <%= props.name.raw %>', () => { + describe('Invalid <%- props.name.raw %>', () => { - it('should not update invalid <%= props.name.pascalCase %>', async () => { + it('should not update invalid <%- props.name.pascalCase %>', async () => { // Given const injection = {} const req = <%- props.request.invalid %> // When - const uc = update<%= props.name.pascalCase %>(injection)() + const uc = update<%- props.name.pascalCase %>(injection)() await uc.authorize(authorizedUser) const ret = await uc.run(req) diff --git a/src/templates/domain/useCases/update.ejs b/src/templates/domain/useCases/update.ejs index 2346027..6b28f34 100644 --- a/src/templates/domain/useCases/update.ejs +++ b/src/templates/domain/useCases/update.ejs @@ -1,41 +1,41 @@ const merge = require('deepmerge') const { usecase, step, Ok, Err } = require('@herbsjs/herbs') -const { <%= props.name.pascalCase %> } = require('../../entities') +const { <%- props.name.pascalCase %> } = require('../../entities') -const useCase = ({ <%= props.name.camelCase %>Repository }) => () => - usecase('Update <%= props.name.pascalCase %>', { +const useCase = ({ <%- props.name.camelCase %>Repository }) => () => + usecase('Update <%- props.name.pascalCase %>', { // Input/Request metadata and validation request: { id: Number, - <%= props.request %> + <%- props.request %> }, // Output/Response metadata - response: <%= props.name.pascalCase %>, + response: <%- props.name.pascalCase %>, //Authorization with Audit - // authorize: (user) => (user.canUpdate<%= props.name.pascalCase %> ? Ok() : Err()), + // authorize: (user) => (user.canUpdate<%- props.name.pascalCase %> ? Ok() : Err()), authorize: () => Ok(), //Step description and function - 'Check if the <%= props.name.raw %> is valid': step(async ctx => { - const <%= props.name.camelCase %> = await <%= props.name.camelCase %>Repository.findByID(parseInt(ctx.req.id)) - const new<%= props.name.pascalCase %> = merge.all([ <%= props.name.camelCase %>, ctx.req ]) - ctx.<%= props.name.camelCase %> = <%= props.name.pascalCase %>.fromJSON(new<%= props.name.pascalCase %>) + 'Check if the <%- props.name.raw %> is valid': step(async ctx => { + const <%- props.name.camelCase %> = await <%- props.name.camelCase %>Repository.findByID(parseInt(ctx.req.id)) + const new<%- props.name.pascalCase %> = merge.all([ <%- props.name.camelCase %>, ctx.req ]) + ctx.<%- props.name.camelCase %> = <%- props.name.pascalCase %>.fromJSON(new<%- props.name.pascalCase %>) - if (!ctx.<%= props.name.camelCase %>.isValid()) return Err.invalidEntity({ - message: 'The <%= props.name.pascalCase %> entity is invalid', - payload: { entity: '<%= props.name.raw %>' }, - cause: ctx.<%= props.name.camelCase %>.errors + if (!ctx.<%- props.name.camelCase %>.isValid()) return Err.invalidEntity({ + message: 'The <%- props.name.pascalCase %> entity is invalid', + payload: { entity: '<%- props.name.raw %>' }, + cause: ctx.<%- props.name.camelCase %>.errors }) // returning Ok continues to the next step. Err stops the use case execution. return Ok() }), - 'Update the <%= props.name.raw %>': step(async ctx => { + 'Update the <%- props.name.raw %>': step(async ctx => { // ctx.ret is the return value of a use case - return (ctx.ret = await <%= props.name.camelCase %>Repository.update(ctx.<%= props.name.camelCase %>)) + return (ctx.ret = await <%- props.name.camelCase %>Repository.update(ctx.<%- props.name.camelCase %>)) }) }) diff --git a/src/templates/infra/data/repository/mongo/repository.ejs b/src/templates/infra/data/repository/mongo/repository.ejs index f833b36..187bbbd 100644 --- a/src/templates/infra/data/repository/mongo/repository.ejs +++ b/src/templates/infra/data/repository/mongo/repository.ejs @@ -1,11 +1,11 @@ -const { <%= props.name.pascalCase %> } = require('../../../domain/entities') +const { <%- props.name.pascalCase %> } = require('../../../domain/entities') const { Repository } = require('@herbsjs/herbs2mongo') -module.exports = class <%= props.name.pascalCase %>Repository extends Repository { +module.exports = class <%- props.name.pascalCase %>Repository extends Repository { constructor(connection){ super({ - entity: <%= props.name.pascalCase %>, - collection: '<%= props.name.camelCase %>', + entity: <%- props.name.pascalCase %>, + collection: '<%- props.name.camelCase %>', ids: ['id'], mongodb: connection }) diff --git a/src/templates/infra/data/repository/postgres/repository.ejs b/src/templates/infra/data/repository/postgres/repository.ejs index 6ef88e2..6600625 100644 --- a/src/templates/infra/data/repository/postgres/repository.ejs +++ b/src/templates/infra/data/repository/postgres/repository.ejs @@ -1,10 +1,10 @@ const { Repository } = require("@herbsjs/herbs2knex") -const { <%= props.name.pascalCase %> } = require('../../../domain/entities') +const { <%- props.name.pascalCase %> } = require('../../../domain/entities') -module.exports = class <%= props.name.pascalCase %>Repository extends Repository { +module.exports = class <%- props.name.pascalCase %>Repository extends Repository { constructor(connection) { super({ - entity: <%= props.name.pascalCase %>, + entity: <%- props.name.pascalCase %>, table: "<%- props.table %>", ids: ["id"], knex: connection diff --git a/src/templates/infra/data/repository/sqlserver/repository.ejs b/src/templates/infra/data/repository/sqlserver/repository.ejs index 6ef88e2..6600625 100644 --- a/src/templates/infra/data/repository/sqlserver/repository.ejs +++ b/src/templates/infra/data/repository/sqlserver/repository.ejs @@ -1,10 +1,10 @@ const { Repository } = require("@herbsjs/herbs2knex") -const { <%= props.name.pascalCase %> } = require('../../../domain/entities') +const { <%- props.name.pascalCase %> } = require('../../../domain/entities') -module.exports = class <%= props.name.pascalCase %>Repository extends Repository { +module.exports = class <%- props.name.pascalCase %>Repository extends Repository { constructor(connection) { super({ - entity: <%= props.name.pascalCase %>, + entity: <%- props.name.pascalCase %>, table: "<%- props.table %>", ids: ["id"], knex: connection diff --git a/src/templates/package.json.ejs b/src/templates/package.json.ejs index fb21689..f6db6e9 100644 --- a/src/templates/package.json.ejs +++ b/src/templates/package.json.ejs @@ -1,7 +1,7 @@ { - "name": "<%= props.name %>", + "name": "<%- props.name %>", "version": "1.0.0", - "description": "<%= props.description %>", + "description": "<%- props.description %>", "main": "src/index.js", "scripts": { "start": "node src/index.js", @@ -11,8 +11,8 @@ "test": "mocha --timeout 999999 --colors --recursive src/**/*.test.js" }, "keywords": ["herbs"], - "author": "<%= props.author %>", - "license": "<%= props.license %>", + "author": "<%- props.author %>", + "license": "<%- props.license %>", "dependencies": { } } \ No newline at end of file From c49e67447b5a6f08d5907cc45f0acfea2014d47e Mon Sep 17 00:00:00 2001 From: vortx Date: Tue, 7 Dec 2021 17:33:24 -0300 Subject: [PATCH 11/13] refactor: refactored requests into tests --- src/generators/index.js | 3 ++- src/generators/src/domain/usecases/unitTests.js | 16 ++++++++-------- src/generators/src/domain/usecases/useCases.js | 12 +----------- .../infra/database/repositories/repositories.js | 6 +++--- src/generators/utils.js | 14 ++++++++++---- .../domain/useCases/tests/create.test.ejs | 8 ++++++-- .../domain/useCases/tests/delete.test.ejs | 8 ++++++-- .../domain/useCases/tests/getById.test.ejs | 14 ++++++++++---- .../domain/useCases/tests/update.test.ejs | 12 +++++++++--- 9 files changed, 55 insertions(+), 38 deletions(-) diff --git a/src/generators/index.js b/src/generators/index.js index 809c011..39c72c5 100644 --- a/src/generators/index.js +++ b/src/generators/index.js @@ -23,7 +23,8 @@ module.exports = async (tools) => { }, update: { entities: await require('./src/domain/entities')(tools, true), - useCases: await require('./src/domain/usecases/useCases')(tools, true), + useCases: await require('./src/domain/usecases/useCases')(tools), + useCasesTests: await require('./src/domain/usecases/unitTests')(tools), migrations: updateMigration ? await require('./src/infra/database/migrations/migrations')(tools) : ignore, repositories: await require('./src/infra/database/repositories/repositories')(tools, true) } diff --git a/src/generators/src/domain/usecases/unitTests.js b/src/generators/src/domain/usecases/unitTests.js index 3847874..bade1f1 100644 --- a/src/generators/src/domain/usecases/unitTests.js +++ b/src/generators/src/domain/usecases/unitTests.js @@ -69,12 +69,12 @@ function generateRequestObject (schema, action, validReq = true) { module.exports = async ({ template: { generate }, filesystem }) => async () => { const entities = require(`${filesystem.cwd()}/src/domain/entities`) - for (const entity of Object.keys(entities)) { + Object.keys(entities).map(async (entity) => { const { name, schema } = entities[entity].prototype.meta - for (const action of useCases) { + useCases.map(async(action) => { const ucPath = `${filesystem.cwd()}/src/domain/usecases/${camelCase(name)}/${action}.test.js` - if (fs.existsSync(ucPath)) continue + if (fs.existsSync(ucPath)) return await generate({ template: `domain/useCases/tests/${action}.test.ejs`, target: ucPath, @@ -85,12 +85,12 @@ module.exports = async ({ template: { generate }, filesystem }) => async () => { raw: camelCase(name).replace(/([a-z0-9])([A-Z])/g, '$1 $2') }, request: { - valid: objToString(generateRequestObject(schema, action)), - invalid: objToString(generateRequestObject(schema, action, false)) + valid: objToString(generateRequestObject(schema, action), 4, true), + invalid: objToString(generateRequestObject(schema, action, false), 4, true) }, - mock: objToString(generateMockObj(schema)) + mock: objToString(generateMockObj(schema), 4, true) } }) - } - } + }) + }) } diff --git a/src/generators/src/domain/usecases/useCases.js b/src/generators/src/domain/usecases/useCases.js index db356f1..d78ae13 100644 --- a/src/generators/src/domain/usecases/useCases.js +++ b/src/generators/src/domain/usecases/useCases.js @@ -13,17 +13,7 @@ async function generateRequestschema (schema) { obj[name] = type.name || type.constructor.name return obj }, {}) - - // convert plain JSON and remove quotation marks(") - const str = JSON.stringify(obj, null, 8) - .replace(/"/g, '') - .split('\n') - - // remove first and last lines - str.shift() - str.pop() - - return str.join('\n').trim() + return objToString(obj, 4, true) } module.exports = async ({ template: { generate }, filesystem }) => async () => { diff --git a/src/generators/src/infra/database/repositories/repositories.js b/src/generators/src/infra/database/repositories/repositories.js index afa6904..4006ed6 100644 --- a/src/generators/src/infra/database/repositories/repositories.js +++ b/src/generators/src/infra/database/repositories/repositories.js @@ -31,9 +31,9 @@ async function generateRepositories(generate, filesystem, db) { async function updateRepositories(generate, filesystem) { const paths = { - mongo: '/src/infra/database/repositories/mongo.js', - sqlserver: '/src/infra/database/repositories/sqlserver.js', - postgres: '/src/infra/database/repositories/postgres.js' + mongo: '/src/infra/config/mongo.js', + sqlserver: '/src/infra/config/sqlserver.js', + postgres: '/src/infra/config/postgres.js' } const db = Object.keys(paths).filter(key => fs.existsSync(`${filesystem.cwd()}${paths[key]}`)) diff --git a/src/generators/utils.js b/src/generators/utils.js index b39be30..cee2563 100644 --- a/src/generators/utils.js +++ b/src/generators/utils.js @@ -2,11 +2,17 @@ const camelCase = require('lodash.camelcase') const startCase = require('lodash.startCase') module.exports = { - objToString: (obj, spaces = 4, removeFirstLine = false) => { + objToString: (obj, spaces = 4, removeQuotes = false) => { let json = JSON.stringify(obj, null, spaces).replace(/"/g, '') - if(removeFirstLine) - return json.replace('{\n', '').split('\n').shift().join('\n') - return json + + if(!removeQuotes) return json + + const array = json.split('\n') + + array.shift() + array.pop() + + return array.join('\n').trim() }, arrayToStringList: (arr, spaces = 1) => { const list = JSON.stringify(arr, null, spaces) diff --git a/src/templates/domain/useCases/tests/create.test.ejs b/src/templates/domain/useCases/tests/create.test.ejs index a2e09fd..6d3f831 100644 --- a/src/templates/domain/useCases/tests/create.test.ejs +++ b/src/templates/domain/useCases/tests/create.test.ejs @@ -15,7 +15,9 @@ describe('Create <%- props.name.raw %>', () => { }) } - const req = <%- props.request.valid %> + const req = { + <%- props.request.valid %> + } // When const uc = create<%- props.name.pascalCase %>(injection)() @@ -35,7 +37,9 @@ describe('Create <%- props.name.raw %>', () => { // Given const injection = {} - const req = <%- props.request.invalid %> + const req = { + <%- props.request.invalid %> + } // When const uc = create<%- props.name.pascalCase %>(injection)() diff --git a/src/templates/domain/useCases/tests/delete.test.ejs b/src/templates/domain/useCases/tests/delete.test.ejs index 4fb7981..b8e2fca 100644 --- a/src/templates/domain/useCases/tests/delete.test.ejs +++ b/src/templates/domain/useCases/tests/delete.test.ejs @@ -15,7 +15,9 @@ describe('Delete the <%- props.name.raw %>', () => { }) } - const req = <%- props.request.valid %> + const req = { + <%- props.request.valid %> + } // When const uc = delete<%- props.name.pascalCase %>(injection)() @@ -32,7 +34,9 @@ describe('Delete the <%- props.name.raw %>', () => { it('should not delete the invalid <%- props.name.pascalCase %>', async () => { // Given - /*{ <%- props.request %> }*/ + /*{ + <%- props.request %> + }*/ const req = { id : '5' } // When diff --git a/src/templates/domain/useCases/tests/getById.test.ejs b/src/templates/domain/useCases/tests/getById.test.ejs index 05822c7..638985f 100644 --- a/src/templates/domain/useCases/tests/getById.test.ejs +++ b/src/templates/domain/useCases/tests/getById.test.ejs @@ -9,7 +9,9 @@ describe('Find one <%- props.name.raw %>', () => { it('should return <%- props.name.raw %>', async () => { // Given - const mock = <%- props.mock %> + const mock = { + <%- props.mock %> + } const injection = { <%- props.name.camelCase %>Repository: new ( class <%- props.name.camelCase %>Repository { @@ -17,7 +19,9 @@ describe('Find one <%- props.name.raw %>', () => { }) } - const req = <%- props.request.valid %> + const req = { + <%- props.request.valid %> + } // When const uc = get<%- props.name.pascalCase %>ById(injection)() @@ -41,7 +45,9 @@ describe('Find one <%- props.name.raw %>', () => { }) } - const req = <%- props.request.invalid %> + const req = { + <%- props.request.invalid %> + } // When const uc = get<%- props.name.pascalCase %>ById(injection)() @@ -50,7 +56,7 @@ describe('Find one <%- props.name.raw %>', () => { // Then assert.ok(ret.isErr) - assert.ok(ret.err.isNotFoundError) + assert.ok(ret.isNotFoundError) }) }) }) diff --git a/src/templates/domain/useCases/tests/update.test.ejs b/src/templates/domain/useCases/tests/update.test.ejs index a6d51a6..18d672e 100644 --- a/src/templates/domain/useCases/tests/update.test.ejs +++ b/src/templates/domain/useCases/tests/update.test.ejs @@ -10,7 +10,9 @@ describe('Update <%- props.name.raw %>', () => { it('should update <%- props.name.raw %> if is valid', async () => { // Given - const retInjection = <%- props.name.pascalCase %>.fromJSON(<%- props.mock %>) + const retInjection = <%- props.name.pascalCase %>.fromJSON({ + <%- props.mock %> + }) const injection = { <%- props.name.camelCase %>Repository: new ( class <%- props.name.camelCase %>Repository { async findByID(id) { return ([retInjection]) } @@ -18,7 +20,9 @@ describe('Update <%- props.name.raw %>', () => { }) } - const req = <%- props.request.valid %> + const req = { + <%- props.request.valid %> + } // When const uc = update<%- props.name.pascalCase %>(injection)() @@ -36,7 +40,9 @@ describe('Update <%- props.name.raw %>', () => { it('should not update invalid <%- props.name.pascalCase %>', async () => { // Given const injection = {} - const req = <%- props.request.invalid %> + const req = { + <%- props.request.invalid %> + } // When const uc = update<%- props.name.pascalCase %>(injection)() From 6da2891a8461a7021d5cb0bb7071700d5dd1da12 Mon Sep 17 00:00:00 2001 From: vortx Date: Wed, 8 Dec 2021 10:33:54 -0300 Subject: [PATCH 12/13] style(fixed identations at objtostring): fixed identations at objtostring --- .../src/domain/usecases/unitTests.js | 8 +++++--- .../src/domain/usecases/useCases.js | 3 ++- src/generators/utils.js | 20 ++++++++++++------- src/templates/domain/useCases/create.ejs | 2 +- .../domain/useCases/tests/delete.test.ejs | 3 --- src/templates/domain/useCases/update.ejs | 4 ++-- 6 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/generators/src/domain/usecases/unitTests.js b/src/generators/src/domain/usecases/unitTests.js index bade1f1..d1f226a 100644 --- a/src/generators/src/domain/usecases/unitTests.js +++ b/src/generators/src/domain/usecases/unitTests.js @@ -75,6 +75,8 @@ module.exports = async ({ template: { generate }, filesystem }) => async () => { const ucPath = `${filesystem.cwd()}/src/domain/usecases/${camelCase(name)}/${action}.test.js` if (fs.existsSync(ucPath)) return + + const objOptions = { spaces: 4, extraSpaces: 4, removeQuotes: true } await generate({ template: `domain/useCases/tests/${action}.test.ejs`, target: ucPath, @@ -85,10 +87,10 @@ module.exports = async ({ template: { generate }, filesystem }) => async () => { raw: camelCase(name).replace(/([a-z0-9])([A-Z])/g, '$1 $2') }, request: { - valid: objToString(generateRequestObject(schema, action), 4, true), - invalid: objToString(generateRequestObject(schema, action, false), 4, true) + valid: objToString(generateRequestObject(schema, action), objOptions), + invalid: objToString(generateRequestObject(schema, action, false), objOptions) }, - mock: objToString(generateMockObj(schema), 4, true) + mock: objToString(generateMockObj(schema), objOptions) } }) }) diff --git a/src/generators/src/domain/usecases/useCases.js b/src/generators/src/domain/usecases/useCases.js index d78ae13..a936a71 100644 --- a/src/generators/src/domain/usecases/useCases.js +++ b/src/generators/src/domain/usecases/useCases.js @@ -13,7 +13,8 @@ async function generateRequestschema (schema) { obj[name] = type.name || type.constructor.name return obj }, {}) - return objToString(obj, 4, true) + + return objToString(obj, { spaces: 2, removeQuotes: true, extraSpaces: 4 }) } module.exports = async ({ template: { generate }, filesystem }) => async () => { diff --git a/src/generators/utils.js b/src/generators/utils.js index cee2563..cbadf10 100644 --- a/src/generators/utils.js +++ b/src/generators/utils.js @@ -2,17 +2,23 @@ const camelCase = require('lodash.camelcase') const startCase = require('lodash.startCase') module.exports = { - objToString: (obj, spaces = 4, removeQuotes = false) => { + objToString: (obj, { spaces = 4, removeQuotes = false, extraSpaces = 0 } = {}) => { + let json = JSON.stringify(obj, null, spaces).replace(/"/g, '') - if(!removeQuotes) return json - - const array = json.split('\n') + let lines = json.split('\n') - array.shift() - array.pop() + if(removeQuotes) { + lines.shift() + lines.pop() + } + + if(extraSpaces) + lines.forEach(function(line, index) { + this[index] = `${' '.repeat(extraSpaces)}${line}` + }, lines) - return array.join('\n').trim() + return lines.join('\n').trim() }, arrayToStringList: (arr, spaces = 1) => { const list = JSON.stringify(arr, null, spaces) diff --git a/src/templates/domain/useCases/create.ejs b/src/templates/domain/useCases/create.ejs index f1d7767..a993177 100644 --- a/src/templates/domain/useCases/create.ejs +++ b/src/templates/domain/useCases/create.ejs @@ -5,7 +5,7 @@ const useCase = ({ <%- props.name.camelCase %>Repository }) => () => usecase('Create <%- props.name.pascalCase %>', { // Input/Request metadata and validation request: { - <%- props.request %> + <%- props.request %> }, // Output/Response metadata diff --git a/src/templates/domain/useCases/tests/delete.test.ejs b/src/templates/domain/useCases/tests/delete.test.ejs index b8e2fca..029ab3e 100644 --- a/src/templates/domain/useCases/tests/delete.test.ejs +++ b/src/templates/domain/useCases/tests/delete.test.ejs @@ -34,9 +34,6 @@ describe('Delete the <%- props.name.raw %>', () => { it('should not delete the invalid <%- props.name.pascalCase %>', async () => { // Given - /*{ - <%- props.request %> - }*/ const req = { id : '5' } // When diff --git a/src/templates/domain/useCases/update.ejs b/src/templates/domain/useCases/update.ejs index 6b28f34..2d4ceaa 100644 --- a/src/templates/domain/useCases/update.ejs +++ b/src/templates/domain/useCases/update.ejs @@ -6,8 +6,8 @@ const useCase = ({ <%- props.name.camelCase %>Repository }) => () => usecase('Update <%- props.name.pascalCase %>', { // Input/Request metadata and validation request: { - id: Number, - <%- props.request %> + id: Number, + <%- props.request %> }, // Output/Response metadata From a8129654ea9d2962e88b3327b618876f25655381 Mon Sep 17 00:00:00 2001 From: vortx Date: Wed, 8 Dec 2021 11:56:44 -0300 Subject: [PATCH 13/13] (refactor): move api generators --- src/generators/index.js | 6 ++--- src/generators/src/infra/api/rest.js | 35 ++++++++++++++++++++++++-- src/generators/src/infra/graphql.js | 31 ----------------------- src/generators/src/infra/rest.js | 37 ---------------------------- src/generators/src/infra/server.js | 7 ------ 5 files changed, 36 insertions(+), 80 deletions(-) delete mode 100644 src/generators/src/infra/graphql.js delete mode 100644 src/generators/src/infra/rest.js delete mode 100644 src/generators/src/infra/server.js diff --git a/src/generators/index.js b/src/generators/index.js index 5ecd465..3ea327b 100644 --- a/src/generators/index.js +++ b/src/generators/index.js @@ -15,10 +15,10 @@ module.exports = async (tools) => { migrations: (options.postgres || options.sqlserver) ? await require('./src/infra/database/migrations/migrations')(tools) : ignore, useCases: await require('./src/domain/useCases/useCases')(tools), useCasesTests: await require('./src/domain/usecases/unitTests')(tools), - graphql: options.graphql ? await require('./src/infra/graphql')(tools) : ignore, - rest: options.rest ? await require('./src/infra/rest')(tools) : ignore, + graphql: options.graphql ? await require('./src/infra/api/graphql')(tools) : ignore, + rest: options.rest ? await require('./src/infra/api/rest')(tools) : ignore, config: infra ? await require('./src/infra/config')(tools) : ignore, - server: infra ? await require('./src/infra/server')(tools) : ignore, + server: infra ? await require('./src/infra/api/server')(tools) : ignore, index: await require('./src/index')(tools), git: options.git ? await require('./src/infra/git')(tools) : ignore }, diff --git a/src/generators/src/infra/api/rest.js b/src/generators/src/infra/api/rest.js index d992a40..5a0c8d2 100644 --- a/src/generators/src/infra/api/rest.js +++ b/src/generators/src/infra/api/rest.js @@ -1,6 +1,37 @@ -module.exports = async ({ generate }) => async () => { +const camelCase = require('lodash.camelcase') +const { objToString } = require('../../../utils') + +const requireRoute = (type, entityName, useId = false) => `{ usecase: require('../../../domain/usecases/${camelCase(entityName)}/${type}${entityName}')(repositories)${useId ? ', id: \'id\'' : ''}}` + +module.exports = async ({ template: { generate }, filesystem }) => async () => { + const usecases = require(`${filesystem.cwd()}/src/domain/usecases`) + + // groupBy tags + const ucByTag = usecases.reduce((acc, d) => { + if (Object.keys(acc).includes(d.tags.group)) return acc + acc[d.tags.group] = usecases.filter(g => g.tags.group === d.tags.group) + return acc + }, {}) + const routes = [] + for (const tag of Object.keys(ucByTag)) { + const route = { + name: `'${camelCase(tag)}'` + } + for (const obj of ucByTag[tag]) { + const ucDescription = obj.usecase({})().description + const entityName = tag.slice(0, -1) + if (ucDescription.includes('Create')) route.post = requireRoute('create', entityName) + if (ucDescription.includes('Find')) route.getById = requireRoute('getById', entityName, true) + if (ucDescription.includes('FindAll')) route.getAll = requireRoute('getAll', entityName) + if (ucDescription.includes('Update')) route.put = requireRoute('update', entityName) + if (ucDescription.includes('Delete')) route.delete = requireRoute('delete', entityName) + } + routes.push(route) + } + await generate({ template: 'infra/api/rest/index.ejs', - target: 'src/infra/api/rest/index.js' + target: 'src/infra/api/rest/index.js', + props: { routes: objToString(routes) } }) } diff --git a/src/generators/src/infra/graphql.js b/src/generators/src/infra/graphql.js deleted file mode 100644 index cc85e5c..0000000 --- a/src/generators/src/infra/graphql.js +++ /dev/null @@ -1,31 +0,0 @@ -module.exports = async ({ template: { generate } }) => async () => { - await generate({ - template: 'infra/api/graphql/mutations.ejs', - target: 'src/infra/api/graphql/mutations.js' - }) - - await generate({ - template: 'infra/api/graphql/queries.ejs', - target: 'src/infra/api/graphql/queries.js' - }) - - await generate({ - template: 'infra/api/graphql/types.ejs', - target: 'src/infra/api/graphql/types.js' - }) - - await generate({ - template: 'infra/api/graphql/inputs.ejs', - target: 'src/infra/api/graphql/inputs.js' - }) - - await generate({ - template: 'infra/api/graphql/defaultResolver.ejs', - target: 'src/infra/api/graphql/defaultResolver.js' - }) - - await generate({ - template: 'infra/api/graphql/index.ejs', - target: 'src/infra/api/graphql/index.js' - }) -} diff --git a/src/generators/src/infra/rest.js b/src/generators/src/infra/rest.js deleted file mode 100644 index 77218ab..0000000 --- a/src/generators/src/infra/rest.js +++ /dev/null @@ -1,37 +0,0 @@ -const camelCase = require('lodash.camelcase') -const { objToString } = require('../../utils') - -const requireRoute = (type, entityName, useId = false) => `{ usecase: require('../../../domain/usecases/${camelCase(entityName)}/${type}${entityName}')(repositories)${useId ? ', id: \'id\'' : ''}}` - -module.exports = async ({ template: { generate }, filesystem }) => async () => { - const usecases = require(`${filesystem.cwd()}/src/domain/usecases`) - - // groupBy tags - const ucByTag = usecases.reduce((acc, d) => { - if (Object.keys(acc).includes(d.tags.group)) return acc - acc[d.tags.group] = usecases.filter(g => g.tags.group === d.tags.group) - return acc - }, {}) - const routes = [] - for (const tag of Object.keys(ucByTag)) { - const route = { - name: `'${camelCase(tag)}'` - } - for (const obj of ucByTag[tag]) { - const ucDescription = obj.usecase({})().description - const entityName = tag.slice(0, -1) - if (ucDescription.includes('Create')) route.post = requireRoute('create', entityName) - if (ucDescription.includes('Find')) route.getById = requireRoute('getById', entityName, true) - if (ucDescription.includes('FindAll')) route.getAll = requireRoute('getAll', entityName) - if (ucDescription.includes('Update')) route.put = requireRoute('update', entityName) - if (ucDescription.includes('Delete')) route.delete = requireRoute('delete', entityName) - } - routes.push(route) - } - - await generate({ - template: 'infra/api/rest/index.ejs', - target: 'src/infra/api/rest/index.js', - props: { routes: objToString(routes) } - }) -} diff --git a/src/generators/src/infra/server.js b/src/generators/src/infra/server.js deleted file mode 100644 index 1d4f89c..0000000 --- a/src/generators/src/infra/server.js +++ /dev/null @@ -1,7 +0,0 @@ -module.exports = async ({ template: { generate }, parameters: { options } }) => async () => { - await generate({ - template: 'infra/api/server.ejs', - target: 'src/infra/api/server.js', - props: { ...options } - }) -}