Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdemartini committed Sep 29, 2020
1 parent ee406d3 commit 402aaf3
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 20 deletions.
1 change: 1 addition & 0 deletions packages/data-mate/src/vector/ListVector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class ListVector<T = unknown> extends Vector<Vector<T>> {
valueToJSON: ListVector.valueToJSON,
...options,
});
this.sortable = false;
}

fork(data: Data<Vector<T>>): ListVector<T> {
Expand Down
9 changes: 9 additions & 0 deletions packages/data-mate/src/vector/Vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ export abstract class Vector<T = unknown> {
*/
readonly data: Data<T>;

/**
* If set to false, the Vector is not sortable
*/
sortable = true;

protected readonly _size: number;

constructor(
Expand Down Expand Up @@ -123,6 +128,10 @@ export abstract class Vector<T = unknown> {
* an array with the updated indices.
*/
getSortedIndices(direction?: SortOrder): number[] {
if (!this.sortable) {
throw new Error(`Sorting is not supported for ${this.constructor.name}`);
}

const indices: number[] = Array(this._size);
const original: [number, Maybe<T>][] = Array(this._size);

Expand Down
1 change: 1 addition & 0 deletions packages/data-mate/src/vector/types/ObjectVector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class ObjectVector<
> extends Vector<T> {
constructor(options: VectorOptions<T>) {
super(VectorType.Object, options);
this.sortable = false;
}

fork(data: Data<T>): ObjectVector<T> {
Expand Down
6 changes: 5 additions & 1 deletion packages/data-mate/test/column/column-number-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,11 @@ describe('Column (Number Types)', () => {
}));
});

test.todo('should NOT be able to sort the values');
it('should NOT be able to sort the values', () => {
expect(() => {
col.sort();
}).toThrow('Sorting is not supported for ListVector');
});
});

describe(`when field type is ${FieldType.Long}`, () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/data-mate/test/column/column-string-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ describe('Column (String Types)', () => {
}));
});

test.todo('should be immutable');
it('should be immutable', () => {
expect(() => {
// @ts-expect-error
newCol.vector = 'hi' as any;
}).toThrow();
});
});
});
14 changes: 13 additions & 1 deletion packages/data-mate/test/data-frame-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,19 @@ describe('DataFrame', () => {
});
});

test.todo('should be immutable');
it('should be immutable', () => {
const dataFrame = DataFrame.fromJSON({
version: LATEST_VERSION,
fields: {
name: { type: FieldType.Keyword }
}
}, [{ name: 'Billy' }]);

expect(() => {
// @ts-expect-error
dataFrame.columns[0] = 'hi';
}).toThrow();
});

describe('when manipulating a DataFrame', () => {
type Person = { name: string; age: number; friends: string[] }
Expand Down
5 changes: 0 additions & 5 deletions packages/data-mate/test/vector/list-vector-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,5 @@ describe('ListVector', () => {
it('should be an instance of a Vector', () => {
expect(vector).toBeInstanceOf(Vector);
});

test.todo('should be immutable');
});

test.todo('->reduce');
test.todo('->filter');
});
60 changes: 48 additions & 12 deletions packages/data-mate/test/vector/vector-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import {
import { Builder, Vector } from '../../src';

describe('Vector', () => {
type Case = [type: FieldType, input: any[], output?: any[]];
type Case = [
type: FieldType,
input: any[],
output?: any[],
invalid?: any[]
];
const nowDate = new Date();
const now = nowDate.getTime();
const testCases: Case[] = [
Expand All @@ -21,8 +26,9 @@ describe('Vector', () => {
],
[
FieldType.String,
['foo', 'bar', 1, 2, null, undefined],
['foo', 'bar', '1', '2', null, null]
['foo', 'bar', true, 1, 2, null, undefined],
['foo', 'bar', 'true', '1', '2', null, null],
[{ foo: 'bar' }]
],
[
FieldType.Float,
Expand All @@ -32,7 +38,20 @@ describe('Vector', () => {
[
FieldType.Integer,
[12.344, '2.01', BigInt(200), 1, 2, null, undefined],
[12, 2, 200, 1, 2, null, null]
[12, 2, 200, 1, 2, null, null],
[-(2 ** 31) - 1, 2 ** 31 + 1, 'foo']
],
[
FieldType.Byte,
[12.344, '2.01', -1, 2, null, undefined],
[12, 2, -1, 2, null, null],
[128, -129, 'bar']
],
[
FieldType.Short,
[12.344, '-2.01', 1000, 2, null, undefined],
[12, -2, 1000, 2, null, null],
[32_768, -32_769, 'baz', '++1']
],
[
FieldType.Long,
Expand All @@ -42,7 +61,8 @@ describe('Vector', () => {
[
FieldType.Boolean,
['yes', 'no', true, false, 0, 1, null, undefined],
[true, false, true, false, false, true, null, null]
[true, false, true, false, false, true, null, null],
['Y E S', 'foo', 23]
],
[
FieldType.Date,
Expand All @@ -54,7 +74,8 @@ describe('Vector', () => {
'1941-08-20T07:00:00.000Z',
null,
null
]
],
['not a date', Number.NaN],
],
[
FieldType.IP,
Expand Down Expand Up @@ -113,7 +134,7 @@ describe('Vector', () => {
{ lat: 89.002, lon: 20.034990 },
null,
null
]
],
],
[
FieldType.GeoJSON,
Expand Down Expand Up @@ -219,10 +240,14 @@ describe('Vector', () => {
null,
null
],
[
[],
'foo',
],
],
];

describe.each(testCases)('when field type is %s', (type, input, output) => {
describe.each(testCases)('when field type is %s', (type, input, output, invalid) => {
let vector: Vector<any>;
let expected: any[];
beforeAll(() => {
Expand Down Expand Up @@ -265,13 +290,24 @@ describe('Vector', () => {
}
});

if (invalid?.length) {
test.each(invalid)('should NOT be able to parse %p', (val) => {
const builder = Builder.make({ type, array: false });
expect(() => {
builder.append(val);
}).toThrowError();
});
}

it('should be an instance of a Vector', () => {
expect(vector).toBeInstanceOf(Vector);
});

test.todo('should be immutable');
it('should be immutable', () => {
expect(() => {
// @ts-expect-error
vector.data.values[0] = '10';
}).toThrow();
});
});

test.todo('->reduce');
test.todo('->filter');
});

0 comments on commit 402aaf3

Please sign in to comment.