11import { createTestClient } from '@zenstackhq/testtools' ;
2+ import { sql } from 'kysely' ;
23import { describe , expect , it } from 'vitest' ;
34
45describe ( 'Computed fields tests' , ( ) => {
@@ -7,91 +8,86 @@ describe('Computed fields tests', () => {
78 `
89model User {
910 id Int @id @default(autoincrement())
10- name String
11- otherName String @computed
11+ firstName String
12+ lastName String
13+ fullName String @computed
1214}
1315` ,
1416 {
1517 computedFields : {
1618 User : {
17- otherName : ( eb : any ) =>
18- eb . fn ( 'concat' , [
19- eb . ref ( 'name' ) ,
20- // type casting is needed because some db (e.g. postgres) cannot
21- // automatically infer parameter types
22- eb . cast ( eb . val ( '!' ) , 'text' ) ,
23- ] ) ,
19+ fullName : ( eb : any ) => eb . fn ( 'concat' , [ eb . ref ( 'firstName' ) , sql . lit ( ' ' ) , eb . ref ( 'lastName' ) ] ) ,
2420 } ,
2521 } ,
2622 } as any ,
2723 ) ;
2824
2925 await expect (
3026 db . user . create ( {
31- data : { id : 1 , name : 'Alex' } ,
27+ data : { id : 1 , firstName : 'Alex' , lastName : 'Smith ' } ,
3228 } ) ,
3329 ) . resolves . toMatchObject ( {
34- otherName : 'Alex! ' ,
30+ fullName : 'Alex Smith ' ,
3531 } ) ;
3632
3733 await expect (
3834 db . user . findUnique ( {
3935 where : { id : 1 } ,
40- select : { otherName : true } ,
36+ select : { fullName : true } ,
4137 } ) ,
4238 ) . resolves . toMatchObject ( {
43- otherName : 'Alex! ' ,
39+ fullName : 'Alex Smith ' ,
4440 } ) ;
4541
4642 await expect (
4743 db . user . findFirst ( {
48- where : { otherName : 'Alex! ' } ,
44+ where : { fullName : 'Alex Smith ' } ,
4945 } ) ,
5046 ) . resolves . toMatchObject ( {
51- otherName : 'Alex! ' ,
47+ fullName : 'Alex Smith ' ,
5248 } ) ;
5349
5450 await expect (
5551 db . user . findFirst ( {
56- where : { otherName : 'Alex' } ,
52+ where : { fullName : 'Alex' } ,
5753 } ) ,
5854 ) . toResolveNull ( ) ;
5955
6056 await expect (
6157 db . user . findFirst ( {
62- orderBy : { otherName : 'desc' } ,
58+ orderBy : { fullName : 'desc' } ,
6359 } ) ,
6460 ) . resolves . toMatchObject ( {
65- otherName : 'Alex! ' ,
61+ fullName : 'Alex Smith ' ,
6662 } ) ;
6763
6864 await expect (
6965 db . user . findFirst ( {
70- orderBy : { otherName : 'desc' } ,
66+ orderBy : { fullName : 'desc' } ,
7167 take : 1 ,
7268 } ) ,
7369 ) . resolves . toMatchObject ( {
74- otherName : 'Alex! ' ,
70+ fullName : 'Alex Smith ' ,
7571 } ) ;
7672
7773 await expect (
7874 db . user . aggregate ( {
79- _count : { otherName : true } ,
75+ _count : { fullName : true } ,
8076 } ) ,
8177 ) . resolves . toMatchObject ( {
82- _count : { otherName : 1 } ,
78+ _count : { fullName : 1 } ,
8379 } ) ;
8480
8581 await expect (
8682 db . user . groupBy ( {
87- by : [ 'name' , 'otherName '] ,
88- _count : { otherName : true } ,
89- _max : { otherName : true } ,
83+ by : [ 'fullName ' ] ,
84+ _count : { fullName : true } ,
85+ _max : { fullName : true } ,
9086 } ) ,
9187 ) . resolves . toEqual ( [
9288 expect . objectContaining ( {
93- _count : { otherName : 1 } ,
94- _max : { otherName : 'Alex! ' } ,
89+ _count : { fullName : 1 } ,
90+ _max : { fullName : 'Alex Smith ' } ,
9591 } ) ,
9692 ] ) ;
9793 } ) ;
0 commit comments