Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BonapartePC committed Sep 12, 2024
1 parent 16598ed commit e8f82fc
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/warehouse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ function isStringLikeObject(obj) {
if (numKey > maxKey) maxKey = numKey;
}

for (let i = minKey; i <= maxKey; i++) {
if (!keys.includes(i.toString())) return false;
}

return (minKey === 0 || minKey === 1) && maxKey - minKey + 1 === keys.length;
}

Expand Down
100 changes: 89 additions & 11 deletions test/__tests__/warehouse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1236,17 +1236,95 @@ describe("isBlank", () => {
});

describe("context traits", () => {
for (const e of eventTypes) {
let i = input(e);
i.message.context= {"traits": {1: "f", 2: "o"}};
i.message.traits = "bar";
if (i.metadata) delete i.metadata.sourceCategory;
transformers.forEach((transformer, index) => {
const received = transformer.process(i);
let columns = Object.keys(received[0].metadata.columns);
console.log(columns);
expect(received[0].metadata.columns[integrationCasedString(integrations[index], "context_traits")]).toEqual("string");
expect(received[0].data[integrationCasedString(integrations[index], "context_traits")]).toEqual("fo");
const testCases = [
{
name: "context traits with string like object",
input: {"1":"f","2":"o", "3":"o"},
expectedData: "foo",
expectedMetadata: "string",
expectedColumns: ["context_traits"]
},
{
name: "context traits with string like object with missing keys",
input: {"1":"a","3":"a"},
expectedData: "a",
expectedMetadata: "string",
expectedColumns: ["context_traits_1", "context_traits_3"]
},
{
name: "context traits with empty object",
input: {},
expectedData: {},
expectedColumns: [],
},
{
name: "context traits with empty array",
input: [],
expectedData: [],
expectedMetadata: "array",
expectedColumns: []
},
{
name: "context traits with null",
input: null,
expectedData: null,
expectedMetadata: "null",
expectedColumns: []
},
{
name: "context traits with undefined",
input: undefined,
expectedData: undefined,
expectedMetadata: "undefined",
expectedColumns: []
},
{
name: "context traits with string",
input: "already a string",
expectedData: "already a string",
expectedMetadata: "string",
expectedColumns: ["context_traits"]
},
{
name: "context traits with number",
input: 42,
expectedData: 42,
expectedMetadata: "int",
expectedColumns: ["context_traits"]
},
{
name: "context traits with boolean",
input: true,
expectedData: true,
expectedMetadata: "boolean",
expectedColumns: ["context_traits"]
},
{
name: "context traits with array",
input: ["a", "b", "cd"],
expectedData: ["a", "b", "cd"],
expectedMetadata: "string",
expectedColumns: ["context_traits"]
}
];
for (const t of testCases) {
it(`should return ${t.expectedData} for ${t.name}`, () => {
for (const e of eventTypes) {
let i = input(e);
i.message.context = {"traits": t.input};
if (i.metadata) delete i.metadata.sourceCategory;
transformers.forEach((transformer, index) => {
const received = transformer.process(i);
if(t.expectedColumns.length === 0) {
expect(received[0].metadata.columns[integrationCasedString(integrations[index], "context_traits")]).toEqual(undefined);
expect(received[0].metadata.columns[integrationCasedString(integrations[index], "context_traits")]).toEqual(undefined);
}
for (const column of t.expectedColumns) {
expect(received[0].metadata.columns[integrationCasedString(integrations[index], column)]).toEqual(t.expectedMetadata);
expect(received[0].data[integrationCasedString(integrations[index], column)]).toEqual(t.expectedData);
}
});
}
});
}
})

0 comments on commit e8f82fc

Please sign in to comment.