Skip to content

Commit 6289411

Browse files
committed
Move local query tests into their own describe block
There are a couple of tests that check whether we can correctly compare two local queries. These shouldn't be applied to remote queries [1] so let's just make that a bit clearer by moving them into a local queries describe block and using the `localHistory` array to choose items to compare instead of the `allHistory` array. [1]: https://github.com/github/vscode-codeql/blob/bf1e3c10dbf23480d0e0121fe1990e8d07b707c1/extensions/ql-vscode/src/query-history.ts#L1311-L1314
1 parent 0164d10 commit 6289411

1 file changed

Lines changed: 108 additions & 106 deletions

File tree

extensions/ql-vscode/src/vscode-tests/no-workspace/query-history.test.ts

Lines changed: 108 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -135,77 +135,129 @@ describe('query-history', () => {
135135
allHistory = [...localQueryHistory];
136136
});
137137

138-
describe('findOtherQueryToCompare', () => {
139-
it('should find the second query to compare when one is selected', async () => {
140-
const thisQuery = allHistory[3];
141-
queryHistoryManager = await createMockQueryHistory(allHistory);
142-
showQuickPickSpy.returns({ query: allHistory[0] });
138+
describe('Local Queries', () => {
139+
describe('findOtherQueryToCompare', () => {
140+
it('should find the second query to compare when one is selected', async () => {
141+
const thisQuery = localQueryHistory[3];
142+
queryHistoryManager = await createMockQueryHistory(allHistory);
143+
showQuickPickSpy.returns({ query: localQueryHistory[0] });
144+
145+
const otherQuery = await (queryHistoryManager as any).findOtherQueryToCompare(thisQuery, []);
146+
expect(otherQuery).to.eq(localQueryHistory[0]);
147+
148+
// only called with first item, other items filtered out
149+
expect(showQuickPickSpy.getCalls().length).to.eq(1);
150+
expect(showQuickPickSpy.firstCall.args[0][0].query).to.eq(localQueryHistory[0]);
151+
});
143152

144-
const otherQuery = await (queryHistoryManager as any).findOtherQueryToCompare(thisQuery, []);
145-
expect(otherQuery).to.eq(allHistory[0]);
153+
it('should handle cancelling out of the quick select', async () => {
154+
const thisQuery = localQueryHistory[3];
155+
queryHistoryManager = await createMockQueryHistory(allHistory);
146156

147-
// only called with first item, other items filtered out
148-
expect(showQuickPickSpy.getCalls().length).to.eq(1);
149-
expect(showQuickPickSpy.firstCall.args[0][0].query).to.eq(allHistory[0]);
150-
});
157+
const otherQuery = await (queryHistoryManager as any).findOtherQueryToCompare(thisQuery, []);
158+
expect(otherQuery).to.be.undefined;
151159

152-
it('should handle cancelling out of the quick select', async () => {
153-
const thisQuery = allHistory[3];
154-
queryHistoryManager = await createMockQueryHistory(allHistory);
160+
// only called with first item, other items filtered out
161+
expect(showQuickPickSpy.getCalls().length).to.eq(1);
162+
expect(showQuickPickSpy.firstCall.args[0][0].query).to.eq(localQueryHistory[0]);
163+
});
155164

156-
const otherQuery = await (queryHistoryManager as any).findOtherQueryToCompare(thisQuery, []);
157-
expect(otherQuery).to.be.undefined;
165+
it('should compare against 2 queries', async () => {
166+
const thisQuery = localQueryHistory[3];
167+
queryHistoryManager = await createMockQueryHistory(allHistory);
158168

159-
// only called with first item, other items filtered out
160-
expect(showQuickPickSpy.getCalls().length).to.eq(1);
161-
expect(showQuickPickSpy.firstCall.args[0][0].query).to.eq(allHistory[0]);
162-
});
169+
const otherQuery = await (queryHistoryManager as any).findOtherQueryToCompare(thisQuery, [thisQuery, localQueryHistory[0]]);
170+
expect(otherQuery).to.eq(localQueryHistory[0]);
171+
expect(showQuickPickSpy).not.to.have.been.called;
172+
});
163173

164-
it('should compare against 2 queries', async () => {
165-
const thisQuery = allHistory[3];
166-
queryHistoryManager = await createMockQueryHistory(allHistory);
174+
it('should throw an error when a query is not successful', async () => {
175+
const thisQuery = localQueryHistory[3];
176+
queryHistoryManager = await createMockQueryHistory(allHistory);
177+
allHistory[0] = createMockLocalQuery('a', createMockQueryWithResults(sandbox, false));
167178

168-
const otherQuery = await (queryHistoryManager as any).findOtherQueryToCompare(thisQuery, [thisQuery, allHistory[0]]);
169-
expect(otherQuery).to.eq(allHistory[0]);
170-
expect(showQuickPickSpy).not.to.have.been.called;
171-
});
179+
try {
180+
await (queryHistoryManager as any).findOtherQueryToCompare(thisQuery, [thisQuery, allHistory[0]]);
181+
assert(false, 'Should have thrown');
182+
} catch (e) {
183+
expect(getErrorMessage(e)).to.eq('Please select a successful query.');
184+
}
185+
});
172186

173-
it('should throw an error when a query is not successful', async () => {
174-
const thisQuery = allHistory[3];
175-
queryHistoryManager = await createMockQueryHistory(allHistory);
176-
allHistory[0] = createMockLocalQuery('a', createMockQueryWithResults(sandbox, false));
187+
it('should throw an error when a databases are not the same', async () => {
188+
queryHistoryManager = await createMockQueryHistory(allHistory);
177189

178-
try {
179-
await (queryHistoryManager as any).findOtherQueryToCompare(thisQuery, [thisQuery, allHistory[0]]);
180-
assert(false, 'Should have thrown');
181-
} catch (e) {
182-
expect(getErrorMessage(e)).to.eq('Please select a successful query.');
183-
}
190+
try {
191+
// localQueryHistory[0] is database a
192+
// localQueryHistory[1] is database b
193+
await (queryHistoryManager as any).findOtherQueryToCompare(localQueryHistory[0], [localQueryHistory[0], localQueryHistory[1]]);
194+
assert(false, 'Should have thrown');
195+
} catch (e) {
196+
expect(getErrorMessage(e)).to.eq('Query databases must be the same.');
197+
}
198+
});
199+
200+
it('should throw an error when more than 2 queries selected', async () => {
201+
const thisQuery = localQueryHistory[3];
202+
queryHistoryManager = await createMockQueryHistory(allHistory);
203+
204+
try {
205+
await (queryHistoryManager as any).findOtherQueryToCompare(thisQuery, [thisQuery, localQueryHistory[0], localQueryHistory[1]]);
206+
assert(false, 'Should have thrown');
207+
} catch (e) {
208+
expect(getErrorMessage(e)).to.eq('Please select no more than 2 queries.');
209+
}
210+
});
184211
});
185212

186-
it('should throw an error when a databases are not the same', async () => {
187-
queryHistoryManager = await createMockQueryHistory(allHistory);
213+
describe('Compare callback', () => {
214+
it('should call the compare callback', async () => {
215+
queryHistoryManager = await createMockQueryHistory(allHistory);
216+
await queryHistoryManager.handleCompareWith(localQueryHistory[0], [localQueryHistory[0], localQueryHistory[3]]);
217+
expect(doCompareCallback).to.have.been.calledOnceWith(localQueryHistory[0], localQueryHistory[3]);
218+
});
188219

189-
try {
190-
// allHistory[0] is database a
191-
// allHistory[1] is database b
192-
await (queryHistoryManager as any).findOtherQueryToCompare(allHistory[0], [allHistory[0], allHistory[1]]);
193-
assert(false, 'Should have thrown');
194-
} catch (e) {
195-
expect(getErrorMessage(e)).to.eq('Query databases must be the same.');
196-
}
220+
it('should avoid calling the compare callback when only one item is selected', async () => {
221+
queryHistoryManager = await createMockQueryHistory(allHistory);
222+
await queryHistoryManager.handleCompareWith(localQueryHistory[0], [localQueryHistory[0]]);
223+
expect(doCompareCallback).not.to.have.been.called;
224+
});
197225
});
198226

199-
it('should throw an error when more than 2 queries selected', async () => {
200-
const thisQuery = allHistory[3];
201-
queryHistoryManager = await createMockQueryHistory(allHistory);
227+
describe('updateCompareWith', () => {
228+
it('should update compareWithItem when there is a single item', async () => {
229+
queryHistoryManager = await createMockQueryHistory([]);
230+
(queryHistoryManager as any).updateCompareWith(['a']);
231+
expect(queryHistoryManager.compareWithItem).to.be.eq('a');
232+
});
202233

203-
try {
204-
await (queryHistoryManager as any).findOtherQueryToCompare(thisQuery, [thisQuery, allHistory[0], allHistory[1]]);
205-
assert(false, 'Should have thrown');
206-
} catch (e) {
207-
expect(getErrorMessage(e)).to.eq('Please select no more than 2 queries.');
208-
}
234+
it('should delete compareWithItem when there are 0 items', async () => {
235+
queryHistoryManager = await createMockQueryHistory([]);
236+
queryHistoryManager.compareWithItem = localQueryHistory[0];
237+
(queryHistoryManager as any).updateCompareWith([]);
238+
expect(queryHistoryManager.compareWithItem).to.be.undefined;
239+
});
240+
241+
it('should delete compareWithItem when there are more than 2 items', async () => {
242+
queryHistoryManager = await createMockQueryHistory(allHistory);
243+
queryHistoryManager.compareWithItem = localQueryHistory[0];
244+
(queryHistoryManager as any).updateCompareWith([localQueryHistory[0], localQueryHistory[1], localQueryHistory[2]]);
245+
expect(queryHistoryManager.compareWithItem).to.be.undefined;
246+
});
247+
248+
it('should delete compareWithItem when there are 2 items and disjoint from compareWithItem', async () => {
249+
queryHistoryManager = await createMockQueryHistory([]);
250+
queryHistoryManager.compareWithItem = localQueryHistory[0];
251+
(queryHistoryManager as any).updateCompareWith([localQueryHistory[1], localQueryHistory[2]]);
252+
expect(queryHistoryManager.compareWithItem).to.be.undefined;
253+
});
254+
255+
it('should do nothing when compareWithItem exists and exactly 2 items', async () => {
256+
queryHistoryManager = await createMockQueryHistory([]);
257+
queryHistoryManager.compareWithItem = localQueryHistory[0];
258+
(queryHistoryManager as any).updateCompareWith([localQueryHistory[0], localQueryHistory[1]]);
259+
expect(queryHistoryManager.compareWithItem).to.be.eq(localQueryHistory[0]);
260+
});
209261
});
210262
});
211263

@@ -289,56 +341,6 @@ describe('query-history', () => {
289341
expect(localQueriesResultsViewStub.showResults).to.have.been.calledOnceWith(newSelected);
290342
});
291343

292-
describe('Compare callback', () => {
293-
it('should call the compare callback', async () => {
294-
queryHistoryManager = await createMockQueryHistory(allHistory);
295-
await queryHistoryManager.handleCompareWith(allHistory[0], [allHistory[0], allHistory[3]]);
296-
expect(doCompareCallback).to.have.been.calledOnceWith(allHistory[0], allHistory[3]);
297-
});
298-
299-
it('should avoid calling the compare callback when only one item is selected', async () => {
300-
queryHistoryManager = await createMockQueryHistory(allHistory);
301-
await queryHistoryManager.handleCompareWith(allHistory[0], [allHistory[0]]);
302-
expect(doCompareCallback).not.to.have.been.called;
303-
});
304-
});
305-
306-
describe('updateCompareWith', () => {
307-
it('should update compareWithItem when there is a single item', async () => {
308-
queryHistoryManager = await createMockQueryHistory([]);
309-
(queryHistoryManager as any).updateCompareWith(['a']);
310-
expect(queryHistoryManager.compareWithItem).to.be.eq('a');
311-
});
312-
313-
it('should delete compareWithItem when there are 0 items', async () => {
314-
queryHistoryManager = await createMockQueryHistory([]);
315-
queryHistoryManager.compareWithItem = localQueryHistory[0];
316-
(queryHistoryManager as any).updateCompareWith([]);
317-
expect(queryHistoryManager.compareWithItem).to.be.undefined;
318-
});
319-
320-
it('should delete compareWithItem when there are more than 2 items', async () => {
321-
queryHistoryManager = await createMockQueryHistory(allHistory);
322-
queryHistoryManager.compareWithItem = localQueryHistory[0];
323-
(queryHistoryManager as any).updateCompareWith([localQueryHistory[0], localQueryHistory[1], localQueryHistory[2]]);
324-
expect(queryHistoryManager.compareWithItem).to.be.undefined;
325-
});
326-
327-
it('should delete compareWithItem when there are 2 items and disjoint from compareWithItem', async () => {
328-
queryHistoryManager = await createMockQueryHistory([]);
329-
queryHistoryManager.compareWithItem = localQueryHistory[0];
330-
(queryHistoryManager as any).updateCompareWith([localQueryHistory[1], localQueryHistory[2]]);
331-
expect(queryHistoryManager.compareWithItem).to.be.undefined;
332-
});
333-
334-
it('should do nothing when compareWithItem exists and exactly 2 items', async () => {
335-
queryHistoryManager = await createMockQueryHistory([]);
336-
queryHistoryManager.compareWithItem = localQueryHistory[0];
337-
(queryHistoryManager as any).updateCompareWith([localQueryHistory[0], localQueryHistory[1]]);
338-
expect(queryHistoryManager.compareWithItem).to.be.eq(localQueryHistory[0]);
339-
});
340-
});
341-
342344
describe('HistoryTreeDataProvider', () => {
343345
let historyTreeDataProvider: HistoryTreeDataProvider;
344346
let labelProvider: HistoryItemLabelProvider;

0 commit comments

Comments
 (0)