|
| 1 | +import * as chai from 'chai'; |
| 2 | +import * as path from 'path'; |
| 3 | +import 'mocha'; |
| 4 | +import 'sinon-chai'; |
| 5 | +import * as sinon from 'sinon'; |
| 6 | +import * as chaiAsPromised from 'chai-as-promised'; |
| 7 | + |
| 8 | +import { QueryInfo } from '../../run-queries'; |
| 9 | +import { QlProgram, Severity, compileQuery } from '../../messages'; |
| 10 | +import { DatabaseItem } from '../../databases'; |
| 11 | + |
| 12 | +chai.use(chaiAsPromised); |
| 13 | +const expect = chai.expect; |
| 14 | + |
| 15 | +describe('run-queries', () => { |
| 16 | + it('should create a QueryInfo', () => { |
| 17 | + const info = createMockQueryInfo(); |
| 18 | + |
| 19 | + const queryID = info.queryID; |
| 20 | + expect(path.basename(info.compiledQueryPath)).to.eq(`compiledQuery${queryID}.qlo`); |
| 21 | + expect(path.basename(info.dilPath)).to.eq(`results${queryID}.dil`); |
| 22 | + expect(path.basename(info.resultsPaths.resultsPath)).to.eq(`results${queryID}.bqrs`); |
| 23 | + expect(path.basename(info.resultsPaths.interpretedResultsPath)).to.eq(`interpretedResults${queryID}.sarif`); |
| 24 | + expect(info.dataset).to.eq('file:///abc'); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('compile', () => { |
| 28 | + it('should compile', async () => { |
| 29 | + const info = createMockQueryInfo(); |
| 30 | + const qs = createMockQueryServerClient(); |
| 31 | + const mockProgress = 'progress-monitor'; |
| 32 | + const mockCancel = 'cancel-token'; |
| 33 | + |
| 34 | + const results = await info.compile( |
| 35 | + qs as any, |
| 36 | + mockProgress as any, |
| 37 | + mockCancel as any |
| 38 | + ); |
| 39 | + |
| 40 | + expect(results).to.deep.eq([ |
| 41 | + { message: 'err', severity: Severity.ERROR } |
| 42 | + ]); |
| 43 | + |
| 44 | + expect(qs.sendRequest).to.have.been.calledOnceWith( |
| 45 | + compileQuery, |
| 46 | + { |
| 47 | + compilationOptions: { |
| 48 | + computeNoLocationUrls: true, |
| 49 | + failOnWarnings: false, |
| 50 | + fastCompilation: false, |
| 51 | + includeDilInQlo: true, |
| 52 | + localChecking: false, |
| 53 | + noComputeGetUrl: false, |
| 54 | + noComputeToString: false, |
| 55 | + }, |
| 56 | + extraOptions: { |
| 57 | + timeoutSecs: 5 |
| 58 | + }, |
| 59 | + queryToCheck: 'my-program', |
| 60 | + resultPath: info.compiledQueryPath, |
| 61 | + target: { query: {} } |
| 62 | + }, |
| 63 | + mockCancel, |
| 64 | + mockProgress |
| 65 | + ); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + function createMockQueryInfo() { |
| 70 | + return new QueryInfo( |
| 71 | + 'my-program' as unknown as QlProgram, |
| 72 | + { |
| 73 | + contents: { |
| 74 | + datasetUri: 'file:///abc' |
| 75 | + } |
| 76 | + } as unknown as DatabaseItem, |
| 77 | + 'my-scheme' // queryDbscheme |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + function createMockQueryServerClient() { |
| 82 | + return { |
| 83 | + config: { |
| 84 | + timeoutSecs: 5 |
| 85 | + }, |
| 86 | + sendRequest: sinon.stub().returns(new Promise(resolve => { |
| 87 | + resolve({ |
| 88 | + messages: [ |
| 89 | + { message: 'err', severity: Severity.ERROR }, |
| 90 | + { message: 'warn', severity: Severity.WARNING }, |
| 91 | + ] |
| 92 | + }); |
| 93 | + })), |
| 94 | + logger: { |
| 95 | + log: sinon.spy() |
| 96 | + } |
| 97 | + }; |
| 98 | + } |
| 99 | +}); |
0 commit comments