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

Add tests and fix for regressed column update. #186

Merged
merged 8 commits into from
Aug 10, 2018
22 changes: 18 additions & 4 deletions packages/perspective/src/js/perspective.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,26 @@ function parse_data(data, names, types) {
}

} else if (Array.isArray(data[Object.keys(data)[0]])) {

// Column oriented

names = Object.keys(data);

let colNum = 0;
for (let name in data) {
names.push(name);
types.push(infer_type(data[name][0]));
let transformed = transform_data(types[types.length - 1], data[name]);
// Infer column type if necessary
if (!preloaded) {
let i = 0;
let inferredType = null;
while(inferredType === null && i < 100 && i < data[name].length) {
inferredType = infer_type(data[name][i]);
i++;
}
inferredType = inferredType || __MODULE__.t_dtype.DTYPE_STR;
types.push(inferredType);
}

let transformed = transform_data(types[colNum], data[name]);
colNum++;
cdata.push(transformed);
row_count = transformed.length
}
Expand Down
13 changes: 13 additions & 0 deletions packages/perspective/test/js/constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ var data = [
{'x': 4, 'y':'d', 'z': false}
];

var col_data = {
'x': [1, 2, 3, 4],
'y': ['a', 'b', 'c', 'd'],
'z': [true, false, true, false]
};

var meta = {
'x': "integer",
'y': "string",
Expand Down Expand Up @@ -181,6 +187,13 @@ module.exports = (perspective) => {
expect(data).toEqual(result);
});

it("JSON column oriented constructor", async function () {
var table = perspective.table(col_data);
var view = table.view();
let result = await view.to_json();
expect(data).toEqual(result);
});

it("Arrow constructor", async function () {
var table = perspective.table(arrow.slice());
var view = table.view();
Expand Down
36 changes: 36 additions & 0 deletions packages/perspective/test/js/updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ var data = [
{'x': 4, 'y':'d', 'z': false}
];

var col_data = {
'x': [1, 2, 3, 4],
'y': ['a', 'b', 'c', 'd'],
'z': [true, false, true, false]
};

var meta = {
'x': "integer",
'y': "string",
Expand Down Expand Up @@ -84,6 +90,36 @@ module.exports = (perspective) => {
expect(data).toEqual(result);
});

it("Meta constructor then column oriented `update()`", async function () {
var table = perspective.table(meta);
table.update(col_data);
var view = table.view();
let result = await view.to_json();
expect(result).toEqual(data);
});

it("Column data constructor then column oriented `update()`", async function () {
var colUpdate = {
'x': [3, 4, 5],
'y': ['h', 'i', 'j'],
'z': [false, true, false],
};

var expected = [
{'x': 1, 'y':'a', 'z': true},
{'x': 2, 'y':'b', 'z': false},
{'x': 3, 'y':'h', 'z': false},
{'x': 4, 'y':'i', 'z': true},
{'x': 5, 'y':'j', 'z': false}
];

var table = perspective.table(col_data, {index: "x"});
table.update(colUpdate);
var view = table.view();
let result = await view.to_json();
expect(result).toEqual(expected);
});

it("Multiple `update()`s", async function () {
var table = perspective.table(meta);
table.update(data);
Expand Down