Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
},
testMatch: ['**/src/util/**/*.test.(ts|tsx)'],
testMatch: ['**/src/util/**/*.test.(ts|tsx)','**/src/commands/fts/SearchWorkbench/test/*.test.ts'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
Expand Down
10 changes: 8 additions & 2 deletions src/commands/fts/SearchWorkbench/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,16 @@ export default class UntitledSearchJsonDocumentService {



public async openSearchJsonTextDocument(searchIndexNode: SearchIndexNode,memFs: MemFS): Promise<vscode.TextEditor> {
public async openSearchJsonTextDocument(searchIndexNode: SearchIndexNode, memFs: MemFS): Promise<vscode.TextEditor> {
const uri = vscode.Uri.parse(`couchbase:/search-workbench-${searchIndexNode.searchIndexName}-${this.untitledCount}.cbs.json`);
this.untitledCount++;
let documentContent = Buffer.from('');
const defaultJsonContent = `{
"query": {
"query": "your_query_here"
},
"fields": ["*"]
}`;
Comment on lines +41 to +46
Copy link
Contributor

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

let documentContent = Buffer.from(defaultJsonContent);
memFs.writeFile(uri, documentContent, {
create: true,
overwrite: true,
Expand Down
297 changes: 297 additions & 0 deletions src/commands/fts/SearchWorkbench/test/booleanObject.test.ts
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);
}
}
});
Loading