Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/language/test/attribute-application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/,
);
});
});
71 changes: 71 additions & 0 deletions packages/language/test/this-resolution.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});