@@ -3,6 +3,94 @@ 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 (
66+ 'Computed field "upperName" in model "User" has an invalid configuration: expected a function but received string' ,
67+ ) ;
68+ } ) ;
69+
70+ it ( 'throws error when computed field configuration is a non-function object' , async ( ) => {
71+ await expect (
72+ createTestClient (
73+ `
74+ model User {
75+ id Int @id @default(autoincrement())
76+ name String
77+ computed1 String @computed
78+ }
79+ ` ,
80+ {
81+ computedFields : {
82+ User : {
83+ // providing an object instead of a function
84+ computed1 : { key : 'value' } as any ,
85+ } ,
86+ } ,
87+ } as any ,
88+ ) ,
89+ ) . rejects . toThrow (
90+ 'Computed field "computed1" in model "User" has an invalid configuration: expected a function but received object' ,
91+ ) ;
92+ } ) ;
93+
694 it ( 'works with non-optional fields' , async ( ) => {
795 const db = await createTestClient (
896 `
@@ -102,6 +190,11 @@ model User {
102190}
103191` ,
104192 {
193+ computedFields : {
194+ User : {
195+ upperName : ( eb : any ) => eb . fn ( 'upper' , [ 'name' ] ) ,
196+ } ,
197+ } ,
105198 extraSourceFiles : {
106199 main : `
107200import { ZenStackClient } from '@zenstackhq/orm';
@@ -169,6 +262,11 @@ model User {
169262}
170263` ,
171264 {
265+ computedFields : {
266+ User : {
267+ upperName : ( eb : any ) => eb . lit ( null ) ,
268+ } ,
269+ } ,
172270 extraSourceFiles : {
173271 main : `
174272import { ZenStackClient } from '@zenstackhq/orm';
0 commit comments