Skip to content

Commit 0492085

Browse files
authored
test(zmodel): more validation tests (#459)
1 parent d894f4b commit 0492085

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

packages/language/test/attribute-application.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,29 @@ describe('Attribute application validation tests', () => {
2020
`"before()" is only allowed in "post-update" policy rules`,
2121
);
2222
});
23+
24+
it('requires relation and fk to have consistent optionality', async () => {
25+
await loadSchemaWithError(
26+
`
27+
datasource db {
28+
provider = 'sqlite'
29+
url = 'file:./dev.db'
30+
}
31+
32+
model Foo {
33+
id Int @id @default(autoincrement())
34+
bar Bar @relation(fields: [barId], references: [id])
35+
barId Int?
36+
@@allow('all', true)
37+
}
38+
39+
model Bar {
40+
id Int @id @default(autoincrement())
41+
foos Foo[]
42+
@@allow('all', true)
43+
}
44+
`,
45+
/relation "bar" is not optional/,
46+
);
47+
});
2348
});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { loadSchema, loadSchemaWithError } from './utils';
3+
4+
describe('This keyword resolution tests', () => {
5+
it('always resolves to the containing model', async () => {
6+
await loadSchemaWithError(
7+
`
8+
datasource db {
9+
provider = 'sqlite'
10+
url = 'file:./dev.db'
11+
}
12+
13+
model A {
14+
id Int @id @default(autoincrement())
15+
av Int
16+
b B[]
17+
18+
@@allow('read', b?[c?[cv == this.cv]])
19+
}
20+
21+
model B {
22+
id Int @id @default(autoincrement())
23+
bv Int
24+
aId Int
25+
a A @relation(fields: [aId], references: [id])
26+
c C[]
27+
}
28+
29+
model C {
30+
id Int @id @default(autoincrement())
31+
cv Int
32+
bId Int
33+
b B @relation(fields: [bId], references: [id])
34+
}
35+
`,
36+
/MemberAccessTarget named 'cv'/,
37+
);
38+
39+
await expect(
40+
loadSchema(`
41+
datasource db {
42+
provider = 'sqlite'
43+
url = 'file:./dev.db'
44+
}
45+
46+
model A {
47+
id Int @id @default(autoincrement())
48+
av Int
49+
b B[]
50+
51+
@@allow('read', b?[c?[cv == this.av]])
52+
}
53+
54+
model B {
55+
id Int @id @default(autoincrement())
56+
bv Int
57+
aId Int
58+
a A @relation(fields: [aId], references: [id])
59+
c C[]
60+
}
61+
62+
model C {
63+
id Int @id @default(autoincrement())
64+
cv Int
65+
bId Int
66+
b B @relation(fields: [bId], references: [id])
67+
}
68+
`),
69+
).resolves.toBeTruthy();
70+
});
71+
});

0 commit comments

Comments
 (0)