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