forked from ps73/feathers-prisma
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.test.js
More file actions
165 lines (123 loc) · 4.49 KB
/
utils.test.js
File metadata and controls
165 lines (123 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const { PrismaClient } = require('@prisma/client');
const feathers = require('@feathersjs/feathers');
const { prismaService } = require('../dist');
const { buildPrismaQueryParams } = require('../dist/utils');
const { expect } = require('chai');
const app = feathers();
const prismaClient = new PrismaClient();
const todos = prismaService({
model: 'todo',
multi: ['create', 'patch', 'remove'],
whitelist: ['$eager'],
}, prismaClient);
const users = prismaService({
model: 'user',
events: ['testing'],
whitelist: ['$eager', '$rawWhere'],
}, prismaClient);
app.use('/todos', todos);
app.use('/users', users);
describe('Feathers Prisma Utils', () => {
before(async () => {
await prismaClient.$connect();
await prismaClient.todo.deleteMany({});
await prismaClient.people.deleteMany({});
await prismaClient.user.deleteMany({});
await prismaClient.peopleId.deleteMany({});
});
after(async () => {
await prismaClient.$disconnect();
});
afterEach(async () => {
await prismaClient.todo.deleteMany({});
await prismaClient.people.deleteMany({});
await prismaClient.user.deleteMany({});
await prismaClient.peopleId.deleteMany({});
});
describe('the \'buildPrismaQueryParams\' function', () => {
beforeEach(async () => {
const data = await prismaClient.user.create({
data: {
name: 'Max Power',
age: 19,
todos: {
create: [
{ title: 'Todo1', prio: 1 }
],
}
}
});
await prismaClient.todo.create({ data: { title: 'Lorem', prio: 1, userId: data.id }, });
await prismaClient.todo.create({ data: { title: 'Lorem Ipsum', prio: 1, userId: data.id, tag1: 'TEST' }, });
await prismaClient.todo.create({ data: { title: '[TODO]', prio: 1, userId: data.id, tag1: 'TEST2' }, });
});
it('should only have one result if the id field is set', async () => {
const [first, second, third] = await prismaClient.todo.findMany();
const { where } = buildPrismaQueryParams({
query: {
id: {
$in: [first.id, second.id, third.id]
}
},
id: first.id,
filters: {},
whitelist: ['$in']
}, 'id');
const result = await prismaClient.todo.findFirst({ where });
expect(result).to.be.not.null;
expect(result).to.be.deep.eq(first);
const todos = await prismaClient.todo.findMany({ where });
expect(todos).to.be.have.length(1);
expect(todos[0]).to.be.deep.eq(first);
});
it('should only have one result if the id field is set even if the prisma query overwrites are set', async () => {
const [first, second, third] = await prismaClient.todo.findMany();
const { where } = buildPrismaQueryParams({
query: {},
id: first.id,
filters: {},
whitelist: ['$in']
}, 'id', { where: { id: { in: [first.id, second.id, third.id] } } });
const result = await prismaClient.todo.findFirst({ where });
expect(result).to.be.not.null;
expect(result).to.be.deep.eq(first);
const todos = await prismaClient.todo.findMany({ where });
expect(todos).to.be.have.length(1);
expect(todos[0]).to.be.deep.eq(first);
});
it('should have NO result because the requested id is exclude with the feathers query', async () => {
const [first] = await prismaClient.todo.findMany();
const { where } = buildPrismaQueryParams({
query: {
id: {
$nin: [first.id]
}
},
id: first.id,
filters: {},
whitelist: ['$in', '$nin']
}, 'id');
const result = await prismaClient.todo.findFirst({ where });
expect(result).to.be.null;
const todos = await prismaClient.todo.findMany({ where });
expect(todos).to.be.have.length(0);
});
it('should have NO result because the requested id is exclude with the prisma query overwrite', async () => {
const [first, second, third] = await prismaClient.todo.findMany();
const { where } = buildPrismaQueryParams({
query: {
id: {
$in: [first.id]
}
},
id: first.id,
filters: {},
whitelist: ['$in', '$nin']
}, 'id', { where: { id: { in: [second.id, third.id] } } });
const result = await prismaClient.todo.findFirst({ where });
expect(result).to.be.null;
const todos = await prismaClient.todo.findMany({ where });
expect(todos).to.be.have.length(0);
});
});
});