|
| 1 | +import path from 'path'; |
| 2 | +import { afterEach, beforeAll, describe, expect, it } from 'vitest'; |
| 3 | +import { createPolicyTestClient } from './utils'; |
| 4 | +import { QueryError } from '../../src'; |
| 5 | + |
| 6 | +describe('With Policy: multi-field unique', () => { |
| 7 | + let origDir: string; |
| 8 | + |
| 9 | + beforeAll(async () => { |
| 10 | + origDir = path.resolve('.'); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + process.chdir(origDir); |
| 15 | + }); |
| 16 | + |
| 17 | + it('toplevel crud test unnamed constraint', async () => { |
| 18 | + const db = await createPolicyTestClient( |
| 19 | + ` |
| 20 | + model Model { |
| 21 | + id String @id @default(uuid()) |
| 22 | + a String |
| 23 | + b String |
| 24 | + x Int |
| 25 | + @@unique([a, b]) |
| 26 | +
|
| 27 | + @@allow('all', x > 0) |
| 28 | + @@deny('update', x > 1) |
| 29 | + } |
| 30 | + `, |
| 31 | + ); |
| 32 | + |
| 33 | + await expect(db.model.create({ data: { a: 'a1', b: 'b1', x: 1 } })).toResolveTruthy(); |
| 34 | + await expect(db.model.create({ data: { a: 'a1', b: 'b1', x: 2 } })).rejects.toThrow(QueryError); |
| 35 | + await expect(db.model.create({ data: { a: 'a2', b: 'b2', x: 0 } })).toBeRejectedByPolicy(); |
| 36 | + |
| 37 | + await expect(db.model.findUnique({ where: { a_b: { a: 'a1', b: 'b1' } } })).toResolveTruthy(); |
| 38 | + await expect(db.model.findUnique({ where: { a_b: { a: 'a1', b: 'b2' } } })).toResolveFalsy(); |
| 39 | + await expect(db.model.update({ where: { a_b: { a: 'a1', b: 'b1' } }, data: { x: 2 } })).toResolveTruthy(); |
| 40 | + await expect(db.model.update({ where: { a_b: { a: 'a1', b: 'b1' } }, data: { x: 0 } })).toBeRejectedNotFound(); |
| 41 | + |
| 42 | + await expect(db.model.delete({ where: { a_b: { a: 'a1', b: 'b1' } } })).toResolveTruthy(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('toplevel crud test named constraint', async () => { |
| 46 | + const db = await createPolicyTestClient( |
| 47 | + ` |
| 48 | + model Model { |
| 49 | + id String @id @default(uuid()) |
| 50 | + a String |
| 51 | + b String |
| 52 | + x Int |
| 53 | + @@unique([a, b], name: 'myconstraint') |
| 54 | +
|
| 55 | + @@allow('all', x > 0) |
| 56 | + @@deny('update', x > 1) |
| 57 | + } |
| 58 | + `, |
| 59 | + ); |
| 60 | + |
| 61 | + await expect(db.model.create({ data: { a: 'a1', b: 'b1', x: 1 } })).toResolveTruthy(); |
| 62 | + await expect(db.model.findUnique({ where: { myconstraint: { a: 'a1', b: 'b1' } } })).toResolveTruthy(); |
| 63 | + await expect(db.model.findUnique({ where: { myconstraint: { a: 'a1', b: 'b2' } } })).toResolveFalsy(); |
| 64 | + await expect( |
| 65 | + db.model.update({ where: { myconstraint: { a: 'a1', b: 'b1' } }, data: { x: 2 } }), |
| 66 | + ).toResolveTruthy(); |
| 67 | + await expect( |
| 68 | + db.model.update({ where: { myconstraint: { a: 'a1', b: 'b1' } }, data: { x: 0 } }), |
| 69 | + ).toBeRejectedNotFound(); |
| 70 | + await expect(db.model.delete({ where: { myconstraint: { a: 'a1', b: 'b1' } } })).toResolveTruthy(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('nested crud test', async () => { |
| 74 | + const db = await createPolicyTestClient( |
| 75 | + ` |
| 76 | + model M1 { |
| 77 | + id String @id @default(uuid()) |
| 78 | + m2 M2[] |
| 79 | + @@allow('all', true) |
| 80 | + } |
| 81 | +
|
| 82 | + model M2 { |
| 83 | + id String @id @default(uuid()) |
| 84 | + a String |
| 85 | + b String |
| 86 | + x Int |
| 87 | + m1 M1 @relation(fields: [m1Id], references: [id]) |
| 88 | + m1Id String |
| 89 | +
|
| 90 | + @@unique([a, b]) |
| 91 | + @@allow('all', x > 0) |
| 92 | + } |
| 93 | + `, |
| 94 | + ); |
| 95 | + |
| 96 | + await expect(db.m1.create({ data: { id: '1', m2: { create: { a: 'a1', b: 'b1', x: 1 } } } })).toResolveTruthy(); |
| 97 | + await expect(db.m1.create({ data: { id: '2', m2: { create: { a: 'a1', b: 'b1', x: 2 } } } })).rejects.toThrow( |
| 98 | + QueryError, |
| 99 | + ); |
| 100 | + await expect( |
| 101 | + db.m1.create({ data: { id: '3', m2: { create: { a: 'a1', b: 'b2', x: 0 } } } }), |
| 102 | + ).toBeRejectedByPolicy(); |
| 103 | + |
| 104 | + await expect( |
| 105 | + db.m1.update({ |
| 106 | + where: { id: '1' }, |
| 107 | + data: { |
| 108 | + m2: { |
| 109 | + connectOrCreate: { |
| 110 | + where: { a_b: { a: 'a1', b: 'b1' } }, |
| 111 | + create: { a: 'a1', b: 'b1', x: 2 }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }), |
| 116 | + ).toResolveTruthy(); |
| 117 | + await expect(db.m2.count()).resolves.toBe(1); |
| 118 | + |
| 119 | + await expect( |
| 120 | + db.m1.update({ |
| 121 | + where: { id: '1' }, |
| 122 | + data: { |
| 123 | + m2: { |
| 124 | + connectOrCreate: { |
| 125 | + where: { a_b: { a: 'a1', b: 'b2' } }, |
| 126 | + create: { a: 'a1', b: 'b2', x: 2 }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + }, |
| 130 | + }), |
| 131 | + ).toResolveTruthy(); |
| 132 | + await expect(db.m2.count()).resolves.toBe(2); |
| 133 | + |
| 134 | + await expect( |
| 135 | + db.m1.update({ |
| 136 | + where: { id: '1' }, |
| 137 | + data: { |
| 138 | + m2: { |
| 139 | + connectOrCreate: { |
| 140 | + where: { a_b: { a: 'a2', b: 'b2' } }, |
| 141 | + create: { a: 'a2', b: 'b2', x: 0 }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + }), |
| 146 | + ).toBeRejectedByPolicy(); |
| 147 | + |
| 148 | + await expect( |
| 149 | + db.m1.update({ |
| 150 | + where: { id: '1' }, |
| 151 | + data: { |
| 152 | + m2: { |
| 153 | + update: { |
| 154 | + where: { a_b: { a: 'a1', b: 'b2' } }, |
| 155 | + data: { x: 3 }, |
| 156 | + }, |
| 157 | + }, |
| 158 | + }, |
| 159 | + }), |
| 160 | + ).toResolveTruthy(); |
| 161 | + await expect(db.m2.findUnique({ where: { a_b: { a: 'a1', b: 'b2' } } })).resolves.toEqual( |
| 162 | + expect.objectContaining({ x: 3 }), |
| 163 | + ); |
| 164 | + |
| 165 | + await expect( |
| 166 | + db.m1.update({ |
| 167 | + where: { id: '1' }, |
| 168 | + data: { |
| 169 | + m2: { |
| 170 | + delete: { |
| 171 | + a_b: { a: 'a1', b: 'b1' }, |
| 172 | + }, |
| 173 | + }, |
| 174 | + }, |
| 175 | + }), |
| 176 | + ).toResolveTruthy(); |
| 177 | + await expect(db.m2.count()).resolves.toBe(1); |
| 178 | + }); |
| 179 | +}); |
0 commit comments