prisma-mock 0.5.5 → 0.7.0
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/defaults/cuid.js +5 -7
- package/lib/defaults/index.js +3 -0
- package/lib/defaults/uuid.d.ts +3 -0
- package/lib/defaults/uuid.js +18 -0
- package/lib/index.js +85 -9
- package/lib/utils/getNestedValue.d.ts +1 -0
- package/lib/utils/getNestedValue.js +17 -0
- package/lib/utils/pad.d.ts +1 -0
- package/lib/utils/pad.js +9 -0
- package/package.json +3 -2
package/lib/defaults/cuid.js
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.ResetCuid = void 0;
|
|
7
|
+
const pad_1 = __importDefault(require("../utils/pad"));
|
|
4
8
|
let ciud_cache = 0;
|
|
5
|
-
function pad(s, size) {
|
|
6
|
-
while (s.length < (size || 2)) {
|
|
7
|
-
s = "0" + s;
|
|
8
|
-
}
|
|
9
|
-
return s;
|
|
10
|
-
}
|
|
11
9
|
// Format from: https://cuid.marcoonroad.dev/
|
|
12
10
|
const Cuid = () => {
|
|
13
11
|
ciud_cache++;
|
|
14
|
-
return `c00p6qup2${
|
|
12
|
+
return `c00p6qup2${(0, pad_1.default)(String(ciud_cache), 4)}ckkzslahp5pn`;
|
|
15
13
|
};
|
|
16
14
|
function ResetCuid() {
|
|
17
15
|
ciud_cache = 0;
|
package/lib/defaults/index.js
CHANGED
|
@@ -30,10 +30,12 @@ exports.ResetDefaults = void 0;
|
|
|
30
30
|
const autoincrement_1 = __importStar(require("./autoincrement"));
|
|
31
31
|
const cuid_1 = __importStar(require("./cuid"));
|
|
32
32
|
const now_1 = __importDefault(require("./now"));
|
|
33
|
+
const uuid_1 = __importStar(require("./uuid"));
|
|
33
34
|
// const registry = new Map<string, (string, Prisma.DMMF.Field, PrismaMockData) => any>();
|
|
34
35
|
const registry = new Map();
|
|
35
36
|
registry.set("autoincrement", autoincrement_1.default);
|
|
36
37
|
registry.set("cuid", cuid_1.default);
|
|
38
|
+
registry.set("uuid", uuid_1.default);
|
|
37
39
|
registry.set("now", now_1.default);
|
|
38
40
|
function HandleDefault(prop, field, data) {
|
|
39
41
|
const key = field.default.name;
|
|
@@ -44,5 +46,6 @@ exports.default = HandleDefault;
|
|
|
44
46
|
function ResetDefaults() {
|
|
45
47
|
(0, autoincrement_1.reset)();
|
|
46
48
|
(0, cuid_1.ResetCuid)();
|
|
49
|
+
(0, uuid_1.ResetUuid)();
|
|
47
50
|
}
|
|
48
51
|
exports.ResetDefaults = ResetDefaults;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ResetUuid = void 0;
|
|
7
|
+
const pad_1 = __importDefault(require("../utils/pad"));
|
|
8
|
+
let uuid_cache = 0;
|
|
9
|
+
// https://en.wikipedia.org/wiki/Universally_unique_identifier
|
|
10
|
+
const Uuid = () => {
|
|
11
|
+
uuid_cache++;
|
|
12
|
+
return `123e4567-e89b-12d3-a456-${(0, pad_1.default)(String(uuid_cache), 12)}`;
|
|
13
|
+
};
|
|
14
|
+
function ResetUuid() {
|
|
15
|
+
uuid_cache = 0;
|
|
16
|
+
}
|
|
17
|
+
exports.ResetUuid = ResetUuid;
|
|
18
|
+
exports.default = Uuid;
|
package/lib/index.js
CHANGED
|
@@ -22,14 +22,17 @@ 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
|
-
const runtime_1 = require("@prisma/client/runtime");
|
|
28
30
|
const jest_mock_extended_1 = require("jest-mock-extended");
|
|
29
31
|
const defaults_1 = __importStar(require("./defaults"));
|
|
30
32
|
const shallowCompare_1 = require("./utils/shallowCompare");
|
|
31
33
|
const deepEqual_1 = require("./utils/deepEqual");
|
|
32
34
|
const deepCopy_1 = require("./utils/deepCopy");
|
|
35
|
+
const getNestedValue_1 = __importDefault(require("./utils/getNestedValue"));
|
|
33
36
|
function IsFieldDefault(f) {
|
|
34
37
|
return f.name !== undefined;
|
|
35
38
|
}
|
|
@@ -40,16 +43,16 @@ const throwUnkownError = (message, cause) => {
|
|
|
40
43
|
// from: constructor(message: string, code: string, clientVersion: string, meta?: any)
|
|
41
44
|
// to: constructor(message: string, { code, clientVersion, meta, batchRequestIdx }: KnownErrorParams)
|
|
42
45
|
let error;
|
|
43
|
-
if (
|
|
46
|
+
if (client_1.Prisma.PrismaClientKnownRequestError.length === 2) {
|
|
44
47
|
// @ts-ignore
|
|
45
|
-
error = new
|
|
48
|
+
error = new client_1.Prisma.PrismaClientKnownRequestError(message, {
|
|
46
49
|
code,
|
|
47
50
|
clientVersion,
|
|
48
51
|
});
|
|
49
52
|
}
|
|
50
53
|
else {
|
|
51
54
|
// @ts-ignore
|
|
52
|
-
error = new
|
|
55
|
+
error = new client_1.Prisma.PrismaClientKnownRequestError(message, code,
|
|
53
56
|
// @ts-ignore
|
|
54
57
|
clientVersion);
|
|
55
58
|
}
|
|
@@ -173,7 +176,7 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
173
176
|
}
|
|
174
177
|
const keys = Object.keys(orderBy);
|
|
175
178
|
if (keys.length > 1) {
|
|
176
|
-
throw new
|
|
179
|
+
throw new client_1.Prisma.PrismaClientValidationError(`Argument orderBy of needs exactly one argument, but you provided ${keys.join(" and ")}. Please choose one.`);
|
|
177
180
|
}
|
|
178
181
|
const incl = includes({
|
|
179
182
|
include: keys.reduce((acc, key) => ({ ...acc, [key]: true }), {}),
|
|
@@ -621,12 +624,58 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
621
624
|
}
|
|
622
625
|
});
|
|
623
626
|
}
|
|
627
|
+
if ("path" in matchFilter) {
|
|
628
|
+
val = (0, getNestedValue_1.default)(matchFilter.path, val);
|
|
629
|
+
}
|
|
624
630
|
if ("equals" in matchFilter && match) {
|
|
625
|
-
match =
|
|
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
|
+
}
|
|
626
648
|
}
|
|
627
649
|
if ("startsWith" in matchFilter && match) {
|
|
628
650
|
match = val.indexOf(matchFilter.startsWith) === 0;
|
|
629
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
|
+
}
|
|
630
679
|
if ("endsWith" in matchFilter && match) {
|
|
631
680
|
match =
|
|
632
681
|
val.indexOf(matchFilter.endsWith) ===
|
|
@@ -651,18 +700,32 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
651
700
|
match = matchFilter.in.includes(val);
|
|
652
701
|
}
|
|
653
702
|
if ("not" in matchFilter && match) {
|
|
654
|
-
|
|
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
|
+
}
|
|
655
714
|
}
|
|
656
715
|
if ("notIn" in matchFilter && match) {
|
|
657
716
|
match = !matchFilter.notIn.includes(val);
|
|
658
717
|
}
|
|
659
|
-
if (!match)
|
|
718
|
+
if (!match) {
|
|
719
|
+
console.log("has no match", item);
|
|
660
720
|
return false;
|
|
721
|
+
}
|
|
722
|
+
console.log("has match", item);
|
|
661
723
|
}
|
|
662
724
|
else if (val !== filter) {
|
|
663
725
|
return false;
|
|
664
726
|
}
|
|
665
727
|
}
|
|
728
|
+
console.log("matchFilter", filter, item, child, filter?.equals === item[child]);
|
|
666
729
|
return true;
|
|
667
730
|
};
|
|
668
731
|
const matchItems = (item, where) => {
|
|
@@ -697,7 +760,7 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
697
760
|
const findOrThrow = (args) => {
|
|
698
761
|
const found = findOne(args);
|
|
699
762
|
if (!found) {
|
|
700
|
-
throw new
|
|
763
|
+
throw new client_1.Prisma.PrismaClientKnownRequestError(`No ${prop.slice(0, 1).toUpperCase()}${prop.slice(1)} found`, "P2025",
|
|
701
764
|
// @ts-ignore
|
|
702
765
|
"1.2.3");
|
|
703
766
|
}
|
|
@@ -739,6 +802,19 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
739
802
|
const end = args?.take !== undefined ? start + args.take : undefined;
|
|
740
803
|
res = res.slice(start, end);
|
|
741
804
|
}
|
|
805
|
+
// Replace nulls
|
|
806
|
+
res = res.map((item) => {
|
|
807
|
+
const newItem = {};
|
|
808
|
+
Object.keys(item).forEach((key) => {
|
|
809
|
+
if (item[key] === client_1.Prisma.JsonNull || item[key] === client_1.Prisma.DbNull) {
|
|
810
|
+
newItem[key] = null;
|
|
811
|
+
}
|
|
812
|
+
else {
|
|
813
|
+
newItem[key] = item[key];
|
|
814
|
+
}
|
|
815
|
+
});
|
|
816
|
+
return newItem;
|
|
817
|
+
});
|
|
742
818
|
return res;
|
|
743
819
|
};
|
|
744
820
|
const updateMany = (args) => {
|
|
@@ -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;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function pad(s: string, size: number): string;
|
package/lib/utils/pad.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prisma-mock",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Mock prisma for unit testing database",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"repository": "https://github.com/demonsters/prisma-mock",
|
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
"jest-mock-extended": "^2.0.4",
|
|
19
19
|
"prisma": "4.7.1",
|
|
20
20
|
"ts-jest": "^27.0.7",
|
|
21
|
-
"typescript": "^4.4.4"
|
|
21
|
+
"typescript": "^4.4.4",
|
|
22
|
+
"uuid": "^9.0.0"
|
|
22
23
|
},
|
|
23
24
|
"scripts": {
|
|
24
25
|
"preversion": "tsc",
|