Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/database/postgres/blobs.test.ts
1503 views
1
import getPool, { initEphemeralDatabase } from "@cocalc/database/pool";
2
import { uuid } from "@cocalc/util/misc";
3
import { sha1 } from "@cocalc/backend/misc_node";
4
import { db } from "@cocalc/database";
5
6
beforeAll(async () => {
7
await initEphemeralDatabase();
8
}, 15000);
9
10
afterAll(async () => {
11
await getPool().end();
12
});
13
14
describe("test archiving and unarchiving syncstrings with no edit history", () => {
15
const project_id = uuid();
16
const path = "a.txt";
17
const string_id = sha1(`${project_id}${path}`);
18
const path2 = "a2.txt";
19
const string_id2 = sha1(`${project_id}${path2}`);
20
const pool = getPool();
21
22
it("creates two syncstrings", async () => {
23
await pool.query(
24
"INSERT INTO syncstrings(string_id,project_id,path) VALUES($1,$2,$3)",
25
[string_id, project_id, path],
26
);
27
await pool.query(
28
"INSERT INTO syncstrings(string_id,project_id,path) VALUES($1,$2,$3)",
29
[string_id2, project_id, path2],
30
);
31
});
32
33
it("archives their history", async () => {
34
const d = db();
35
await d.archivePatches({ string_id });
36
await d.archivePatches({ string_id: string_id2 });
37
const { rows } = await pool.query(
38
"SELECT archived from syncstrings WHERE string_id=$1 OR string_id=$2",
39
[string_id, string_id2],
40
);
41
expect(rows[0].archived.length).toBe(36);
42
expect(rows[1].archived.length).toBe(36);
43
});
44
});
45
46
describe("test archiving and unarchiving two syncstrings with nontrivial but equal edit histories", () => {
47
const project_id = uuid();
48
const path = "a.txt";
49
const string_id = sha1(`${project_id}${path}`);
50
const path2 = "a2.txt";
51
const string_id2 = sha1(`${project_id}${path2}`);
52
const patch = "fake patch";
53
const time = new Date();
54
const pool = getPool();
55
56
it("creates two syncstrings", async () => {
57
await pool.query(
58
"INSERT INTO syncstrings(string_id,project_id,path) VALUES($1,$2,$3)",
59
[string_id, project_id, path],
60
);
61
await pool.query(
62
"INSERT INTO syncstrings(string_id,project_id,path) VALUES($1,$2,$3)",
63
[string_id2, project_id, path2],
64
);
65
await pool.query(
66
"INSERT INTO patches(string_id,time,patch,is_snapshot) VALUES($1,$2,$3,false)",
67
[string_id, time, patch],
68
);
69
await pool.query(
70
"INSERT INTO patches(string_id,time,patch,is_snapshot) VALUES($1,$2,$3,false)",
71
[string_id2, time, patch],
72
);
73
});
74
75
it("archives their history", async () => {
76
const d = db();
77
await d.archivePatches({ string_id });
78
await d.archivePatches({ string_id: string_id2 });
79
const { rows } = await pool.query(
80
"SELECT archived from syncstrings WHERE string_id=$1 OR string_id=$2",
81
[string_id, string_id2],
82
);
83
expect(rows[0].archived.length).toBe(36);
84
expect(rows[1].archived.length).toBe(36);
85
const { rows: rows2 } = await pool.query(
86
"SELECT count(*) AS count from patches where string_id=$1 OR string_id=$2",
87
[string_id, string_id2],
88
);
89
expect(rows2[0].count).toBe("0");
90
});
91
});
92
93