@@ -2,11 +2,6 @@ import { describe, it, expect, vi } from 'vitest';
22import { DataFrame } from '../../../../src/core/dataframe/DataFrame.js' ;
33import { print } from '../../../../src/methods/dataframe/display/print.js' ;
44
5- import {
6- testWithBothStorageTypes ,
7- createDataFrameWithStorage ,
8- } from '../../../utils/storageTestUtils.js' ;
9-
105// Test data to be used in all tests
116const sampleData = [
127 { value : 10 , category : 'A' , mixed : '20' } ,
@@ -17,134 +12,119 @@ const sampleData = [
1712] ;
1813
1914describe ( 'DataFrame print method' , ( ) => {
20- // Run tests with both storage types
21- testWithBothStorageTypes ( ( storageType ) => {
22- describe ( `with ${ storageType } storage` , ( ) => {
23- // Create test data frame with people data for better readability in tests
24- const testData = [
25- { name : 'Alice' , age : 25 , city : 'New York' } ,
26- { name : 'Bob' , age : 30 , city : 'Boston' } ,
27- { name : 'Charlie' , age : 35 , city : 'Chicago' } ,
28- { name : 'David' , age : 40 , city : 'Denver' } ,
29- { name : 'Eve' , age : 45 , city : 'El Paso' } ,
30- ] ;
31-
32- // Create DataFrame with the specified storage type
33- const df = createDataFrameWithStorage ( DataFrame , testData , storageType ) ;
34-
35- it ( 'should format data as a table string' , ( ) => {
36- // Mock console.log to check output
37- const consoleSpy = vi
38- . spyOn ( console , 'log' )
39- . mockImplementation ( ( ) => { } ) ;
40-
41- // Call print function directly
42- const printFn = print ( ) ;
43- printFn ( df ) ;
44-
45- // Check that console.log was called
46- expect ( consoleSpy ) . toHaveBeenCalled ( ) ;
47-
48- // Get the argument passed to console.log
49- const output = consoleSpy . mock . calls [ 0 ] [ 0 ] ;
50-
51- // Check that the output contains column headers
52- expect ( output ) . toContain ( 'name' ) ;
53- expect ( output ) . toContain ( 'age' ) ;
54- expect ( output ) . toContain ( 'city' ) ;
55-
56- // Check that the output contains data
57- expect ( output ) . toContain ( 'Alice' ) ;
58- expect ( output ) . toContain ( '25' ) ;
59- expect ( output ) . toContain ( 'New York' ) ;
60-
61- // Restore console.log
62- consoleSpy . mockRestore ( ) ;
63- } ) ;
64-
65- it ( 'should return the frame for method chaining' , ( ) => {
66- // Mock console.log to prevent output
67- const consoleSpy = vi
68- . spyOn ( console , 'log' )
69- . mockImplementation ( ( ) => { } ) ;
70-
71- // Call print function and check the return value
72- const printFn = print ( ) ;
73- const result = printFn ( df ) ;
74-
75- // Check that the function returns the frame
76- expect ( result ) . toBe ( df ) ;
77-
78- // Restore console.log
79- consoleSpy . mockRestore ( ) ;
80- } ) ;
81-
82- it ( 'should respect rows limit' , ( ) => {
83- // Create a frame with many rows
84- const largeData = Array . from ( { length : 20 } , ( _ , i ) => ( {
85- id : i ,
86- value : i * 10 ,
87- } ) ) ;
88-
89- const largeDf = createDataFrameWithStorage (
90- DataFrame ,
91- largeData ,
92- storageType ,
93- ) ;
94-
95- // Mock console.log
96- const consoleSpy = vi
97- . spyOn ( console , 'log' )
98- . mockImplementation ( ( ) => { } ) ;
99-
100- // Call print function with row limit
101- const printFn = print ( ) ;
102- printFn ( largeDf , 5 ) ;
103-
104- // Get the output
105- const output = consoleSpy . mock . calls [ 0 ] [ 0 ] ;
106-
107- // Check that the output contains message about additional rows
108- expect ( output ) . toContain ( 'more rows' ) ;
109-
110- // Restore console.log
111- consoleSpy . mockRestore ( ) ;
112- } ) ;
113-
114- it ( 'should respect cols limit' , ( ) => {
115- // Create a frame with many columns
116- const wideData = {
117- col1 : [ 1 , 2 , 3 ] ,
118- col2 : [ 4 , 5 , 6 ] ,
119- col3 : [ 7 , 8 , 9 ] ,
120- col4 : [ 10 , 11 , 12 ] ,
121- col5 : [ 13 , 14 , 15 ] ,
122- } ;
123-
124- const wideDf = createDataFrameWithStorage (
125- DataFrame ,
126- wideData ,
127- storageType ,
128- ) ;
129-
130- // Mock console.log
131- const consoleSpy = vi
132- . spyOn ( console , 'log' )
133- . mockImplementation ( ( ) => { } ) ;
134-
135- // Call print function with column limit
136- const printFn = print ( ) ;
137- printFn ( wideDf , Infinity , 2 ) ;
138-
139- // Get the output
140- const output = consoleSpy . mock . calls [ 0 ] [ 0 ] ;
141-
142- // Check that the output contains message about additional columns
143- expect ( output ) . toContain ( 'more columns' ) ;
144-
145- // Restore console.log
146- consoleSpy . mockRestore ( ) ;
147- } ) ;
15+ describe ( 'with standard storage' , ( ) => {
16+ // Create test data frame with people data for better readability in tests
17+ const testData = [
18+ { name : 'Alice' , age : 25 , city : 'New York' } ,
19+ { name : 'Bob' , age : 30 , city : 'Boston' } ,
20+ { name : 'Charlie' , age : 35 , city : 'Chicago' } ,
21+ { name : 'David' , age : 40 , city : 'Denver' } ,
22+ { name : 'Eve' , age : 45 , city : 'El Paso' } ,
23+ ] ;
24+
25+ // Create DataFrame using fromRows
26+ const df = DataFrame . fromRows ( testData ) ;
27+
28+ it ( 'should format data as a table string' , ( ) => {
29+ // Mock console.log to check output
30+ const consoleSpy = vi . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ;
31+
32+ // Call print function directly
33+ const printFn = print ( ) ;
34+ printFn ( df ) ;
35+
36+ // Check that console.log was called
37+ expect ( consoleSpy ) . toHaveBeenCalled ( ) ;
38+
39+ // Get the argument passed to console.log
40+ const output = consoleSpy . mock . calls [ 0 ] [ 0 ] ;
41+
42+ // Check that the output contains column headers
43+ expect ( output ) . toContain ( 'name' ) ;
44+ expect ( output ) . toContain ( 'age' ) ;
45+ expect ( output ) . toContain ( 'city' ) ;
46+
47+ // Check that the output contains data
48+ expect ( output ) . toContain ( 'Alice' ) ;
49+ expect ( output ) . toContain ( '25' ) ;
50+ expect ( output ) . toContain ( 'New York' ) ;
51+
52+ // Restore console.log
53+ consoleSpy . mockRestore ( ) ;
54+ } ) ;
55+
56+ it ( 'should return the frame for method chaining' , ( ) => {
57+ // Mock console.log to prevent output
58+ const consoleSpy = vi . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ;
59+
60+ // Call print function and check the return value
61+ const printFn = print ( ) ;
62+ const result = printFn ( df ) ;
63+
64+ // Check that the function returns the frame
65+ expect ( result ) . toBe ( df ) ;
66+
67+ // Restore console.log
68+ consoleSpy . mockRestore ( ) ;
69+ } ) ;
70+
71+ it ( 'should respect rows limit' , ( ) => {
72+ // Create a frame with many rows
73+ const largeData = Array . from ( { length : 20 } , ( _ , i ) => ( {
74+ id : i ,
75+ value : i * 10 ,
76+ } ) ) ;
77+
78+ const largeDf = DataFrame . fromRows ( largeData ) ;
79+
80+ // Mock console.log
81+ const consoleSpy = vi . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ;
82+
83+ // Call print function with row limit
84+ const printFn = print ( ) ;
85+ printFn ( largeDf , 5 ) ;
86+
87+ // Get the output
88+ const output = consoleSpy . mock . calls [ 0 ] [ 0 ] ;
89+
90+ // Check that the output contains message about additional rows
91+ expect ( output ) . toContain ( 'more rows' ) ;
92+
93+ // Restore console.log
94+ consoleSpy . mockRestore ( ) ;
95+ } ) ;
96+
97+ it ( 'should respect cols limit' , ( ) => {
98+ // Create a frame with many columns
99+ const wideData = {
100+ col1 : [ 1 , 2 , 3 ] ,
101+ col2 : [ 4 , 5 , 6 ] ,
102+ col3 : [ 7 , 8 , 9 ] ,
103+ col4 : [ 10 , 11 , 12 ] ,
104+ col5 : [ 13 , 14 , 15 ] ,
105+ } ;
106+
107+ const wideDf = DataFrame . fromRows ( [
108+ { col1 : 1 , col2 : 4 , col3 : 7 , col4 : 10 , col5 : 13 } ,
109+ { col1 : 2 , col2 : 5 , col3 : 8 , col4 : 11 , col5 : 14 } ,
110+ { col1 : 3 , col2 : 6 , col3 : 9 , col4 : 12 , col5 : 15 } ,
111+ ] ) ;
112+
113+ // Mock console.log
114+ const consoleSpy = vi . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ;
115+
116+ // Call print function with column limit
117+ const printFn = print ( ) ;
118+ printFn ( wideDf , Infinity , 2 ) ;
119+
120+ // Get the output
121+ const output = consoleSpy . mock . calls [ 0 ] [ 0 ] ;
122+
123+ // Check that the output contains message about additional columns
124+ expect ( output ) . toContain ( 'more columns' ) ;
125+
126+ // Restore console.log
127+ consoleSpy . mockRestore ( ) ;
148128 } ) ;
149129 } ) ;
150130} ) ;
0 commit comments