-
Notifications
You must be signed in to change notification settings - Fork 5
Add support for json query validation + tests #401
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
Draft
prajwal-pai77
wants to merge
1
commit into
DA-388-FtsSupport
Choose a base branch
from
DA-392-Add-Search-Query-Validation
base: DA-388-FtsSupport
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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
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
297 changes: 297 additions & 0 deletions
297
src/commands/fts/SearchWorkbench/test/booleanObject.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,297 @@ | ||
import * as vscode from 'vscode'; | ||
import { validateDocument } from '../validators/validationUtil'; | ||
import { ValidationHelper } from '../validators/validationHelper'; | ||
|
||
let mockText = ""; | ||
|
||
jest.mock('vscode', () => ({ | ||
languages: { | ||
createDiagnosticCollection: jest.fn().mockImplementation(() => { | ||
const diagnosticsMap = new Map(); | ||
return { | ||
clear: jest.fn(() => diagnosticsMap.clear()), | ||
dispose: jest.fn(), | ||
get: jest.fn(uri => diagnosticsMap.get(uri.toString())), | ||
set: jest.fn((uri, diagnostics) => diagnosticsMap.set(uri.toString(), diagnostics)), | ||
delete: jest.fn(uri => diagnosticsMap.delete(uri.toString())), | ||
forEach: jest.fn(callback => diagnosticsMap.forEach(callback)), | ||
}; | ||
}), | ||
}, | ||
Uri: { | ||
parse: jest.fn().mockImplementation((str) => ({ | ||
toString: () => str, | ||
})), | ||
}, | ||
workspace: { | ||
openTextDocument: jest.fn().mockImplementation((uri) => ({ | ||
getText: jest.fn(() => mockText), | ||
uri: uri, | ||
positionAt: jest.fn().mockImplementation((index) => { | ||
return new vscode.Position(Math.floor(index / 100), index % 100); | ||
}), | ||
})), | ||
fs: { | ||
writeFile: jest.fn(), | ||
} | ||
}, | ||
Range: jest.fn().mockImplementation((start, end) => ({ | ||
start: start, | ||
end: end | ||
})), | ||
Position: jest.fn().mockImplementation((line, character) => ({ | ||
line: line, | ||
character: character | ||
})), | ||
Diagnostic: jest.fn().mockImplementation((range, message, severity) => ({ | ||
range: range, | ||
message: message, | ||
severity: severity | ||
})), | ||
DiagnosticSeverity: { | ||
Error: 0, | ||
Warning: 1, | ||
Information: 2, | ||
Hint: 3 | ||
} | ||
})); | ||
|
||
|
||
|
||
const setMockText = (text:any) => { | ||
mockText = text; | ||
}; | ||
|
||
describe("CBSBooleanInspection Tests", () => { | ||
let diagnosticsCollection: vscode.DiagnosticCollection; | ||
|
||
beforeEach(() => { | ||
diagnosticsCollection = vscode.languages.createDiagnosticCollection('testCollection'); | ||
}); | ||
|
||
afterEach(() => { | ||
diagnosticsCollection.dispose(); | ||
}); | ||
|
||
test("Valid Must", async () => { | ||
const json = `{ | ||
"query": { | ||
"must": { | ||
"conjuncts": [ | ||
{ | ||
"field": "reviews.content", | ||
"match": "location" | ||
}, | ||
{ | ||
"field": "reviews.content", | ||
"match_phrase": "nice view" | ||
} | ||
] | ||
} | ||
} | ||
}`; | ||
|
||
setMockText(json); | ||
|
||
await getDiagnosticsForJson(json); | ||
const uri = vscode.Uri.parse('untitled:test.json'); | ||
expect(diagnosticsCollection.get(uri)?.length).toBe(0); | ||
}); | ||
|
||
test("Invalid Must Disjuncts", async () => { | ||
const json = ` { | ||
"query":{ | ||
"must":{ | ||
"disjuncts":[ | ||
{ | ||
"field": "reviews.content", | ||
"match": "location" | ||
}, | ||
{ | ||
"field":"reviews.content", | ||
"match_phrase": "nice view" | ||
} | ||
] | ||
} | ||
} | ||
}`; | ||
|
||
setMockText(json); | ||
|
||
await getDiagnosticsForJson(json); | ||
const uri = vscode.Uri.parse('untitled:test.json'); | ||
const diagnostics = diagnosticsCollection.get(uri) ?? []; | ||
expect(diagnostics.some(diagnostic => diagnostic.message.includes(ValidationHelper.getMustConjunctsErrorMessage()))).toBeTruthy(); | ||
}); | ||
|
||
test("Valid Should", async () => { | ||
const json = ` { | ||
"query":{ | ||
"should":{ | ||
"disjuncts":[ | ||
{ | ||
"field": "free_parking", | ||
"bool": true | ||
}, | ||
{ | ||
"field": "free_internet", | ||
"bool": true | ||
} | ||
], | ||
"min": 1 | ||
} | ||
} | ||
}`; | ||
|
||
setMockText(json); | ||
|
||
await getDiagnosticsForJson(json); | ||
const uri = vscode.Uri.parse('untitled:test.json'); | ||
expect(diagnosticsCollection.get(uri)?.length).toBe(0); | ||
}); | ||
|
||
test("Invalid Should", async () => { | ||
const json = ` { | ||
"query":{ | ||
"should":{ | ||
"conjuncts":[ | ||
{ | ||
"field": "free_parking", | ||
"bool": true | ||
}, | ||
{ | ||
"field": "free_internet", | ||
"bool": true | ||
} | ||
], | ||
"min": 1 | ||
} | ||
} | ||
}`; | ||
|
||
setMockText(json); | ||
|
||
await getDiagnosticsForJson(json); | ||
const uri = vscode.Uri.parse('untitled:test.json'); | ||
const diagnostics = diagnosticsCollection.get(uri) ?? []; | ||
expect(diagnostics.some(diagnostic => diagnostic.message.includes(ValidationHelper.getShouldErrorMessage()))).toBeTruthy(); | ||
}); | ||
|
||
test("Invalid Must", async () => { | ||
const json = ` { | ||
"query":{ | ||
"must":{ | ||
"conjuncts":[ | ||
{ | ||
"field": "reviews.content", | ||
"match": "location" | ||
}, | ||
{ | ||
"field":"reviews.content", | ||
"match_phrase": "nice view" | ||
} | ||
], | ||
"min":5 | ||
} | ||
} | ||
}`; | ||
|
||
setMockText(json); | ||
|
||
await getDiagnosticsForJson(json); | ||
const uri = vscode.Uri.parse('untitled:test.json'); | ||
const diagnostics = diagnosticsCollection.get(uri) ?? []; | ||
expect(diagnostics.some(diagnostic => diagnostic.message.includes(ValidationHelper.getMustConjunctsErrorMessage()))).toBeTruthy(); | ||
}); | ||
|
||
test("Valid Must not", async () => { | ||
const json = ` { | ||
"query":{ | ||
"must_not":{ | ||
"disjuncts":[ | ||
{ | ||
"field": "free_breakfast", | ||
"bool": false | ||
}, | ||
{ | ||
"field": "ratings.Cleanliness", | ||
"min": 1, | ||
"max": 3 | ||
} | ||
] | ||
} | ||
} | ||
}`; | ||
|
||
setMockText(json); | ||
|
||
await getDiagnosticsForJson(json); | ||
const uri = vscode.Uri.parse('untitled:test.json'); | ||
expect(diagnosticsCollection.get(uri)?.length).toBe(0); | ||
}); | ||
|
||
test("Invalid Must Not Conjunct", async () => { | ||
const json = ` { | ||
"query":{ | ||
"must_not":{ | ||
"conjuncts":[ | ||
{ | ||
"field": "reviews.content", | ||
"match": "location" | ||
}, | ||
{ | ||
"field":"reviews.content", | ||
"match_phrase": "nice view" | ||
} | ||
] | ||
} | ||
} | ||
}`; | ||
|
||
setMockText(json); | ||
|
||
await getDiagnosticsForJson(json); | ||
const uri = vscode.Uri.parse('untitled:test.json'); | ||
const diagnostics = diagnosticsCollection.get(uri) ?? []; | ||
expect(diagnostics.some(diagnostic => diagnostic.message.includes(ValidationHelper.getMustNotDisjunctsErrorMessage()))).toBeTruthy(); | ||
}); | ||
|
||
test("Invalid Must Not Min", async () => { | ||
const json = ` { | ||
"query":{ | ||
"must_not":{ | ||
"disjuncts":[ | ||
{ | ||
"field": "free_breakfast", | ||
"bool": false | ||
}, | ||
{ | ||
"field": "ratings.Cleanliness", | ||
"min": 1, | ||
"max": 3 | ||
} | ||
], | ||
"min": 1 | ||
} | ||
} | ||
}`; | ||
|
||
setMockText(json); | ||
|
||
await getDiagnosticsForJson(json); | ||
const uri = vscode.Uri.parse('untitled:test.json'); | ||
const diagnostics = diagnosticsCollection.get(uri) ?? []; | ||
expect(diagnostics.some(diagnostic => diagnostic.message.includes(ValidationHelper.getMustNotDisjunctsErrorMessage()))).toBeTruthy(); | ||
}); | ||
|
||
async function getDiagnosticsForJson(jsonText: string) { | ||
try { | ||
const uri = vscode.Uri.parse('untitled:test.json'); | ||
await vscode.workspace.fs.writeFile(uri, Buffer.from(jsonText)); | ||
const document = await vscode.workspace.openTextDocument(uri); | ||
validateDocument(document, diagnosticsCollection); | ||
} catch (error) { | ||
console.error("Error during testing:", error); | ||
} | ||
} | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make sure of the formatting
(in code as well as in result)
And result should be correct first