Skip to content

Commit 9110af8

Browse files
committed
Add test
1 parent ee056ce commit 9110af8

4 files changed

Lines changed: 257 additions & 33 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { DbItem, DbItemKind } from "./db-item";
2+
3+
export function getSelectedDbItem(dbItems: DbItem[]): DbItem | undefined {
4+
for (const dbItem of dbItems) {
5+
if (
6+
dbItem.kind === DbItemKind.RootRemote ||
7+
dbItem.kind === DbItemKind.RootLocal
8+
) {
9+
for (const child of dbItem.children) {
10+
switch (child.kind) {
11+
case DbItemKind.LocalList:
12+
if (child.selected) {
13+
return child;
14+
}
15+
for (const database of child.databases) {
16+
if (database.selected) {
17+
return database;
18+
}
19+
}
20+
break;
21+
case DbItemKind.RemoteUserDefinedList:
22+
if (child.selected) {
23+
return child;
24+
}
25+
for (const repo of child.repos) {
26+
if (repo.selected) {
27+
return repo;
28+
}
29+
}
30+
break;
31+
default:
32+
if (child.selected) {
33+
return child;
34+
}
35+
}
36+
}
37+
}
38+
}
39+
return undefined;
40+
}

extensions/ql-vscode/src/databases/db-item.ts

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -124,35 +124,3 @@ const SelectableDbItemKinds = [
124124
DbItemKind.RemoteOwner,
125125
DbItemKind.RemoteRepo,
126126
];
127-
128-
export function getSelectedDbItem(dbItems: DbItem[]): DbItem | undefined {
129-
for (const dbItem of dbItems) {
130-
if (
131-
dbItem.kind === DbItemKind.RootRemote ||
132-
dbItem.kind === DbItemKind.RootLocal
133-
) {
134-
for (const child of dbItem.children) {
135-
switch (child.kind) {
136-
case DbItemKind.LocalList:
137-
for (const database of child.databases) {
138-
if (database.selected) {
139-
return database;
140-
}
141-
}
142-
break;
143-
case DbItemKind.RemoteUserDefinedList:
144-
for (const repo of child.repos) {
145-
if (repo.selected) {
146-
return repo;
147-
}
148-
}
149-
break;
150-
default:
151-
if (child.selected) {
152-
return child;
153-
}
154-
}
155-
}
156-
}
157-
}
158-
}

extensions/ql-vscode/src/databases/db-manager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { App } from "../common/app";
22
import { AppEvent, AppEventEmitter } from "../common/events";
33
import { ValueResult } from "../common/value-result";
44
import { DbConfigStore } from "./config/db-config-store";
5-
import { DbItem, getSelectedDbItem } from "./db-item";
5+
import { DbItem } from "./db-item";
6+
import { getSelectedDbItem } from "./db-item-selection";
67
import { createLocalTree, createRemoteTree } from "./db-tree-creator";
78

89
export class DbManager {
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
import { DbItem, DbItemKind } from "../../../src/databases/db-item";
2+
import { getSelectedDbItem } from "../../../src/databases/db-item-selection";
3+
describe("db item selection", () => {
4+
it("return undefined if no item is selected", () => {
5+
const dbItems: DbItem[] = [
6+
{
7+
kind: DbItemKind.RootRemote,
8+
children: [
9+
{
10+
kind: DbItemKind.RemoteSystemDefinedList,
11+
listName: "top_10",
12+
listDisplayName: "Top 10 repositories",
13+
listDescription: "Top 10 repositories of a language",
14+
selected: false,
15+
},
16+
{
17+
kind: DbItemKind.RemoteSystemDefinedList,
18+
listName: "top_100",
19+
listDisplayName: "Top 100 repositories",
20+
listDescription: "Top 100 repositories of a language",
21+
selected: false,
22+
},
23+
{
24+
kind: DbItemKind.RemoteSystemDefinedList,
25+
listName: "top_1000",
26+
listDisplayName: "Top 1000 repositories",
27+
listDescription: "Top 1000 repositories of a language",
28+
selected: false,
29+
},
30+
{
31+
kind: DbItemKind.RemoteOwner,
32+
ownerName: "github",
33+
selected: false,
34+
},
35+
{
36+
kind: DbItemKind.RemoteUserDefinedList,
37+
listName: "my list",
38+
repos: [
39+
{
40+
kind: DbItemKind.RemoteRepo,
41+
repoFullName: "owner1/repo2",
42+
selected: false,
43+
},
44+
{
45+
kind: DbItemKind.RemoteRepo,
46+
repoFullName: "owner1/repo3",
47+
selected: false,
48+
},
49+
],
50+
selected: false,
51+
},
52+
],
53+
},
54+
{
55+
kind: DbItemKind.RootLocal,
56+
children: [
57+
{
58+
kind: DbItemKind.LocalList,
59+
listName: "list-1",
60+
databases: [
61+
{
62+
kind: DbItemKind.LocalDatabase,
63+
databaseName: "db1",
64+
dateAdded: 1234,
65+
language: "javascript",
66+
storagePath: "/foo/bar",
67+
selected: false,
68+
},
69+
{
70+
kind: DbItemKind.LocalDatabase,
71+
databaseName: "db2",
72+
dateAdded: 1234,
73+
language: "javascript",
74+
storagePath: "/foo/bar",
75+
selected: false,
76+
},
77+
],
78+
selected: false,
79+
},
80+
{
81+
kind: DbItemKind.LocalDatabase,
82+
databaseName: "db3",
83+
dateAdded: 1234,
84+
language: "javascript",
85+
storagePath: "/foo/bar",
86+
selected: false,
87+
},
88+
],
89+
},
90+
];
91+
92+
expect(getSelectedDbItem(dbItems)).toBeUndefined();
93+
});
94+
95+
it("return correct local database item from DbItem list", () => {
96+
const dbItems: DbItem[] = [
97+
{
98+
kind: DbItemKind.RootLocal,
99+
children: [
100+
{
101+
kind: DbItemKind.LocalList,
102+
listName: "list-1",
103+
databases: [
104+
{
105+
kind: DbItemKind.LocalDatabase,
106+
databaseName: "db1",
107+
dateAdded: 1234,
108+
language: "javascript",
109+
storagePath: "/foo/bar",
110+
selected: false,
111+
},
112+
{
113+
kind: DbItemKind.LocalDatabase,
114+
databaseName: "db2",
115+
dateAdded: 1234,
116+
language: "javascript",
117+
storagePath: "/foo/bar",
118+
selected: true,
119+
},
120+
],
121+
selected: false,
122+
},
123+
{
124+
kind: DbItemKind.LocalDatabase,
125+
databaseName: "db3",
126+
dateAdded: 1234,
127+
language: "javascript",
128+
storagePath: "/foo/bar",
129+
selected: false,
130+
},
131+
],
132+
},
133+
];
134+
135+
expect(getSelectedDbItem(dbItems)).toEqual({
136+
kind: DbItemKind.LocalDatabase,
137+
databaseName: "db2",
138+
dateAdded: 1234,
139+
language: "javascript",
140+
storagePath: "/foo/bar",
141+
selected: true,
142+
});
143+
});
144+
145+
it("return correct remote database list item from DbItem list", () => {
146+
const dbItems: DbItem[] = [
147+
{
148+
kind: DbItemKind.RootRemote,
149+
children: [
150+
{
151+
kind: DbItemKind.RemoteSystemDefinedList,
152+
listName: "top_10",
153+
listDisplayName: "Top 10 repositories",
154+
listDescription: "Top 10 repositories of a language",
155+
selected: false,
156+
},
157+
{
158+
kind: DbItemKind.RemoteSystemDefinedList,
159+
listName: "top_100",
160+
listDisplayName: "Top 100 repositories",
161+
listDescription: "Top 100 repositories of a language",
162+
selected: false,
163+
},
164+
{
165+
kind: DbItemKind.RemoteSystemDefinedList,
166+
listName: "top_1000",
167+
listDisplayName: "Top 1000 repositories",
168+
listDescription: "Top 1000 repositories of a language",
169+
selected: false,
170+
},
171+
{
172+
kind: DbItemKind.RemoteOwner,
173+
ownerName: "github",
174+
selected: false,
175+
},
176+
{
177+
kind: DbItemKind.RemoteUserDefinedList,
178+
listName: "my list",
179+
repos: [
180+
{
181+
kind: DbItemKind.RemoteRepo,
182+
repoFullName: "owner1/repo2",
183+
selected: false,
184+
},
185+
{
186+
kind: DbItemKind.RemoteRepo,
187+
repoFullName: "owner1/repo3",
188+
selected: false,
189+
},
190+
],
191+
selected: true,
192+
},
193+
],
194+
},
195+
];
196+
197+
expect(getSelectedDbItem(dbItems)).toEqual({
198+
kind: DbItemKind.RemoteUserDefinedList,
199+
listName: "my list",
200+
repos: [
201+
{
202+
kind: DbItemKind.RemoteRepo,
203+
repoFullName: "owner1/repo2",
204+
selected: false,
205+
},
206+
{
207+
kind: DbItemKind.RemoteRepo,
208+
repoFullName: "owner1/repo3",
209+
selected: false,
210+
},
211+
],
212+
selected: true,
213+
});
214+
});
215+
});

0 commit comments

Comments
 (0)