|
| 1 | +import type { ClientContract } from '@zenstackhq/orm'; |
| 2 | +import { createTestClient } from '@zenstackhq/testtools'; |
| 3 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 4 | +import { schema } from '../schemas/procedures/schema'; |
| 5 | +import type { User } from '../schemas/procedures/models'; |
| 6 | + |
| 7 | +describe('Procedures tests', () => { |
| 8 | + let client: ClientContract<typeof schema>; |
| 9 | + |
| 10 | + beforeEach(async () => { |
| 11 | + client = await createTestClient(schema, { |
| 12 | + procedures: { |
| 13 | + // Query procedure that returns a single User |
| 14 | + getUser: async ({ args: { id } }) => { |
| 15 | + return await client.user.findUniqueOrThrow({ |
| 16 | + where: { id }, |
| 17 | + }); |
| 18 | + }, |
| 19 | + |
| 20 | + // Query procedure that returns an array of Users |
| 21 | + listUsers: async () => { |
| 22 | + return await client.user.findMany(); |
| 23 | + }, |
| 24 | + |
| 25 | + // Mutation procedure that creates a User |
| 26 | + signUp: async ({ client, args: { name, role } }) => { |
| 27 | + return await client.user.create({ |
| 28 | + data: { |
| 29 | + name, |
| 30 | + role, |
| 31 | + }, |
| 32 | + }); |
| 33 | + }, |
| 34 | + |
| 35 | + // Query procedure that returns Void |
| 36 | + setAdmin: async ({ client, args: { userId } }) => { |
| 37 | + await client.user.update({ |
| 38 | + where: { id: userId }, |
| 39 | + data: { role: 'ADMIN' }, |
| 40 | + }); |
| 41 | + }, |
| 42 | + |
| 43 | + // Query procedure that returns a custom type |
| 44 | + getOverview: async () => { |
| 45 | + const userIds = await client.user.findMany({ select: { id: true } }); |
| 46 | + const total = await client.user.count(); |
| 47 | + return { |
| 48 | + userIds: userIds.map((u) => u.id), |
| 49 | + total, |
| 50 | + roles: ['ADMIN', 'USER'], |
| 51 | + meta: { hello: 'world' }, |
| 52 | + }; |
| 53 | + }, |
| 54 | + |
| 55 | + createMultiple: async ({ client, args: { names } }) => { |
| 56 | + return await client.$transaction(async (tx) => { |
| 57 | + const createdUsers: User[] = []; |
| 58 | + for (const name of names) { |
| 59 | + const user = await tx.user.create({ |
| 60 | + data: { name }, |
| 61 | + }); |
| 62 | + createdUsers.push(user); |
| 63 | + } |
| 64 | + return createdUsers; |
| 65 | + }); |
| 66 | + }, |
| 67 | + }, |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + afterEach(async () => { |
| 72 | + await client?.$disconnect(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('works with query proc with parameters', async () => { |
| 76 | + // Create a user first |
| 77 | + const created = await client.user.create({ |
| 78 | + data: { |
| 79 | + name: 'Alice', |
| 80 | + role: 'USER', |
| 81 | + }, |
| 82 | + }); |
| 83 | + |
| 84 | + // Call the procedure |
| 85 | + const result = await client.$procs.getUser({ args: { id: created.id } }); |
| 86 | + |
| 87 | + expect(result).toMatchObject({ |
| 88 | + id: created.id, |
| 89 | + name: 'Alice', |
| 90 | + role: 'USER', |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('works with query proc without parameters', async () => { |
| 95 | + // Create multiple users |
| 96 | + await client.user.create({ |
| 97 | + data: { name: 'Alice', role: 'USER' }, |
| 98 | + }); |
| 99 | + await client.user.create({ |
| 100 | + data: { name: 'Bob', role: 'ADMIN' }, |
| 101 | + }); |
| 102 | + await client.user.create({ |
| 103 | + data: { name: 'Charlie', role: 'USER' }, |
| 104 | + }); |
| 105 | + |
| 106 | + const result = await client.$procs.listUsers(); |
| 107 | + |
| 108 | + expect(result).toHaveLength(3); |
| 109 | + expect(result).toEqual( |
| 110 | + expect.arrayContaining([ |
| 111 | + expect.objectContaining({ name: 'Alice', role: 'USER' }), |
| 112 | + expect.objectContaining({ name: 'Bob', role: 'ADMIN' }), |
| 113 | + expect.objectContaining({ name: 'Charlie', role: 'USER' }), |
| 114 | + ]), |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + it('works with mutation with parameters', async () => { |
| 119 | + const result = await client.$procs.signUp({ args: { name: 'Alice' } }); |
| 120 | + |
| 121 | + expect(result).toMatchObject({ |
| 122 | + id: expect.any(Number), |
| 123 | + name: 'Alice', |
| 124 | + role: 'USER', |
| 125 | + }); |
| 126 | + |
| 127 | + // Verify user was created in database |
| 128 | + const users = await client.user.findMany(); |
| 129 | + expect(users).toHaveLength(1); |
| 130 | + expect(users[0]).toMatchObject({ |
| 131 | + name: 'Alice', |
| 132 | + role: 'USER', |
| 133 | + }); |
| 134 | + |
| 135 | + // accepts optional role parameter |
| 136 | + const result1 = await client.$procs.signUp({ |
| 137 | + args: { |
| 138 | + name: 'Bob', |
| 139 | + role: 'ADMIN', |
| 140 | + }, |
| 141 | + }); |
| 142 | + |
| 143 | + expect(result1).toMatchObject({ |
| 144 | + id: expect.any(Number), |
| 145 | + name: 'Bob', |
| 146 | + role: 'ADMIN', |
| 147 | + }); |
| 148 | + |
| 149 | + // Verify user was created with correct role |
| 150 | + const user1 = await client.user.findUnique({ |
| 151 | + where: { id: result1.id }, |
| 152 | + }); |
| 153 | + expect(user1?.role).toBe('ADMIN'); |
| 154 | + }); |
| 155 | + |
| 156 | + it('works with mutation proc that returns void', async () => { |
| 157 | + // Create a regular user |
| 158 | + const user = await client.user.create({ |
| 159 | + data: { name: 'Alice', role: 'USER' }, |
| 160 | + }); |
| 161 | + |
| 162 | + expect(user.role).toBe('USER'); |
| 163 | + |
| 164 | + // Call setAdmin procedure |
| 165 | + const result = await client.$procs.setAdmin({ args: { userId: user.id } }); |
| 166 | + |
| 167 | + // Procedure returns void |
| 168 | + expect(result).toBeUndefined(); |
| 169 | + |
| 170 | + // Verify user role was updated |
| 171 | + const updated = await client.user.findUnique({ |
| 172 | + where: { id: user.id }, |
| 173 | + }); |
| 174 | + expect(updated?.role).toBe('ADMIN'); |
| 175 | + }); |
| 176 | + |
| 177 | + it('works with procedure returning custom type', async () => { |
| 178 | + await client.user.create({ data: { name: 'Alice', role: 'USER' } }); |
| 179 | + await client.user.create({ data: { name: 'Bob', role: 'ADMIN' } }); |
| 180 | + |
| 181 | + const result = await client.$procs.getOverview(); |
| 182 | + expect(result.total).toBe(2); |
| 183 | + expect(result.userIds).toHaveLength(2); |
| 184 | + expect(result.roles).toEqual(expect.arrayContaining(['ADMIN', 'USER'])); |
| 185 | + expect(result.meta).toEqual({ hello: 'world' }); |
| 186 | + }); |
| 187 | + |
| 188 | + it('works with transactional mutation procs', async () => { |
| 189 | + // unique constraint violation should rollback the transaction |
| 190 | + await expect(client.$procs.createMultiple({ args: { names: ['Alice', 'Alice'] } })).rejects.toThrow(); |
| 191 | + await expect(client.user.count()).resolves.toBe(0); |
| 192 | + |
| 193 | + // successful transaction |
| 194 | + await expect(client.$procs.createMultiple({ args: { names: ['Alice', 'Bob'] } })).resolves.toEqual( |
| 195 | + expect.arrayContaining([ |
| 196 | + expect.objectContaining({ name: 'Alice' }), |
| 197 | + expect.objectContaining({ name: 'Bob' }), |
| 198 | + ]), |
| 199 | + ); |
| 200 | + }); |
| 201 | +}); |
0 commit comments