Convert integration tests to unit tests (#1986)
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
||||
} from "../../../src/databases/config/db-config";
|
||||
import { DbConfigStore } from "../../../src/databases/config/db-config-store";
|
||||
import {
|
||||
DbListKind,
|
||||
flattenDbItems,
|
||||
isLocalDatabaseDbItem,
|
||||
isLocalListDbItem,
|
||||
@@ -67,6 +68,189 @@ describe("db manager", () => {
|
||||
dbConfigStore.dispose();
|
||||
});
|
||||
|
||||
describe("adding items", () => {
|
||||
describe("adding a remote repo", () => {
|
||||
it("should add a new remote repo", async () => {
|
||||
const dbConfig: DbConfig = createDbConfig({
|
||||
remoteRepos: ["owner1/repo1"],
|
||||
});
|
||||
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
await dbManager.addNewRemoteRepo("owner2/repo2");
|
||||
|
||||
const dbConfigFileContents = await readDbConfigDirectly();
|
||||
expect(
|
||||
dbConfigFileContents.databases.variantAnalysis.repositories.length,
|
||||
).toBe(2);
|
||||
expect(
|
||||
dbConfigFileContents.databases.variantAnalysis.repositories[1],
|
||||
).toEqual("owner2/repo2");
|
||||
});
|
||||
|
||||
it("should add a new remote repo to a user defined list", async () => {
|
||||
const dbConfig: DbConfig = createDbConfig({
|
||||
remoteLists: [
|
||||
{
|
||||
name: "my-list-1",
|
||||
repositories: ["owner1/repo1"],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
await dbManager.addNewRemoteRepo("owner2/repo2", "my-list-1");
|
||||
|
||||
const dbConfigFileContents = await readDbConfigDirectly();
|
||||
expect(
|
||||
dbConfigFileContents.databases.variantAnalysis.repositoryLists.length,
|
||||
).toBe(1);
|
||||
|
||||
expect(
|
||||
dbConfigFileContents.databases.variantAnalysis.repositoryLists[0],
|
||||
).toEqual({
|
||||
name: "my-list-1",
|
||||
repositories: ["owner1/repo1", "owner2/repo2"],
|
||||
});
|
||||
});
|
||||
|
||||
it("should not allow adding a new remote db with empty name", async () => {
|
||||
const dbConfig = createDbConfig();
|
||||
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
await expect(dbManager.addNewRemoteRepo("")).rejects.toThrow(
|
||||
new Error("Repository name cannot be empty"),
|
||||
);
|
||||
});
|
||||
|
||||
it("should not allow adding a remote db with duplicate name", async () => {
|
||||
const dbConfig = createDbConfig({
|
||||
remoteRepos: ["owner1/repo1"],
|
||||
});
|
||||
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
await expect(
|
||||
dbManager.addNewRemoteRepo("owner1/repo1"),
|
||||
).rejects.toThrow(
|
||||
new Error(
|
||||
"A variant analysis repository with the name 'owner1/repo1' already exists",
|
||||
),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("adding a list", () => {
|
||||
it("should add a new remote list", async () => {
|
||||
const dbConfig: DbConfig = createDbConfig({
|
||||
remoteLists: [
|
||||
{
|
||||
name: "my-list-1",
|
||||
repositories: ["owner1/repo1", "owner1/repo2"],
|
||||
},
|
||||
],
|
||||
selected: {
|
||||
kind: SelectedDbItemKind.VariantAnalysisUserDefinedList,
|
||||
listName: "my-list-1",
|
||||
},
|
||||
});
|
||||
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
await dbManager.addNewList(DbListKind.Remote, "my-list-2");
|
||||
|
||||
const dbConfigFileContents = await readDbConfigDirectly();
|
||||
expect(
|
||||
dbConfigFileContents.databases.variantAnalysis.repositoryLists.length,
|
||||
).toBe(2);
|
||||
expect(
|
||||
dbConfigFileContents.databases.variantAnalysis.repositoryLists[1],
|
||||
).toEqual({
|
||||
name: "my-list-2",
|
||||
repositories: [],
|
||||
});
|
||||
});
|
||||
|
||||
it("should throw error when adding a new list to a local node", async () => {
|
||||
const dbConfig: DbConfig = createDbConfig({
|
||||
localLists: [
|
||||
{
|
||||
name: "my-list-1",
|
||||
databases: [],
|
||||
},
|
||||
],
|
||||
});
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
await dbManager.addNewList(DbListKind.Local, "my-list-2");
|
||||
|
||||
const dbConfigFileContents = await readDbConfigDirectly();
|
||||
expect(dbConfigFileContents.databases.local.lists.length).toBe(2);
|
||||
expect(dbConfigFileContents.databases.local.lists[1]).toEqual({
|
||||
name: "my-list-2",
|
||||
databases: [],
|
||||
});
|
||||
});
|
||||
|
||||
it("should not allow adding a new list with empty name", async () => {
|
||||
const dbConfig = createDbConfig();
|
||||
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
await expect(
|
||||
dbManager.addNewList(DbListKind.Remote, ""),
|
||||
).rejects.toThrow(new Error("List name cannot be empty"));
|
||||
});
|
||||
|
||||
it("should not allow adding a list with duplicate name", async () => {
|
||||
const dbConfig = createDbConfig({
|
||||
remoteLists: [
|
||||
{
|
||||
name: "my-list-1",
|
||||
repositories: ["owner1/repo1", "owner1/repo2"],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
await expect(
|
||||
dbManager.addNewList(DbListKind.Remote, "my-list-1"),
|
||||
).rejects.toThrow(
|
||||
new Error(
|
||||
"A variant analysis list with the name 'my-list-1' already exists",
|
||||
),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("adding an owner", () => {
|
||||
it("should not allow adding a new remote owner with empty name", async () => {
|
||||
const dbConfig = createDbConfig();
|
||||
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
await expect(dbManager.addNewRemoteOwner("")).rejects.toThrow(
|
||||
new Error("Owner name cannot be empty"),
|
||||
);
|
||||
});
|
||||
|
||||
it("should not allow adding a remote owner with duplicate name", async () => {
|
||||
const dbConfig = createDbConfig({
|
||||
remoteOwners: ["owner1"],
|
||||
});
|
||||
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
await expect(dbManager.addNewRemoteOwner("owner1")).rejects.toThrow(
|
||||
new Error("An owner with the name 'owner1' already exists"),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("renaming items", () => {
|
||||
const remoteList = {
|
||||
name: "my-list-1",
|
||||
|
||||
@@ -5,7 +5,6 @@ import { DbConfig } from "../../../../src/databases/config/db-config";
|
||||
import { DbManager } from "../../../../src/databases/db-manager";
|
||||
import { DbConfigStore } from "../../../../src/databases/config/db-config-store";
|
||||
import { DbTreeDataProvider } from "../../../../src/databases/ui/db-tree-data-provider";
|
||||
import { DbListKind } from "../../../../src/databases/db-item";
|
||||
import { DbTreeViewItem } from "../../../../src/databases/ui/db-tree-view-item";
|
||||
import { ExtensionApp } from "../../../../src/common/vscode/vscode-app";
|
||||
import { createMockExtensionContext } from "../../../factories/extension-context";
|
||||
@@ -104,85 +103,6 @@ describe("db panel", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("name validation", () => {
|
||||
it("should not allow adding a new list with empty name", async () => {
|
||||
const dbConfig = createDbConfig();
|
||||
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
await expect(dbManager.addNewList(DbListKind.Remote, "")).rejects.toThrow(
|
||||
new Error("List name cannot be empty"),
|
||||
);
|
||||
});
|
||||
|
||||
it("should not allow adding a list with duplicate name", async () => {
|
||||
const dbConfig = createDbConfig({
|
||||
remoteLists: [
|
||||
{
|
||||
name: "my-list-1",
|
||||
repositories: ["owner1/repo1", "owner1/repo2"],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
await expect(
|
||||
dbManager.addNewList(DbListKind.Remote, "my-list-1"),
|
||||
).rejects.toThrow(
|
||||
new Error(
|
||||
"A variant analysis list with the name 'my-list-1' already exists",
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it("should not allow adding a new remote db with empty name", async () => {
|
||||
const dbConfig = createDbConfig();
|
||||
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
await expect(dbManager.addNewRemoteRepo("")).rejects.toThrow(
|
||||
new Error("Repository name cannot be empty"),
|
||||
);
|
||||
});
|
||||
|
||||
it("should not allow adding a remote db with duplicate name", async () => {
|
||||
const dbConfig = createDbConfig({
|
||||
remoteRepos: ["owner1/repo1"],
|
||||
});
|
||||
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
await expect(dbManager.addNewRemoteRepo("owner1/repo1")).rejects.toThrow(
|
||||
new Error(
|
||||
"A variant analysis repository with the name 'owner1/repo1' already exists",
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it("should not allow adding a new remote owner with empty name", async () => {
|
||||
const dbConfig = createDbConfig();
|
||||
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
await expect(dbManager.addNewRemoteOwner("")).rejects.toThrow(
|
||||
new Error("Owner name cannot be empty"),
|
||||
);
|
||||
});
|
||||
|
||||
it("should not allow adding a remote owner with duplicate name", async () => {
|
||||
const dbConfig = createDbConfig({
|
||||
remoteOwners: ["owner1"],
|
||||
});
|
||||
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
await expect(dbManager.addNewRemoteOwner("owner1")).rejects.toThrow(
|
||||
new Error("An owner with the name 'owner1' already exists"),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
async function saveDbConfig(dbConfig: DbConfig): Promise<void> {
|
||||
await writeJson(dbConfigFilePath, dbConfig);
|
||||
|
||||
|
||||
@@ -1,241 +0,0 @@
|
||||
import { join } from "path";
|
||||
import { ensureDir, readJSON, remove, writeJson } from "fs-extra";
|
||||
import {
|
||||
DbConfig,
|
||||
SelectedDbItemKind,
|
||||
} from "../../../../src/databases/config/db-config";
|
||||
import { DbManager } from "../../../../src/databases/db-manager";
|
||||
import { DbConfigStore } from "../../../../src/databases/config/db-config-store";
|
||||
import { DbTreeDataProvider } from "../../../../src/databases/ui/db-tree-data-provider";
|
||||
import { DbItemKind, DbListKind } from "../../../../src/databases/db-item";
|
||||
import { ExtensionApp } from "../../../../src/common/vscode/vscode-app";
|
||||
import { createMockExtensionContext } from "../../../factories/extension-context";
|
||||
import { createDbConfig } from "../../../factories/db-config-factories";
|
||||
|
||||
describe("db panel add items", () => {
|
||||
const workspaceStoragePath = join(__dirname, "test-workspace-storage");
|
||||
const globalStoragePath = join(__dirname, "test-global-storage");
|
||||
const extensionPath = join(__dirname, "../../../../");
|
||||
const dbConfigFilePath = join(
|
||||
workspaceStoragePath,
|
||||
DbConfigStore.databaseConfigFileName,
|
||||
);
|
||||
let dbTreeDataProvider: DbTreeDataProvider;
|
||||
let dbManager: DbManager;
|
||||
let dbConfigStore: DbConfigStore;
|
||||
|
||||
beforeAll(async () => {
|
||||
const extensionContext = createMockExtensionContext({
|
||||
extensionPath,
|
||||
globalStoragePath,
|
||||
workspaceStoragePath,
|
||||
});
|
||||
await ensureDir(workspaceStoragePath);
|
||||
|
||||
const app = new ExtensionApp(extensionContext);
|
||||
|
||||
dbConfigStore = new DbConfigStore(app, false);
|
||||
dbManager = new DbManager(app, dbConfigStore);
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await ensureDir(workspaceStoragePath);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await remove(workspaceStoragePath);
|
||||
});
|
||||
|
||||
describe("addNewRemoteRepo", () => {
|
||||
it("should add a new remote repo", async () => {
|
||||
const dbConfig: DbConfig = createDbConfig({
|
||||
remoteRepos: ["owner1/repo1"],
|
||||
});
|
||||
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
const dbTreeItems = await dbTreeDataProvider.getChildren();
|
||||
|
||||
expect(dbTreeItems).toBeTruthy();
|
||||
const items = dbTreeItems!;
|
||||
|
||||
const remoteRootNode = items[0];
|
||||
const remoteRepos = remoteRootNode.children.filter(
|
||||
(c) => c.dbItem?.kind === DbItemKind.RemoteRepo,
|
||||
);
|
||||
const repo1 = remoteRootNode.children.find(
|
||||
(c) =>
|
||||
c.dbItem?.kind === DbItemKind.RemoteRepo &&
|
||||
c.dbItem?.repoFullName === "owner1/repo1",
|
||||
);
|
||||
|
||||
expect(remoteRepos.length).toBe(1);
|
||||
expect(remoteRepos[0]).toBe(repo1);
|
||||
|
||||
await dbManager.addNewRemoteRepo("owner2/repo2");
|
||||
|
||||
const dbConfigFileContents = await readDbConfigDirectly();
|
||||
expect(
|
||||
dbConfigFileContents.databases.variantAnalysis.repositories.length,
|
||||
).toBe(2);
|
||||
expect(
|
||||
dbConfigFileContents.databases.variantAnalysis.repositories[1],
|
||||
).toEqual("owner2/repo2");
|
||||
});
|
||||
|
||||
it("should add a new remote repo to a user defined list", async () => {
|
||||
const dbConfig: DbConfig = createDbConfig({
|
||||
remoteLists: [
|
||||
{
|
||||
name: "my-list-1",
|
||||
repositories: ["owner1/repo1"],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
const dbTreeItems = await dbTreeDataProvider.getChildren();
|
||||
|
||||
expect(dbTreeItems).toBeTruthy();
|
||||
const items = dbTreeItems!;
|
||||
|
||||
const remoteRootNode = items[0];
|
||||
const remoteUserDefinedLists = remoteRootNode.children.filter(
|
||||
(c) => c.dbItem?.kind === DbItemKind.VariantAnalysisUserDefinedList,
|
||||
);
|
||||
const list1 = remoteRootNode.children.find(
|
||||
(c) =>
|
||||
c.dbItem?.kind === DbItemKind.VariantAnalysisUserDefinedList &&
|
||||
c.dbItem?.listName === "my-list-1",
|
||||
);
|
||||
|
||||
expect(remoteUserDefinedLists.length).toBe(1);
|
||||
expect(remoteUserDefinedLists[0]).toBe(list1);
|
||||
|
||||
await dbManager.addNewRemoteRepo("owner2/repo2", "my-list-1");
|
||||
|
||||
// Read the workspace databases JSON file directly to check that the new repo has been added.
|
||||
// We can't use the dbConfigStore's `read` function here because it depends on the file watcher
|
||||
// picking up changes, and we don't control the timing of that.
|
||||
const dbConfigFileContents = await readJSON(dbConfigFilePath);
|
||||
expect(
|
||||
dbConfigFileContents.databases.variantAnalysis.repositoryLists.length,
|
||||
).toBe(1);
|
||||
|
||||
expect(
|
||||
dbConfigFileContents.databases.variantAnalysis.repositoryLists[0],
|
||||
).toEqual({
|
||||
name: "my-list-1",
|
||||
repositories: ["owner1/repo1", "owner2/repo2"],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("addNewList", () => {
|
||||
it("should add a new remote list", async () => {
|
||||
const dbConfig: DbConfig = createDbConfig({
|
||||
remoteLists: [
|
||||
{
|
||||
name: "my-list-1",
|
||||
repositories: ["owner1/repo1", "owner1/repo2"],
|
||||
},
|
||||
],
|
||||
selected: {
|
||||
kind: SelectedDbItemKind.VariantAnalysisUserDefinedList,
|
||||
listName: "my-list-1",
|
||||
},
|
||||
});
|
||||
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
const dbTreeItems = await dbTreeDataProvider.getChildren();
|
||||
|
||||
expect(dbTreeItems).toBeTruthy();
|
||||
const items = dbTreeItems!;
|
||||
|
||||
const remoteRootNode = items[0];
|
||||
const remoteUserDefinedLists = remoteRootNode.children.filter(
|
||||
(c) => c.dbItem?.kind === DbItemKind.VariantAnalysisUserDefinedList,
|
||||
);
|
||||
const list1 = remoteRootNode.children.find(
|
||||
(c) =>
|
||||
c.dbItem?.kind === DbItemKind.VariantAnalysisUserDefinedList &&
|
||||
c.dbItem?.listName === "my-list-1",
|
||||
);
|
||||
|
||||
expect(remoteUserDefinedLists.length).toBe(1);
|
||||
expect(remoteUserDefinedLists[0]).toBe(list1);
|
||||
|
||||
await dbManager.addNewList(DbListKind.Remote, "my-list-2");
|
||||
|
||||
const dbConfigFileContents = await readDbConfigDirectly();
|
||||
expect(
|
||||
dbConfigFileContents.databases.variantAnalysis.repositoryLists.length,
|
||||
).toBe(2);
|
||||
expect(
|
||||
dbConfigFileContents.databases.variantAnalysis.repositoryLists[1],
|
||||
).toEqual({
|
||||
name: "my-list-2",
|
||||
repositories: [],
|
||||
});
|
||||
});
|
||||
|
||||
it("should throw error when adding a new list to a local node", async () => {
|
||||
const dbConfig: DbConfig = createDbConfig({
|
||||
localLists: [
|
||||
{
|
||||
name: "my-list-1",
|
||||
databases: [],
|
||||
},
|
||||
],
|
||||
});
|
||||
await saveDbConfig(dbConfig);
|
||||
|
||||
const dbTreeItems = await dbTreeDataProvider.getChildren();
|
||||
|
||||
expect(dbTreeItems).toBeTruthy();
|
||||
const items = dbTreeItems!;
|
||||
|
||||
const localRootNode = items[1];
|
||||
const localUserDefinedLists = localRootNode.children.filter(
|
||||
(c) => c.dbItem?.kind === DbItemKind.LocalList,
|
||||
);
|
||||
const list1 = localRootNode.children.find(
|
||||
(c) =>
|
||||
c.dbItem?.kind === DbItemKind.LocalList &&
|
||||
c.dbItem?.listName === "my-list-1",
|
||||
);
|
||||
|
||||
expect(localUserDefinedLists.length).toBe(1);
|
||||
expect(localUserDefinedLists[0]).toBe(list1);
|
||||
|
||||
await dbManager.addNewList(DbListKind.Local, "my-list-2");
|
||||
|
||||
const dbConfigFileContents = await readDbConfigDirectly();
|
||||
expect(dbConfigFileContents.databases.local.lists.length).toBe(2);
|
||||
expect(dbConfigFileContents.databases.local.lists[1]).toEqual({
|
||||
name: "my-list-2",
|
||||
databases: [],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
async function saveDbConfig(dbConfig: DbConfig): Promise<void> {
|
||||
await writeJson(dbConfigFilePath, dbConfig);
|
||||
|
||||
// Ideally we would just initialise the db config store at the start
|
||||
// of each test and then rely on the file watcher to update the config.
|
||||
// However, this requires adding sleep to the tests to allow for the
|
||||
// file watcher to catch up, so we instead initialise the config store here.
|
||||
await dbConfigStore.initialize();
|
||||
dbTreeDataProvider = new DbTreeDataProvider(dbManager);
|
||||
}
|
||||
|
||||
async function readDbConfigDirectly(): Promise<DbConfig> {
|
||||
// Read the workspace databases JSON file directly to check that the new list has been added.
|
||||
// We can't use the dbConfigStore's `read` function here because it depends on the file watcher
|
||||
// picking up changes, and we don't control the timing of that.
|
||||
return (await readJSON(dbConfigFilePath)) as DbConfig;
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user