Skip to content

Commit 9cf5088

Browse files
shati-patelcharisk
andauthored
Add db manager tests for "rename" functions (github#1939)
Co-authored-by: Charis Kyriakou <charisk@users.noreply.github.com>
1 parent ed2bdd8 commit 9cf5088

File tree

1 file changed

+234
-0
lines changed

1 file changed

+234
-0
lines changed
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
import { ensureDir, readJSON, remove, writeJson } from "fs-extra";
2+
import { join } from "path";
3+
import { DbConfig } from "../../../src/databases/config/db-config";
4+
import { DbConfigStore } from "../../../src/databases/config/db-config-store";
5+
import {
6+
flattenDbItems,
7+
isLocalDatabaseDbItem,
8+
isLocalListDbItem,
9+
isRemoteUserDefinedListDbItem,
10+
LocalDatabaseDbItem,
11+
LocalListDbItem,
12+
RemoteUserDefinedListDbItem,
13+
} from "../../../src/databases/db-item";
14+
import { DbManager } from "../../../src/databases/db-manager";
15+
import {
16+
createDbConfig,
17+
createLocalDbConfigItem,
18+
} from "../../factories/db-config-factories";
19+
import { createMockApp } from "../../__mocks__/appMock";
20+
21+
// Note: Although these are "unit tests" (i.e. not integrating with VS Code), they do
22+
// test the interaction/"integration" between the DbManager and the DbConfigStore.
23+
describe("db manager", () => {
24+
let dbManager: DbManager;
25+
let dbConfigStore: DbConfigStore;
26+
let tempWorkspaceStoragePath: string;
27+
let dbConfigFilePath: string;
28+
29+
beforeEach(async () => {
30+
tempWorkspaceStoragePath = join(__dirname, "test-workspace");
31+
32+
const extensionPath = join(__dirname, "../../..");
33+
const app = createMockApp({
34+
extensionPath,
35+
workspaceStoragePath: tempWorkspaceStoragePath,
36+
});
37+
38+
dbConfigStore = new DbConfigStore(app);
39+
dbManager = new DbManager(app, dbConfigStore);
40+
await ensureDir(tempWorkspaceStoragePath);
41+
42+
dbConfigFilePath = join(
43+
tempWorkspaceStoragePath,
44+
"workspace-databases.json",
45+
);
46+
});
47+
48+
afterEach(async () => {
49+
await remove(tempWorkspaceStoragePath);
50+
dbConfigStore.dispose();
51+
});
52+
53+
describe("renaming items", () => {
54+
const remoteList = {
55+
name: "my-list-1",
56+
repositories: ["owner1/repo1", "owner1/repo2"],
57+
};
58+
const localDb = createLocalDbConfigItem({ name: "db1" });
59+
const localList = {
60+
name: "my-list-1",
61+
databases: [localDb],
62+
};
63+
64+
it("should rename remote db list", async () => {
65+
const dbConfig = createDbConfig({
66+
remoteLists: [remoteList],
67+
localLists: [localList],
68+
});
69+
70+
await saveDbConfig(dbConfig);
71+
72+
const dbItem = getRemoteUserDefinedListDbItem("my-list-1");
73+
74+
await dbManager.renameList(dbItem, "my-list-2");
75+
76+
const dbConfigFileContents = await readDbConfigDirectly();
77+
78+
// Check that the remote list has been renamed
79+
const remoteLists = dbConfigFileContents.databases.remote.repositoryLists;
80+
expect(remoteLists.length).toBe(1);
81+
expect(remoteLists[0]).toEqual({
82+
name: "my-list-2",
83+
repositories: ["owner1/repo1", "owner1/repo2"],
84+
});
85+
86+
// Check that the local list has not been renamed
87+
const localLists = dbConfigFileContents.databases.local.lists;
88+
expect(localLists.length).toBe(1);
89+
expect(localLists[0]).toEqual({
90+
name: "my-list-1",
91+
databases: [localDb],
92+
});
93+
});
94+
95+
it("should rename local db list", async () => {
96+
const dbConfig = createDbConfig({
97+
remoteLists: [remoteList],
98+
localLists: [localList],
99+
});
100+
101+
await saveDbConfig(dbConfig);
102+
103+
const dbItem = getLocalListDbItem("my-list-1");
104+
105+
await dbManager.renameList(dbItem, "my-list-2");
106+
107+
const dbConfigFileContents = await readDbConfigDirectly();
108+
109+
// Check that the local list has been renamed
110+
const localLists = dbConfigFileContents.databases.local.lists;
111+
expect(localLists.length).toBe(1);
112+
expect(localLists[0]).toEqual({
113+
name: "my-list-2",
114+
databases: [localDb],
115+
});
116+
117+
// Check that the remote list has not been renamed
118+
const remoteLists = dbConfigFileContents.databases.remote.repositoryLists;
119+
expect(remoteLists.length).toBe(1);
120+
expect(remoteLists[0]).toEqual({
121+
name: "my-list-1",
122+
repositories: ["owner1/repo1", "owner1/repo2"],
123+
});
124+
});
125+
126+
it("should rename local db outside a list", async () => {
127+
const dbConfig = createDbConfig({
128+
localLists: [localList],
129+
localDbs: [localDb],
130+
});
131+
132+
await saveDbConfig(dbConfig);
133+
134+
const dbItem = getLocalDatabaseDbItem("db1");
135+
136+
await dbManager.renameLocalDb(dbItem, "db2");
137+
138+
const dbConfigFileContents = await readDbConfigDirectly();
139+
140+
// Check that the db outside of the list has been renamed
141+
const localDbs = dbConfigFileContents.databases.local.databases;
142+
expect(localDbs.length).toBe(1);
143+
expect(localDbs[0].name).toEqual("db2");
144+
145+
// Check that the db inside the list has not been renamed
146+
const localLists = dbConfigFileContents.databases.local.lists;
147+
expect(localLists.length).toBe(1);
148+
expect(localLists[0]).toEqual({
149+
name: "my-list-1",
150+
databases: [localDb],
151+
});
152+
});
153+
154+
it("should rename local db inside a list", async () => {
155+
const dbConfig = createDbConfig({
156+
localLists: [localList],
157+
localDbs: [localDb],
158+
});
159+
160+
await saveDbConfig(dbConfig);
161+
162+
const dbItem = getLocalDatabaseDbItem("db1", "my-list-1");
163+
164+
await dbManager.renameLocalDb(dbItem, "db2");
165+
166+
const dbConfigFileContents = await readDbConfigDirectly();
167+
168+
// Check that the db inside the list has been renamed
169+
const localListDbs =
170+
dbConfigFileContents.databases.local.lists[0].databases;
171+
expect(localListDbs.length).toBe(1);
172+
expect(localListDbs[0].name).toEqual("db2");
173+
174+
// Check that the db outside of the list has not been renamed
175+
const localDbs = dbConfigFileContents.databases.local.databases;
176+
expect(localDbs.length).toBe(1);
177+
expect(localDbs[0]).toEqual(localDb);
178+
});
179+
});
180+
181+
async function saveDbConfig(dbConfig: DbConfig): Promise<void> {
182+
await writeJson(dbConfigFilePath, dbConfig);
183+
184+
// Ideally we would just initialize the db config store at the start
185+
// of each test and then rely on the file watcher to update the config.
186+
// However, this requires adding sleep to the tests to allow for the
187+
// file watcher to catch up, so we instead initialize the config store here.
188+
await dbConfigStore.initialize();
189+
}
190+
191+
async function readDbConfigDirectly(): Promise<DbConfig> {
192+
return (await readJSON(dbConfigFilePath)) as DbConfig;
193+
}
194+
195+
function getLocalListDbItem(listName: string): LocalListDbItem {
196+
const dbItemsResult = dbManager.getDbItems();
197+
const dbItems = flattenDbItems(dbItemsResult.value);
198+
const listDbItems = dbItems
199+
.filter(isLocalListDbItem)
200+
.filter((i) => i.listName === listName);
201+
202+
expect(listDbItems.length).toEqual(1);
203+
return listDbItems[0];
204+
}
205+
206+
function getLocalDatabaseDbItem(
207+
dbName: string,
208+
parentListName?: string,
209+
): LocalDatabaseDbItem {
210+
const dbItemsResult = dbManager.getDbItems();
211+
const dbItems = flattenDbItems(dbItemsResult.value);
212+
const localDbItems = dbItems
213+
.filter(isLocalDatabaseDbItem)
214+
.filter(
215+
(i) => i.databaseName === dbName && i.parentListName === parentListName,
216+
);
217+
218+
expect(localDbItems.length).toEqual(1);
219+
return localDbItems[0];
220+
}
221+
222+
function getRemoteUserDefinedListDbItem(
223+
listName: string,
224+
): RemoteUserDefinedListDbItem {
225+
const dbItemsResult = dbManager.getDbItems();
226+
const dbItems = flattenDbItems(dbItemsResult.value);
227+
const listDbItems = dbItems
228+
.filter(isRemoteUserDefinedListDbItem)
229+
.filter((i) => i.listName === listName);
230+
231+
expect(listDbItems.length).toEqual(1);
232+
return listDbItems[0];
233+
}
234+
});

0 commit comments

Comments
 (0)