Skip to content

Commit 8d44dcd

Browse files
committed
update tests
1 parent 6d2c03d commit 8d44dcd

6 files changed

Lines changed: 113 additions & 59 deletions

File tree

js/src/vector/table.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,25 @@ import { StructVector, StructRow } from './struct';
2020
import { readVectors, readVectorsAsync } from '../reader/arrow';
2121

2222
export class Table<T> extends StructVector<T> {
23-
static from(buffers: Iterable<Uint8Array | Buffer | string>) {
23+
static from(buffers?: Iterable<Uint8Array | Buffer | string>) {
2424
let columns: Vector<any>[] = [];
25-
for (let vectors of readVectors(buffers)) {
26-
columns = columns.length === 0 ? vectors : vectors.map((vec, i, _vs, col = columns[i]) =>
27-
vec && col && col.concat(vec) || col || vec
28-
) as Vector<any>[];
25+
if (buffers) {
26+
for (let vectors of readVectors(buffers)) {
27+
columns = columns.length === 0 ? vectors : vectors.map((vec, i, _vs, col = columns[i]) =>
28+
vec && col && col.concat(vec) || col || vec
29+
) as Vector<any>[];
30+
}
2931
}
3032
return new Table({ columns });
3133
}
32-
static async fromAsync(buffers: AsyncIterable<Uint8Array | Buffer | string>) {
34+
static async fromAsync(buffers?: AsyncIterable<Uint8Array | Buffer | string>) {
3335
let columns: Vector<any>[] = [];
34-
for await (let vectors of readVectorsAsync(buffers)) {
35-
columns = columns.length === 0 ? vectors : vectors.map((vec, i, _vs, col = columns[i]) =>
36-
vec && col && col.concat(vec) || col || vec
37-
) as Vector<any>[];
36+
if (buffers) {
37+
for await (let vectors of readVectorsAsync(buffers)) {
38+
columns = columns.length === 0 ? vectors : vectors.map((vec, i, _vs, col = columns[i]) =>
39+
vec && col && col.concat(vec) || col || vec
40+
) as Vector<any>[];
41+
}
3842
}
3943
return new Table({ columns });
4044
}

js/test/reader-tests.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
import { readBuffers } from './Arrow';
18+
import Arrow from './Arrow';
19+
const { readVectors } = Arrow;
1920
import { config, sources, formats } from './test-config';
2021

2122
describe(`readBuffers`, () => {
@@ -38,7 +39,7 @@ describe(`readBuffers`, () => {
3839
function testReaderIterator(buffers: Uint8Array[]) {
3940
test(`reads each batch as an Array of Vectors`, () => {
4041
expect.hasAssertions();
41-
for (const vectors of readBuffers(...buffers)) {
42+
for (const vectors of readVectors(buffers)) {
4243
for (const vector of vectors) {
4344
expect(vector.name).toMatchSnapshot();
4445
expect(vector.type).toMatchSnapshot();
@@ -54,7 +55,7 @@ function testReaderIterator(buffers: Uint8Array[]) {
5455
function testVectorIterator(buffers: Uint8Array[]) {
5556
test(`vector iterators report the same values as get`, () => {
5657
expect.hasAssertions();
57-
for (const vectors of readBuffers(...buffers)) {
58+
for (const vectors of readVectors(buffers)) {
5859
for (const vector of vectors) {
5960
let i = -1, n = vector.length;
6061
for (let v of vector) {

js/test/table-tests.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
import { Table, readBuffers } from './Arrow';
18+
import Arrow from './Arrow';
19+
const { Table, readVectors } = Arrow;
1920
import { config, sources, formats } from './test-config';
2021

2122
describe(`Table`, () => {
@@ -44,7 +45,7 @@ describe(`Table`, () => {
4445
function testTableFromBuffers(buffers: Uint8Array[]) {
4546
test(`creates a Table from Arrow buffers`, () => {
4647
expect.hasAssertions();
47-
const table = Table.from(...buffers);
48+
const table = Table.from(buffers);
4849
for (const vector of table.columns) {
4950
expect(vector.name).toMatchSnapshot();
5051
expect(vector.type).toMatchSnapshot();
@@ -59,7 +60,7 @@ function testTableFromBuffers(buffers: Uint8Array[]) {
5960
function testColumnIterators(buffers: Uint8Array[]) {
6061
test(`vector iterators report the same values as get`, () => {
6162
expect.hasAssertions();
62-
const table = Table.from(...buffers);
63+
const table = Table.from(buffers);
6364
for (const vector of table.columns) {
6465
let i = -1, n = vector.length;
6566
for (let v of vector) {
@@ -75,8 +76,8 @@ function testReaderVectorsAndTableColumns(buffers: Uint8Array[]) {
7576
test(`batch and Table Vectors report the same values`, () => {
7677
expect.hasAssertions();
7778
let rowsTotal = 0;
78-
let table = Table.from(...buffers);
79-
for (let vectors of readBuffers(...buffers)) {
79+
let table = Table.from(buffers);
80+
for (let vectors of readVectors(buffers)) {
8081
let rowsNow = Math.max(...vectors.map((v) => v.length));
8182
for (let vi = -1, vn = vectors.length; ++vi < vn;) {
8283
let v1 = vectors[vi];
@@ -95,7 +96,7 @@ function testReaderVectorsAndTableColumns(buffers: Uint8Array[]) {
9596
function testTableRowIterator(buffers: Uint8Array[]) {
9697
test(`enumerates Table rows`, () => {
9798
expect.hasAssertions();
98-
const table = Table.from(...buffers);
99+
const table = Table.from(buffers);
99100
expect(table.length).toMatchSnapshot();
100101
expect(table.columns.length).toMatchSnapshot();
101102
for (const row of table) {
@@ -107,7 +108,7 @@ function testTableRowIterator(buffers: Uint8Array[]) {
107108
function testTableRowIteratorCompact(buffers: Uint8Array[]) {
108109
test(`enumerates Table rows compact`, () => {
109110
expect.hasAssertions();
110-
const table = Table.from(...buffers);
111+
const table = Table.from(buffers);
111112
expect(table.length).toMatchSnapshot();
112113
expect(table.columns.length).toMatchSnapshot();
113114
for (const row of table) {
@@ -124,12 +125,12 @@ function testEmptyTableToString() {
124125

125126
function testTableToStringPretty(buffers: Uint8Array[]) {
126127
test(`toString() prints a pretty Table`, () => {
127-
expect(Table.from(...buffers).toString()).toMatchSnapshot();
128+
expect(Table.from(buffers).toString()).toMatchSnapshot();
128129
});
129130
}
130131

131132
function testTableToStringPrettyWithIndex(buffers: Uint8Array[]) {
132133
test(`toString({ index: true }) prints a pretty Table with an Index column`, () => {
133-
expect(Table.from(...buffers).toString({ index: true })).toMatchSnapshot();
134+
expect(Table.from(buffers).toString({ index: true })).toMatchSnapshot();
134135
});
135136
}

js/test/test-config.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,20 @@ export const config = sources.reduce((sources, source) => ({
4242
export type Arrows = { name: string, buffers: Uint8Array[] }[];
4343

4444
function loadArrows(source: string, format: string) {
45+
const files: any = {
46+
nested: true,
47+
simple: true,
48+
decimal: true,
49+
datetime: false, // <-- known to fail
50+
primitive: true,
51+
dictionary: true,
52+
struct_example: true
53+
};
4554
const arrows = [];
4655
const filenames = glob.sync(path.resolve(__dirname, `arrows/${source}/${format}`, `*.arrow`));
4756
for (const filename of filenames) {
4857
const { name } = path.parse(filename);
49-
if (name === 'decimal') { continue; }
58+
if (files[name] !== true) { continue; }
5059
arrows.push({ name, buffers: [fs.readFileSync(filename)] });
5160
}
5261
return arrows as Arrows;

js/test/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"extends": "../tsconfig.json",
33
"include": ["./**/*.ts"],
44
"compilerOptions": {
5-
"target": "ESNEXT",
5+
"target": "es2015",
66
"module": "commonjs",
77
"allowJs": true,
88
"importHelpers": false,

js/test/vector-tests.ts

Lines changed: 74 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
import { flatbuffers } from 'flatbuffers';
19-
import Long = flatbuffers.Long;
18+
import Arrow from './Arrow';
2019
import {
20+
TypedArray,
21+
TypedArrayConstructor,
22+
NumericVectorConstructor,
23+
} from './Arrow';
24+
25+
const {
2126
BoolVector,
22-
TypedVector,
2327
Int64Vector,
2428
Uint64Vector,
2529
Int8Vector,
@@ -28,28 +32,29 @@ import {
2832
Uint8Vector,
2933
Uint16Vector,
3034
Uint32Vector,
35+
Float16Vector,
3136
Float32Vector,
3237
Float64Vector,
33-
} from './Arrow';
38+
} = Arrow;
3439

35-
const LongVectors = {
36-
Int64Vector: [Int64Vector, Int32Array],
37-
Uint64Vector: [Uint64Vector, Uint32Array]
40+
const FixedSizeVectors = {
41+
Int64Vector: [Int64Vector, Int32Array] as [NumericVectorConstructor<number, any>, any],
42+
Uint64Vector: [Uint64Vector, Uint32Array] as [NumericVectorConstructor<number, any>, any]
3843
};
3944

40-
const TypedVectors = {
41-
Int8Vector: [Int8Vector, Int8Array],
42-
Int16Vector: [Int16Vector, Int16Array],
43-
Int32Vector: [Int32Vector, Int32Array],
44-
Uint8Vector: [Uint8Vector, Uint8Array],
45-
Uint16Vector: [Uint16Vector, Uint16Array],
46-
Uint32Vector: [Uint32Vector, Uint32Array],
47-
Float32Vector: [Float32Vector, Float32Array],
48-
Float64Vector: [Float64Vector, Float64Array]
45+
const FixedWidthVectors = {
46+
Int8Vector: [Int8Vector, Int8Array] as [NumericVectorConstructor<number, any>, any],
47+
Int16Vector: [Int16Vector, Int16Array] as [NumericVectorConstructor<number, any>, any],
48+
Int32Vector: [Int32Vector, Int32Array] as [NumericVectorConstructor<number, any>, any],
49+
Uint8Vector: [Uint8Vector, Uint8Array] as [NumericVectorConstructor<number, any>, any],
50+
Uint16Vector: [Uint16Vector, Uint16Array] as [NumericVectorConstructor<number, any>, any],
51+
Uint32Vector: [Uint32Vector, Uint32Array] as [NumericVectorConstructor<number, any>, any],
52+
Float32Vector: [Float32Vector, Float32Array] as [NumericVectorConstructor<number, any>, any],
53+
Float64Vector: [Float64Vector, Float64Array] as [NumericVectorConstructor<number, any>, any]
4954
};
5055

51-
const longVectors = toMap<[typeof TypedVector, any]>(LongVectors, Object.keys(LongVectors));
52-
const byteVectors = toMap<[typeof TypedVector, any]>(TypedVectors, Object.keys(TypedVectors));
56+
const fixedSizeVectors = toMap(FixedSizeVectors, Object.keys(FixedSizeVectors));
57+
const fixedWidthVectors = toMap(FixedWidthVectors, Object.keys(FixedWidthVectors));
5358
const bytes = Array.from(
5459
{ length: 5 },
5560
() => Uint8Array.from(
@@ -127,30 +132,66 @@ describe(`BoolVector`, () => {
127132
});
128133
});
129134

130-
for (const [VectorName, [VectorType, ArrayType]] of longVectors) {
135+
describe('Float16Vector', () => {
136+
const values = concatTyped(Uint16Array, ...bytes);
137+
const vector = bytes
138+
.map((b) => new Float16Vector({ data: new Uint16Array(b.buffer) }))
139+
.reduce((v: any, v2) => v.concat(v2));
140+
const n = values.length;
141+
const clamp = (x: number) => Math.min((x - 32767) / 32767, 1);
142+
test(`gets expected values`, () => {
143+
let i = -1;
144+
while (++i < n) {
145+
expect(vector.get(i)).toEqual(clamp(values[i]));
146+
}
147+
});
148+
test(`iterates expected values`, () => {
149+
expect.hasAssertions();
150+
let i = -1;
151+
for (let v of vector) {
152+
expect(++i).toBeLessThan(n);
153+
expect(v).toEqual(clamp(values[i]));
154+
}
155+
});
156+
test(`slices the entire array`, () => {
157+
expect(vector.slice()).toEqual(values);
158+
});
159+
test(`slice returns a TypedArray`, () => {
160+
expect(vector.slice()).toBeInstanceOf(Uint16Array);
161+
});
162+
test(`slices from -20 to length`, () => {
163+
expect(vector.slice(-20)).toEqual(values.slice(-20));
164+
});
165+
test(`slices from 0 to -20`, () => {
166+
expect(vector.slice(0, -20)).toEqual(values.slice(0, -20));
167+
});
168+
test(`slices the array from 0 to length - 20`, () => {
169+
expect(vector.slice(0, n - 20)).toEqual(values.slice(0, n - 20));
170+
});
171+
test(`slices the array from 0 to length + 20`, () => {
172+
expect(vector.slice(0, n + 20)).toEqual(
173+
concatTyped(Uint16Array, values, values.slice(0, 20)));
174+
});
175+
});
176+
177+
for (const [VectorName, [VectorType, ArrayType]] of fixedSizeVectors) {
131178
describe(`${VectorName}`, () => {
132179
const values = concatTyped(ArrayType, ...bytes);
133180
const vector = bytes
134-
.map((b) => new VectorType<Long, any>({
135-
data: new ArrayType(b.buffer)
136-
}))
181+
.map((b) => new VectorType({ data: new ArrayType(b.buffer) }))
137182
.reduce((v: any, v2) => v.concat(v2));
138183
const n = values.length * 0.5;
139184
test(`gets expected values`, () => {
140185
let i = -1;
141186
while (++i < n) {
142-
expect(vector.get(i)).toEqual(new Long(
143-
values[i * 2], values[i * 2 + 1]
144-
));
187+
expect(vector.get(i)).toEqual(values.slice(2 * i, 2 * (i + 1)));
145188
}
146189
});
147190
test(`iterates expected values`, () => {
148191
let i = -1;
149192
for (let v of vector) {
150193
expect(++i).toBeLessThan(n);
151-
expect(v).toEqual(new Long(
152-
values[i * 2], values[i * 2 + 1]
153-
));
194+
expect(v).toEqual(values.slice(2 * i, 2 * (i + 1)));
154195
}
155196
});
156197
test(`slices the entire array`, () => {
@@ -175,13 +216,11 @@ for (const [VectorName, [VectorType, ArrayType]] of longVectors) {
175216
});
176217
}
177218

178-
for (const [VectorName, [VectorType, ArrayType]] of byteVectors) {
219+
for (const [VectorName, [VectorType, ArrayType]] of fixedWidthVectors) {
179220
describe(`${VectorName}`, () => {
180221
const values = concatTyped(ArrayType, ...bytes);
181222
const vector = bytes
182-
.map((b) => new VectorType<number, any>({
183-
data: new ArrayType(b.buffer)
184-
}))
223+
.map((b) => new VectorType({ data: new ArrayType(b.buffer) }))
185224
.reduce((v: any, v2) => v.concat(v2));
186225

187226
const n = values.length;
@@ -221,14 +260,14 @@ for (const [VectorName, [VectorType, ArrayType]] of byteVectors) {
221260
});
222261
}
223262

224-
function toMap<T>(entries: any, keys: string[]) {
263+
function toMap<T>(entries: Record<string, T>, keys: string[]) {
225264
return keys.reduce((map, key) => {
226265
map.set(key, entries[key] as T);
227266
return map;
228267
}, new Map<string, T>());
229268
}
230269

231-
function concatTyped(ArrayType: any, ...bytes: any[]) {
270+
function concatTyped<T extends TypedArray>(ArrayType: TypedArrayConstructor<T>, ...bytes: any[]) {
232271
const BPE = ArrayType.BYTES_PER_ELEMENT;
233272
return bytes.reduce((v, bytes) => {
234273
const l = bytes.byteLength / BPE;
@@ -237,5 +276,5 @@ function concatTyped(ArrayType: any, ...bytes: any[]) {
237276
a.set(v);
238277
a.set(b, v.length);
239278
return a;
240-
}, new ArrayType(0)) as Array<number>;
279+
}, new ArrayType(0)) as T;
241280
}

0 commit comments

Comments
 (0)