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

Max/add secondary enrichment #91

Merged
merged 2 commits into from
Oct 7, 2024
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
40 changes: 40 additions & 0 deletions src/us_enrichment/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
196 changes: 190 additions & 6 deletions tests/us_enrichment/test_Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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);
Expand All @@ -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 () {
Expand All @@ -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 () {
Expand All @@ -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.", () => {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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);
});
})
});
Loading