-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathpush.programmatic.tables.test.ts
More file actions
109 lines (100 loc) · 3.24 KB
/
push.programmatic.tables.test.ts
File metadata and controls
109 lines (100 loc) · 3.24 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
import { describe, expect, test, mock } from "bun:test";
describe("Push.pushTables programmatic mode", () => {
test("uses injected project client for table columns and indexes", async () => {
const projectClient = { _tag: "project-client" } as any;
const consoleClient = { _tag: "console-client" } as any;
const databaseServiceClientArgs: any[] = [];
const tableServiceClientArgs: any[] = [];
mock.module("../lib/services.js", () => {
const databasesService = {
createStringAttribute: async () => ({}),
createIndex: async () => ({}),
listAttributes: async () => ({
total: 1,
attributes: [{ key: "title", status: "available" }],
}),
listIndexes: async () => ({
total: 1,
indexes: [{ key: "idx_title", status: "available" }],
}),
};
const tablesService = {
getTable: async () => {
throw { code: 404 };
},
createTable: async () => ({}),
};
const throwIfNoProjectClient = (sdk?: any) => {
if (!sdk) {
throw new Error(
"Project is not set. Please run `appwrite init project`.",
);
}
};
return {
getProxyService: async () => ({}),
getConsoleService: async () => ({}),
getFunctionsService: async () => ({}),
getSitesService: async () => ({}),
getStorageService: async () => ({}),
getMessagingService: async () => ({}),
getOrganizationsService: async () => ({}),
getTeamsService: async () => ({}),
getProjectsService: async () => ({}),
getDatabasesService: async (sdk?: any) => {
databaseServiceClientArgs.push(sdk);
throwIfNoProjectClient(sdk);
return databasesService as any;
},
getTablesDBService: async (sdk?: any) => {
tableServiceClientArgs.push(sdk);
throwIfNoProjectClient(sdk);
return tablesService as any;
},
};
});
const { Push } = await import("../lib/commands/push.ts");
const push = new Push(projectClient, consoleClient, true);
const result = await push.pushTables(
[
{
$id: "tbl_1",
databaseId: "db_1",
name: "Regression Table",
rowSecurity: true,
enabled: true,
columns: [
{
key: "title",
type: "string",
size: 255,
required: true,
default: null,
array: false,
encrypt: false,
},
],
indexes: [
{
key: "idx_title",
type: "key",
columns: ["title"],
orders: ["ASC"],
},
],
},
],
{ attempts: 1, skipConfirmation: true },
);
expect(result.errors).toHaveLength(0);
expect(result.successfullyPushed).toBe(1);
expect(databaseServiceClientArgs.length).toBeGreaterThan(0);
expect(
databaseServiceClientArgs.every((sdk) => sdk === projectClient),
).toBe(true);
expect(tableServiceClientArgs.length).toBeGreaterThan(0);
expect(tableServiceClientArgs.every((sdk) => sdk === projectClient)).toBe(
true,
);
});
});