Skip to content

Commit

Permalink
Add testcases for handling odata (#11321)
Browse files Browse the repository at this point in the history
* Add testcases for handling odata

* Formatting code

* PR Comments

* Minor Formatting

* Update sdk/search/search-documents/src/odata.ts

Co-authored-by: Jeff Fisher <[email protected]>

* Update sdk/search/search-documents/src/odata.ts

Co-authored-by: Jeff Fisher <[email protected]>

Co-authored-by: Jeff Fisher <[email protected]>
  • Loading branch information
sarangan12 and xirzec authored Sep 19, 2020
1 parent b750841 commit 4a96e96
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
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";
}

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

0 comments on commit 4a96e96

Please sign in to comment.