@@ -3,6 +3,90 @@ import { sql } from 'kysely';
33import { describe , expect , it } from 'vitest' ;
44
55describe ( 'Computed fields tests' , ( ) => {
6+ it ( 'throws error when computed field configuration is missing' , async ( ) => {
7+ await expect (
8+ createTestClient (
9+ `
10+ model User {
11+ id Int @id @default(autoincrement())
12+ name String
13+ upperName String @computed
14+ }
15+ ` ,
16+ {
17+ // missing computedFields configuration
18+ } as any ,
19+ ) ,
20+ ) . rejects . toThrow ( 'Computed field "upperName" in model "User" does not have a configuration' ) ;
21+ } ) ;
22+
23+ it ( 'throws error when computed field is missing from configuration' , async ( ) => {
24+ await expect (
25+ createTestClient (
26+ `
27+ model User {
28+ id Int @id @default(autoincrement())
29+ name String
30+ upperName String @computed
31+ lowerName String @computed
32+ }
33+ ` ,
34+ {
35+ computedFields : {
36+ User : {
37+ // only providing one of two computed fields
38+ upperName : ( eb : any ) => eb . fn ( 'upper' , [ 'name' ] ) ,
39+ } ,
40+ } ,
41+ } as any ,
42+ ) ,
43+ ) . rejects . toThrow ( 'Computed field "lowerName" in model "User" does not have a configuration' ) ;
44+ } ) ;
45+
46+ it ( 'throws error when computed field configuration is not a function' , async ( ) => {
47+ await expect (
48+ createTestClient (
49+ `
50+ model User {
51+ id Int @id @default(autoincrement())
52+ name String
53+ upperName String @computed
54+ }
55+ ` ,
56+ {
57+ computedFields : {
58+ User : {
59+ // providing a string instead of a function
60+ upperName : 'not a function' as any ,
61+ } ,
62+ } ,
63+ } as any ,
64+ ) ,
65+ ) . rejects . toThrow ( 'Computed field "upperName" in model "User" has an invalid configuration: expected a function but received string' ) ;
66+ } ) ;
67+
68+ it ( 'throws error when computed field configuration is a non-function object' , async ( ) => {
69+ await expect (
70+ createTestClient (
71+ `
72+ model User {
73+ id Int @id @default(autoincrement())
74+ name String
75+ computed1 String @computed
76+ }
77+ ` ,
78+ {
79+ computedFields : {
80+ User : {
81+ // providing an object instead of a function
82+ computed1 : { key : 'value' } as any ,
83+ } ,
84+ } ,
85+ } as any ,
86+ ) ,
87+ ) . rejects . toThrow ( 'Computed field "computed1" in model "User" has an invalid configuration: expected a function but received object' ) ;
88+ } ) ;
89+
690 it ( 'works with non-optional fields' , async ( ) => {
791 const db = await createTestClient (
892 `
@@ -102,6 +186,11 @@ model User {
102186}
103187` ,
104188 {
189+ computedFields : {
190+ User : {
191+ upperName : ( eb : any ) => eb . fn ( 'upper' , [ 'name' ] ) ,
192+ } ,
193+ } ,
105194 extraSourceFiles : {
106195 main : `
107196import { ZenStackClient } from '@zenstackhq/orm';
@@ -169,6 +258,11 @@ model User {
169258}
170259` ,
171260 {
261+ computedFields : {
262+ User : {
263+ upperName : ( eb : any ) => eb . lit ( null ) ,
264+ } ,
265+ } ,
172266 extraSourceFiles : {
173267 main : `
174268import { ZenStackClient } from '@zenstackhq/orm';
0 commit comments