From 353fe781e83ce63c2f3f013d6716363782866ff0 Mon Sep 17 00:00:00 2001 From: Sarangan Rajamanickam Date: Thu, 17 Sep 2020 17:27:45 -0700 Subject: [PATCH 1/6] Add testcases for handling odata --- sdk/search/search-documents/src/odata.ts | 5 ++ .../search-documents/test/odata.spec.ts | 58 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/sdk/search/search-documents/src/odata.ts b/sdk/search/search-documents/src/odata.ts index a4b5839dacdd..9c90a177bdce 100644 --- a/sdk/search/search-documents/src/odata.ts +++ b/sdk/search/search-documents/src/odata.ts @@ -4,6 +4,10 @@ function escapeQuotesIfString(input: unknown, previous: string): string | unknown { let result = input; + if(input == null) { + return "null"; + } + if (typeof input === "string") { result = input.replace(/'/g, "''"); // check if we need to escape this literal @@ -11,6 +15,7 @@ function escapeQuotesIfString(input: unknown, previous: string): string | unknow result = `'${result}'`; } } + return result; } diff --git a/sdk/search/search-documents/test/odata.spec.ts b/sdk/search/search-documents/test/odata.spec.ts index a2f864e95a4e..5c532845e6ce 100644 --- a/sdk/search/search-documents/test/odata.spec.ts +++ b/sdk/search/search-documents/test/odata.spec.ts @@ -30,6 +30,64 @@ 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"); + }); + + 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"); + }); + + 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"); + }); + + 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(); }); From 4649625bf957327200c28c999d3c2283df4c8d7c Mon Sep 17 00:00:00 2001 From: Sarangan Rajamanickam Date: Thu, 17 Sep 2020 17:29:48 -0700 Subject: [PATCH 2/6] Formatting code --- sdk/search/search-documents/src/odata.ts | 2 +- .../search-documents/test/odata.spec.ts | 33 ++++++++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/sdk/search/search-documents/src/odata.ts b/sdk/search/search-documents/src/odata.ts index 9c90a177bdce..f166b3dd68ae 100644 --- a/sdk/search/search-documents/src/odata.ts +++ b/sdk/search/search-documents/src/odata.ts @@ -4,7 +4,7 @@ function escapeQuotesIfString(input: unknown, previous: string): string | unknown { let result = input; - if(input == null) { + if (input == null) { return "null"; } diff --git a/sdk/search/search-documents/test/odata.spec.ts b/sdk/search/search-documents/test/odata.spec.ts index 5c532845e6ce..b4223436aad1 100644 --- a/sdk/search/search-documents/test/odata.spec.ts +++ b/sdk/search/search-documents/test/odata.spec.ts @@ -35,14 +35,23 @@ describe("odata", () => { }); it("one argument", () => { - assert.strictEqual(odata`Foo eq ${2}`, "Foo eq 2"); + 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"); + 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" + ); }); it("null", () => { @@ -50,7 +59,7 @@ describe("odata", () => { }); it("bool", () => { - let x:boolean = true; + let x: boolean = true; assert.strictEqual(odata`Foo eq ${x}`, "Foo eq true"); assert.strictEqual(odata`Foo eq ${true}`, "Foo eq true"); @@ -63,7 +72,7 @@ describe("odata", () => { 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"); + assert.strictEqual(odata`Foo eq ${2.5}`, "Foo eq 2.5"); }); it("limits", () => { @@ -75,17 +84,17 @@ describe("odata", () => { }); it("dates", () => { - const result:string = odata`Foo eq ${new Date(1912, 6, 23, 11, 59, 59)}`; + 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 ${"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\"'" ); + 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(() => { From af6a4ff041de67f0f5aa65d3df263b226179f1d3 Mon Sep 17 00:00:00 2001 From: Sarangan Rajamanickam Date: Fri, 18 Sep 2020 15:18:44 -0700 Subject: [PATCH 3/6] PR Comments --- sdk/search/search-documents/src/odata.ts | 18 +++++++++++++----- sdk/search/search-documents/test/odata.spec.ts | 15 --------------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/sdk/search/search-documents/src/odata.ts b/sdk/search/search-documents/src/odata.ts index f166b3dd68ae..4a0bafdc0608 100644 --- a/sdk/search/search-documents/src/odata.ts +++ b/sdk/search/search-documents/src/odata.ts @@ -1,13 +1,17 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -function escapeQuotesIfString(input: unknown, previous: string): string | unknown { - let result = input; - - if (input == null) { +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; + if (typeof input === "string") { result = input.replace(/'/g, "''"); // check if we need to escape this literal @@ -36,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(""); diff --git a/sdk/search/search-documents/test/odata.spec.ts b/sdk/search/search-documents/test/odata.spec.ts index b4223436aad1..914d48879a99 100644 --- a/sdk/search/search-documents/test/odata.spec.ts +++ b/sdk/search/search-documents/test/odata.spec.ts @@ -39,15 +39,6 @@ describe("odata", () => { }); 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" @@ -62,10 +53,6 @@ describe("odata", () => { 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"); }); it("numbers", () => { @@ -79,8 +66,6 @@ describe("odata", () => { 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"); }); it("dates", () => { From 627eb61196b1c3bf58d188b3806ad7c94c8e6444 Mon Sep 17 00:00:00 2001 From: Sarangan Rajamanickam Date: Fri, 18 Sep 2020 15:19:14 -0700 Subject: [PATCH 4/6] Minor Formatting --- sdk/search/search-documents/src/odata.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/search/search-documents/src/odata.ts b/sdk/search/search-documents/src/odata.ts index 4a0bafdc0608..b8d3d3389a15 100644 --- a/sdk/search/search-documents/src/odata.ts +++ b/sdk/search/search-documents/src/odata.ts @@ -40,7 +40,7 @@ export function odata(strings: TemplateStringsArray, ...values: unknown[]): stri for (let i = 0; i < strings.length; i++) { results.push(strings[i]); if (i < values.length) { - if(values[i] == null || values[i] == undefined) { + if (values[i] == null || values[i] == undefined) { results.push(formatNullAndUndefined(values[i])); } else { results.push(escapeQuotesIfString(values[i], strings[i])); From 7d2c4cc5fc936483ccdd985b939693ebe74ab8de Mon Sep 17 00:00:00 2001 From: Sarangan Rajamanickam Date: Fri, 18 Sep 2020 23:50:54 +0000 Subject: [PATCH 5/6] Update sdk/search/search-documents/src/odata.ts Co-authored-by: Jeff Fisher --- sdk/search/search-documents/src/odata.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/search/search-documents/src/odata.ts b/sdk/search/search-documents/src/odata.ts index b8d3d3389a15..eaf891abd7a1 100644 --- a/sdk/search/search-documents/src/odata.ts +++ b/sdk/search/search-documents/src/odata.ts @@ -40,7 +40,7 @@ export function odata(strings: TemplateStringsArray, ...values: unknown[]): stri for (let i = 0; i < strings.length; i++) { results.push(strings[i]); if (i < values.length) { - if (values[i] == null || values[i] == undefined) { + if (values[i] === null || values[i] === undefined) { results.push(formatNullAndUndefined(values[i])); } else { results.push(escapeQuotesIfString(values[i], strings[i])); From 11ccadc0d85d434eaa9a97336b1350037cfda617 Mon Sep 17 00:00:00 2001 From: Sarangan Rajamanickam Date: Sat, 19 Sep 2020 00:48:14 +0000 Subject: [PATCH 6/6] Update sdk/search/search-documents/src/odata.ts Co-authored-by: Jeff Fisher --- sdk/search/search-documents/src/odata.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/search/search-documents/src/odata.ts b/sdk/search/search-documents/src/odata.ts index eaf891abd7a1..fb4898746a0e 100644 --- a/sdk/search/search-documents/src/odata.ts +++ b/sdk/search/search-documents/src/odata.ts @@ -2,7 +2,7 @@ // Licensed under the MIT license. function formatNullAndUndefined(input: unknown): string | unknown { - if (input == null || input == undefined) { + if (input === null || input === undefined) { return "null"; }