-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathat.test.js
More file actions
104 lines (86 loc) · 2.87 KB
/
at.test.js
File metadata and controls
104 lines (86 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* Unit tests for at method
*/
import { describe, test, expect } from 'vitest';
import { DataFrame } from '../../../../src/core/dataframe/DataFrame.js';
import { registerDataFrameIndexing } from '../../../../src/methods/dataframe/indexing/register.js';
// Test data for use in all tests
const testData = [
{ name: 'Alice', age: 25, city: 'New York', salary: 70000 },
{ name: 'Bob', age: 30, city: 'San Francisco', salary: 85000 },
{ name: 'Charlie', age: 35, city: 'Chicago', salary: 90000 },
];
describe('At Method', () => {
// Register indexing methods for DataFrame
registerDataFrameIndexing(DataFrame);
describe('with standard storage', () => {
// Create DataFrame using fromRows
const df = DataFrame.fromRows(testData);
// Create DataFrame with typed arrays for testing type preservation
const typedDf = DataFrame.fromRows(testData, {
columns: {
age: { type: 'int32' },
salary: { type: 'float64' },
},
});
test('should select a row by index', () => {
const result = df.at(1);
// Check that the result is an object with the correct values
expect(result).toEqual({
name: 'Bob',
age: 30,
city: 'San Francisco',
salary: 85000,
});
});
test('should select the first row with index 0', () => {
const result = df.at(0);
// Check that the result is an object with the correct values
expect(result).toEqual({
name: 'Alice',
age: 25,
city: 'New York',
salary: 70000,
});
});
test('should select the last row with the last index', () => {
const result = df.at(2);
// Check that the result is an object with the correct values
expect(result).toEqual({
name: 'Charlie',
age: 35,
city: 'Chicago',
salary: 90000,
});
});
test('should throw error for negative index', () => {
expect(() => df.at(-1)).toThrow();
});
test('should throw error for index out of bounds', () => {
expect(() => df.at(3)).toThrow();
});
test('should throw error for non-integer index', () => {
expect(() => df.at(1.5)).toThrow();
expect(() => df.at('1')).toThrow();
});
test('should handle empty DataFrame', () => {
// Create empty DataFrame
const emptyDf = DataFrame.fromRows([]);
expect(() => emptyDf.at(0)).toThrow();
});
test('should handle typed arrays', () => {
// Use DataFrame with typed arrays created above
const result = typedDf.at(1);
// Check that the result contains correct values
expect(result).toEqual({
name: 'Bob',
age: 30,
city: 'San Francisco',
salary: 85000,
});
// Check that numeric values have the correct type
expect(typeof result.age).toBe('number');
expect(typeof result.salary).toBe('number');
});
});
});