@@ -12,112 +12,105 @@ import {
1212 register ,
1313} from '../../../../src/methods/dataframe/aggregation/first.js' ;
1414import { DataFrame } from '../../../../src/core/dataframe/DataFrame.js' ;
15- import { describe , it , expect , vi , beforeEach } from 'vitest' ;
16-
17- import {
18- testWithBothStorageTypes ,
19- createDataFrameWithStorage ,
20- } from '../../../utils/storageTestUtils.js' ;
15+ import { describe , it , expect , vi } from 'vitest' ;
2116
2217// Register the first method in DataFrame for tests
2318register ( DataFrame ) ;
2419
25- // Test data for use in all tests
26- const testData = [
27- { value : 10 , category : 'A' , mixed : '20' } ,
28- { value : 20 , category : 'B' , mixed : 30 } ,
29- { value : 30 , category : 'A' , mixed : null } ,
30- { value : 40 , category : 'C' , mixed : undefined } ,
31- { value : 50 , category : 'B' , mixed : NaN } ,
32- ] ;
33-
3420describe ( 'first method' , ( ) => {
35- // Run tests with both storage types
36- testWithBothStorageTypes ( ( storageType ) => {
37- describe ( `with ${ storageType } storage` , ( ) => {
38- // Create a DataFrame with the specified storage type
39- const df = createDataFrameWithStorage ( DataFrame , testData , storageType ) ;
40-
41- // Test the first function directly
42- it ( 'should return the first value in a column' , ( ) => {
43- // Create a first function with a mock validator
44- const validateColumn = vi . fn ( ) ;
45- const firstFn = first ( { validateColumn } ) ;
46-
47- // Call the first function
48- const result = firstFn ( df , 'value' ) ;
49-
50- // Check the result
51- expect ( result ) . toBe ( 10 ) ;
52- expect ( validateColumn ) . toHaveBeenCalledWith ( df , 'value' ) ;
53- } ) ;
54-
55- it ( 'should handle special values (null, undefined, NaN)' , ( ) => {
56- // Create a first function with a mock validator
57- const validateColumn = vi . fn ( ) ;
58- const firstFn = first ( { validateColumn } ) ;
59-
60- // Check that the first values are returned correctly
61- expect ( firstFn ( df , 'mixed' ) ) . toBe ( '20' ) ;
62- expect ( validateColumn ) . toHaveBeenCalledWith ( df , 'mixed' ) ;
63- } ) ;
64-
65- it ( 'should return undefined for empty DataFrame' , ( ) => {
66- // Create an empty DataFrame
67- const emptyDf = createDataFrameWithStorage ( DataFrame , [ ] , storageType ) ;
68-
69- // Create a first function with a mock validator
70- const validateColumn = vi . fn ( ) ;
71- const firstFn = first ( { validateColumn } ) ;
72-
73- // Call the first function
74- const result = firstFn ( emptyDf , 'value' ) ;
75-
76- // Check the result
77- expect ( result ) . toBeUndefined ( ) ;
78- // For an empty DataFrame, the validator is not called, as we immediately return undefined
79- } ) ;
80-
81- it ( 'should throw error for non-existent column' , ( ) => {
82- // Create a validator that throws an error
83- const validateColumn = ( df , column ) => {
84- if ( ! df . columns . includes ( column ) ) {
85- throw new Error ( `Column '${ column } ' not found` ) ;
86- }
87- } ;
88-
89- // Create a first function with our validator
90- const firstFn = first ( { validateColumn } ) ;
91-
92- // Check that the function throws an error for non-existent columns
93- expect ( ( ) => firstFn ( df , 'nonexistent' ) ) . toThrow (
94- 'Column \'nonexistent\' not found' ,
95- ) ;
96- } ) ;
97-
98- // Test the DataFrame.first method
99- it ( 'should be available as a DataFrame method' , ( ) => {
100- // Check that the first method is available in DataFrame
101- expect ( typeof df . first ) . toBe ( 'function' ) ;
102-
103- // Call the first method and check the result
104- expect ( df . first ( 'value' ) ) . toBe ( 10 ) ;
105- expect ( df . first ( 'category' ) ) . toBe ( 'A' ) ;
106- } ) ;
107- it ( 'should handle empty DataFrame gracefully' , ( ) => {
108- // Create an empty DataFrame
109- const emptyDf = createDataFrameWithStorage ( DataFrame , [ ] , storageType ) ;
110-
111- // Check that the first method returns undefined for an empty DataFrame
112- expect ( emptyDf . first ( 'value' ) ) . toBeUndefined ( ) ;
113- } ) ;
114-
115- it ( 'should throw error for non-existent column' , ( ) => {
116- // Check that the first method throws an error for non-existent columns
117- expect ( ( ) => df . first ( 'nonexistent' ) ) . toThrow (
118- 'Column \'nonexistent\' not found' ,
119- ) ;
120- } ) ;
21+ describe ( 'with standard storage' , ( ) => {
22+ // Test data for use in all tests
23+ const testData = [
24+ { value : 10 , category : 'A' , mixed : '20' } ,
25+ { value : 20 , category : 'B' , mixed : 30 } ,
26+ { value : 30 , category : 'A' , mixed : null } ,
27+ { value : 40 , category : 'C' , mixed : undefined } ,
28+ { value : 50 , category : 'B' , mixed : NaN } ,
29+ ] ;
30+
31+ // Create DataFrame using fromRows for proper column names
32+ const df = DataFrame . fromRows ( testData ) ;
33+
34+ // Test the first function directly
35+ it ( 'should return the first value in a column' , ( ) => {
36+ // Create a first function with a mock validator
37+ const validateColumn = vi . fn ( ) ;
38+ const firstFn = first ( { validateColumn } ) ;
39+
40+ // Call the first function
41+ const result = firstFn ( df , 'value' ) ;
42+
43+ // Check the result
44+ expect ( result ) . toBe ( 10 ) ;
45+ expect ( validateColumn ) . toHaveBeenCalledWith ( df , 'value' ) ;
46+ } ) ;
47+
48+ it ( 'should handle special values (null, undefined, NaN)' , ( ) => {
49+ // Create a first function with a mock validator
50+ const validateColumn = vi . fn ( ) ;
51+ const firstFn = first ( { validateColumn } ) ;
52+
53+ // Check that the first values are returned correctly
54+ expect ( firstFn ( df , 'mixed' ) ) . toBe ( '20' ) ;
55+ expect ( validateColumn ) . toHaveBeenCalledWith ( df , 'mixed' ) ;
56+ } ) ;
57+
58+ it ( 'should return undefined for empty DataFrame' , ( ) => {
59+ // Create an empty DataFrame using fromRows
60+ const emptyDf = DataFrame . fromRows ( [ ] ) ;
61+
62+ // Create a first function with a mock validator
63+ const validateColumn = vi . fn ( ) ;
64+ const firstFn = first ( { validateColumn } ) ;
65+
66+ // Call the first function
67+ const result = firstFn ( emptyDf , 'value' ) ;
68+
69+ // Check the result
70+ expect ( result ) . toBeUndefined ( ) ;
71+ // For an empty DataFrame, the validator is not called, as we immediately return undefined
72+ } ) ;
73+
74+ it ( 'should throw error for non-existent column' , ( ) => {
75+ // Create a validator that throws an error
76+ const validateColumn = ( df , column ) => {
77+ if ( ! df . columns . includes ( column ) ) {
78+ throw new Error ( `Column '${ column } ' not found` ) ;
79+ }
80+ } ;
81+
82+ // Create a first function with our validator
83+ const firstFn = first ( { validateColumn } ) ;
84+
85+ // Check that the function throws an error for non-existent columns
86+ expect ( ( ) => firstFn ( df , 'nonexistent' ) ) . toThrow (
87+ "Column 'nonexistent' not found" ,
88+ ) ;
89+ } ) ;
90+
91+ // Test the DataFrame.first method
92+ it ( 'should be available as a DataFrame method' , ( ) => {
93+ // Check that the first method is available in DataFrame
94+ expect ( typeof df . first ) . toBe ( 'function' ) ;
95+
96+ // Call the first method and check the result
97+ expect ( df . first ( 'value' ) ) . toBe ( 10 ) ;
98+ expect ( df . first ( 'category' ) ) . toBe ( 'A' ) ;
99+ } ) ;
100+
101+ it ( 'should handle empty DataFrame gracefully' , ( ) => {
102+ // Create an empty DataFrame using fromRows
103+ const emptyDf = DataFrame . fromRows ( [ ] ) ;
104+
105+ // Check that the first method returns undefined for an empty DataFrame
106+ expect ( emptyDf . first ( 'value' ) ) . toBeUndefined ( ) ;
107+ } ) ;
108+
109+ it ( 'should throw error for non-existent column' , ( ) => {
110+ // Check that the first method throws an error for non-existent columns
111+ expect ( ( ) => df . first ( 'nonexistent' ) ) . toThrow (
112+ "Column 'nonexistent' not found" ,
113+ ) ;
121114 } ) ;
122115 } ) ;
123116} ) ;
0 commit comments