From dc2a29546bcf1e68e323157d6e8dd5674259fe08 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Mills Date: Wed, 19 Oct 2022 08:56:00 -1000 Subject: [PATCH] test(client): middleware arg consistency #15644 --- .../15644-middleware-arg-types/_matrix.ts | 9 + .../prisma/_schema.ts | 19 ++ .../15644-middleware-arg-types/tests.ts | 183 ++++++++++++++++++ 3 files changed, 211 insertions(+) create mode 100644 packages/client/tests/functional/issues/15644-middleware-arg-types/_matrix.ts create mode 100644 packages/client/tests/functional/issues/15644-middleware-arg-types/prisma/_schema.ts create mode 100644 packages/client/tests/functional/issues/15644-middleware-arg-types/tests.ts diff --git a/packages/client/tests/functional/issues/15644-middleware-arg-types/_matrix.ts b/packages/client/tests/functional/issues/15644-middleware-arg-types/_matrix.ts new file mode 100644 index 000000000000..6a962230983f --- /dev/null +++ b/packages/client/tests/functional/issues/15644-middleware-arg-types/_matrix.ts @@ -0,0 +1,9 @@ +import { defineMatrix } from '../../_utils/defineMatrix' + +export default defineMatrix(() => [ + [ + { + provider: 'sqlite', + }, + ], +]) diff --git a/packages/client/tests/functional/issues/15644-middleware-arg-types/prisma/_schema.ts b/packages/client/tests/functional/issues/15644-middleware-arg-types/prisma/_schema.ts new file mode 100644 index 000000000000..712016f5da91 --- /dev/null +++ b/packages/client/tests/functional/issues/15644-middleware-arg-types/prisma/_schema.ts @@ -0,0 +1,19 @@ +import { idForProvider } from '../../../_utils/idForProvider' +import testMatrix from '../_matrix' + +export default testMatrix.setupSchema(({ provider }) => { + return /* Prisma */ ` + generator client { + provider = "prisma-client-js" + } + + datasource db { + provider = "${provider}" + url = env("DATABASE_URI_${provider}") + } + + model Resource { + id ${idForProvider(provider)} + } + ` +}) diff --git a/packages/client/tests/functional/issues/15644-middleware-arg-types/tests.ts b/packages/client/tests/functional/issues/15644-middleware-arg-types/tests.ts new file mode 100644 index 000000000000..4b626d01d6fa --- /dev/null +++ b/packages/client/tests/functional/issues/15644-middleware-arg-types/tests.ts @@ -0,0 +1,183 @@ +import testMatrix from './_matrix' +// @ts-ignore +import type { PrismaClient } from './node_modules/@prisma/client' + +declare let prisma: PrismaClient + +testMatrix.setupTestSuite( + (suiteConfig, suiteMeta) => { + test('middleware with count', async () => { + prisma.$use((params, next) => { + if (params.action === 'count') { + expect(params.args).toStrictEqual({ skip: 1 }) + } + + return next(params) + }) + + await prisma.resource.count({ skip: 1 }) + }) + + test('middleware with aggregate', async () => { + prisma.$use((params, next) => { + if (params.action === 'aggregate') { + expect(params.args).toStrictEqual({ skip: 1 }) + } + + return next(params) + }) + + await prisma.resource.aggregate({ skip: 1 }) + }) + + test('middleware with groupBy', async () => { + prisma.$use((params, next) => { + if (params.action === ('groupBy' as string)) { + expect(params.args).toStrictEqual({ by: ['id'], orderBy: {} }) + } + + return next(params) + }) + + await prisma.resource.groupBy({ by: ['id'], orderBy: {} }) + }) + + test('middleware with findFirst', async () => { + prisma.$use((params, next) => { + if (params.action === 'findFirst') { + expect(params.args).toStrictEqual({ take: 1 }) + } + + return next(params) + }) + + await prisma.resource.findFirst({ take: 1 }) + }) + + test('middleware with findUnique', async () => { + prisma.$use((params, next) => { + if (params.action === 'findUnique') { + expect(params.args).toStrictEqual({ where: { id: '0' } }) + } + + return next(params) + }) + + await prisma.resource.findUnique({ where: { id: '0' } }) + }) + + test('middleware with findFirstOrThrow', async () => { + prisma.$use((params, next) => { + if (params.action === ('findFirstOrThrow' as string)) { + expect(params.args).toStrictEqual({ take: 1 }) + } + + return next(params) + }) + + await prisma.resource.findFirstOrThrow({ take: 1 }).catch(() => {}) + }) + + test('middleware with findUniqueOrThrow', async () => { + prisma.$use((params, next) => { + if (params.action === ('findUniqueOrThrow' as string)) { + expect(params.args).toStrictEqual({ where: { id: '0' } }) + } + + return next(params) + }) + + await prisma.resource.findUniqueOrThrow({ where: { id: '0' } }).catch(() => {}) + }) + + test('middleware with create', async () => { + prisma.$use((params, next) => { + if (params.action === 'create') { + expect(params.args).toStrictEqual({ data: {} }) + } + + return next(params) + }) + + await prisma.resource.create({ data: {} }) + }) + + test('middleware with delete', async () => { + prisma.$use((params, next) => { + if (params.action === 'delete') { + expect(params.args).toStrictEqual({ where: { id: '0' } }) + } + + return next(params) + }) + + await prisma.resource.delete({ where: { id: '0' } }).catch(() => {}) + }) + + test('middleware with deleteMany', async () => { + prisma.$use((params, next) => { + if (params.action === 'deleteMany') { + expect(params.args).toStrictEqual({ where: { id: '0' } }) + } + + return next(params) + }) + + await prisma.resource.deleteMany({ where: { id: '0' } }) + }) + + test('middleware with findMany', async () => { + prisma.$use((params, next) => { + if (params.action === 'findMany') { + expect(params.args).toStrictEqual({ where: { id: '0' } }) + } + + return next(params) + }) + + await prisma.resource.findMany({ where: { id: '0' } }) + }) + + test('middleware with update', async () => { + prisma.$use((params, next) => { + if (params.action === 'update') { + expect(params.args).toStrictEqual({ where: { id: '0' }, data: {} }) + } + + return next(params) + }) + + await prisma.resource.update({ where: { id: '0' }, data: {} }).catch(() => {}) + }) + + test('middleware with updateMany', async () => { + prisma.$use((params, next) => { + if (params.action === 'updateMany') { + expect(params.args).toStrictEqual({ where: { id: '0' }, data: {} }) + } + + return next(params) + }) + + await prisma.resource.updateMany({ where: { id: '0' }, data: {} }) + }) + + test('middleware with upsert', async () => { + prisma.$use((params, next) => { + if (params.action === 'upsert') { + expect(params.args).toStrictEqual({ where: { id: '0' }, create: {}, update: {} }) + } + + return next(params) + }) + + await prisma.resource.upsert({ where: { id: '0' }, create: {}, update: {} }) + }) + }, + { + optOut: { + from: ['cockroachdb', 'mongodb', 'mysql', 'postgresql', 'sqlserver'], + reason: 'Testing on a single provider is enough for testing this issue', + }, + }, +)