diff --git a/packages/language/test/attribute-application.test.ts b/packages/language/test/attribute-application.test.ts index 71d39323..ce17ce49 100644 --- a/packages/language/test/attribute-application.test.ts +++ b/packages/language/test/attribute-application.test.ts @@ -20,4 +20,29 @@ describe('Attribute application validation tests', () => { `"before()" is only allowed in "post-update" policy rules`, ); }); + + it('requires relation and fk to have consistent optionality', async () => { + await loadSchemaWithError( + ` + datasource db { + provider = 'sqlite' + url = 'file:./dev.db' + } + + model Foo { + id Int @id @default(autoincrement()) + bar Bar @relation(fields: [barId], references: [id]) + barId Int? + @@allow('all', true) + } + + model Bar { + id Int @id @default(autoincrement()) + foos Foo[] + @@allow('all', true) + } + `, + /relation "bar" is not optional/, + ); + }); }); diff --git a/packages/language/test/this-resolution.test.ts b/packages/language/test/this-resolution.test.ts new file mode 100644 index 00000000..a9caa5a3 --- /dev/null +++ b/packages/language/test/this-resolution.test.ts @@ -0,0 +1,71 @@ +import { describe, expect, it } from 'vitest'; +import { loadSchema, loadSchemaWithError } from './utils'; + +describe('This keyword resolution tests', () => { + it('always resolves to the containing model', async () => { + await loadSchemaWithError( + ` + datasource db { + provider = 'sqlite' + url = 'file:./dev.db' + } + + model A { + id Int @id @default(autoincrement()) + av Int + b B[] + + @@allow('read', b?[c?[cv == this.cv]]) + } + + model B { + id Int @id @default(autoincrement()) + bv Int + aId Int + a A @relation(fields: [aId], references: [id]) + c C[] + } + + model C { + id Int @id @default(autoincrement()) + cv Int + bId Int + b B @relation(fields: [bId], references: [id]) + } + `, + /MemberAccessTarget named 'cv'/, + ); + + await expect( + loadSchema(` + datasource db { + provider = 'sqlite' + url = 'file:./dev.db' + } + + model A { + id Int @id @default(autoincrement()) + av Int + b B[] + + @@allow('read', b?[c?[cv == this.av]]) + } + + model B { + id Int @id @default(autoincrement()) + bv Int + aId Int + a A @relation(fields: [aId], references: [id]) + c C[] + } + + model C { + id Int @id @default(autoincrement()) + cv Int + bId Int + b B @relation(fields: [bId], references: [id]) + } + `), + ).resolves.toBeTruthy(); + }); +});