-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdf_test.go
More file actions
908 lines (793 loc) · 23.7 KB
/
Copy pathdf_test.go
File metadata and controls
908 lines (793 loc) · 23.7 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
package otters
import (
"errors"
"strings"
"testing"
"time"
)
func TestDataFrame_Len(t *testing.T) {
data := map[string]any{
"col1": []int64{1, 2, 3},
}
df, _ := NewDataFrameFromMap(data)
if df.Len() != 3 {
t.Errorf("Len() = %v, want 3", df.Len())
}
emptyDf := NewDataFrame()
if emptyDf.Len() != 0 {
t.Error("Len() should return 0 for empty DataFrame")
}
}
func TestDataFrame_Width(t *testing.T) {
data := map[string]any{
"col1": []int64{1, 2, 3},
"col2": []string{"a", "b", "c"},
}
df, _ := NewDataFrameFromMap(data)
if df.Width() != 2 {
t.Errorf("Width() = %v, want 2", df.Width())
}
emptyDf := NewDataFrame()
if emptyDf.Width() != 0 {
t.Error("Width() should return 0 for empty DataFrame")
}
}
func TestDataFrame_String(t *testing.T) {
data := map[string]any{
"name": []string{"Alice", "Bob"},
"age": []int64{25, 30},
}
df, _ := NewDataFrameFromMap(data)
str := df.String()
if str == "" {
t.Error("String() should not return empty string")
}
if !strings.Contains(str, "Alice") || !strings.Contains(str, "Bob") {
t.Error("String() should contain data values")
}
}
func TestDataFrame_Info(t *testing.T) {
data := map[string]any{
"name": []string{"Alice", "Bob", "Carol"},
"age": []int64{25, 30, 35},
"salary": []float64{50000, 60000, 70000},
}
df, _ := NewDataFrameFromMap(data)
info := df.Info()
if info == "" {
t.Error("Info() should not return empty string")
}
if !strings.Contains(info, "3") {
t.Error("Info() should contain row/column counts")
}
}
func TestDataFrame_GetEdgeCases(t *testing.T) {
data := map[string]any{
"col1": []int64{1, 2, 3},
}
df, _ := NewDataFrameFromMap(data)
_, err := df.Get(-1, "col1")
if err == nil {
t.Error("Get() should error on negative index")
}
_, err = df.Get(0, "nonexistent")
if err == nil {
t.Error("Get() should error on nonexistent column")
}
_, err = df.Get(100, "col1")
if err == nil {
t.Error("Get() should error on out of bounds index")
}
}
func TestDataFrame_SetEdgeCases(t *testing.T) {
data := map[string]any{
"col1": []int64{1, 2, 3},
}
df, _ := NewDataFrameFromMap(data)
err := df.Set(-1, "col1", int64(99))
if err == nil {
t.Error("Set() should error on negative index")
}
err = df.Set(0, "nonexistent", int64(99))
if err == nil {
t.Error("Set() should error on nonexistent column")
}
err = df.Set(100, "col1", int64(99))
if err == nil {
t.Error("Set() should error on out of bounds index")
}
err = df.Set(0, "col1", "wrong type")
if err == nil {
t.Error("Set() should error on type mismatch")
}
}
func TestDataFrame_HeadEdgeCases(t *testing.T) {
data := map[string]any{
"col1": []int64{1, 2, 3},
}
df, _ := NewDataFrameFromMap(data)
head := df.Head(-1)
if head.Len() != 0 {
t.Error("Head(-1) should return empty DataFrame")
}
head = df.Head(100)
if head.Len() != 3 {
t.Error("Head(100) should return all rows")
}
}
func TestDataFrame_TailEdgeCases(t *testing.T) {
data := map[string]any{
"col1": []int64{1, 2, 3, 4, 5},
}
df, _ := NewDataFrameFromMap(data)
tail := df.Tail(-1)
if tail.Len() != 0 {
t.Error("Tail(-1) should return empty DataFrame")
}
tail = df.Tail(100)
if tail.Len() != 5 {
t.Error("Tail(100) should return all rows")
}
tail = df.Tail(2)
if tail.Len() != 2 {
t.Error("Tail(2) should return 2 rows")
}
val, _ := tail.Get(0, "col1")
if val != int64(4) {
t.Error("Tail should return last rows")
}
}
func TestDataFrame_GetSeriesEdgeCases(t *testing.T) {
data := map[string]any{
"col1": []int64{1, 2, 3},
}
df, _ := NewDataFrameFromMap(data)
_, err := df.GetSeries("nonexistent")
if err == nil {
t.Error("GetSeries() should error on nonexistent column")
}
series, err := df.GetSeries("col1")
if err != nil || series == nil {
t.Error("GetSeries() should succeed for existing column")
}
}
func TestDataFrame_AddColumnEdgeCases(t *testing.T) {
data := map[string]any{
"col1": []int64{1, 2, 3},
"col2": []string{"a", "b", "c"},
}
df, _ := NewDataFrameFromMap(data)
s1, _ := NewSeries("col1", []int64{4, 5, 6})
err := df.AddColumn(s1)
if err == nil {
t.Error("AddColumn() should error on duplicate column")
}
s2, _ := NewSeries("col3", []int64{1, 2})
err = df.AddColumn(s2)
if err == nil {
t.Error("AddColumn() should error on length mismatch")
}
}
func TestDataFrame_DropColumnEdgeCases(t *testing.T) {
data := map[string]any{
"col1": []int64{1, 2, 3},
"col2": []string{"a", "b", "c"},
}
df, _ := NewDataFrameFromMap(data)
result := df.DropColumn("nonexistent")
if result.Error() == nil {
t.Error("DropColumn() should error on nonexistent column")
}
result = df.DropColumn("col1")
if result.Error() != nil {
t.Error("DropColumn() should succeed")
}
if result.Width() != 1 {
t.Error("DropColumn() should remove column")
}
}
func TestDataFrame_RenameColumnEdgeCases(t *testing.T) {
data := map[string]any{
"col1": []int64{1, 2, 3},
"col2": []string{"a", "b", "c"},
}
df, _ := NewDataFrameFromMap(data)
result := df.RenameColumn("nonexistent", "new")
if result.Error() == nil {
t.Error("RenameColumn() should error on nonexistent column")
}
result = df.RenameColumn("col1", "col2")
if result.Error() == nil {
t.Error("RenameColumn() should error on duplicate name")
}
result = df.RenameColumn("col1", "newcol")
if result.Error() != nil {
t.Error("RenameColumn() should succeed")
}
if !result.HasColumn("newcol") {
t.Error("RenameColumn() should create new column name")
}
}
func TestDataFrame_GetColumnTypeEdgeCases(t *testing.T) {
data := map[string]any{
"col1": []int64{1, 2, 3},
}
df, _ := NewDataFrameFromMap(data)
_, err := df.GetColumnType("nonexistent")
if err == nil {
t.Error("GetColumnType() should error on nonexistent column")
}
ct, err := df.GetColumnType("col1")
if err != nil || ct != Int64Type {
t.Error("GetColumnType() should return correct type")
}
}
func TestDataFrame_HasColumnEdgeCases(t *testing.T) {
data := map[string]any{
"col1": []int64{1, 2, 3},
}
df, _ := NewDataFrameFromMap(data)
if !df.HasColumn("col1") {
t.Error("HasColumn() should return true for existing column")
}
if df.HasColumn("nonexistent") {
t.Error("HasColumn() should return false for nonexistent column")
}
}
func TestDataFrame_CopyEdgeCases(t *testing.T) {
data := map[string]any{
"col1": []int64{1, 2, 3},
}
df, _ := NewDataFrameFromMap(data)
copied := df.Copy()
if copied.Len() != df.Len() || copied.Width() != df.Width() {
t.Error("Copy() should create identical DataFrame")
}
copied.Set(0, "col1", int64(99))
val, _ := df.Get(0, "col1")
if val == int64(99) {
t.Error("Copy() should create independent DataFrame")
}
}
func TestDF_Slice_AllTypes(t *testing.T) {
t1 := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
t2 := time.Date(2023, 1, 2, 0, 0, 0, 0, time.UTC)
t3 := time.Date(2023, 1, 3, 0, 0, 0, 0, time.UTC)
data := map[string]any{
"s": []string{"a", "b", "c", "d"},
"i": []int64{1, 2, 3, 4},
"f": []float64{1.1, 2.2, 3.3, 4.4},
"b": []bool{true, false, true, false},
"t": []time.Time{t1, t2, t3, t1},
}
df, err := NewDataFrameFromMap(data)
if err != nil {
t.Fatalf("NewDataFrameFromMap error: %v", err)
}
sliced := df.slice(1, 3, "Slice")
if sliced.Error() != nil {
t.Fatalf("slice error: %v", sliced.Error())
}
if sliced.Len() != 2 {
t.Fatalf("expected 2 rows, got %d", sliced.Len())
}
}
func TestDF_Slice_InvalidRanges(t *testing.T) {
data := map[string]any{"i": []int64{1, 2, 3}}
df, _ := NewDataFrameFromMap(data)
if df.slice(-1, 1, "Slice").Error() == nil {
t.Error("expected error for negative start")
}
if df.slice(0, 10, "Slice").Error() == nil {
t.Error("expected error for end > length")
}
if df.slice(2, 2, "Slice").Error() == nil {
t.Error("expected error for start >= end")
}
}
func TestDF_Slice_UnsupportedType(t *testing.T) {
df := NewDataFrame()
df.length = 2
df.columns["x"] = &Series{Name: "x", Type: ColumnType(99), Data: []int64{1, 2}, Length: 2}
df.order = append(df.order, "x")
if df.slice(0, 1, "Slice").Error() == nil {
t.Error("expected error for unsupported type")
}
}
func TestDF_Copy_ErrorBranch(t *testing.T) {
df := NewDataFrame()
df.err = errors.New("boom")
copied := df.Copy()
if copied.Error() == nil {
t.Error("expected Copy to keep error")
}
if copied.Len() != 0 || copied.Width() != 0 {
t.Error("expected error DataFrame to have zero shape")
}
}
func TestDF_Copy_DeepCopy(t *testing.T) {
data := map[string]any{
"col1": []int64{1, 2, 3},
"col2": []string{"a", "b", "c"},
"col3": []float64{1.1, 2.2, 3.3},
}
df, _ := NewDataFrameFromMap(data)
copied := df.Copy()
if copied.Width() != 3 || copied.Len() != 3 {
t.Error("Copy should preserve dimensions")
}
df.Set(0, "col1", int64(99))
val, _ := copied.Get(0, "col1")
if val == int64(99) {
t.Error("Copy should be independent")
}
}
func TestDF_String_SmallAndLarge(t *testing.T) {
df1, _ := NewDataFrameFromMap(map[string]any{"col1": []int64{1, 2}})
if df1.String() == "" {
t.Error("String() should return representation")
}
largeData := make([]int64, 20)
for i := range largeData {
largeData[i] = int64(i)
}
df2, _ := NewDataFrameFromMap(map[string]any{"col1": largeData})
if df2.String() == "" {
t.Error("String() should return representation for large DataFrame")
}
}
func TestDF_AddColumn_AllBranches(t *testing.T) {
data := map[string]any{"existing": []int64{1, 2, 3}}
df, _ := NewDataFrameFromMap(data)
s1, _ := NewSeries("new", []int64{4, 5, 6})
if r := df.AddColumn(s1); r.Error() != nil {
t.Errorf("AddColumn matching length should succeed: %v", r.Error())
}
s2, _ := NewSeries("existing", []int64{7, 8, 9})
if r := df.AddColumn(s2); r.Error() == nil {
t.Error("AddColumn with duplicate name should error")
}
s3, _ := NewSeries("mismatch", []int64{1, 2})
if r := df.AddColumn(s3); r.Error() == nil {
t.Error("AddColumn with mismatched length should error")
}
}
func TestDF_NewDataFrameFromSeries_AllBranches(t *testing.T) {
df1, err1 := NewDataFrameFromSeries()
if err1 != nil || df1 == nil {
t.Error("NewDataFrameFromSeries with no series should succeed")
}
s1, _ := NewSeries("col1", []int64{1, 2, 3})
df2, err2 := NewDataFrameFromSeries(s1)
if err2 != nil || df2.Width() != 1 {
t.Error("NewDataFrameFromSeries with single series should succeed")
}
s2, _ := NewSeries("col2", []int64{4, 5, 6})
df3, err3 := NewDataFrameFromSeries(s1, s2)
if err3 != nil || df3.Width() != 2 {
t.Error("NewDataFrameFromSeries with multiple series should succeed")
}
s3, _ := NewSeries("col3", []int64{1, 2})
df4, err4 := NewDataFrameFromSeries(s1, s3)
if err4 == nil || df4 != nil {
t.Error("NewDataFrameFromSeries with mismatched lengths should error")
}
}
func TestDF_NewDataFrameFromMap_AllBranches(t *testing.T) {
df1, err1 := NewDataFrameFromMap(map[string]any{})
if err1 != nil || df1 == nil {
t.Error("NewDataFrameFromMap with empty map should succeed")
}
df2, err2 := NewDataFrameFromMap(map[string]any{"col1": []int64{1, 2, 3}})
if err2 != nil || df2.Width() != 1 {
t.Error("NewDataFrameFromMap with single column should succeed")
}
df3, err3 := NewDataFrameFromMap(map[string]any{
"col1": []int64{1, 2, 3},
"col2": []string{"a", "b", "c"},
})
if err3 != nil || df3.Width() != 2 {
t.Error("NewDataFrameFromMap with multiple columns should succeed")
}
df4, err4 := NewDataFrameFromMap(map[string]any{
"col1": []int64{1, 2, 3},
"col2": []string{"a", "b"},
})
if err4 == nil || df4 != nil {
t.Error("NewDataFrameFromMap with mismatched lengths should error")
}
}
func TestDF_HasColumn_GetColumnType_AllBranches(t *testing.T) {
data := map[string]any{
"str": []string{"a", "b"},
"int": []int64{1, 2},
"float": []float64{1.1, 2.2},
"bool": []bool{true, false},
}
df, _ := NewDataFrameFromMap(data)
if !df.HasColumn("str") || df.HasColumn("nonexistent") {
t.Error("HasColumn failed")
}
strType, _ := df.GetColumnType("str")
intType, _ := df.GetColumnType("int")
floatType, _ := df.GetColumnType("float")
boolType, _ := df.GetColumnType("bool")
if strType != StringType || intType != Int64Type || floatType != Float64Type || boolType != BoolType {
t.Error("GetColumnType returned wrong type")
}
if _, err := df.GetColumnType("nonexistent"); err == nil {
t.Error("GetColumnType should error for nonexistent column")
}
}
func TestDF_HasColumn_GetColumnType_ErrorBranch(t *testing.T) {
df := NewDataFrame()
df.err = errors.New("boom")
if df.HasColumn("anything") {
t.Error("HasColumn should be false when DataFrame has error")
}
if _, err := df.GetColumnType("anything"); err == nil {
t.Error("GetColumnType should error when DataFrame has error")
}
}
func TestDF_Tail_EdgeCases(t *testing.T) {
data := map[string]any{"col1": []int64{1, 2, 3, 4, 5}}
df, _ := NewDataFrameFromMap(data)
if df.Tail(0).Len() != 0 {
t.Error("Tail(0) should return 0 rows")
}
if df.Tail(10).Len() != 5 {
t.Error("Tail(10) should return all 5 rows")
}
if df.Tail(5).Len() != 5 {
t.Error("Tail(5) should return all 5 rows")
}
if df.Tail(2).Len() != 2 {
t.Error("Tail(2) should return 2 rows")
}
}
func TestDF_Get_Set_EdgeCases(t *testing.T) {
data := map[string]any{"col1": []int64{1, 2, 3}}
df, _ := NewDataFrameFromMap(data)
val, err := df.Get(0, "col1")
if err != nil || val != int64(1) {
t.Error("Get should succeed")
}
if _, err := df.Get(10, "col1"); err == nil {
t.Error("Get should error on invalid row")
}
if _, err := df.Get(0, "nonexistent"); err == nil {
t.Error("Get should error on invalid column")
}
if err := df.Set(0, "col1", int64(99)); err != nil {
t.Error("Set should succeed")
}
if err := df.Set(10, "col1", int64(99)); err == nil {
t.Error("Set should error on invalid row")
}
if err := df.Set(0, "nonexistent", int64(99)); err == nil {
t.Error("Set should error on invalid column")
}
}
func TestDF_GetSeries_EdgeCases(t *testing.T) {
data := map[string]any{"col1": []int64{1, 2, 3}}
df, _ := NewDataFrameFromMap(data)
if s, err := df.GetSeries("col1"); err != nil || s == nil {
t.Error("GetSeries should succeed")
}
if _, err := df.GetSeries("nonexistent"); err == nil {
t.Error("GetSeries should error on nonexistent column")
}
}
func TestDF_Shape_Columns_ErrorBranch(t *testing.T) {
df := NewDataFrame()
df.err = newOpError("test", "error")
rows, cols := df.Shape()
if rows != 0 || cols != 0 {
t.Error("Shape should return 0,0 on error")
}
if len(df.Columns()) != 0 {
t.Error("Columns should return empty on error")
}
}
func TestDF_Count_ResetIndex_ErrorBranch(t *testing.T) {
df := NewDataFrame()
df.err = errors.New("boom")
if df.Count() != 0 {
t.Error("Count should return 0 when DataFrame has error")
}
if df.ResetIndex().Error() == nil {
t.Error("ResetIndex should preserve error")
}
}
func TestDF_Count_ResetIndex_SuccessBranch(t *testing.T) {
df, _ := NewDataFrameFromMap(map[string]any{"col1": []int64{1, 2, 3, 4}})
if df.Count() != 4 {
t.Fatalf("expected count 4, got %d", df.Count())
}
res := df.ResetIndex()
if res.Error() != nil || res.Len() != 4 {
t.Fatalf("ResetIndex failed: %v", res.Error())
}
}
func TestDF_ValidationFunctions(t *testing.T) {
data := map[string]any{"col1": []int64{1, 2, 3}, "col2": []string{"a", "b", "c"}}
df, _ := NewDataFrameFromMap(data)
if err := df.validateColumnExists("col1"); err != nil {
t.Error("validateColumnExists should succeed for existing column")
}
if err := df.validateColumnExists("nonexistent"); err == nil {
t.Error("validateColumnExists should error for nonexistent column")
}
if err := df.validateRowIndex(0); err != nil {
t.Error("validateRowIndex should succeed for valid index")
}
if err := df.validateRowIndex(-1); err == nil {
t.Error("validateRowIndex should error for negative index")
}
if err := df.validateRowIndex(100); err == nil {
t.Error("validateRowIndex should error for out of bounds index")
}
if err := df.validateNotEmpty(); err != nil {
t.Error("validateNotEmpty should succeed for non-empty DataFrame")
}
if err := NewDataFrame().validateNotEmpty(); err == nil {
t.Error("validateNotEmpty should error for empty DataFrame")
}
if err := df.validateColumnsExist([]string{"col1", "col2"}); err != nil {
t.Error("validateColumnsExist should succeed for existing columns")
}
if err := df.validateColumnsExist([]string{"col1", "nonexistent"}); err == nil {
t.Error("validateColumnsExist should error for nonexistent column")
}
}
func TestDF_SortFilterGroupBy_ErrorPropagation(t *testing.T) {
df := NewDataFrame()
df.err = newOpError("test", "error")
if df.SortBy([]string{"col1"}, []bool{true}).Error() == nil {
t.Error("SortBy should propagate error")
}
if df.Filter("col1", "==", 1).Error() == nil {
t.Error("Filter should propagate error")
}
if df.GroupBy("col1").err == nil {
t.Error("GroupBy should propagate error")
}
}
func TestDF_AddColumnWithEmptyDF(t *testing.T) {
df := NewDataFrame()
s, _ := NewSeries("col1", []int64{1, 2, 3})
err := df.AddColumn(s)
if err == nil && df.Width() != 1 {
t.Error("AddColumn should either error or add column")
}
}
func TestDF_DropColumnLastColumn(t *testing.T) {
data := map[string]any{"col1": []int64{1, 2, 3}}
df, _ := NewDataFrameFromMap(data)
result := df.DropColumn("col1")
if result.Error() != nil || result.Width() != 0 {
t.Errorf("DropColumn last column failed: %v", result.Error())
}
}
func TestDF_RenameColumnSuccess(t *testing.T) {
data := map[string]any{"old": []int64{1, 2, 3}}
df, _ := NewDataFrameFromMap(data)
result := df.RenameColumn("old", "new")
if result.Error() != nil || !result.HasColumn("new") || result.HasColumn("old") {
t.Errorf("RenameColumn failed: %v", result.Error())
}
}
// Regression: NewSeries/NewDataFrameFromMap used to keep a reference to the
// caller's slice; mutating the source slice afterwards mutated the DataFrame.
func TestDataFrameDoesNotAliasCallerSlice(t *testing.T) {
src := []int64{1, 2, 3}
df, err := NewDataFrameFromMap(map[string]any{"n": src})
if err != nil {
t.Fatal(err)
}
src[0] = 999
v, err := df.Get(0, "n")
if err != nil {
t.Fatal(err)
}
if v.(int64) != 1 {
t.Errorf("mutating the source slice changed the DataFrame: got %v, want 1", v)
}
}
// TestDataFrameBasics covers basic DataFrame creation and operations.
func TestDataFrameBasics(t *testing.T) {
data := map[string]any{
"numbers": []int64{1, 2, 3, 4, 5},
"names": []string{"a", "b", "c", "d", "e"},
}
df, err := NewDataFrameFromMap(data)
if err != nil {
t.Fatalf("Failed to create DataFrame: %v", err)
}
// Test shape
rows, cols := df.Shape()
if rows != 5 || cols != 2 {
t.Errorf("Expected shape (5, 2), got (%d, %d)", rows, cols)
}
// Test columns
columns := df.Columns()
if len(columns) != 2 {
t.Errorf("Expected 2 columns, got %d", len(columns))
}
// Test filtering
filtered := df.Filter("numbers", ">", int64(3))
if err := filtered.Error(); err != nil {
t.Errorf("Filter error: %v", err)
}
filteredRows, _ := filtered.Shape()
if filteredRows != 2 {
t.Errorf("Expected 2 filtered rows, got %d", filteredRows)
}
}
// TestTimeTypeHeadTail verifies that Head and Tail work on DataFrames with
// TimeType columns (regression for missing TimeType case in slice()).
func TestTimeTypeHeadTail(t *testing.T) {
t1, _ := time.Parse("2006-01-02", "2024-01-01")
t2, _ := time.Parse("2006-01-02", "2024-01-02")
t3, _ := time.Parse("2006-01-02", "2024-01-03")
s, err := NewSeries("date", []time.Time{t1, t2, t3})
if err != nil {
t.Fatalf("setup failed: %v", err)
}
df, err := NewDataFrameFromSeries(s)
if err != nil {
t.Fatalf("setup failed: %v", err)
}
head := df.Head(2)
if head.Error() != nil {
t.Fatalf("Head on TimeType column failed: %v", head.Error())
}
if rows, _ := head.Shape(); rows != 2 {
t.Errorf("Head(2) returned %d rows, want 2", rows)
}
tail := df.Tail(1)
if tail.Error() != nil {
t.Fatalf("Tail on TimeType column failed: %v", tail.Error())
}
if rows, _ := tail.Shape(); rows != 1 {
t.Errorf("Tail(1) returned %d rows, want 1", rows)
}
// Verify the value is correct
val, err := tail.Get(0, "date")
if err != nil {
t.Fatalf("Get failed: %v", err)
}
if !val.(time.Time).Equal(t3) {
t.Errorf("Tail value = %v, want %v", val, t3)
}
}
// TestSetErrorDoesNotMutateCaller verifies that a failed operation does not
// corrupt the original DataFrame (regression for the setError mutation bug).
func TestSetErrorDoesNotMutateCaller(t *testing.T) {
data := map[string]any{
"a": []int64{1, 2, 3},
}
df, err := NewDataFrameFromMap(data)
if err != nil {
t.Fatalf("setup failed: %v", err)
}
_ = df.Filter("nonexistent", "==", int64(1))
if df.Error() != nil {
t.Errorf("Filter on nonexistent column mutated the original DataFrame: %v", df.Error())
}
rows, cols := df.Shape()
if rows != 3 || cols != 1 {
t.Errorf("original DataFrame shape changed after failed Filter: got (%d, %d), want (3, 1)", rows, cols)
}
}
// TestDeterministicFromMap verifies that NewDataFrameFromMap always produces
// columns in alphabetical order, regardless of map iteration order.
func TestDeterministicFromMap(t *testing.T) {
data := map[string]any{
"zebra": []int64{1, 2, 3},
"apple": []int64{4, 5, 6},
"mango": []int64{7, 8, 9},
}
expected := []string{"apple", "mango", "zebra"}
for i := 0; i < 20; i++ {
df, err := NewDataFrameFromMap(data)
if err != nil {
t.Fatalf("NewDataFrameFromMap failed on iteration %d: %v", i, err)
}
cols := df.Columns()
if len(cols) != len(expected) {
t.Fatalf("iteration %d: expected %d columns, got %d", i, len(expected), len(cols))
}
for j, col := range cols {
if col != expected[j] {
t.Errorf("iteration %d: column[%d] = %q, want %q", i, j, col, expected[j])
}
}
}
}
// TestDataFrameManipulation covers Tail, Set, GetSeries, AddColumn, DropColumn,
// RenameColumn, IsEmpty, and HasColumn.
func TestDataFrameManipulation(t *testing.T) {
data := map[string]any{
"id": []int64{1, 2, 3, 4, 5},
"name": []string{"a", "b", "c", "d", "e"},
}
df, err := NewDataFrameFromMap(data)
if err != nil {
t.Fatalf("setup failed: %v", err)
}
// Tail
tail := df.Tail(2)
if err := tail.Error(); err != nil {
t.Fatalf("Tail error: %v", err)
}
rows, _ := tail.Shape()
if rows != 2 {
t.Errorf("Tail(2) returned %d rows, want 2", rows)
}
// Set
if err := df.Set(0, "id", int64(99)); err != nil {
t.Fatalf("Set error: %v", err)
}
val, err := df.Get(0, "id")
if err != nil {
t.Fatalf("Get after Set error: %v", err)
}
if val.(int64) != 99 {
t.Errorf("Set: got %v, want 99", val)
}
// GetSeries
s, err := df.GetSeries("id")
if err != nil {
t.Fatalf("GetSeries error: %v", err)
}
if s == nil || s.Name != "id" {
t.Errorf("GetSeries returned unexpected series: %v", s)
}
// AddColumn (mutates df in place)
scoreSeries, err := NewSeries("score", []float64{10.0, 20.0, 30.0, 40.0, 50.0})
if err != nil {
t.Fatalf("NewSeries error: %v", err)
}
df.AddColumn(scoreSeries)
if !df.HasColumn("score") {
t.Error("AddColumn: 'score' column not found")
}
// DropColumn (returns copy)
dfDropped := df.DropColumn("score")
if err := dfDropped.Error(); err != nil {
t.Fatalf("DropColumn error: %v", err)
}
if dfDropped.HasColumn("score") {
t.Error("DropColumn: 'score' still present in returned DataFrame")
}
if !df.HasColumn("score") {
t.Error("DropColumn: 'score' removed from original DataFrame unexpectedly")
}
// RenameColumn (returns copy)
dfRenamed := df.RenameColumn("id", "user_id")
if err := dfRenamed.Error(); err != nil {
t.Fatalf("RenameColumn error: %v", err)
}
if !dfRenamed.HasColumn("user_id") {
t.Error("RenameColumn: 'user_id' not found in result")
}
if dfRenamed.HasColumn("id") {
t.Error("RenameColumn: old 'id' still present in result")
}
// IsEmpty
empty := NewDataFrame()
if !empty.IsEmpty() {
t.Error("IsEmpty: expected true for new empty DataFrame")
}
if df.IsEmpty() {
t.Error("IsEmpty: expected false for non-empty DataFrame")
}
// HasColumn
if !df.HasColumn("name") {
t.Error("HasColumn: 'name' should exist")
}
if df.HasColumn("nonexistent") {
t.Error("HasColumn: 'nonexistent' should not exist")
}
}