@@ -4,13 +4,14 @@ import { PropertyDeclaration, SourceFile } from 'ts-morph';
4
4
5
5
import { generate } from './generate' ;
6
6
import { generatorOptions , getImportDeclarations , stringContains } from './testing' ;
7
+ import { GeneratorConfigurationOptions } from './types' ;
7
8
8
9
describe ( 'main generate' , ( ) => {
9
10
let property : PropertyDeclaration | undefined ;
10
11
let sourceFile : SourceFile | undefined ;
11
12
let sourceFiles : SourceFile [ ] ;
12
13
let sourceText : string ;
13
- async function getResult ( args : { schema : string } & Record < string , any > ) {
14
+ async function getResult ( args : { schema : string } & GeneratorConfigurationOptions ) {
14
15
const { schema, ...options } = args ;
15
16
const generateOptions = {
16
17
...( await generatorOptions ( schema , options ) ) ,
@@ -218,7 +219,7 @@ describe('main generate', () => {
218
219
219
220
it ( 'get rid of atomic number operations' , async ( ) => {
220
221
await getResult ( {
221
- atomicNumberOperations : false ,
222
+ atomicNumberOperations : ' false' ,
222
223
schema : `
223
224
model User {
224
225
id String @id
@@ -278,7 +279,7 @@ describe('main generate', () => {
278
279
279
280
it ( 'user args type' , async ( ) => {
280
281
await getResult ( {
281
- atomicNumberOperations : false ,
282
+ atomicNumberOperations : ' false' ,
282
283
schema : `
283
284
model User {
284
285
id String @id
@@ -326,12 +327,8 @@ describe('main generate', () => {
326
327
decoratorArguments = struct . decorators ?. [ 0 ] . arguments ;
327
328
assert . strictEqual ( decoratorArguments ?. [ 0 ] , '() => UserMaxAggregateInput' ) ;
328
329
329
- const imports = sourceFile . getImportDeclarations ( ) . flatMap ( ( d ) =>
330
- d . getNamedImports ( ) . map ( ( index ) => ( {
331
- name : index . getName ( ) ,
332
- specifier : d . getModuleSpecifierValue ( ) ,
333
- } ) ) ,
334
- ) ;
330
+ const imports = getImportDeclarations ( sourceFile ) ;
331
+
335
332
assert ( imports . find ( ( x ) => x . name === 'UserAvgAggregateInput' ) ) ;
336
333
assert ( imports . find ( ( x ) => x . name === 'UserSumAggregateInput' ) ) ;
337
334
assert ( imports . find ( ( x ) => x . name === 'UserMinAggregateInput' ) ) ;
@@ -340,7 +337,7 @@ describe('main generate', () => {
340
337
341
338
it ( 'aggregate output types' , async ( ) => {
342
339
await getResult ( {
343
- atomicNumberOperations : false ,
340
+ atomicNumberOperations : ' false' ,
344
341
schema : `
345
342
model User {
346
343
id String @id
@@ -376,7 +373,7 @@ describe('main generate', () => {
376
373
date DateTime?
377
374
}
378
375
` ,
379
- combineScalarFilters : false ,
376
+ combineScalarFilters : ' false' ,
380
377
} ) ;
381
378
const filePaths = sourceFiles . map ( ( s ) => String ( s . getFilePath ( ) ) ) ;
382
379
const userWhereInput = sourceFiles . find ( ( s ) =>
@@ -410,7 +407,7 @@ describe('main generate', () => {
410
407
USER
411
408
}
412
409
` ,
413
- combineScalarFilters : true ,
410
+ combineScalarFilters : ' true' ,
414
411
} ) ;
415
412
const filePaths = sourceFiles . map ( ( s ) => String ( s . getFilePath ( ) ) ) ;
416
413
for ( const filePath of filePaths ) {
@@ -445,13 +442,13 @@ describe('main generate', () => {
445
442
USER
446
443
}
447
444
` ,
448
- atomicNumberOperations : false ,
445
+ atomicNumberOperations : ' false' ,
449
446
} ) ;
450
447
expect ( sourceFiles . length ) . to . be . greaterThan ( 0 ) ;
451
448
for ( const sourceFile of sourceFiles ) {
452
449
sourceFile . getClasses ( ) . forEach ( ( classDeclaration ) => {
453
450
if ( classDeclaration . getName ( ) ?. endsWith ( 'FieldUpdateOperationsInput' ) ) {
454
- assert . fail ( `Class should not exists ${ classDeclaration . getName ( ) ! } ` ) ;
451
+ expect . fail ( `Class should not exists ${ classDeclaration . getName ( ) ! } ` ) ;
455
452
}
456
453
} ) ;
457
454
}
@@ -469,8 +466,29 @@ describe('main generate', () => {
469
466
} ) )
470
467
. forEach ( ( struct ) => {
471
468
if ( struct . types . find ( ( s ) => s . endsWith ( 'FieldUpdateOperationsInput' ) ) ) {
472
- expect . fail ( `Property ${ struct . name } typed ${ struct . type } ` ) ;
469
+ expect . fail ( `Property ${ struct . name } typed ${ String ( struct . type ) } ` ) ;
473
470
}
474
471
} ) ;
475
472
} ) ;
473
+
474
+ it ( 'custom property mapping' , async ( ) => {
475
+ await getResult ( {
476
+ schema : `
477
+ model User {
478
+ id String @id
479
+ d Decimal
480
+ }
481
+ ` ,
482
+ customPropertyTypes : 'Decimal:MyDec:decimal.js' ,
483
+ } ) ;
484
+ const sourceFile = sourceFiles . find ( ( s ) => s . getFilePath ( ) . endsWith ( 'user.model.ts' ) ) ;
485
+ assert ( sourceFile ) ;
486
+ const property = sourceFile . getClasses ( ) [ 0 ] ?. getProperty ( 'd' ) ?. getStructure ( ) ;
487
+ expect ( property ?. type ) . to . equal ( 'MyDec' ) ;
488
+ const imports = getImportDeclarations ( sourceFile ) ;
489
+ expect ( imports ) . to . deep . contain ( {
490
+ name : 'MyDec' ,
491
+ specifier : 'decimal.js' ,
492
+ } ) ;
493
+ } ) ;
476
494
} ) ;
0 commit comments