Permalink
Browse files

changes get() and getRaw() methods of entity dictionary: Now casting …

…returned numbers to strings, aligning both methods to the already specified return type (string|undefined). Previously, if entities in request/extraction have been numbers, dictionary did not return them as string, resulting in unexpected/untyped behaviour.
  • Loading branch information...
antoniusostermann committed Jan 4, 2019
1 parent a2c4033 commit a0eb9fc9f549164cb9b6fb084069b32f0e546f7d
Showing with 105 additions and 49 deletions.
  1. +1 −0 CHANGELOG
  2. +97 −48 spec/components/unifier/entity-dictionary.spec.ts
  3. +7 −1 src/components/unifier/entity-dictionary.ts
@@ -1,5 +1,6 @@
version 0.4.0
- changes default behaviour of AssistantJSSetup's constructor to create a new container instead of using a global one. Removes global container.
- changes get() and getRaw() methods of entity dictionary: Now casting returned numbers to strings, aligning both methods to the already specified return type (string|undefined). Previously, if entities in request/extraction have been numbers, dictionary did not return them as string, resulting in unexpected/untyped behaviour.
- changes get() method of entity dictionary: If you are getting an assistantjs-configured custom entitiy and user passed a synonym of an entity value, get() automatically resolves to the entity value. Use getRaw() f you want to get the original passed value.
- changes get() method of entity dictionary: If you are getting an assistantjs-configured custom entitiy, get() now always return the value with the nearest levenshtein distance instead of the original value. If you want the original value, use getRaw() instead. For non-custom-entities, get() returns the regular value.
- adds listKeys and getSubset to session interface
@@ -4,13 +4,13 @@ import { LocalesLoaderMock } from "../../support/mocks/unifier/mock-locale-loade
import { createRequestScope } from "../../support/util/setup";
import { ThisContext } from "../../this-context";

interface MyThisContext extends ThisContext {
interface CurrentThisContext extends ThisContext {
createExtraction(entities: { [name: string]: any });
getEntityDictionary(): EntityDictionary;
}

describe("EntityDictionary", function() {
beforeAll(function(this: MyThisContext) {
beforeAll(function(this: CurrentThisContext) {
this.createExtraction = function(entities) {
return {
entities,
@@ -22,7 +22,7 @@ describe("EntityDictionary", function() {
};
});

beforeEach(async function(this: MyThisContext) {
beforeEach(async function(this: CurrentThisContext) {
this.getEntityDictionary = () => this.container.inversifyInstance.get("core:unifier:current-entity-dictionary");
});

@@ -48,7 +48,7 @@ describe("EntityDictionary", function() {
this.entities.set("myEntity", "myEntity");
});

describe("getDistanceSet", function() {
describe("#getDistanceSet", function() {
describe("if entity is not present", function() {
it("returns undefined", function() {
expect(this.entities.getDistanceSet("notExisting", this.distanceSet)).toBeUndefined();
@@ -71,7 +71,7 @@ describe("EntityDictionary", function() {
});
});

describe("getClosest", function() {
describe("#getClosest", function() {
describe("if entity is not present", function() {
it("returns undefined", function() {
expect(this.entities.getClosest("notExisting", this.distanceSet)).toBeUndefined();
@@ -107,83 +107,132 @@ describe("EntityDictionary", function() {
});
});

describe("get custom entities", function() {
beforeEach(function(this: MyThisContext) {
this.container.inversifyInstance.unbind(injectionNames.localesLoader);
this.container.inversifyInstance.bind(injectionNames.localesLoader).to(LocalesLoaderMock);
});

describe("if custom entity is not present", function() {
beforeEach(function(this: MyThisContext) {
createRequestScope(this.specHelper);
});

it("returns undefined", function(this: MyThisContext) {
expect(this.getEntityDictionary().get("color")).toBeUndefined();
});
});

describe("extraction contains exact reference value", function() {
beforeEach(function(this: MyThisContext) {
describe("#getRaw", function() {
describe("for numbers in store", function() {
beforeEach(async function(this: CurrentThisContext) {
createRequestScope(
this.specHelper,
this.createExtraction({
color: "red",
numberKey: 1234,
})
);
});

it("returns the correct reference value", function(this: MyThisContext) {
expect(this.getEntityDictionary().get("color")).toBe("red");
it("casts value to string", async function(this: CurrentThisContext) {
expect(this.getEntityDictionary().getRaw("numberKey")).toEqual("1234");
});
});

describe("extraction contains exact synonym", function() {
beforeEach(function(this: MyThisContext) {
describe("for null values in store", function() {
beforeEach(async function(this: CurrentThisContext) {
createRequestScope(
this.specHelper,
this.createExtraction({
color: "corn",
nullValue: null,
})
);
});

it("returns the correct reference value", function(this: MyThisContext) {
expect(this.getEntityDictionary().get("color")).toBe("yellow");
it("casts them to undefined", async function(this: CurrentThisContext) {
expect(this.getEntityDictionary().getRaw("nullValue")).toBeUndefined();
});
});

describe("extraction contains similar synonym", function() {
beforeEach(function(this: MyThisContext) {
describe("for everything else", function() {
beforeEach(async function(this: CurrentThisContext) {
createRequestScope(
this.specHelper,
this.createExtraction({
color: "scarlet",
complexObject: ["a", "b"],
})
);
});

it("returns the correct reference value", function(this: MyThisContext) {
expect(this.getEntityDictionary().get("color")).toBe("red");
it("returns raw store value", async function(this: CurrentThisContext) {
expect(this.getEntityDictionary().getRaw("complexObject")).toEqual(["a", "b"] as any);
});
});
});

describe("extraction contains unsupported synonym or entity variation", function() {
beforeEach(function(this: MyThisContext) {
createRequestScope(
this.specHelper,
this.createExtraction({
color: "ferrari",
})
);
describe("#get", function() {
describe("regarding custom entities", function() {
beforeEach(function(this: CurrentThisContext) {
this.container.inversifyInstance.unbind(injectionNames.localesLoader);
this.container.inversifyInstance.bind(injectionNames.localesLoader).to(LocalesLoaderMock);
});

describe("if custom entity is not present", function() {
beforeEach(function(this: CurrentThisContext) {
createRequestScope(this.specHelper);
});

it("returns undefined", function(this: CurrentThisContext) {
expect(this.getEntityDictionary().get("color")).toBeUndefined();
});
});

it("returns an unexpected value", function(this: MyThisContext) {
expect(this.getEntityDictionary().get("color")).toBe("green");
describe("extraction contains exact reference value", function() {
beforeEach(function(this: CurrentThisContext) {
createRequestScope(
this.specHelper,
this.createExtraction({
color: "red",
})
);
});

it("returns the correct reference value", function(this: CurrentThisContext) {
expect(this.getEntityDictionary().get("color")).toBe("red");
});
});

describe("extraction contains exact synonym", function() {
beforeEach(function(this: CurrentThisContext) {
createRequestScope(
this.specHelper,
this.createExtraction({
color: "corn",
})
);
});

it("returns the correct reference value", function(this: CurrentThisContext) {
expect(this.getEntityDictionary().get("color")).toBe("yellow");
});
});

describe("extraction contains similar synonym", function() {
beforeEach(function(this: CurrentThisContext) {
createRequestScope(
this.specHelper,
this.createExtraction({
color: "scarlet",
})
);
});

it("returns the correct reference value", function(this: CurrentThisContext) {
expect(this.getEntityDictionary().get("color")).toBe("red");
});
});

it("also gives access to raw entity value", function(this: MyThisContext) {
expect(this.getEntityDictionary().getRaw("color")).toBe("ferrari");
describe("extraction contains unsupported synonym or entity variation", function() {
beforeEach(function(this: CurrentThisContext) {
createRequestScope(
this.specHelper,
this.createExtraction({
color: "ferrari",
})
);
});

it("returns an unexpected value", function(this: CurrentThisContext) {
expect(this.getEntityDictionary().get("color")).toBe("green");
});

it("also gives access to raw entity value", function(this: CurrentThisContext) {
expect(this.getEntityDictionary().getRaw("color")).toBe("ferrari");
});
});
});
});
@@ -32,7 +32,13 @@ export class EntityDictionary implements EntityDictionaryInterface {
}

public getRaw(name: string) {
return this.store[name] === null ? undefined : this.store[name];
// Cast "null" values to undefined
if (this.store[name] === null) {
return undefined;
}

// Cast "number" values to string
return typeof this.store[name] === "number" ? String(this.store[name]) : this.store[name];
}

public contains(name: string) {

0 comments on commit a0eb9fc

Please sign in to comment.