-
Notifications
You must be signed in to change notification settings - Fork 18
/
testingFunctions.ts
56 lines (51 loc) · 1.66 KB
/
testingFunctions.ts
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
import {
customAction,
customMutation,
customQuery,
} from "convex-helpers/server/customFunctions";
import { action, mutation, query } from "./_generated/server";
import schema from "./schema";
// Wrappers to use for function that should only be called from tests
export const testingQuery = customQuery(query, {
args: {},
input: async (_ctx, _args) => {
if (process.env.IS_TEST === undefined) {
throw new Error(
"Calling a test only function in an unexpected environment"
);
}
return { ctx: {}, args: {} };
},
});
export const testingMutation = customMutation(mutation, {
args: {},
input: async (_ctx, _args) => {
if (process.env.IS_TEST === undefined) {
throw new Error(
"Calling a test only function in an unexpected environment"
);
}
return { ctx: {}, args: {} };
},
});
export const testingAction = customAction(action, {
args: {},
input: async (_ctx, _args) => {
if (process.env.IS_TEST === undefined) {
throw new Error(
"Calling a test only function in an unexpected environment"
);
}
return { ctx: {}, args: {} };
},
});
export const clearAll = testingMutation(async ({ db, scheduler, storage }) => {
for (const table of Object.keys(schema.tables)) {
const docs = await db.query(table as any).collect();
await Promise.all(docs.map((doc) => db.delete(doc._id)));
}
const scheduled = await db.system.query("_scheduled_functions").collect();
await Promise.all(scheduled.map((s) => scheduler.cancel(s._id)));
const storedFiles = await db.system.query("_storage").collect();
await Promise.all(storedFiles.map((s) => storage.delete(s._id)));
});