-
Notifications
You must be signed in to change notification settings - Fork 226
Create method that returns selected DbItem #1830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ee056ce
Create method to expose selected DbItem
norascheuch 9110af8
Add test
norascheuch 60b1f14
Rename and reduce test lines
norascheuch 9362881
Rename method on db Manager
norascheuch 7a2f976
Refactor selection method to allow all types of DbItem lists
norascheuch 1050968
Update extensions/ql-vscode/test/pure-tests/databases/db-item-selecti…
norascheuch 425befa
Add tests
norascheuch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { DbItem, DbItemKind, LocalDbItem, RemoteDbItem } from "./db-item"; | ||
|
|
||
| export function getSelectedDbItem(dbItems: DbItem[]): DbItem | undefined { | ||
| for (const dbItem of dbItems) { | ||
| if ( | ||
| dbItem.kind === DbItemKind.RootRemote || | ||
| dbItem.kind === DbItemKind.RootLocal | ||
| ) { | ||
| for (const child of dbItem.children) { | ||
| const selectedItem = extractSelected(child); | ||
| if (selectedItem) return selectedItem; | ||
| } | ||
| } else { | ||
| const selectedItem = extractSelected(dbItem); | ||
| if (selectedItem) return selectedItem; | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| function extractSelected( | ||
| dbItem: RemoteDbItem | LocalDbItem, | ||
| ): DbItem | undefined { | ||
| if (dbItem.selected) { | ||
| return dbItem; | ||
| } | ||
| switch (dbItem.kind) { | ||
| case DbItemKind.LocalList: | ||
| for (const database of dbItem.databases) { | ||
| if (database.selected) { | ||
| return database; | ||
| } | ||
| } | ||
| break; | ||
| case DbItemKind.RemoteUserDefinedList: | ||
| for (const repo of dbItem.repos) { | ||
| if (repo.selected) { | ||
| return repo; | ||
| } | ||
| } | ||
| break; | ||
| } | ||
| return undefined; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
278 changes: 278 additions & 0 deletions
278
extensions/ql-vscode/test/pure-tests/databases/db-item-selection.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,278 @@ | ||
| import { DbItem, DbItemKind } from "../../../src/databases/db-item"; | ||
| import { getSelectedDbItem } from "../../../src/databases/db-item-selection"; | ||
| describe("db item selection", () => { | ||
| it("should return undefined if no item is selected", () => { | ||
| const dbItems: DbItem[] = [ | ||
| { | ||
| kind: DbItemKind.RootRemote, | ||
| children: [ | ||
| { | ||
|
charisk marked this conversation as resolved.
|
||
| kind: DbItemKind.RemoteSystemDefinedList, | ||
| listName: "top_10", | ||
| listDisplayName: "Top 10 repositories", | ||
| listDescription: "Top 10 repositories of a language", | ||
| selected: false, | ||
| }, | ||
| { | ||
| kind: DbItemKind.RemoteSystemDefinedList, | ||
| listName: "top_100", | ||
| listDisplayName: "Top 100 repositories", | ||
| listDescription: "Top 100 repositories of a language", | ||
| selected: false, | ||
| }, | ||
| { | ||
| kind: DbItemKind.RemoteOwner, | ||
| ownerName: "github", | ||
| selected: false, | ||
| }, | ||
| { | ||
| kind: DbItemKind.RemoteUserDefinedList, | ||
| listName: "my list", | ||
| repos: [ | ||
| { | ||
| kind: DbItemKind.RemoteRepo, | ||
| repoFullName: "owner1/repo2", | ||
| selected: false, | ||
| }, | ||
| { | ||
| kind: DbItemKind.RemoteRepo, | ||
| repoFullName: "owner1/repo3", | ||
| selected: false, | ||
| }, | ||
| ], | ||
| selected: false, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| kind: DbItemKind.RootLocal, | ||
| children: [ | ||
| { | ||
| kind: DbItemKind.LocalList, | ||
| listName: "list-1", | ||
| databases: [ | ||
| { | ||
| kind: DbItemKind.LocalDatabase, | ||
| databaseName: "db1", | ||
| dateAdded: 1234, | ||
| language: "javascript", | ||
| storagePath: "/foo/bar", | ||
| selected: false, | ||
| }, | ||
| { | ||
| kind: DbItemKind.LocalDatabase, | ||
| databaseName: "db2", | ||
| dateAdded: 1234, | ||
| language: "javascript", | ||
| storagePath: "/foo/bar", | ||
| selected: false, | ||
| }, | ||
| ], | ||
| selected: false, | ||
| }, | ||
| { | ||
| kind: DbItemKind.LocalDatabase, | ||
| databaseName: "db3", | ||
| dateAdded: 1234, | ||
| language: "javascript", | ||
| storagePath: "/foo/bar", | ||
| selected: false, | ||
| }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| expect(getSelectedDbItem(dbItems)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("should return correct local database item from DbItem list", () => { | ||
| const dbItems: DbItem[] = [ | ||
| { | ||
| kind: DbItemKind.RootLocal, | ||
| children: [ | ||
| { | ||
| kind: DbItemKind.LocalList, | ||
| listName: "list-1", | ||
| databases: [ | ||
| { | ||
| kind: DbItemKind.LocalDatabase, | ||
| databaseName: "db1", | ||
| dateAdded: 1234, | ||
| language: "javascript", | ||
| storagePath: "/foo/bar", | ||
| selected: false, | ||
| }, | ||
| { | ||
| kind: DbItemKind.LocalDatabase, | ||
| databaseName: "db2", | ||
| dateAdded: 1234, | ||
| language: "javascript", | ||
| storagePath: "/foo/bar", | ||
| selected: true, | ||
| }, | ||
| ], | ||
| selected: false, | ||
| }, | ||
| { | ||
| kind: DbItemKind.LocalDatabase, | ||
| databaseName: "db3", | ||
| dateAdded: 1234, | ||
| language: "javascript", | ||
| storagePath: "/foo/bar", | ||
| selected: false, | ||
| }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| expect(getSelectedDbItem(dbItems)).toEqual({ | ||
| kind: DbItemKind.LocalDatabase, | ||
| databaseName: "db2", | ||
| dateAdded: 1234, | ||
| language: "javascript", | ||
| storagePath: "/foo/bar", | ||
| selected: true, | ||
| }); | ||
| }); | ||
|
|
||
| it("should return correct remote database list item from DbItem list", () => { | ||
| const dbItems: DbItem[] = [ | ||
| { | ||
| kind: DbItemKind.RootRemote, | ||
| children: [ | ||
| { | ||
| kind: DbItemKind.RemoteSystemDefinedList, | ||
| listName: "top_10", | ||
| listDisplayName: "Top 10 repositories", | ||
| listDescription: "Top 10 repositories of a language", | ||
| selected: false, | ||
| }, | ||
| { | ||
| kind: DbItemKind.RemoteOwner, | ||
| ownerName: "github", | ||
| selected: false, | ||
| }, | ||
| { | ||
| kind: DbItemKind.RemoteUserDefinedList, | ||
| listName: "my list", | ||
| repos: [ | ||
| { | ||
| kind: DbItemKind.RemoteRepo, | ||
| repoFullName: "owner1/repo2", | ||
| selected: false, | ||
| }, | ||
| { | ||
| kind: DbItemKind.RemoteRepo, | ||
| repoFullName: "owner1/repo3", | ||
| selected: false, | ||
| }, | ||
| ], | ||
| selected: true, | ||
| }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| expect(getSelectedDbItem(dbItems)).toEqual({ | ||
| kind: DbItemKind.RemoteUserDefinedList, | ||
| listName: "my list", | ||
| repos: [ | ||
| { | ||
| kind: DbItemKind.RemoteRepo, | ||
| repoFullName: "owner1/repo2", | ||
| selected: false, | ||
| }, | ||
| { | ||
| kind: DbItemKind.RemoteRepo, | ||
| repoFullName: "owner1/repo3", | ||
| selected: false, | ||
| }, | ||
| ], | ||
| selected: true, | ||
| }); | ||
| }); | ||
|
charisk marked this conversation as resolved.
|
||
|
|
||
| it("should handle arbitrary list of db items", () => { | ||
| const dbItems: DbItem[] = [ | ||
| { | ||
| kind: DbItemKind.RootRemote, | ||
| children: [ | ||
| { | ||
| kind: DbItemKind.RemoteSystemDefinedList, | ||
| listName: "top_10", | ||
| listDisplayName: "Top 10 repositories", | ||
| listDescription: "Top 10 repositories of a language", | ||
| selected: false, | ||
| }, | ||
| { | ||
| kind: DbItemKind.RemoteOwner, | ||
| ownerName: "github", | ||
| selected: false, | ||
| }, | ||
| { | ||
| kind: DbItemKind.RemoteUserDefinedList, | ||
| listName: "my list", | ||
| repos: [ | ||
| { | ||
| kind: DbItemKind.RemoteRepo, | ||
| repoFullName: "owner1/repo2", | ||
| selected: false, | ||
| }, | ||
| { | ||
| kind: DbItemKind.RemoteRepo, | ||
| repoFullName: "owner1/repo3", | ||
| selected: false, | ||
| }, | ||
| ], | ||
| selected: false, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| kind: DbItemKind.RemoteSystemDefinedList, | ||
| listName: "top_10", | ||
| listDisplayName: "Top 10 repositories", | ||
| listDescription: "Top 10 repositories of a language", | ||
| selected: true, | ||
| }, | ||
| ]; | ||
|
|
||
| expect(getSelectedDbItem(dbItems)).toEqual({ | ||
| kind: DbItemKind.RemoteSystemDefinedList, | ||
| listName: "top_10", | ||
| listDisplayName: "Top 10 repositories", | ||
| listDescription: "Top 10 repositories of a language", | ||
| selected: true, | ||
| }); | ||
| }); | ||
|
|
||
| it("should handle empty db item lists", () => { | ||
| const dbItems: DbItem[] = [ | ||
| { | ||
| kind: DbItemKind.RootRemote, | ||
| children: [ | ||
| { | ||
| kind: DbItemKind.RemoteSystemDefinedList, | ||
| listName: "top_10", | ||
| listDisplayName: "Top 10 repositories", | ||
| listDescription: "Top 10 repositories of a language", | ||
| selected: false, | ||
| }, | ||
| { | ||
| kind: DbItemKind.RemoteOwner, | ||
| ownerName: "github", | ||
| selected: false, | ||
| }, | ||
| { | ||
| kind: DbItemKind.RemoteUserDefinedList, | ||
| listName: "my list", | ||
| repos: [], | ||
| selected: false, | ||
| }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| expect(getSelectedDbItem(dbItems)).toBeUndefined(); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.