Merge pull request #1947 from github/nora/db-config-store-tests

Improve tests coverage in db-config-store
This commit is contained in:
Nora
2023-01-12 14:25:54 +01:00
committed by GitHub

View File

@@ -3,6 +3,7 @@ import { join } from "path";
import { App } from "../../../../src/common/app";
import {
DbConfig,
SelectedDbItem,
SelectedDbItemKind,
} from "../../../../src/databases/config/db-config";
import { DbConfigStore } from "../../../../src/databases/config/db-config-store";
@@ -184,6 +185,131 @@ describe("db config store", () => {
});
});
describe("db and list addition", () => {
let app: App;
let configPath: string;
beforeEach(async () => {
app = createMockApp({
extensionPath,
workspaceStoragePath: tempWorkspaceStoragePath,
});
configPath = join(tempWorkspaceStoragePath, "workspace-databases.json");
});
it("should add a remote repository", async () => {
// Initial set up
const dbConfig = createDbConfig();
const configStore = await initializeConfig(dbConfig, configPath, app);
// Add
await configStore.addRemoteRepo("repo1");
// Read the config file
const updatedDbConfig = (await readJSON(configPath)) as DbConfig;
// Check that the config file has been updated
const updatedRemoteDbs = updatedDbConfig.databases.remote;
expect(updatedRemoteDbs.repositories).toHaveLength(1);
expect(updatedRemoteDbs.repositories).toEqual(["repo1"]);
configStore.dispose();
});
it("should add a remote repository to the correct list", async () => {
// Initial set up
const dbConfig = createDbConfig({
remoteLists: [
{
name: "list1",
repositories: [],
},
],
});
const configStore = await initializeConfig(dbConfig, configPath, app);
// Add
await configStore.addRemoteRepo("repo1", "list1");
// Read the config file
const updatedDbConfig = (await readJSON(configPath)) as DbConfig;
// Check that the config file has been updated
const updatedRemoteDbs = updatedDbConfig.databases.remote;
expect(updatedRemoteDbs.repositories).toHaveLength(0);
expect(updatedRemoteDbs.repositoryLists).toHaveLength(1);
expect(updatedRemoteDbs.repositoryLists[0]).toEqual({
name: "list1",
repositories: ["repo1"],
});
configStore.dispose();
});
it("should add a remote owner", async () => {
// Initial set up
const dbConfig = createDbConfig();
const configStore = await initializeConfig(dbConfig, configPath, app);
// Add
await configStore.addRemoteOwner("owner1");
// Read the config file
const updatedDbConfig = (await readJSON(configPath)) as DbConfig;
// Check that the config file has been updated
const updatedRemoteDbs = updatedDbConfig.databases.remote;
expect(updatedRemoteDbs.owners).toHaveLength(1);
expect(updatedRemoteDbs.owners).toEqual(["owner1"]);
configStore.dispose();
});
it("should add a local list", async () => {
// Initial set up
const dbConfig = createDbConfig();
const configStore = await initializeConfig(dbConfig, configPath, app);
// Add
await configStore.addLocalList("list1");
// Read the config file
const updatedDbConfig = (await readJSON(configPath)) as DbConfig;
// Check that the config file has been updated
const updatedLocalDbs = updatedDbConfig.databases.local;
expect(updatedLocalDbs.lists).toHaveLength(1);
expect(updatedLocalDbs.lists[0].name).toEqual("list1");
configStore.dispose();
});
it("should add a remote list", async () => {
// Initial set up
const dbConfig = createDbConfig();
const configStore = await initializeConfig(dbConfig, configPath, app);
// Add
await configStore.addRemoteList("list1");
// Read the config file
const updatedDbConfig = (await readJSON(configPath)) as DbConfig;
// Check that the config file has been updated
const updatedRemoteDbs = updatedDbConfig.databases.remote;
expect(updatedRemoteDbs.repositoryLists).toHaveLength(1);
expect(updatedRemoteDbs.repositoryLists[0].name).toEqual("list1");
configStore.dispose();
});
});
describe("db and list renaming", () => {
let app: App;
let configPath: string;
@@ -213,10 +339,7 @@ describe("db config store", () => {
},
});
await writeJSON(configPath, dbConfig);
const configStore = new DbConfigStore(app, false);
await configStore.initialize();
const configStore = await initializeConfig(dbConfig, configPath, app);
// Rename
const currentDbItem = createRemoteUserDefinedListDbItem({
@@ -260,10 +383,7 @@ describe("db config store", () => {
},
});
await writeJSON(configPath, dbConfig);
const configStore = new DbConfigStore(app, false);
await configStore.initialize();
const configStore = await initializeConfig(dbConfig, configPath, app);
// Rename
const currentDbItem = createLocalListDbItem({
@@ -307,10 +427,7 @@ describe("db config store", () => {
},
});
await writeJSON(configPath, dbConfig);
const configStore = new DbConfigStore(app, false);
await configStore.initialize();
const configStore = await initializeConfig(dbConfig, configPath, app);
// Rename
const currentDbItem = createLocalDatabaseDbItem({
@@ -351,10 +468,7 @@ describe("db config store", () => {
],
});
await writeJSON(configPath, dbConfig);
const configStore = new DbConfigStore(app, false);
await configStore.initialize();
const configStore = await initializeConfig(dbConfig, configPath, app);
// Rename
const currentDbItem = createRemoteUserDefinedListDbItem({
@@ -391,10 +505,7 @@ describe("db config store", () => {
},
});
await writeJSON(configPath, dbConfig);
const configStore = new DbConfigStore(app, false);
await configStore.initialize();
const configStore = await initializeConfig(dbConfig, configPath, app);
// Remove
const currentDbItem = createRemoteOwnerDbItem({
@@ -430,10 +541,7 @@ describe("db config store", () => {
},
});
await writeJSON(configPath, dbConfig);
const configStore = new DbConfigStore(app, false);
await configStore.initialize();
const configStore = await initializeConfig(dbConfig, configPath, app);
// Remove
const currentDbItem = createRemoteUserDefinedListDbItem({
@@ -469,10 +577,7 @@ describe("db config store", () => {
},
});
await writeJSON(configPath, dbConfig);
const configStore = new DbConfigStore(app, false);
await configStore.initialize();
const configStore = await initializeConfig(dbConfig, configPath, app);
// Remove
const currentDbItem = createRemoteRepoDbItem({
@@ -496,4 +601,178 @@ describe("db config store", () => {
configStore.dispose();
});
});
describe("set selected item", () => {
let app: App;
let configPath: string;
beforeEach(async () => {
app = createMockApp({
extensionPath,
workspaceStoragePath: tempWorkspaceStoragePath,
});
configPath = join(tempWorkspaceStoragePath, "workspace-databases.json");
});
it("should set the selected item", async () => {
// Initial set up
const configStore = new DbConfigStore(app, false);
await configStore.initialize();
// Set selected
const selectedItem: SelectedDbItem = {
kind: SelectedDbItemKind.RemoteOwner,
ownerName: "owner2",
};
await configStore.setSelectedDbItem(selectedItem);
// Read the config file
const updatedDbConfig = (await readJSON(configPath)) as DbConfig;
// Check that the config file has been updated
expect(updatedDbConfig.selected).toEqual(selectedItem);
configStore.dispose();
});
});
describe("existence checks", () => {
let app: App;
let configPath: string;
beforeEach(async () => {
app = createMockApp({
extensionPath,
workspaceStoragePath: tempWorkspaceStoragePath,
});
configPath = join(tempWorkspaceStoragePath, "workspace-databases.json");
});
it("should return true if a remote owner exists", async () => {
// Initial set up
const dbConfig = createDbConfig({
remoteOwners: ["owner1", "owner2"],
});
const configStore = await initializeConfig(dbConfig, configPath, app);
// Check
const doesExist = await configStore.doesRemoteOwnerExist("owner1");
expect(doesExist).toEqual(true);
configStore.dispose();
});
it("should return true if a remote list exists", async () => {
// Initial set up
const dbConfig = createDbConfig({
remoteLists: [
{
name: "list1",
repositories: ["owner/repo1", "owner/repo2"],
},
],
});
const configStore = await initializeConfig(dbConfig, configPath, app);
// Check
const doesExist = await configStore.doesRemoteListExist("list1");
expect(doesExist).toEqual(true);
configStore.dispose();
});
it("should return true if a remote db exists", async () => {
// Initial set up
const dbConfig = createDbConfig({
remoteLists: [
{
name: "list1",
repositories: ["owner/repo1", "owner/repo2"],
},
],
});
const configStore = await initializeConfig(dbConfig, configPath, app);
// Check
const doesExist = await configStore.doesRemoteDbExist(
"owner/repo1",
"list1",
);
expect(doesExist).toEqual(true);
configStore.dispose();
});
it("should return true if a local db and local list exists", async () => {
// Initial set up
const dbConfig = createDbConfig({
localLists: [
{
name: "list1",
databases: [createLocalDbConfigItem({ name: "db1" })],
},
],
});
const configStore = await initializeConfig(dbConfig, configPath, app);
// Check
const doesDbExist = await configStore.doesLocalDbExist("db1", "list1");
expect(doesDbExist).toEqual(true);
const doesListExist = await configStore.doesLocalListExist("list1");
expect(doesListExist).toEqual(true);
configStore.dispose();
});
it("should return false if items do not exist", async () => {
// Initial set up
const dbConfig = createDbConfig({});
const configStore = await initializeConfig(dbConfig, configPath, app);
// Check
const doesLocalDbExist = await configStore.doesLocalDbExist(
"db1",
"list1",
);
expect(doesLocalDbExist).toEqual(false);
const doesLocalListExist = await configStore.doesLocalListExist("list1");
expect(doesLocalListExist).toEqual(false);
const doesRemoteDbExist = await configStore.doesRemoteDbExist(
"db1",
"list1",
);
expect(doesRemoteDbExist).toEqual(false);
const doesRemoteListExist = await configStore.doesRemoteListExist(
"list1",
);
expect(doesRemoteListExist).toEqual(false);
const doesRemoteOwnerExist = await configStore.doesRemoteOwnerExist(
"owner1",
);
expect(doesRemoteOwnerExist).toEqual(false);
configStore.dispose();
});
});
async function initializeConfig(
dbConfig: DbConfig,
configPath: string,
app: App,
): Promise<DbConfigStore> {
await writeJSON(configPath, dbConfig);
const configStore = new DbConfigStore(app, false);
await configStore.initialize();
return configStore;
}
});