From d6a290368225d3d61af21c5ee96639ea498a8fc8 Mon Sep 17 00:00:00 2001 From: Max Pothier Date: Fri, 27 Sep 2024 15:19:00 -0600 Subject: [PATCH 1/2] add methods in Client for secondary and secondary count --- src/us_enrichment/Client.js | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/us_enrichment/Client.js b/src/us_enrichment/Client.js index ea0127c..dc90252 100644 --- a/src/us_enrichment/Client.js +++ b/src/us_enrichment/Client.js @@ -67,6 +67,46 @@ class Client { .catch(reject); }); } + + sendSecondary(lookup) { + if (typeof lookup === "undefined") throw new Errors.UndefinedLookupError(); + + let request = new Request(); + request.parameters = buildInputData(lookup, keyTranslationFormat); + + request.baseUrlParam = lookup.smartyKey + "/secondary"; + + return new Promise((resolve, reject) => { + this.sender.send(request) + .then(response => { + if (response.error) reject(response.error); + + lookup.response = response.payload; + resolve(lookup); + }) + .catch(reject); + }); + } + + sendSecondaryCount(lookup) { + if (typeof lookup === "undefined") throw new Errors.UndefinedLookupError(); + + let request = new Request(); + request.parameters = buildInputData(lookup, keyTranslationFormat); + + request.baseUrlParam = lookup.smartyKey + "/secondary/count"; + + return new Promise((resolve, reject) => { + this.sender.send(request) + .then(response => { + if (response.error) reject(response.error); + + lookup.response = response.payload; + resolve(lookup); + }) + .catch(reject); + }); + } } module.exports = Client; \ No newline at end of file From 2e16c5eec18e50b1b5131237234cc2dc47990ac5 Mon Sep 17 00:00:00 2001 From: Max Pothier Date: Mon, 30 Sep 2024 08:28:45 -0600 Subject: [PATCH 2/2] tests for the client --- tests/us_enrichment/test_Client.js | 196 ++++++++++++++++++++++++++++- 1 file changed, 190 insertions(+), 6 deletions(-) diff --git a/tests/us_enrichment/test_Client.js b/tests/us_enrichment/test_Client.js index b219c75..27cc556 100644 --- a/tests/us_enrichment/test_Client.js +++ b/tests/us_enrichment/test_Client.js @@ -41,6 +41,28 @@ describe("A US Enrichment Client", function () { expect(mockSender.request.baseUrlParam).to.deep.equal("0/geo-reference"); }) + it("composes secondary url path properly", function () { + let mockSender = new MockSender(); + let client = new Client(mockSender); + let smartyKey = "0"; + let lookup = new Lookup(smartyKey); + + client.sendSecondary(lookup); + + expect(mockSender.request.baseUrlParam).to.deep.equal("0/secondary"); + }) + + it("composes secondary count url path properly", function () { + let mockSender = new MockSender(); + let client = new Client(mockSender); + let smartyKey = "0"; + let lookup = new Lookup(smartyKey); + + client.sendSecondaryCount(lookup); + + expect(mockSender.request.baseUrlParam).to.deep.equal("0/secondary/count"); + }) + it("correctly builds parameters for a smartyKey only principal lookup.", function () { let mockSender = new MockSender(); let client = new Client(mockSender); @@ -86,10 +108,40 @@ describe("A US Enrichment Client", function () { expect(mockSender.request.parameters).to.deep.equal(expectedParameters); }); + it("correctly builds parameters for a smartyKey only secondary lookup.", function () { + let mockSender = new MockSender(); + let client = new Client(mockSender); + let smartyKey = '(>")>#'; + let include = "1"; + let lookup = new Lookup(smartyKey, include); + let expectedParameters = { + include: include, + }; + + client.sendSecondary(lookup); + + expect(mockSender.request.parameters).to.deep.equal(expectedParameters); + }); + + it("correctly builds parameters for a smartyKey only secondary count lookup.", function () { + let mockSender = new MockSender(); + let client = new Client(mockSender); + let smartyKey = '(>")>#'; + let include = "1"; + let lookup = new Lookup(smartyKey, include); + let expectedParameters = { + include: include, + }; + + client.sendSecondaryCount(lookup); + + expect(mockSender.request.parameters).to.deep.equal(expectedParameters); + }); + it("correctly builds parameters for a fully-populated principal lookup.", function () { let mockSender = new MockSender(); let client = new Client(mockSender); - let lookup = new Lookup("0","1","2","3","4"); + let lookup = new Lookup("0", "1", "2", "3", "4"); let expectedParameters = { include: "1", @@ -105,7 +157,7 @@ describe("A US Enrichment Client", function () { it("correctly builds parameters for a fully-populated financial lookup.", function () { let mockSender = new MockSender(); let client = new Client(mockSender); - let lookup = new Lookup("0","1","2","3","4"); + let lookup = new Lookup("0", "1", "2", "3", "4"); let expectedParameters = { include: "1", @@ -121,7 +173,7 @@ describe("A US Enrichment Client", function () { it("correctly builds parameters for a fully-populated geo lookup.", function () { let mockSender = new MockSender(); let client = new Client(mockSender); - let lookup = new Lookup("0","1","2","3","4"); + let lookup = new Lookup("0", "1", "2", "3", "4"); let expectedParameters = { include: "1", @@ -134,6 +186,38 @@ describe("A US Enrichment Client", function () { expect(mockSender.request.parameters).to.deep.equal(expectedParameters); }); + it("correctly builds parameters for a fully-populated secondary lookup.", function () { + let mockSender = new MockSender(); + let client = new Client(mockSender); + let lookup = new Lookup("0", "1", "2", "3", "4"); + + let expectedParameters = { + include: "1", + exclude: "2", + dataset: "3", + data_subset: "4", + }; + + client.sendSecondary(lookup); + expect(mockSender.request.parameters).to.deep.equal(expectedParameters); + }); + + it("correctly builds parameters for a fully-populated secondary count lookup.", function () { + let mockSender = new MockSender(); + let client = new Client(mockSender); + let lookup = new Lookup("0", "1", "2", "3", "4"); + + let expectedParameters = { + include: "1", + exclude: "2", + dataset: "3", + data_subset: "4", + }; + + client.sendSecondaryCount(lookup); + expect(mockSender.request.parameters).to.deep.equal(expectedParameters); + }); + it("throws an error if sending without a principal lookup.", function () { let mockSender = new MockSender(); let client = new Client(mockSender); @@ -152,13 +236,27 @@ describe("A US Enrichment Client", function () { expect(client.sendGeo).to.throw(errors.UndefinedLookupError); }); + it("throws an error if sending without a secondary lookup.", function () { + let mockSender = new MockSender(); + let client = new Client(mockSender); + expect(client.sendSecondary).to.throw(errors.UndefinedLookupError); + }); + + it("throws an error if sending without a secondary count lookup.", function () { + let mockSender = new MockSender(); + let client = new Client(mockSender); + expect(client.sendSecondaryCount).to.throw(errors.UndefinedLookupError); + }); + it("rejects with an exception if the principal response comes back with an error.", function () { let expectedError = new Error("I'm the error."); let mockSender = new MockSenderWithResponse("", expectedError); let client = new Client(mockSender); let lookup = new Lookup("¯\\_(ツ)_/¯"); - return client.sendPrincipal(lookup).catch((e) => {expect(e).to.equal(expectedError);}); + return client.sendPrincipal(lookup).catch((e) => { + expect(e).to.equal(expectedError); + }); }); it("rejects with an exception if the financial response comes back with an error.", function () { @@ -167,7 +265,9 @@ describe("A US Enrichment Client", function () { let client = new Client(mockSender); let lookup = new Lookup("¯\\_(ツ)_/¯"); - return client.sendFinancial(lookup).catch((e) => {expect(e).to.equal(expectedError);}); + return client.sendFinancial(lookup).catch((e) => { + expect(e).to.equal(expectedError); + }); }); it("rejects with an exception if the geo response comes back with an error.", function () { @@ -176,7 +276,31 @@ describe("A US Enrichment Client", function () { let client = new Client(mockSender); let lookup = new Lookup("¯\\_(ツ)_/¯"); - return client.sendGeo(lookup).catch((e) => {expect(e).to.equal(expectedError);}); + return client.sendGeo(lookup).catch((e) => { + expect(e).to.equal(expectedError); + }); + }); + + it("rejects with an exception if the secondary response comes back with an error.", function () { + let expectedError = new Error("I'm the error."); + let mockSender = new MockSenderWithResponse("", expectedError); + let client = new Client(mockSender); + let lookup = new Lookup("¯\\_(ツ)_/¯"); + + return client.sendSecondary(lookup).catch((e) => { + expect(e).to.equal(expectedError); + }); + }); + + it("rejects with an exception if the secondary count response comes back with an error.", function () { + let expectedError = new Error("I'm the error."); + let mockSender = new MockSenderWithResponse("", expectedError); + let client = new Client(mockSender); + let lookup = new Lookup("¯\\_(ツ)_/¯"); + + return client.sendSecondaryCount(lookup).catch((e) => { + expect(e).to.equal(expectedError); + }); }); it("returns an empty array when no principal respo are returned.", () => { @@ -209,6 +333,26 @@ describe("A US Enrichment Client", function () { }); }); + it("returns an empty array when no secondary suggestions are returned.", () => { + let mockSender = new MockSenderWithResponse({}); + let client = new Client(mockSender); + let lookup = new Lookup("smartyKey"); + + return client.sendSecondary(lookup).then(response => { + expect(lookup.response).to.deep.equal({}); + }); + }); + + it("returns an empty array when no secondary count suggestions are returned.", () => { + let mockSender = new MockSenderWithResponse({}); + let client = new Client(mockSender); + let lookup = new Lookup("smartyKey"); + + return client.sendSecondaryCount(lookup).then(response => { + expect(lookup.response).to.deep.equal({}); + }); + }); + it("attaches response to a principal lookup.", function () { const rawMockResponse = { smarty_key: "a", @@ -268,4 +412,44 @@ describe("A US Enrichment Client", function () { expect(lookup.response).to.deep.equal(mockResponse); }); }) + + it("attaches response to a secondary lookup.", function () { + const rawMockResponse = { + smarty_key: "a", + data_set_name: "b", + data_subset_name: "c", + attributes: { + assessed_improvement_percent: "1" + }, + }; + let mockResponse = new Response(rawMockResponse); + + let mockSender = new MockSenderWithResponse(mockResponse); + let client = new Client(mockSender); + let lookup = new Lookup("smartyKey"); + + return client.sendSecondary(lookup).then(response => { + expect(lookup.response).to.deep.equal(mockResponse); + }); + }) + + it("attaches response to a secondary count lookup.", function () { + const rawMockResponse = { + smarty_key: "a", + data_set_name: "b", + data_subset_name: "c", + attributes: { + assessed_improvement_percent: "1" + }, + }; + let mockResponse = new Response(rawMockResponse); + + let mockSender = new MockSenderWithResponse(mockResponse); + let client = new Client(mockSender); + let lookup = new Lookup("smartyKey"); + + return client.sendSecondaryCount(lookup).then(response => { + expect(lookup.response).to.deep.equal(mockResponse); + }); + }) });