Skip to content

Commit

Permalink
Merged in bugfix/CORE-1879-jswidesky-client-batch.updateo (pull request
Browse files Browse the repository at this point in the history
#40)

Bugfix/CORE-1879 jswidesky client batch.updateo

Approved-by: Elvio Wang
  • Loading branch information
Daniel Henley authored and Elvio Wang committed Jun 3, 2024
2 parents abaf894 + 18e9d37 commit 98e780d
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

#### FIXED
- `WideSkyClient.batch.updateOrCreate` errored with list type tags.

## [3.1.0] - 2024-05-23
### ADDED
- Added new [options](./docs/client/options.md) under `http` which allows an optional configuration
Expand Down
20 changes: 19 additions & 1 deletion src/client/functions/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,24 @@ const createUpdatePayload = (oldEntity, newEntity, logger) => {

if (oldEntity[tag] === undefined) {
updatePayload[tag] = value;
} else if (Array.isArray(value) && Array.isArray(oldEntity[tag])) {
if (value.length !== oldEntity[tag].length) {
// Not the same, no need to verify
updatePayload[tag] = value;
}

// verify list changes
let isDifferent = false;
for (const item of value) {
if (!(oldEntity[tag].includes(item))) {
isDifferent = true;
break;
}
}

if (isDifferent) {
updatePayload[tag] = value;
}
} else if (oldEntity[tag] !== value) {
if (tag.includes("Ref") || ["metaOf"].includes(tag)) {
// Ref can be different by the ID is what matters here
Expand Down Expand Up @@ -1237,4 +1255,4 @@ module.exports = {
addChildrenByFilter,
multiFind,
updateOrCreate
};
};
66 changes: 64 additions & 2 deletions test/client/functions/batch/updateOrCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("client.batch.updateOrCreate", () => {
success: TEST_ENTITIES.map((entity) => [entity]),
errors: []
};
})
});
ws.batch.create = sinon.stub().callsFake((entities) => {
return {
success: {
Expand Down Expand Up @@ -254,6 +254,68 @@ describe("client.batch.updateOrCreate", () => {
});
});

describe("tag types", () => {
describe("list tag", () => {
beforeEach(() => {
const testEntities = JSON.parse(JSON.stringify(TEST_ENTITIES));
testEntities[0].roleRefs = ["r:id1", "r:id2", "r:id3"];
ws.batch.multiFind = sinon.stub().callsFake((filterAndLimits) => {
return {
success: testEntities.map((entity) => [entity]),
errors: []
};
});
});

it("should not update if the list is the same", async () => {
const testEntities = JSON.parse(JSON.stringify(TEST_ENTITIES));
testEntities[0].roleRefs = ["r:id1", "r:id2", "r:id3"];
await ws.batch.updateOrCreate(testEntities);
expect(ws.batch.create.notCalled).to.be.true;
expect(ws.batch.update.notCalled).to.be.true;
});

it("should not update if the list is the same but different order", async () => {
const testEntities = JSON.parse(JSON.stringify(TEST_ENTITIES));
testEntities[0].roleRefs = ["r:id3", "r:id2", "r:id1"];
await ws.batch.updateOrCreate(testEntities);
expect(ws.batch.create.notCalled).to.be.true;
expect(ws.batch.update.notCalled).to.be.true;
});

it("should update if list different sizes", async () => {
const testEntities = JSON.parse(JSON.stringify(TEST_ENTITIES));
testEntities[0].roleRefs = ["r:id2", "r:id1"];
await ws.batch.updateOrCreate(testEntities);
expect(ws.batch.create.notCalled).to.be.true;
expect(ws.batch.update.calledOnce).to.be.true;
expect(ws.batch.update.getCall(0).args[0]).to.eql([{
"id": "r:fbf5ace2-b706-11ec-a270-0242ac120002",
"roleRefs": [
"r:id2",
"r:id1"
]
}]);
});

it("should update if list has 1 item that is different", async () => {
const testEntities = JSON.parse(JSON.stringify(TEST_ENTITIES));
testEntities[0].roleRefs = ["r:id2", "r:id1", "r:id4"];
await ws.batch.updateOrCreate(testEntities);
expect(ws.batch.create.notCalled).to.be.true;
expect(ws.batch.update.calledOnce).to.be.true;
expect(ws.batch.update.getCall(0).args[0]).to.eql([{
"id": "r:fbf5ace2-b706-11ec-a270-0242ac120002",
"roleRefs": [
"r:id2",
"r:id1",
"r:id4"
]
}]);
});
});
});

describe("options", () => {
it("should pass to client.batch.update", async () => {
const testEntities = JSON.parse(JSON.stringify(TEST_ENTITIES));
Expand Down Expand Up @@ -440,4 +502,4 @@ describe("client.batch.updateOrCreate", () => {
});
});
});
});
});

0 comments on commit 98e780d

Please sign in to comment.