@@ -13,63 +13,95 @@ using DataFrames
13
13
14
14
@testset " Tables interface" begin
15
15
@test Tables. istable (typeof (chn))
16
- @test Tables. columnaccess (typeof (chn))
17
- @test Tables. columns (chn) === chn
18
- @test Tables. columnnames (chn) ==
19
- (:iteration , :chain , :a , :b , :c , :d , :e , :f , :g , :h )
20
- @test Tables. getcolumn (chn, :iteration ) == [1 : 1000 ; 1 : 1000 ; 1 : 1000 ; 1 : 1000 ]
21
- @test Tables. getcolumn (chn, :chain ) ==
22
- [fill (1 , 1000 ); fill (2 , 1000 ); fill (3 , 1000 ); fill (4 , 1000 )]
23
- @test Tables. getcolumn (chn, :a ) == [
24
- vec (chn[:, :a , 1 ])
25
- vec (chn[:, :a , 2 ])
26
- vec (chn[:, :a , 3 ])
27
- vec (chn[:, :a , 4 ])
28
- ]
29
- @test_throws Exception Tables. getcolumn (chn, :j )
30
- @test Tables. getcolumn (chn, 1 ) == Tables. getcolumn (chn, :iteration )
31
- @test Tables. getcolumn (chn, 2 ) == Tables. getcolumn (chn, :chain )
32
- @test Tables. getcolumn (chn, 3 ) == Tables. getcolumn (chn, :a )
33
- @test_throws Exception Tables. getcolumn (chn, :i )
34
- @test_throws Exception Tables. getcolumn (chn, 11 )
35
- @test Tables. rowaccess (typeof (chn))
36
- @test Tables. rows (chn) === chn
37
- @test length (Tables. rowtable (chn)) == 4000
38
- nt = Tables. rowtable (chn)[1 ]
39
- @test nt ==
40
- (; (k => Tables. getcolumn (chn, k)[1 ] for k in Tables. columnnames (chn)). .. )
41
- @test nt == collect (Iterators. take (Tables. namedtupleiterator (chn), 1 ))[1 ]
42
- nt = Tables. rowtable (chn)[2 ]
43
- @test nt ==
44
- (; (k => Tables. getcolumn (chn, k)[2 ] for k in Tables. columnnames (chn)). .. )
45
- @test nt == collect (Iterators. take (Tables. namedtupleiterator (chn), 2 ))[2 ]
46
- @test Tables. schema (chn) isa Tables. Schema
47
- @test Tables. schema (chn). names ===
48
- (:iteration , :chain , :a , :b , :c , :d , :e , :f , :g , :h )
49
- @test Tables. schema (chn). types === (
50
- Int,
51
- Int,
52
- Float64,
53
- Float64,
54
- Float64,
55
- Float64,
56
- Float64,
57
- Float64,
58
- Float64,
59
- Float64,
60
- )
61
- @test Tables. matrix (chn[:, :, 1 ])[:, 3 : end ] ≈ chn[:, :, 1 ]. value
62
- @test Tables. matrix (chn[:, :, 2 ])[:, 3 : end ] ≈ chn[:, :, 2 ]. value
63
-
64
- val = rand (1000 , 2 , 4 )
65
- chn2 = Chains (val, [" iteration" , " a" ])
66
- @test_throws Exception Tables. columns (chn2)
67
- @test_throws Exception Tables. rows (chn2)
68
- @test_throws Exception Tables. schema (chn2)
69
- chn3 = Chains (val, [" chain" , " a" ])
70
- @test_throws Exception Tables. columns (chn3)
71
- @test_throws Exception Tables. rows (chn3)
72
- @test_throws Exception Tables. schema (chn3)
16
+
17
+ @testset " column access" begin
18
+ @test Tables. columnaccess (typeof (chn))
19
+ @test Tables. columns (chn) === chn
20
+ @test Tables. columnnames (chn) ==
21
+ (:iteration , :chain , :a , :b , :c , :d , :e , :f , :g , :h )
22
+ @test Tables. getcolumn (chn, :iteration ) == [1 : 1000 ; 1 : 1000 ; 1 : 1000 ; 1 : 1000 ]
23
+ @test Tables. getcolumn (chn, :chain ) ==
24
+ [fill (1 , 1000 ); fill (2 , 1000 ); fill (3 , 1000 ); fill (4 , 1000 )]
25
+ @test Tables. getcolumn (chn, :a ) == [
26
+ vec (chn[:, :a , 1 ])
27
+ vec (chn[:, :a , 2 ])
28
+ vec (chn[:, :a , 3 ])
29
+ vec (chn[:, :a , 4 ])
30
+ ]
31
+ @test_throws Exception Tables. getcolumn (chn, :j )
32
+ @test Tables. getcolumn (chn, 1 ) == Tables. getcolumn (chn, :iteration )
33
+ @test Tables. getcolumn (chn, 2 ) == Tables. getcolumn (chn, :chain )
34
+ @test Tables. getcolumn (chn, 3 ) == Tables. getcolumn (chn, :a )
35
+ @test_throws Exception Tables. getcolumn (chn, :i )
36
+ @test_throws Exception Tables. getcolumn (chn, 11 )
37
+ end
38
+
39
+ @testset " row access" begin
40
+ @test Tables. rowaccess (typeof (chn))
41
+ @test Tables. rows (chn) isa Tables. RowIterator
42
+ @test eltype (Tables. rows (chn)) <: Tables.AbstractRow
43
+ rows = collect (Tables. rows (chn))
44
+ @test eltype (rows) <: Tables.AbstractRow
45
+ @test size (rows) === (4000 ,)
46
+ for chainid in 1 : 4 , iterid in 1 : 1000
47
+ row = rows[(chainid - 1 ) * 1000 + iterid]
48
+ @test Tables. columnnames (row) ==
49
+ (:iteration , :chain , :a , :b , :c , :d , :e , :f , :g , :h )
50
+ @test Tables. getcolumn (row, 1 ) == iterid
51
+ @test Tables. getcolumn (row, 2 ) == chainid
52
+ @test Tables. getcolumn (row, 3 ) == chn[iterid, :a , chainid]
53
+ @test Tables. getcolumn (row, 10 ) == chn[iterid, :h , chainid]
54
+ @test Tables. getcolumn (row, :iteration ) == iterid
55
+ @test Tables. getcolumn (row, :chain ) == chainid
56
+ @test Tables. getcolumn (row, :a ) == chn[iterid, :a , chainid]
57
+ @test Tables. getcolumn (row, :h ) == chn[iterid, :h , chainid]
58
+ end
59
+ end
60
+
61
+ @testset " integration tests" begin
62
+ @test length (Tables. rowtable (chn)) == 4000
63
+ nt = Tables. rowtable (chn)[1 ]
64
+ @test nt ==
65
+ (; (k => Tables. getcolumn (chn, k)[1 ] for k in Tables. columnnames (chn)). .. )
66
+ @test nt == collect (Iterators. take (Tables. namedtupleiterator (chn), 1 ))[1 ]
67
+ nt = Tables. rowtable (chn)[2 ]
68
+ @test nt ==
69
+ (; (k => Tables. getcolumn (chn, k)[2 ] for k in Tables. columnnames (chn)). .. )
70
+ @test nt == collect (Iterators. take (Tables. namedtupleiterator (chn), 2 ))[2 ]
71
+ @test Tables. matrix (chn[:, :, 1 ])[:, 3 : end ] ≈ chn[:, :, 1 ]. value
72
+ @test Tables. matrix (chn[:, :, 2 ])[:, 3 : end ] ≈ chn[:, :, 2 ]. value
73
+ @test Tables. matrix (Tables. rowtable (chn)) == Tables. matrix (Tables. columntable (chn))
74
+ end
75
+
76
+ @testset " schema" begin
77
+ @test Tables. schema (chn) isa Tables. Schema
78
+ @test Tables. schema (chn). names ===
79
+ (:iteration , :chain , :a , :b , :c , :d , :e , :f , :g , :h )
80
+ @test Tables. schema (chn). types === (
81
+ Int,
82
+ Int,
83
+ Float64,
84
+ Float64,
85
+ Float64,
86
+ Float64,
87
+ Float64,
88
+ Float64,
89
+ Float64,
90
+ Float64,
91
+ )
92
+ end
93
+
94
+ @testset " exceptions raised if reserved colname used" begin
95
+ val2 = rand (1000 , 2 , 4 )
96
+ chn2 = Chains (val2, [" iteration" , " a" ])
97
+ @test_throws Exception Tables. columns (chn2)
98
+ @test_throws Exception Tables. rows (chn2)
99
+ @test_throws Exception Tables. schema (chn2)
100
+ chn3 = Chains (val2, [" chain" , " a" ])
101
+ @test_throws Exception Tables. columns (chn3)
102
+ @test_throws Exception Tables. rows (chn3)
103
+ @test_throws Exception Tables. schema (chn3)
104
+ end
73
105
end
74
106
75
107
@testset " TableTraits interface" begin
@@ -82,10 +114,10 @@ using DataFrames
82
114
@test nt ==
83
115
(; (k => Tables. getcolumn (chn, k)[2 ] for k in Tables. columnnames (chn)). .. )
84
116
85
- val = rand (1000 , 2 , 4 )
86
- chn2 = Chains (val , [" iteration" , " a" ])
117
+ val2 = rand (1000 , 2 , 4 )
118
+ chn2 = Chains (val2 , [" iteration" , " a" ])
87
119
@test_throws Exception IteratorInterfaceExtensions. getiterator (chn2)
88
- chn3 = Chains (val , [" chain" , " a" ])
120
+ chn3 = Chains (val2 , [" chain" , " a" ])
89
121
@test_throws Exception IteratorInterfaceExtensions. getiterator (chn3)
90
122
end
91
123
@@ -106,29 +138,54 @@ using DataFrames
106
138
107
139
@testset " Tables interface" begin
108
140
@test Tables. istable (typeof (cdf))
109
- @test Tables. columnaccess (typeof (cdf))
110
- @test Tables. columns (cdf) === cdf
111
- @test Tables. columnnames (cdf) == keys (cdf. nt)
112
- for (k, v) in pairs (cdf. nt)
113
- @test Tables. getcolumn (cdf, k) == v
141
+
142
+ @testset " column access" begin
143
+ @test Tables. columnaccess (typeof (cdf))
144
+ @test Tables. columns (cdf) === cdf
145
+ @test Tables. columnnames (cdf) == keys (cdf. nt)
146
+ for (k, v) in pairs (cdf. nt)
147
+ @test Tables. getcolumn (cdf, k) == v
148
+ end
149
+ @test Tables. getcolumn (cdf, 1 ) == Tables. getcolumn (cdf, keys (cdf. nt)[1 ])
150
+ @test Tables. getcolumn (cdf, 2 ) == Tables. getcolumn (cdf, keys (cdf. nt)[2 ])
151
+ @test_throws Exception Tables. getcolumn (cdf, :blah )
152
+ @test_throws Exception Tables. getcolumn (cdf, length (cdf. nt) + 1 )
153
+ end
154
+
155
+ @testset " row access" begin
156
+ @test Tables. rowaccess (typeof (cdf))
157
+ @test Tables. rows (cdf) isa Tables. RowIterator
158
+ @test eltype (Tables. rows (cdf)) <: Tables.AbstractRow
159
+ rows = collect (Tables. rows (cdf))
160
+ @test eltype (rows) <: Tables.AbstractRow
161
+ @test size (rows) === (2 ,)
162
+ @testset for i in 1 : 2
163
+ row = rows[i]
164
+ @test Tables. columnnames (row) == keys (cdf. nt)
165
+ for j in length (cdf. nt)
166
+ @test Tables. getcolumn (row, j) == cdf. nt[j][i]
167
+ @test Tables. getcolumn (row, keys (cdf. nt)[j]) == cdf. nt[j][i]
168
+ end
169
+ end
170
+ end
171
+
172
+ @testset " integration tests" begin
173
+ @test length (Tables. rowtable (cdf)) == length (cdf. nt[1 ])
174
+ @test Tables. columntable (cdf) == cdf. nt
175
+ nt = Tables. rowtable (cdf)[1 ]
176
+ @test nt == (; (k => v[1 ] for (k, v) in pairs (cdf. nt)). .. )
177
+ @test nt == collect (Iterators. take (Tables. namedtupleiterator (cdf), 1 ))[1 ]
178
+ nt = Tables. rowtable (cdf)[2 ]
179
+ @test nt == (; (k => v[2 ] for (k, v) in pairs (cdf. nt)). .. )
180
+ @test nt == collect (Iterators. take (Tables. namedtupleiterator (cdf), 2 ))[2 ]
181
+ @test Tables. matrix (Tables. rowtable (cdf)) == Tables. matrix (Tables. columntable (cdf))
182
+ end
183
+
184
+ @testset " schema" begin
185
+ @test Tables. schema (cdf) isa Tables. Schema
186
+ @test Tables. schema (cdf). names === keys (cdf. nt)
187
+ @test Tables. schema (cdf). types === eltype .(values (cdf. nt))
114
188
end
115
- @test Tables. getcolumn (cdf, 1 ) == Tables. getcolumn (cdf, keys (cdf. nt)[1 ])
116
- @test Tables. getcolumn (cdf, 2 ) == Tables. getcolumn (cdf, keys (cdf. nt)[2 ])
117
- @test_throws Exception Tables. getcolumn (cdf, :blah )
118
- @test_throws Exception Tables. getcolumn (cdf, length (cdf. nt) + 1 )
119
- @test Tables. rowaccess (typeof (cdf))
120
- @test Tables. rows (cdf) === cdf
121
- @test length (Tables. rowtable (cdf)) == length (cdf. nt[1 ])
122
- @test Tables. columntable (cdf) == cdf. nt
123
- nt = Tables. rowtable (cdf)[1 ]
124
- @test nt == (; (k => v[1 ] for (k, v) in pairs (cdf. nt)). .. )
125
- @test nt == collect (Iterators. take (Tables. namedtupleiterator (cdf), 1 ))[1 ]
126
- nt = Tables. rowtable (cdf)[2 ]
127
- @test nt == (; (k => v[2 ] for (k, v) in pairs (cdf. nt)). .. )
128
- @test nt == collect (Iterators. take (Tables. namedtupleiterator (cdf), 2 ))[2 ]
129
- @test Tables. schema (cdf) isa Tables. Schema
130
- @test Tables. schema (cdf). names === keys (cdf. nt)
131
- @test Tables. schema (cdf). types === eltype .(values (cdf. nt))
132
189
end
133
190
134
191
@testset " TableTraits interface" begin
0 commit comments