prisma-mock 0.10.2 → 0.10.4

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.js CHANGED
@@ -36,28 +36,39 @@ const getNestedValue_1 = __importDefault(require("./utils/getNestedValue"));
36
36
  function IsFieldDefault(f) {
37
37
  return f.name !== undefined;
38
38
  }
39
- const throwKnownError = (message, { code = "P2025", meta } = {}) => {
39
+ function isDefinedWithValue(v, key) {
40
+ return v[key] !== undefined;
41
+ }
42
+ const throwPrismaError = (message, { code = "P2025", meta } = {}, errorClass = client_1.Prisma.PrismaClientKnownRequestError) => {
40
43
  const clientVersion = client_1.Prisma.prismaVersion.client;
41
44
  // PrismaClientKnownRequestError prototype changed in version 4.7.0
42
45
  // from: constructor(message: string, code: string, clientVersion: string, meta?: any)
43
46
  // to: constructor(message: string, { code, clientVersion, meta, batchRequestIdx }: KnownErrorParams)
44
47
  let error;
45
- if (client_1.Prisma.PrismaClientKnownRequestError.length === 2) {
48
+ if (errorClass.length === 2) {
46
49
  // @ts-ignore
47
- error = new client_1.Prisma.PrismaClientKnownRequestError(message, {
50
+ error = new errorClass(message, {
48
51
  code,
49
52
  clientVersion,
50
53
  });
51
54
  }
52
55
  else {
53
56
  // @ts-ignore
54
- error = new client_1.Prisma.PrismaClientKnownRequestError(message, code,
57
+ error = new errorClass(message,
58
+ // @ts-ignore
59
+ code,
55
60
  // @ts-ignore
56
61
  clientVersion);
57
62
  }
58
63
  error.meta = meta;
59
64
  throw error;
60
65
  };
66
+ const throwKnownError = (message, { code = "P2025", meta } = {}) => {
67
+ throwPrismaError(message, { code, meta }, client_1.Prisma.PrismaClientKnownRequestError);
68
+ };
69
+ const throwValidationError = (message, { code = "P2025", meta } = {}) => {
70
+ throwPrismaError(message, { code, meta }, client_1.Prisma.PrismaClientValidationError);
71
+ };
61
72
  const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel, client = (0, jest_mock_extended_1.mockDeep)(), options = {
62
73
  caseInsensitive: false,
63
74
  }) => {
@@ -72,6 +83,7 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
72
83
  };
73
84
  const removeMultiFieldIds = (model, data) => {
74
85
  const c = getCamelCase(model.name);
86
+ // @ts-ignore
75
87
  const idFields = model.idFields || model.primaryKey?.fields;
76
88
  const removeId = (ids) => {
77
89
  const id = ids.join("_");
@@ -174,7 +186,7 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
174
186
  }
175
187
  const keys = Object.keys(orderBy);
176
188
  if (keys.length > 1) {
177
- throw new client_1.Prisma.PrismaClientValidationError(`Argument orderBy of needs exactly one argument, but you provided ${keys.join(" and ")}. Please choose one.`);
189
+ throwValidationError(`Argument orderBy of needs exactly one argument, but you provided ${keys.join(" and ")}. Please choose one.`);
178
190
  }
179
191
  const incl = includes({
180
192
  include: keys.reduce((acc, key) => ({ ...acc, [key]: true }), {}),
@@ -351,21 +363,26 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
351
363
  };
352
364
  }
353
365
  else {
354
- const map = (val) => ({
355
- ...val,
356
- [joinfield.name]: {
357
- connect: joinfield.relationToFields.reduce((prev, cur, index) => {
358
- let val = d[cur];
359
- if (!isCreating && !val) {
360
- val = findOne(args)[cur];
361
- }
362
- return {
363
- ...prev,
364
- [cur]: val,
365
- };
366
- }, {}),
367
- },
368
- });
366
+ const map = (val) => {
367
+ if (joinfield.relationToFields.length === 0) {
368
+ return val;
369
+ }
370
+ return ({
371
+ ...val,
372
+ [joinfield.name]: {
373
+ connect: joinfield.relationToFields.reduce((prev, cur, index) => {
374
+ let val = d[cur];
375
+ if (!isCreating && !val) {
376
+ val = findOne(args)[cur];
377
+ }
378
+ return {
379
+ ...prev,
380
+ [cur]: val,
381
+ };
382
+ }, {}),
383
+ },
384
+ });
385
+ };
369
386
  let createdItems = [];
370
387
  if (c.createMany) {
371
388
  createdItems = delegate._createMany({
@@ -624,6 +641,7 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
624
641
  }
625
642
  return res.length > 0;
626
643
  }
644
+ // @ts-ignore Backwards compatibility
627
645
  const idFields = model.idFields || model.primaryKey?.fields;
628
646
  if (idFields?.length > 1) {
629
647
  if (child === idFields.join("_")) {
@@ -705,29 +723,29 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
705
723
  }
706
724
  }
707
725
  if ("string_ends_with" in matchFilter && match) {
708
- match = val ? val.indexOf(matchFilter.string_ends_with) === val.length - matchFilter.string_ends_with.length : false;
726
+ match = val ? val.lastIndexOf(matchFilter.string_ends_with) === val.length - matchFilter.string_ends_with.length : false;
709
727
  }
710
728
  if ("string_contains" in matchFilter && match) {
711
729
  match = val ? val?.indexOf(matchFilter.string_contains) !== -1 : false;
712
730
  }
713
731
  if ("endsWith" in matchFilter && match) {
714
732
  match =
715
- val.indexOf(matchFilter.endsWith) ===
733
+ val.lastIndexOf(matchFilter.endsWith) ===
716
734
  val.length - matchFilter.endsWith.length;
717
735
  }
718
736
  if ("contains" in matchFilter && match) {
719
737
  match = val.indexOf(matchFilter.contains) > -1;
720
738
  }
721
- if ("gt" in matchFilter && match) {
739
+ if (isDefinedWithValue(matchFilter, "gt") && match) {
722
740
  match = val > matchFilter.gt;
723
741
  }
724
- if ("gte" in matchFilter && match) {
742
+ if (isDefinedWithValue(matchFilter, "gte") && match) {
725
743
  match = val >= matchFilter.gte;
726
744
  }
727
- if ("lt" in matchFilter && match) {
745
+ if (isDefinedWithValue(matchFilter, "lt") && match) {
728
746
  match = val < matchFilter.lt;
729
747
  }
730
- if ("lte" in matchFilter && match) {
748
+ if (isDefinedWithValue(matchFilter, "lte") && match) {
731
749
  match = val <= matchFilter.lte;
732
750
  }
733
751
  if ("in" in matchFilter && match) {
@@ -989,6 +1007,9 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
989
1007
  const model = datamodel.models.find((model) => {
990
1008
  return getCamelCase(model.name) === prop;
991
1009
  });
1010
+ if (!obj[key]) {
1011
+ return;
1012
+ }
992
1013
  if (key === "_count") {
993
1014
  const select = obj[key]?.select;
994
1015
  const subkeys = Object.keys(select);
package/lib/indexes.js CHANGED
@@ -5,7 +5,7 @@ function createIndexes() {
5
5
  let indexedFieldNames = {};
6
6
  let fields = {};
7
7
  let idFieldNames = {};
8
- const addIndexFieldIfNeeded = (tableName, field) => {
8
+ const addIndexFieldIfNeeded = (tableName, field, isPrimary) => {
9
9
  if (!indexedFieldNames[tableName]) {
10
10
  indexedFieldNames[tableName] = [];
11
11
  }
@@ -18,13 +18,15 @@ function createIndexes() {
18
18
  let thisFields = fields[tableName];
19
19
  let thisFieldNames = indexedFieldNames[tableName];
20
20
  let thisIdFieldNames = idFieldNames[tableName];
21
- if (thisFieldNames.includes(field.name))
22
- return;
23
- if (field.isId || field.isUnique) {
24
- thisFieldNames.push(field.name);
21
+ if (field.isId || field.isUnique || isPrimary) {
22
+ if (!thisFieldNames.includes(field.name)) {
23
+ thisFieldNames.push(field.name);
24
+ }
25
25
  }
26
- if (field.isId) {
27
- thisIdFieldNames.push(field.name);
26
+ if (field.isId || isPrimary) {
27
+ if (!thisIdFieldNames.includes(field.name)) {
28
+ thisIdFieldNames.push(field.name);
29
+ }
28
30
  }
29
31
  if (!!field.relationFromFields?.length) {
30
32
  const fieldName = field.relationFromFields[0];
@@ -99,6 +101,7 @@ function createIndexes() {
99
101
  let hasFound = false;
100
102
  for (let i = 0; i < array.length; i++) {
101
103
  const oldItem = array[i];
104
+ thisIdFieldNames; //?
102
105
  for (const thisIdFieldName of thisIdFieldNames) {
103
106
  if (item[thisIdFieldName] === oldItem[thisIdFieldName]) {
104
107
  hasFound = true;
@@ -157,3 +157,37 @@ Array [
157
157
  ]
158
158
  `);
159
159
  });
160
+ test("Should not make multiple items when has mulitple primary keys", () => {
161
+ const indexes = (0, indexes_1.default)();
162
+ indexes.addIndexFieldIfNeeded("UserAnswers", {
163
+ name: "answerId",
164
+ isId: false
165
+ }, true);
166
+ indexes.addIndexFieldIfNeeded("UserAnswers", {
167
+ name: "userId",
168
+ isId: false
169
+ }, true);
170
+ indexes.updateItem("UserAnswers", {
171
+ userId: 1,
172
+ name: "Alice",
173
+ accountId: 1
174
+ });
175
+ indexes.updateItem("UserAnswers", {
176
+ userId: 1,
177
+ name: "Alice 2",
178
+ accountId: 1
179
+ });
180
+ const items = indexes.getIndexedItems("UserAnswers", {
181
+ accountId: 1,
182
+ userId: 1,
183
+ });
184
+ expect(items).toMatchInlineSnapshot(`
185
+ Array [
186
+ Object {
187
+ "accountId": 1,
188
+ "name": "Alice 2",
189
+ "userId": 1,
190
+ },
191
+ ]
192
+ `);
193
+ });
@@ -9,6 +9,9 @@ function deepCopy(source) {
9
9
  else if (Array.isArray(source)) {
10
10
  return source.map(deepCopy);
11
11
  }
12
+ else if (source instanceof Date) {
13
+ return new Date(source);
14
+ }
12
15
  return Object.fromEntries(Object.entries(source).map(([k, v]) => ([k, deepCopy(v)])));
13
16
  }
14
17
  exports.deepCopy = deepCopy;
@@ -3,8 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.deepEqual = void 0;
4
4
  // deepEqual
5
5
  function deepEqual(a, b) {
6
- if ((typeof a == 'object' && a != null) &&
7
- (typeof b == 'object' && b != null)) {
6
+ if (a === b) {
7
+ return true;
8
+ }
9
+ if ((typeof a === 'object' && a !== null) &&
10
+ (typeof b === 'object' && b !== null)) {
11
+ if (a instanceof Date) {
12
+ if (b instanceof Date) {
13
+ return a.getTime() === b.getTime();
14
+ }
15
+ return false;
16
+ }
17
+ else if (b instanceof Date) {
18
+ return false;
19
+ }
8
20
  var count = [0, 0];
9
21
  for (var key in a)
10
22
  count[0]++;
@@ -25,6 +37,6 @@ function deepEqual(a, b) {
25
37
  }
26
38
  return true;
27
39
  }
28
- return a === b;
40
+ return false;
29
41
  }
30
42
  exports.deepEqual = deepEqual;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prisma-mock",
3
- "version": "0.10.2",
3
+ "version": "0.10.4",
4
4
  "description": "Mock prisma for unit testing database",
5
5
  "main": "lib/index.js",
6
6
  "repository": {
@@ -13,12 +13,12 @@
13
13
  "lib/"
14
14
  ],
15
15
  "devDependencies": {
16
- "@prisma/client": "4.7.1",
16
+ "@prisma/client": "5.13.0",
17
17
  "@types/jest": "^27.0.2",
18
18
  "cross-spawn": "^7.0.3",
19
19
  "env-cmd": "^10.1.0",
20
20
  "jest": "^27.3.1",
21
- "prisma": "4.7.1",
21
+ "prisma": "5.13.0",
22
22
  "ts-jest": "^27.0.7",
23
23
  "typescript": "^4.4.4",
24
24
  "uuid": "^9.0.0"
@@ -31,7 +31,7 @@
31
31
  "test:postgres": "env-cmd -e postgres jest --maxWorkers=1"
32
32
  },
33
33
  "peerDependencies": {
34
- "@prisma/client": "^3.5.0 || ^4.7.0 || ^5.0.0"
34
+ "@prisma/client": "^3.5.0 || ^4.7.0 || ^5.0.0 || ^6.0.0"
35
35
  },
36
36
  "dependencies": {
37
37
  "jest-mock-extended": "^3.0.6"