Skip to content

Commit 4ec4072

Browse files
committed
feat(command): add tests to make:crud
1 parent 461e793 commit 4ec4072

19 files changed

+506
-44
lines changed

configurer/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ export default class DatabaseConfigurer extends BaseConfigurer {
107107
'crud-controller',
108108
'node_modules/@athenna/database/templates/crud-controller.edge'
109109
)
110+
.setTo(
111+
'templates',
112+
'crud-service-test',
113+
'node_modules/@athenna/database/templates/crud-service-test.edge'
114+
)
115+
.setTo(
116+
'templates',
117+
'crud-controller-test',
118+
'node_modules/@athenna/database/templates/crud-controller-test.edge'
119+
)
110120
.save()
111121
})
112122

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@athenna/database",
3-
"version": "4.57.0",
3+
"version": "4.58.0",
44
"description": "The Athenna database handler for SQL/NoSQL.",
55
"license": "MIT",
66
"author": "João Lenon <lenon@athenna.io>",
@@ -236,7 +236,9 @@
236236
"crud-model": "./templates/crud-model.edge",
237237
"crud-migration": "./templates/crud-migration.edge",
238238
"crud-service": "./templates/crud-service.edge",
239-
"crud-controller": "./templates/crud-controller.edge"
239+
"crud-controller": "./templates/crud-controller.edge",
240+
"crud-controller-test": "./templates/crud-controller-test.edge",
241+
"crud-service-test": "./templates/crud-service-test.edge"
240242
}
241243
}
242244
}

src/commands/MakeCrudCommand.ts

+175-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class MakeCrudCommand extends BaseCommand {
6262
)
6363

6464
if (addId) {
65-
this.properties.push({ name: 'id', type: 'increments' })
65+
this.properties.push({ name: 'id', type: 'increments', custom: false })
6666
}
6767

6868
const addTimestamps = await this.prompt.confirm(
@@ -106,7 +106,7 @@ export class MakeCrudCommand extends BaseCommand {
106106

107107
options.forEach(option => (optionsObj[option] = true))
108108

109-
this.properties.push({ name, type, ...optionsObj })
109+
this.properties.push({ name, type, custom: true, ...optionsObj })
110110

111111
addMoreProps = await this.prompt.confirm(
112112
'Do you want to add more properties?'
@@ -117,12 +117,14 @@ export class MakeCrudCommand extends BaseCommand {
117117
this.properties.push({
118118
name: 'createdAt',
119119
type: 'Date',
120+
custom: false,
120121
isCreateDate: true
121122
})
122123

123124
this.properties.push({
124125
name: 'updatedAt',
125126
type: 'Date',
127+
custom: false,
126128
isUpdateDate: true
127129
})
128130
}
@@ -131,10 +133,12 @@ export class MakeCrudCommand extends BaseCommand {
131133
this.properties.push({
132134
name: 'deletedAt',
133135
type: 'Date',
136+
custom: false,
134137
isDeleteDate: true
135138
})
136139
}
137140

141+
console.log()
138142
const task = this.logger.task()
139143

140144
if (Config.get('rc.commands.make:crud.model.enabled', true)) {
@@ -156,8 +160,21 @@ export class MakeCrudCommand extends BaseCommand {
156160
task.addPromise('Creating service', () => this.makeService())
157161
}
158162

163+
if (Config.get('rc.commands.make:crud.controller-test.enabled', true)) {
164+
task.addPromise('Creating e2e tests for controller', () =>
165+
this.makeControllerTest()
166+
)
167+
}
168+
169+
if (Config.get('rc.commands.make:crud.service-test.enabled', true)) {
170+
task.addPromise('Creating unitary tests for service', () =>
171+
this.makeServiceTest()
172+
)
173+
}
174+
159175
await task.run()
160176

177+
console.log()
161178
this.logger.success(`CRUD ({yellow} "${this.name}") successfully created.`)
162179
}
163180

@@ -170,6 +187,7 @@ export class MakeCrudCommand extends BaseCommand {
170187
)
171188

172189
let properties = ''
190+
let definitions = ''
173191

174192
this.properties.forEach((p, i) => {
175193
const property = Json.copy(p)
@@ -224,16 +242,28 @@ export class MakeCrudCommand extends BaseCommand {
224242
properties += ` @Column()\n public ${property.name}: ${property.type}`
225243
}
226244

245+
const type = {
246+
string: 'this.faker.string.sample()',
247+
number: 'this.faker.number.int({ max: 10000000 })',
248+
boolean: 'this.faker.datatype.boolean()',
249+
Date: 'this.faker.date.anytime()'
250+
}
251+
252+
if (property.custom) {
253+
definitions += ` ${property.name}: ${type[property.type]}`
254+
}
255+
227256
if (this.properties.length - 1 !== i) {
228257
properties += '\n\n'
258+
if (definitions.length) definitions += ',\n'
229259
}
230260
})
231261

232262
await this.generator
233263
.fileName(this.toCase(this.name))
234264
.destination(destination)
235265
.template('crud-model')
236-
.properties({ properties })
266+
.properties({ properties, definitions })
237267
.setNameProperties(true)
238268
.make()
239269

@@ -332,10 +362,19 @@ export class MakeCrudCommand extends BaseCommand {
332362
Path.services()
333363
)
334364

365+
let properties = ''
366+
367+
this.properties
368+
.filter(p => p.custom)
369+
.forEach(p => {
370+
properties += `'${p.name}', `
371+
})
372+
335373
this.generator
336374
.fileName(this.toCase(`${this.name}Controller`))
337375
.destination(destination)
338376
.properties({
377+
properties: properties.slice(0, properties.length - 2),
339378
serviceImportPath: new Generator()
340379
.fileName(this.toCase(`${this.name}Service`))
341380
.destination(serviceDest)
@@ -366,10 +405,19 @@ export class MakeCrudCommand extends BaseCommand {
366405
Path.models()
367406
)
368407

408+
let propertiesToUpdate = ''
409+
410+
this.properties
411+
.filter(p => p.custom)
412+
.forEach(property => {
413+
propertiesToUpdate += ` ${this.nameLower}.${property.name} = body.${property.name}\n`
414+
})
415+
369416
await this.generator
370417
.fileName(this.toCase(`${this.name}Service`))
371418
.destination(destination)
372419
.properties({
420+
propertiesToUpdate,
373421
idType: this.isMongo ? 'string' : 'number',
374422
modelImportPath: new Generator()
375423
.fileName(this.toCase(this.name))
@@ -386,4 +434,128 @@ export class MakeCrudCommand extends BaseCommand {
386434

387435
await this.rc.pushTo('services', importPath).save()
388436
}
437+
438+
public async makeControllerTest() {
439+
this.cleanGenerator()
440+
441+
const destination = Config.get(
442+
'rc.commands.make:crud.controller-test.destination',
443+
Path.tests('e2e')
444+
)
445+
446+
const modelDest = Config.get(
447+
'rc.commands.make:crud.model.destination',
448+
Path.models()
449+
)
450+
451+
let createBody = ''
452+
let updateBody = ''
453+
let showAssertBody = `id: ${this.nameLower}.id, `
454+
let createAssertBody = ''
455+
let updateAssertBody = `id: ${this.nameLower}.id, `
456+
457+
this.properties
458+
.filter(p => p.custom)
459+
.forEach(property => {
460+
const type = {
461+
string: `'string'`,
462+
number: 1,
463+
boolean: true,
464+
Date: 'new Date()'
465+
}
466+
467+
createBody += `${property.name}: ${type[property.type]}, `
468+
updateBody += `${property.name}: ${type[property.type]}, `
469+
showAssertBody += `${property.name}: ${type[property.type]}, `
470+
createAssertBody += `'${property.name}', `
471+
updateAssertBody += `${property.name}: ${type[property.type]}, `
472+
})
473+
474+
await this.generator
475+
.fileName(this.toCase(`${this.name}ControllerTest`))
476+
.destination(destination)
477+
.properties({
478+
createBody: createBody.slice(0, createBody.length - 2),
479+
updateBody: updateBody.slice(0, updateBody.length - 2),
480+
showAssertBody: showAssertBody.slice(0, showAssertBody.length - 2),
481+
createAssertBody: createAssertBody.slice(
482+
0,
483+
createAssertBody.length - 2
484+
),
485+
updateAssertBody: updateAssertBody.slice(
486+
0,
487+
updateAssertBody.length - 2
488+
),
489+
modelImportPath: new Generator()
490+
.fileName(this.toCase(this.name))
491+
.destination(modelDest)
492+
.getImportPath(),
493+
crudNamePascal: this.namePascal,
494+
crudNamePascalPlural: String.pluralize(this.namePascal),
495+
crudNameLower: this.nameLower,
496+
crudNameLowerPlural: String.pluralize(this.nameLower)
497+
})
498+
.template('crud-controller-test')
499+
.setNameProperties(true)
500+
.make()
501+
}
502+
503+
public async makeServiceTest() {
504+
this.cleanGenerator()
505+
506+
const destination = Config.get(
507+
'rc.commands.make:crud.service-test.destination',
508+
Path.tests('unit')
509+
)
510+
511+
const modelDest = Config.get(
512+
'rc.commands.make:crud.model.destination',
513+
Path.models()
514+
)
515+
516+
const serviceDest = Config.get(
517+
'rc.commands.make:crud.service.destination',
518+
Path.services()
519+
)
520+
521+
let createBody = ''
522+
let updateBody = ''
523+
524+
this.properties
525+
.filter(p => p.custom)
526+
.forEach(property => {
527+
const type = {
528+
string: `'string'`,
529+
number: 1,
530+
boolean: true,
531+
Date: 'new Date()'
532+
}
533+
534+
createBody += `${property.name}: ${type[property.type]}, `
535+
updateBody += `${property.name}: ${type[property.type]}, `
536+
})
537+
538+
await this.generator
539+
.fileName(this.toCase(`${this.name}ServiceTest`))
540+
.destination(destination)
541+
.properties({
542+
createBody: createBody.slice(0, createBody.length - 2),
543+
updateBody: updateBody.slice(0, updateBody.length - 2),
544+
modelImportPath: new Generator()
545+
.fileName(this.toCase(this.name))
546+
.destination(modelDest)
547+
.getImportPath(),
548+
serviceImportPath: new Generator()
549+
.fileName(this.toCase(`${this.name}Service`))
550+
.destination(serviceDest)
551+
.getImportPath(),
552+
crudNamePascal: this.namePascal,
553+
crudNamePascalPlural: String.pluralize(this.namePascal),
554+
crudNameLower: this.nameLower,
555+
crudNameLowerPlural: String.pluralize(this.nameLower)
556+
})
557+
.template('crud-service-test')
558+
.setNameProperties(true)
559+
.make()
560+
}
389561
}

0 commit comments

Comments
 (0)