-
-
Notifications
You must be signed in to change notification settings - Fork 11
fix: stricter type def validation and compound unique field fix #223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { createPolicyTestClient } from './utils'; | ||
|
|
||
| describe('Abstract models', () => { | ||
| it('connect test1', async () => { | ||
| const db = await createPolicyTestClient( | ||
| ` | ||
| model User { | ||
| id Int @id @default(autoincrement()) | ||
| profile Profile? @relation(fields: [profileId], references: [id]) | ||
| profileId Int? @unique | ||
|
|
||
| @@allow('create,read', true) | ||
| @@allow('update', auth().id == 1) | ||
| } | ||
|
|
||
| type BaseProfile { | ||
| id Int @id @default(autoincrement()) | ||
|
|
||
| @@allow('all', true) | ||
| } | ||
|
|
||
| model Profile with BaseProfile { | ||
| name String | ||
| user User? | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| const dbUser2 = db.$setAuth({ id: 2 }); | ||
| const user = await dbUser2.user.create({ data: { id: 1 } }); | ||
| const profile = await dbUser2.profile.create({ data: { id: 1, name: 'John' } }); | ||
| await expect( | ||
| dbUser2.profile.update({ where: { id: 1 }, data: { user: { connect: { id: user.id } } } }), | ||
| ).toBeRejectedNotFound(); | ||
| await expect( | ||
| dbUser2.user.update({ where: { id: 1 }, data: { profile: { connect: { id: profile.id } } } }), | ||
| ).toBeRejectedNotFound(); | ||
|
|
||
| const dbUser1 = db.$setAuth({ id: 1 }); | ||
| await expect( | ||
| dbUser1.profile.update({ where: { id: 1 }, data: { user: { connect: { id: user.id } } } }), | ||
| ).toResolveTruthy(); | ||
| await expect( | ||
| dbUser1.user.update({ where: { id: 1 }, data: { profile: { connect: { id: profile.id } } } }), | ||
| ).toResolveTruthy(); | ||
| }); | ||
|
|
||
| it('connect test2', async () => { | ||
| const db = await createPolicyTestClient( | ||
| ` | ||
| model User { | ||
| id Int @id @default(autoincrement()) | ||
| profile Profile? | ||
|
|
||
| @@allow('all', true) | ||
| } | ||
|
|
||
| type BaseProfile { | ||
| id Int @id @default(autoincrement()) | ||
|
|
||
| @@allow('create,read', true) | ||
| @@allow('update', auth().id == 1) | ||
| } | ||
|
|
||
| model Profile with BaseProfile { | ||
| name String | ||
| user User? @relation(fields: [userId], references: [id]) | ||
| userId Int? @unique | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| const dbUser2 = db.$setAuth({ id: 2 }); | ||
| const user = await dbUser2.user.create({ data: { id: 1 } }); | ||
| const profile = await dbUser2.profile.create({ data: { id: 1, name: 'John' } }); | ||
| await expect( | ||
| dbUser2.profile.update({ where: { id: 1 }, data: { user: { connect: { id: user.id } } } }), | ||
| ).toBeRejectedNotFound(); | ||
| await expect( | ||
| dbUser2.user.update({ where: { id: 1 }, data: { profile: { connect: { id: profile.id } } } }), | ||
| ).toBeRejectedNotFound(); | ||
|
|
||
| const dbUser1 = db.$setAuth({ id: 1 }); | ||
| await expect( | ||
| dbUser1.profile.update({ where: { id: 1 }, data: { user: { connect: { id: user.id } } } }), | ||
| ).toResolveTruthy(); | ||
| await expect( | ||
| dbUser1.user.update({ where: { id: 1 }, data: { profile: { connect: { id: profile.id } } } }), | ||
| ).toResolveTruthy(); | ||
| }); | ||
| }); |
179 changes: 179 additions & 0 deletions
179
packages/runtime/test/policy/multi-field-unique.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| import path from 'path'; | ||
| import { afterEach, beforeAll, describe, expect, it } from 'vitest'; | ||
| import { createPolicyTestClient } from './utils'; | ||
| import { QueryError } from '../../src'; | ||
|
|
||
| describe('With Policy: multi-field unique', () => { | ||
| let origDir: string; | ||
|
|
||
| beforeAll(async () => { | ||
| origDir = path.resolve('.'); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| process.chdir(origDir); | ||
| }); | ||
|
|
||
| it('toplevel crud test unnamed constraint', async () => { | ||
| const db = await createPolicyTestClient( | ||
| ` | ||
| model Model { | ||
| id String @id @default(uuid()) | ||
| a String | ||
| b String | ||
| x Int | ||
| @@unique([a, b]) | ||
|
|
||
| @@allow('all', x > 0) | ||
| @@deny('update', x > 1) | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| await expect(db.model.create({ data: { a: 'a1', b: 'b1', x: 1 } })).toResolveTruthy(); | ||
| await expect(db.model.create({ data: { a: 'a1', b: 'b1', x: 2 } })).rejects.toThrow(QueryError); | ||
| await expect(db.model.create({ data: { a: 'a2', b: 'b2', x: 0 } })).toBeRejectedByPolicy(); | ||
|
|
||
| await expect(db.model.findUnique({ where: { a_b: { a: 'a1', b: 'b1' } } })).toResolveTruthy(); | ||
| await expect(db.model.findUnique({ where: { a_b: { a: 'a1', b: 'b2' } } })).toResolveFalsy(); | ||
| await expect(db.model.update({ where: { a_b: { a: 'a1', b: 'b1' } }, data: { x: 2 } })).toResolveTruthy(); | ||
| await expect(db.model.update({ where: { a_b: { a: 'a1', b: 'b1' } }, data: { x: 0 } })).toBeRejectedNotFound(); | ||
|
|
||
| await expect(db.model.delete({ where: { a_b: { a: 'a1', b: 'b1' } } })).toResolveTruthy(); | ||
| }); | ||
|
|
||
| it('toplevel crud test named constraint', async () => { | ||
| const db = await createPolicyTestClient( | ||
| ` | ||
| model Model { | ||
| id String @id @default(uuid()) | ||
| a String | ||
| b String | ||
| x Int | ||
| @@unique([a, b], name: 'myconstraint') | ||
|
|
||
| @@allow('all', x > 0) | ||
| @@deny('update', x > 1) | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| await expect(db.model.create({ data: { a: 'a1', b: 'b1', x: 1 } })).toResolveTruthy(); | ||
| await expect(db.model.findUnique({ where: { myconstraint: { a: 'a1', b: 'b1' } } })).toResolveTruthy(); | ||
| await expect(db.model.findUnique({ where: { myconstraint: { a: 'a1', b: 'b2' } } })).toResolveFalsy(); | ||
| await expect( | ||
| db.model.update({ where: { myconstraint: { a: 'a1', b: 'b1' } }, data: { x: 2 } }), | ||
| ).toResolveTruthy(); | ||
| await expect( | ||
| db.model.update({ where: { myconstraint: { a: 'a1', b: 'b1' } }, data: { x: 0 } }), | ||
| ).toBeRejectedNotFound(); | ||
| await expect(db.model.delete({ where: { myconstraint: { a: 'a1', b: 'b1' } } })).toResolveTruthy(); | ||
| }); | ||
|
|
||
| it('nested crud test', async () => { | ||
| const db = await createPolicyTestClient( | ||
| ` | ||
| model M1 { | ||
| id String @id @default(uuid()) | ||
| m2 M2[] | ||
| @@allow('all', true) | ||
| } | ||
|
|
||
| model M2 { | ||
| id String @id @default(uuid()) | ||
| a String | ||
| b String | ||
| x Int | ||
| m1 M1 @relation(fields: [m1Id], references: [id]) | ||
| m1Id String | ||
|
|
||
| @@unique([a, b]) | ||
| @@allow('all', x > 0) | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| await expect(db.m1.create({ data: { id: '1', m2: { create: { a: 'a1', b: 'b1', x: 1 } } } })).toResolveTruthy(); | ||
| await expect(db.m1.create({ data: { id: '2', m2: { create: { a: 'a1', b: 'b1', x: 2 } } } })).rejects.toThrow( | ||
| QueryError, | ||
| ); | ||
| await expect( | ||
| db.m1.create({ data: { id: '3', m2: { create: { a: 'a1', b: 'b2', x: 0 } } } }), | ||
| ).toBeRejectedByPolicy(); | ||
|
|
||
| await expect( | ||
| db.m1.update({ | ||
| where: { id: '1' }, | ||
| data: { | ||
| m2: { | ||
| connectOrCreate: { | ||
| where: { a_b: { a: 'a1', b: 'b1' } }, | ||
| create: { a: 'a1', b: 'b1', x: 2 }, | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| ).toResolveTruthy(); | ||
| await expect(db.m2.count()).resolves.toBe(1); | ||
|
|
||
| await expect( | ||
| db.m1.update({ | ||
| where: { id: '1' }, | ||
| data: { | ||
| m2: { | ||
| connectOrCreate: { | ||
| where: { a_b: { a: 'a1', b: 'b2' } }, | ||
| create: { a: 'a1', b: 'b2', x: 2 }, | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| ).toResolveTruthy(); | ||
| await expect(db.m2.count()).resolves.toBe(2); | ||
|
|
||
| await expect( | ||
| db.m1.update({ | ||
| where: { id: '1' }, | ||
| data: { | ||
| m2: { | ||
| connectOrCreate: { | ||
| where: { a_b: { a: 'a2', b: 'b2' } }, | ||
| create: { a: 'a2', b: 'b2', x: 0 }, | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| ).toBeRejectedByPolicy(); | ||
|
|
||
| await expect( | ||
| db.m1.update({ | ||
| where: { id: '1' }, | ||
| data: { | ||
| m2: { | ||
| update: { | ||
| where: { a_b: { a: 'a1', b: 'b2' } }, | ||
| data: { x: 3 }, | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| ).toResolveTruthy(); | ||
| await expect(db.m2.findUnique({ where: { a_b: { a: 'a1', b: 'b2' } } })).resolves.toEqual( | ||
| expect.objectContaining({ x: 3 }), | ||
| ); | ||
|
|
||
| await expect( | ||
| db.m1.update({ | ||
| where: { id: '1' }, | ||
| data: { | ||
| m2: { | ||
| delete: { | ||
| a_b: { a: 'a1', b: 'b1' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| ).toResolveTruthy(); | ||
| await expect(db.m2.count()).resolves.toBe(1); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.