Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Feature/dynamic tests #57

Merged
merged 20 commits into from
Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
32f56f2
feat(generating dynamic tests): generating dynamic tests
italojs Sep 30, 2021
4e166fd
test: generating valid and invalid usecase request objects while i'm …
italojs Sep 30, 2021
a157c97
test: added dynamiclly return mock int ogetbyid and update tests files
italojs Sep 30, 2021
572aa48
feat(🧪 generating test for all entities): 🧪 Generating test for all e…
italojs Oct 5, 2021
c870289
merge: merged with main
italojs Oct 5, 2021
ead78ad
feat: generating two unit test for all usecases
italojs Oct 5, 2021
af4ff2b
style: fix typo
italojs Oct 10, 2021
9691b48
style: update variable names and folder structure
italojs Oct 12, 2021
77bdc89
Merge branch 'main' of github.com:herbsjs/herbs-cli into feature/dyna…
italojs Oct 12, 2021
d111049
merge: resolving conflicst
italojs Oct 12, 2021
ba795f6
fix: git merge and added getAll test
italojs Oct 14, 2021
b2557bd
Merge branch 'main' of github.com:herbsjs/herbs-cli into feature/dyna…
italojs Oct 14, 2021
04eed28
Merge branch 'main' of github.com:herbsjs/herbs-cli into feature/dyna…
italojs Oct 25, 2021
2b89c08
fix: fix unit tests
italojs Nov 20, 2021
4fc074b
wip: saving the code
italojs Nov 20, 2021
66b36ed
feat(not found error test): added not found error test
italojs Nov 21, 2021
c49e674
refactor: refactored requests into tests
italojs Dec 7, 2021
6da2891
style(fixed identations at objtostring): fixed identations at objtost…
italojs Dec 8, 2021
9892c20
refactor: git merge
italojs Dec 8, 2021
a812965
(refactor): move api generators
italojs Dec 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
26 changes: 13 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
```
Expand All @@ -88,7 +88,7 @@ Generate a new project based on your entities(or not)
| Generate graphql layer? | This will generate a graphql layer for your domain. | Y | n |
| Generate rest layer? | Description | Y | n |
| What database do you want to use? | Use arrow keys to choose your Database. | -- | -- |
| Specify your entities folder path? (optional) | If you have [Gotu](https://github.com/herbsjs/gotu) entities, specify the folder path for CLI generate the project around that | empty | ./example/my-custom-entities/ |
| Specify your entities folder path? (optional) | If you have [Gotu](https://github.com/herbsjs/gotu) entities, specify the folder path for CLI generate the project around that | empty | ./example-entities/ |

### herbs update

Expand Down
32 changes: 16 additions & 16 deletions src/generators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || options.sqlserver) ? 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 || 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,
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)
}
}
}
50 changes: 0 additions & 50 deletions src/generators/new/domain/useCasesTests.js

This file was deleted.

94 changes: 94 additions & 0 deletions src/generators/src/domain/usecases/unitTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@

const camelCase = require('lodash.camelcase')
italojs marked this conversation as resolved.
Show resolved Hide resolved
const { objToString } = require('../../../utils')
const fs = require('fs')

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
return obj
},
update: (obj) => obj,
delete: (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(validUseCaseRequests)

const valueType = {
String: "'string'",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: a text, intead of string

Number: 99,
Boolean: true,
Array: []
}

function generateMockObj (schema) {
const obj = {}
for (const key of Object.keys(schema)) {
obj[key] = valueType[schema[key].type.name]
}
return obj
}

function generateRequestObject (schema, action, validReq) {
const obj = generateMockObj(schema)
if (validReq) return validUseCaseRequests[action](obj)
return invalidUseCaseRequests[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))
},
mock: objToString(generateRequestObject(schema, action))
}
})
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const useCases = ['create', 'update', 'delete', 'getById']
const { objToString } = require('../../utils')
const { objToString } = require('../../../utils')
const camelCase = require('lodash.camelcase')
const startCase = require('lodash.startCase')
const fs = require('fs')

async function generateRequest (schema) {
async function generateRequestschema (schema) {
// schema to plain JSON
const obj = Object.keys(schema).reduce((obj, key) => {
const { name, type } = schema[key]
Expand Down Expand Up @@ -54,7 +54,7 @@ module.exports = async ({ template: { generate }, filesystem }) => async () => {
pascalCase: startCase(camelCase(name)).replace(/ /g, ''),
camelCase: camelCase(name)
},
request: await generateRequest(schema)
request: await generateRequestschema(schema)
}
})
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
19 changes: 7 additions & 12 deletions src/templates/domain/useCases/tests/create.test.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ const assert = require('assert')


describe('Create <%= props.name.pascalCase %>', () => {
function aUser({ hasAccess }) {
return { hasAccess }
}
const authorizedUser = { hasAccess: true }

describe('Valid <%= props.name.pascalCase %>', () => {

Expand All @@ -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 %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generated value has multi lines and does not respect the spaces / tabs from the template.

Ex:

      }

      const req = {
    nickname: 'string',
    password: 'string'
}

      // When

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking to resolve it after resolve this issue #14, so the linter will fix it for us


// When
const uc = create<%= props.name.pascalCase %>(injection)()
italojs marked this conversation as resolved.
Show resolved Hide resolved
await uc.authorize(<%= props.name.camelCase %>)
await uc.authorize(authorizedUser)
const ret = await uc.run(req)

// Then
Expand All @@ -37,18 +34,16 @@ 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
assert.ok(ret.isErr)
assert.deepStrictEqual(ret.err, {request :[{password:[{wrongType:"String"}]}]})
})
})
})
7 changes: 3 additions & 4 deletions src/templates/domain/useCases/tests/delete.test.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ describe('Delete the <%= 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
Expand Down
Loading