prisma-mock 0.6.0 → 0.7.1

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/README.md CHANGED
@@ -176,7 +176,13 @@ TODO (has, hasEvery, hasSome, isEmpty, equals)
176
176
 
177
177
  ## JSON filters
178
178
 
179
- TODO (path, string_contains, string_starts_with, string_ends_with, array_contains, array_starts_with, array_ends_with)
179
+ - path
180
+ - string_contains
181
+ - string_starts_with
182
+ - string_ends_with
183
+ - array_contains
184
+ - array_starts_with
185
+ - array_ends_with
180
186
 
181
187
  ## Attributes
182
188
 
@@ -192,7 +198,7 @@ TODO (path, string_contains, string_starts_with, string_ends_with, array_contain
192
198
  - autoincrement()
193
199
  - TODO: auto()
194
200
  - cuid()
195
- - TODO: uuid()
201
+ - uuid()
196
202
  - now()
197
203
  - TODO: dbgenerated()
198
204
 
package/lib/index.js CHANGED
@@ -22,6 +22,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
25
28
  Object.defineProperty(exports, "__esModule", { value: true });
26
29
  const client_1 = require("@prisma/client");
27
30
  const jest_mock_extended_1 = require("jest-mock-extended");
@@ -29,10 +32,11 @@ const defaults_1 = __importStar(require("./defaults"));
29
32
  const shallowCompare_1 = require("./utils/shallowCompare");
30
33
  const deepEqual_1 = require("./utils/deepEqual");
31
34
  const deepCopy_1 = require("./utils/deepCopy");
35
+ const getNestedValue_1 = __importDefault(require("./utils/getNestedValue"));
32
36
  function IsFieldDefault(f) {
33
37
  return f.name !== undefined;
34
38
  }
35
- const throwUnkownError = (message, cause) => {
39
+ const throwKnownError = (message, cause) => {
36
40
  const code = "P2025";
37
41
  const clientVersion = "1.2.3";
38
42
  // PrismaClientKnownRequestError prototype changed in version 4.7.0
@@ -257,7 +261,7 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
257
261
  return (0, shallowCompare_1.shallowCompare)(target, valueToMatch);
258
262
  });
259
263
  if (!matchingRow) {
260
- throwUnkownError("An operation failed because it depends on one or more records that were required but not found. {cause}");
264
+ throwKnownError("An operation failed because it depends on one or more records that were required but not found. {cause}");
261
265
  }
262
266
  }
263
267
  connectionValue = matchingRow[keyToGet];
@@ -620,12 +624,58 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
620
624
  }
621
625
  });
622
626
  }
627
+ if ("path" in matchFilter) {
628
+ val = (0, getNestedValue_1.default)(matchFilter.path, val);
629
+ }
623
630
  if ("equals" in matchFilter && match) {
624
- match = (0, deepEqual_1.deepEqual)(matchFilter.equals, val);
631
+ // match = deepEqual(matchFilter.equals, val)
632
+ if (matchFilter.equals === client_1.Prisma.DbNull) {
633
+ if (val === client_1.Prisma.DbNull) {
634
+ }
635
+ match = val === client_1.Prisma.DbNull;
636
+ }
637
+ else if (matchFilter.equals === client_1.Prisma.AnyNull) {
638
+ match = val === client_1.Prisma.DbNull || val === client_1.Prisma.JsonNull;
639
+ }
640
+ else {
641
+ if (val === client_1.Prisma.DbNull) {
642
+ match = false;
643
+ }
644
+ else {
645
+ match = (0, deepEqual_1.deepEqual)(matchFilter.equals, val);
646
+ }
647
+ }
625
648
  }
626
649
  if ("startsWith" in matchFilter && match) {
627
650
  match = val.indexOf(matchFilter.startsWith) === 0;
628
651
  }
652
+ if ("string_starts_with" in matchFilter && match) {
653
+ match = val?.indexOf(matchFilter.string_starts_with) === 0;
654
+ }
655
+ if ("array_contains" in matchFilter && match) {
656
+ if (Array.isArray(val)) {
657
+ for (const item of matchFilter.array_contains) {
658
+ let hasMatch = false;
659
+ for (const i of val) {
660
+ if ((0, deepEqual_1.deepEqual)(item, i))
661
+ hasMatch = true;
662
+ }
663
+ if (!hasMatch) {
664
+ match = false;
665
+ break;
666
+ }
667
+ }
668
+ }
669
+ else {
670
+ match = false;
671
+ }
672
+ }
673
+ if ("string_ends_with" in matchFilter && match) {
674
+ match = val ? val.indexOf(matchFilter.string_ends_with) === val.length - matchFilter.string_ends_with.length : false;
675
+ }
676
+ if ("string_contains" in matchFilter && match) {
677
+ match = val ? val?.indexOf(matchFilter.string_contains) !== -1 : false;
678
+ }
629
679
  if ("endsWith" in matchFilter && match) {
630
680
  match =
631
681
  val.indexOf(matchFilter.endsWith) ===
@@ -650,13 +700,24 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
650
700
  match = matchFilter.in.includes(val);
651
701
  }
652
702
  if ("not" in matchFilter && match) {
653
- match = !(0, deepEqual_1.deepEqual)(matchFilter.not, val);
703
+ if (matchFilter.not === client_1.Prisma.DbNull) {
704
+ match = val !== client_1.Prisma.DbNull;
705
+ }
706
+ else {
707
+ if (val === client_1.Prisma.DbNull) {
708
+ match = false;
709
+ }
710
+ else {
711
+ match = !(0, deepEqual_1.deepEqual)(matchFilter.not, val);
712
+ }
713
+ }
654
714
  }
655
715
  if ("notIn" in matchFilter && match) {
656
716
  match = !matchFilter.notIn.includes(val);
657
717
  }
658
- if (!match)
718
+ if (!match) {
659
719
  return false;
720
+ }
660
721
  }
661
722
  else if (val !== filter) {
662
723
  return false;
@@ -738,6 +799,19 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
738
799
  const end = args?.take !== undefined ? start + args.take : undefined;
739
800
  res = res.slice(start, end);
740
801
  }
802
+ // Replace nulls
803
+ res = res.map((item) => {
804
+ const newItem = {};
805
+ Object.keys(item).forEach((key) => {
806
+ if (item[key] === client_1.Prisma.JsonNull || item[key] === client_1.Prisma.DbNull) {
807
+ newItem[key] = null;
808
+ }
809
+ else {
810
+ newItem[key] = item[key];
811
+ }
812
+ });
813
+ return newItem;
814
+ });
741
815
  return res;
742
816
  };
743
817
  const updateMany = (args) => {
@@ -931,7 +1005,7 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
931
1005
  delete: (args) => {
932
1006
  const item = findOne(args);
933
1007
  if (!item) {
934
- throwUnkownError("An operation failed because it depends on one or more records that were required but not found. Record to delete does not exist.", "Record to delete does not exist.");
1008
+ throwKnownError("An operation failed because it depends on one or more records that were required but not found. Record to delete does not exist.", "Record to delete does not exist.");
935
1009
  }
936
1010
  const deleted = deleteMany(args);
937
1011
  if (deleted.length) {
@@ -0,0 +1 @@
1
+ export default function getNestedValue(keys: string[], values: any): any;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function getNestedValue(keys, values) {
4
+ const keysCopy = [...keys];
5
+ let key;
6
+ let object = values;
7
+ while ((key = keysCopy.shift())) {
8
+ if (Array.isArray(object)) {
9
+ object = object?.[parseInt(key)];
10
+ }
11
+ else {
12
+ object = object?.[key];
13
+ }
14
+ }
15
+ return object;
16
+ }
17
+ exports.default = getNestedValue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prisma-mock",
3
- "version": "0.6.0",
3
+ "version": "0.7.1",
4
4
  "description": "Mock prisma for unit testing database",
5
5
  "main": "lib/index.js",
6
6
  "repository": "https://github.com/demonsters/prisma-mock",