prisma-mock 0.0.6 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ declare type UnwrapPromise<P extends any> = P extends Promise<infer R> ? R : P;
2
+ declare type PrismaDelegate = {
3
+ findUnique: (...args: Array<any>) => Promise<any>;
4
+ };
5
+ declare type IsTable<S> = S extends `\$${infer fnc}` ? never : S;
6
+ declare type IsString<S extends any> = S extends string ? S : never;
7
+ declare type PrismaList<P extends {
8
+ [key: string]: any;
9
+ }, K extends string> = P[K] extends PrismaDelegate ? Array<Partial<UnwrapPromise<ReturnType<P[K]["findUnique"]>>>> : never;
10
+ export declare type PrismaMockData<P> = Partial<{
11
+ [key in IsTable<Uncapitalize<IsString<keyof P>>>]: PrismaList<P, key>;
12
+ }>;
13
+ declare const createPrismaMock: <P>(data?: Partial<{ [key in IsTable<Uncapitalize<IsString<keyof P>>>]: PrismaList<P, key>; }>, pathToSchema?: string, client?: { [K in keyof P]: P[K] extends (...args: infer A) => infer B ? import("jest-mock-extended").CalledWithMock<B, A> : import("jest-mock-extended").DeepMockProxy<P[K]>; } & P) => Promise<P>;
14
+ export default createPrismaMock;
package/package.json CHANGED
@@ -1,9 +1,13 @@
1
1
  {
2
2
  "name": "prisma-mock",
3
- "version": "0.0.6",
3
+ "version": "0.0.9",
4
4
  "description": "Mock prisma for unit testing database",
5
5
  "main": "lib/index.js",
6
6
  "license": "MIT",
7
+ "types": "lib/",
8
+ "files": [
9
+ "lib/"
10
+ ],
7
11
  "dependencies": {
8
12
  "@prisma/generator-helper": "^3.5.0",
9
13
  "@prisma/sdk": "^3.5.0",
package/jest.config.js DELETED
@@ -1,10 +0,0 @@
1
- module.exports = {
2
- testRegex: '\.test\.ts$',
3
- preset: 'ts-jest',
4
- testPathIgnorePatterns: ['<rootDir>/node_modules/', './dist/'],
5
- // coverageDirectory: '../coverage',
6
- testEnvironment: 'node',
7
- moduleNameMapper: {
8
- '^src/(.*)': '<rootDir>/src/$1',
9
- }
10
- };
@@ -1,51 +0,0 @@
1
- generator client {
2
- provider = "prisma-client-js"
3
- }
4
-
5
- datasource db {
6
- provider = "postgresql"
7
- url = env("DATABASE_URL")
8
- }
9
-
10
- enum Role {
11
- ADMIN
12
- USER
13
- }
14
-
15
- model Account {
16
- id Int @id @default(autoincrement())
17
- users User[] @relation("AccountUsers")
18
- stripe Stripe? @relation
19
- name String?
20
- }
21
-
22
- model User {
23
- id Int @id @default(autoincrement())
24
- account Account? @relation("AccountUsers", fields: [accountId], references: [id])
25
- accountId Int?
26
- role Role @default(ADMIN)
27
- answers UserAnswers[]
28
- }
29
-
30
- model Stripe {
31
- id Int @id @default(autoincrement())
32
- customerId String @unique
33
- accountId Int @unique
34
- account Account @relation(fields: [accountId], references: [id])
35
- }
36
-
37
- model Answers {
38
- id Int @id @default(autoincrement())
39
- title String
40
- users UserAnswers[]
41
- }
42
-
43
- model UserAnswers {
44
- answer Answers @relation(fields: [answerId], references: [id])
45
- answerId Int
46
-
47
- user User @relation(fields: [userId], references: [id])
48
- userId Int
49
-
50
- @@id([userId, answerId])
51
- }
@@ -1,40 +0,0 @@
1
- // @ts-nocheck
2
-
3
- import createPrismaClient from "../";
4
- import { PrismaClient } from "@prisma/client";
5
-
6
- describe("PrismaClient distinct", () => {
7
- const data = {
8
- user: [
9
- {
10
- id: 1,
11
- name: "Piet",
12
- },
13
- {
14
- id: 2,
15
- name: "Piet",
16
- },
17
- {
18
- id: 3,
19
- name: "Henk",
20
- },
21
- {
22
- id: 4,
23
- name: "Henk",
24
- },
25
- ],
26
- };
27
-
28
- test("distinct", async () => {
29
- const client = await createPrismaClient(data);
30
- let users = await client.user.findMany({
31
- distinct: ['id']
32
- });
33
- expect(users.length).toBe(4);
34
- users = await client.user.findMany({
35
- distinct: ['name']
36
- });
37
- expect(users.length).toBe(2);
38
- });
39
-
40
- });
@@ -1,90 +0,0 @@
1
- // @ts-nocheck
2
-
3
- import createPrismaClient from '../'
4
- import { PrismaClient } from '@prisma/client'
5
-
6
-
7
- describe('PrismaClient @@id()', () => {
8
-
9
- const data = {
10
- user: [
11
- {
12
- id: 1,
13
- name: 'sadfsdf',
14
- }
15
- ],
16
- userAnswers: [
17
- {
18
- userId: 1,
19
- answerId: 2
20
- }
21
- ],
22
- answers: [
23
- {
24
- id: 1,
25
- title: "Answer"
26
- },
27
- {
28
- id: 2,
29
- title: "Answer"
30
- }
31
- ],
32
- }
33
-
34
- test('findOne', async () => {
35
- const client = await createPrismaClient(data)
36
- const user = await client.userAnswers.findUnique({
37
- where: {
38
- userId_answerId: {
39
- userId: 1,
40
- answerId: 2
41
- }
42
- },
43
- })
44
- expect(user).toEqual(expect.objectContaining(data.userAnswers[0]))
45
- })
46
-
47
- test('create', async () => {
48
- const client = await createPrismaClient(data)
49
- await client.userAnswers.create({
50
- data: {
51
- user: { connect: { id: 1 }},
52
- answer: { connect: { id: 1 }},
53
- }
54
- })
55
- const user = await client.userAnswers.findUnique({
56
- where: {
57
- userId_answerId: {
58
- userId: 1,
59
- answerId: 1
60
- }
61
- },
62
- })
63
- expect(user).toEqual(expect.objectContaining({
64
- userId_answerId: {
65
- userId: 1,
66
- answerId: 1
67
- }
68
- }))
69
-
70
- })
71
-
72
- test('delete', async () => {
73
-
74
- })
75
-
76
- test('update', async () => {
77
-
78
- })
79
-
80
- test('upsert update', async () => {
81
-
82
- })
83
-
84
- test('upsert insert', async () => {
85
-
86
- })
87
-
88
- test.todo("connect")
89
- test.todo('should throw when there is a duplicate')
90
- })
@@ -1,129 +0,0 @@
1
- // @ts-nocheck
2
-
3
- import createPrismaClient from "../";
4
- import { PrismaClient } from "@prisma/client";
5
-
6
-
7
- describe("PrismaClient include", () => {
8
- const data = {
9
- user: [
10
- {
11
- id: 1,
12
- name: "sadfsdf",
13
- accountId: 1,
14
- },
15
- ],
16
- account: [
17
- {
18
- id: 1,
19
- name: "sadfsdf",
20
- },
21
- {
22
- id: 2,
23
- name: "adsfasdf2",
24
- },
25
- ],
26
- stripe: [
27
- {
28
- id: 1,
29
- accountId: 1,
30
- },
31
- ],
32
- };
33
-
34
- test("findOne to", async () => {
35
- const client = await createPrismaClient(data);
36
- const user = await client.user.findUnique({
37
- where: {
38
- id: 1,
39
- },
40
- include: {
41
- account: true,
42
- },
43
- });
44
- expect(user).toEqual({
45
- ...data.user[0],
46
- account: data.account[0],
47
- });
48
- });
49
-
50
- test("findOne from", async () => {
51
- const client = await createPrismaClient(data);
52
- const stripe = await client.stripe.findUnique({
53
- where: {
54
- id: 1,
55
- },
56
- include: {
57
- account: true,
58
- },
59
- });
60
- expect(stripe).toEqual({
61
- ...data.stripe[0],
62
- account: data.account[0],
63
- });
64
- });
65
-
66
- test("findOne deep", async () => {
67
- const client = await createPrismaClient(data);
68
- const user = await client.user.findUnique({
69
- where: {
70
- id: 1,
71
- },
72
- include: {
73
- account: {
74
- include: {
75
- stripe: true,
76
- },
77
- },
78
- },
79
- });
80
- expect(user).toEqual({
81
- ...data.user[0],
82
- account: {
83
- ...data.account[0],
84
- stripe: data.stripe[0],
85
- },
86
- });
87
- });
88
-
89
- test("findMany deep", async () => {
90
- const client = await createPrismaClient(data);
91
- const users = await client.user.findMany({
92
- where: {
93
- id: 1,
94
- },
95
- include: {
96
- account: {
97
- include: {
98
- stripe: true,
99
- },
100
- },
101
- },
102
- });
103
- expect(users[0]).toEqual({
104
- ...data.user[0],
105
- account: {
106
- ...data.account[0],
107
- stripe: data.stripe[0],
108
- },
109
- });
110
- });
111
-
112
- test("findMany one to many", async () => {
113
- const client = await createPrismaClient(data);
114
- const users = await client.account.findMany({
115
- where: {
116
- id: 1,
117
- },
118
- include: {
119
- users: true
120
- },
121
- });
122
- expect(users[0]).toEqual({
123
- ...data.account[0],
124
- users: [
125
- data.user[0],
126
- ],
127
- });
128
- });
129
- });
@@ -1,182 +0,0 @@
1
- // @ts-nocheck
2
-
3
- import createPrismaClient from '../'
4
-
5
- describe('PrismaClient', () => {
6
-
7
- const data = {
8
- user: [
9
- {
10
- id: 1,
11
- name: 'sadfsdf',
12
- accountId: 1,
13
- role: "ADMIN"
14
- }
15
- ],
16
- account: [
17
- {
18
- id: 1,
19
- name: 'sadfsdf',
20
- },
21
- {
22
- id: 2,
23
- name: 'adsfasdf2',
24
- }
25
- ]
26
- }
27
-
28
- test('findOne', async () => {
29
- const client = await createPrismaClient(data)
30
- const user = await client.user.findUnique({
31
- where: {
32
- id: 1
33
- }
34
- })
35
- expect(user).toBe(data.user[0])
36
- })
37
-
38
- test('findOne by id', async () => {
39
- const client = await createPrismaClient(data)
40
- const user = await client.account.findUnique({
41
- where: {
42
- id: 2
43
- }
44
- })
45
- expect(user).toBe(data.account[1])
46
- })
47
-
48
- test('findMany', async () => {
49
- const client = await createPrismaClient(data)
50
- const accounts = await client.account.findMany()
51
- expect(accounts).toEqual(data.account)
52
- })
53
-
54
- test('findFirst', async () => {
55
- const client = await createPrismaClient(data)
56
- const accounts = await client.account.findFirst()
57
- expect(accounts).toEqual(data.account[0])
58
- })
59
-
60
- test('count', async () => {
61
- const client = await createPrismaClient(data)
62
- const accounts = await client.account.count()
63
- expect(accounts).toEqual(2)
64
- })
65
-
66
- test('create', async () => {
67
- const client = await createPrismaClient(data)
68
- // TODO: Check output
69
- await client.user.create({
70
- data: {
71
- name: 'New user'
72
- }
73
- })
74
- const users = await client.user.findMany()
75
-
76
- expect(users).toEqual([
77
- ...data.user,
78
- {
79
- id: 2,
80
- name: 'New user',
81
- role: "ADMIN"
82
- }
83
- ])
84
- })
85
-
86
- test('create connect', async () => {
87
- const client = await createPrismaClient(data)
88
- // TODO: Check output
89
- await client.user.create({
90
- data: {
91
- name: 'New user',
92
- account: { connect: { id: 1 } }
93
- }
94
- })
95
- const users = await client.user.findMany()
96
-
97
- expect(users).toEqual([
98
- ...data.user,
99
- {
100
- id: 2,
101
- name: 'New user',
102
- role: "ADMIN",
103
- accountId: 1,
104
- }
105
- ])
106
- })
107
-
108
- test('delete', async () => {
109
- const client = await createPrismaClient(data)
110
- const user = await client.account.delete({
111
- where: {
112
- id: 2
113
- }
114
- })
115
- const users = await client.account.findMany()
116
- expect(users).toEqual([data.account[0]])
117
- })
118
-
119
- test('update', async () => {
120
- const client = await createPrismaClient(data)
121
- const user = await client.account.update({
122
- where: {
123
- id: 2
124
- },
125
- data: {
126
- name: "New name"
127
- }
128
- })
129
- const users = await client.account.findMany()
130
- expect(users).toEqual([
131
- data.account[0],
132
- {
133
- id: 2,
134
- name: "New name"
135
- }
136
- ])
137
- })
138
-
139
- test('upsert update', async () => {
140
- const client = await createPrismaClient(data)
141
- const user = await client.account.upsert({
142
- where: {
143
- id: 2
144
- },
145
- update: {
146
- name: "New name"
147
- }
148
- })
149
- const users = await client.account.findMany()
150
- expect(users).toEqual([
151
- data.account[0],
152
- {
153
- id: 2,
154
- name: "New name"
155
- }
156
- ])
157
- })
158
-
159
- test('upsert insert', async () => {
160
- const client = await createPrismaClient(data)
161
- const user = await client.account.upsert({
162
- where: {
163
- id: 3
164
- },
165
- create: {
166
- id: 3,
167
- name: "New name"
168
- }
169
- })
170
- const users = await client.account.findMany()
171
- expect(users).toEqual([
172
- ...data.account,
173
- {
174
- id: 3,
175
- name: "New name"
176
- }
177
- ])
178
- })
179
-
180
- test.todo('connect')
181
-
182
- })