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 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
15 changes: 14 additions & 1 deletion sdk/search/search-documents/src/odata.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

function formatNullAndUndefined(input: unknown): string | unknown {
if (input === null || input === undefined) {
return "null";
sarangan12 marked this conversation as resolved.
Show resolved Hide resolved
}

return input;
}

function escapeQuotesIfString(input: unknown, previous: string): string | unknown {
let result = input;

Expand All @@ -11,6 +19,7 @@ function escapeQuotesIfString(input: unknown, previous: string): string | unknow
result = `'${result}'`;
}
}

return result;
}

Expand All @@ -31,7 +40,11 @@ export function odata(strings: TemplateStringsArray, ...values: unknown[]): stri
for (let i = 0; i < strings.length; i++) {
results.push(strings[i]);
if (i < values.length) {
results.push(escapeQuotesIfString(values[i], strings[i]));
if (values[i] === null || values[i] === undefined) {
results.push(formatNullAndUndefined(values[i]));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, since you do the if check inside of formatNullAndUndefined should we just do something like

const value = formatNullAndUndefined(values[i]);
results.push(escapeQuotesIfString(value, strings[i]));

and avoid doing if/else inside this for loop?

} else {
results.push(escapeQuotesIfString(values[i], strings[i]));
}
}
}
return results.join("");
Expand Down
52 changes: 52 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,58 @@ 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} 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"
);
});

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");
});

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");
});

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