-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_errors.ts
More file actions
141 lines (126 loc) · 4.43 KB
/
Copy pathtest_errors.ts
File metadata and controls
141 lines (126 loc) · 4.43 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
/**
* test_errors.ts — Error handling integration tests for perry-prisma.
* Uses NativeCurrencyPrice model, chainId range 960001–960010.
*/
import { createPrismaClient } from "perry-prisma";
import { eq, ok, isNull, isNotNull, arrLen, finish } from "./helpers";
var url = process.env.DATABASE_URL;
if (!url || url === "undefined") {
url = "mysql://test:k7DRayxy%23ipTebbGE@webserver.skelpo.net:3306/test";
}
var prisma = createPrismaClient({ datasourceUrl: url });
console.log("=== test_errors.ts ===");
console.log("");
try {
prisma.$connect();
// Ensure table exists
prisma.$executeRaw(
"CREATE TABLE IF NOT EXISTS NativeCurrencyPrice (" +
" id VARCHAR(36) NOT NULL," +
" chainId INT NOT NULL," +
" currency VARCHAR(255) NOT NULL," +
" usdPrice DECIMAL(36,18) NOT NULL," +
" createdAt DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)," +
" updatedAt DATETIME(3) NOT NULL," +
" PRIMARY KEY (id)," +
" UNIQUE KEY chainId_unique (chainId)," +
" INDEX idx_chainId (chainId)," +
" INDEX idx_updatedAt (updatedAt)" +
")"
);
// Cleanup
prisma.$executeRaw("DELETE FROM NativeCurrencyPrice WHERE chainId >= 960001 AND chainId <= 960010");
// ── $queryRaw with non-existent table → error ──
console.log("$queryRaw: non-existent table");
var rawTableError = false;
try {
prisma.$queryRaw("SELECT * FROM NonExistentTable_XYZ_12345");
} catch (e: any) {
rawTableError = true;
ok("$queryRaw bad table: error message present", e.message && e.message.length > 0);
}
ok("$queryRaw non-existent table throws", rawTableError);
// ── create with empty data → error ──
console.log("create: empty data");
var emptyDataError = false;
var emptyDataMsg = "";
try {
prisma.nativeCurrencyPrice.create({ data: {} });
} catch (e: any) {
emptyDataError = true;
emptyDataMsg = e.message ? e.message : "";
}
ok("create empty data throws", emptyDataError);
ok("create empty data: has error message", emptyDataMsg.length > 0);
// ── duplicate @unique key → error ──
console.log("duplicate @unique key");
prisma.nativeCurrencyPrice.create({
data: {
id: "err-960001",
chainId: 960001,
currency: "ERR",
usdPrice: 10,
updatedAt: "2026-01-15T10:00:00.000Z"
}
});
var dupError = false;
try {
prisma.nativeCurrencyPrice.create({
data: {
id: "err-960001-dup",
chainId: 960001,
currency: "ERR-DUP",
usdPrice: 20,
updatedAt: "2026-01-15T10:00:00.000Z"
}
});
} catch (e: any) {
dupError = true;
ok("duplicate key: error message present", e.message && e.message.length > 0);
}
ok("duplicate @unique key throws", dupError);
// ── $executeRaw with invalid SQL → error ──
console.log("$executeRaw: invalid SQL");
var invalidSqlError = false;
try {
prisma.$executeRaw("THIS IS NOT VALID SQL AT ALL");
} catch (e: any) {
invalidSqlError = true;
ok("invalid SQL: error message present", e.message && e.message.length > 0);
}
ok("$executeRaw invalid SQL throws", invalidSqlError);
// ── findUniqueOrThrow for missing record → throws ──
console.log("findUniqueOrThrow: missing record");
var throwError = false;
var throwMsg = "";
try {
prisma.nativeCurrencyPrice.findUniqueOrThrow({ where: { chainId: 999666 } });
} catch (e: any) {
throwError = true;
throwMsg = e.message ? e.message : "";
}
ok("findUniqueOrThrow missing record throws", throwError);
ok("findUniqueOrThrow: message mentions model", throwMsg.indexOf("NativeCurrencyPrice") >= 0);
// ── findFirstOrThrow for missing record → throws ──
console.log("findFirstOrThrow: missing record");
var throwError2 = false;
try {
prisma.nativeCurrencyPrice.findFirstOrThrow({ where: { chainId: 999666 } });
} catch (e: any) {
throwError2 = true;
}
ok("findFirstOrThrow missing record throws", throwError2);
// ── update with where matching nothing → returns null ──
console.log("update: no match returns null");
var updateNull = prisma.nativeCurrencyPrice.update({
where: { chainId: 999666 },
data: { currency: "NOPE" }
});
isNull("update non-existent returns null", updateNull);
// Cleanup
prisma.$executeRaw("DELETE FROM NativeCurrencyPrice WHERE chainId >= 960001 AND chainId <= 960010");
prisma.$disconnect();
} catch (e: any) {
console.log("UNEXPECTED ERROR: " + e);
}
finish();