Skip to content
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

Add testcases for handling odata #11321

Merged
merged 6 commits into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions sdk/search/search-documents/src/odata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
function escapeQuotesIfString(input: unknown, previous: string): string | unknown {
let result = input;

if (input == null) {
return "null";
sarangan12 marked this conversation as resolved.
Show resolved Hide resolved
}

if (typeof input === "string") {
result = input.replace(/'/g, "''");
// check if we need to escape this literal
if (!previous.trim().endsWith("'")) {
result = `'${result}'`;
}
}

return result;
}

Expand Down
67 changes: 67 additions & 0 deletions sdk/search/search-documents/test/odata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,73 @@ describe("odata", () => {
assert.strictEqual(result, "search.ismatch('you''re', 'Description')");
});

it("no arguments", () => {
assert.strictEqual(odata`Foo eq 2`, "Foo eq 2");
});

it("one argument", () => {
assert.strictEqual(odata`Foo eq ${2}`, "Foo eq 2");
});

it("many arguments", () => {
assert.strictEqual(odata`Foo eq ${2} and Bar eq ${3}`, "Foo eq 2 and Bar eq 3");
assert.strictEqual(
odata`Foo eq ${2} and Bar eq ${3} and Baz eq ${4}`,
"Foo eq 2 and Bar eq 3 and Baz eq 4"
);
assert.strictEqual(
odata`Foo eq ${2} and Bar eq ${3} and Baz eq ${4} and Qux eq ${5}`,
"Foo eq 2 and Bar eq 3 and Baz eq 4 and Qux eq 5"
);
assert.strictEqual(
odata`Foo eq ${2} and Bar eq ${3} and Baz eq ${4} and Qux eq ${5} and Quux eq ${6}`,
"Foo eq 2 and Bar eq 3 and Baz eq 4 and Qux eq 5 and Quux eq 6"
);
sarangan12 marked this conversation as resolved.
Show resolved Hide resolved
});

it("null", () => {
assert.strictEqual(odata`Foo eq ${null}`, "Foo eq null");
});

it("bool", () => {
let x: boolean = true;
assert.strictEqual(odata`Foo eq ${x}`, "Foo eq true");
assert.strictEqual(odata`Foo eq ${true}`, "Foo eq true");

x = false;
assert.strictEqual(odata`Foo eq ${x}`, "Foo eq false");
assert.strictEqual(odata`Foo eq ${false}`, "Foo eq false");
sarangan12 marked this conversation as resolved.
Show resolved Hide resolved
});

it("numbers", () => {
assert.strictEqual(odata`Foo eq ${0}`, "Foo eq 0");
assert.strictEqual(odata`Foo eq ${2}`, "Foo eq 2");
assert.strictEqual(odata`Foo eq ${-2}`, "Foo eq -2");
assert.strictEqual(odata`Foo eq ${2.5}`, "Foo eq 2.5");
});

it("limits", () => {
assert.strictEqual(odata`Foo eq ${Number.NEGATIVE_INFINITY}`, "Foo eq -Infinity");
assert.strictEqual(odata`Foo eq ${Number.POSITIVE_INFINITY}`, "Foo eq Infinity");
assert.strictEqual(odata`Foo eq ${Number.NaN}`, "Foo eq NaN");
assert.strictEqual(odata`Foo eq ${Number.MAX_VALUE}`, "Foo eq 1.7976931348623157e+308");
assert.strictEqual(odata`Foo eq ${Number.MIN_VALUE}`, "Foo eq 5e-324");
sarangan12 marked this conversation as resolved.
Show resolved Hide resolved
});

it("dates", () => {
const result: string = odata`Foo eq ${new Date(1912, 6, 23, 11, 59, 59)}`;
assert.strictEqual(result.includes("Tue Jul 23 1912 11:59:59"), true);
});

it("text", () => {
assert.strictEqual(odata`Foo eq ${"x"}`, "Foo eq 'x'");
assert.strictEqual(odata`Foo eq ${"'"}`, "Foo eq ''''");
assert.strictEqual(odata`Foo eq ${'"'}`, "Foo eq '\"'");
assert.strictEqual(odata`Foo eq ${"bar"}`, "Foo eq 'bar'");
assert.strictEqual(odata`Foo eq ${"bar's"}`, "Foo eq 'bar''s'");
assert.strictEqual(odata`Foo eq ${'"bar"'}`, "Foo eq '\"bar\"'");
});

afterEach(() => {
sinon.restore();
});
Expand Down