|
1 |
| -@testset "Selection" begin |
2 |
| - @testset "TableSelection" begin |
3 |
| - a = rand(10) |
4 |
| - b = rand(10) |
5 |
| - c = rand(10) |
6 |
| - d = rand(10) |
7 |
| - e = rand(10) |
8 |
| - f = rand(10) |
9 |
| - t = Table(; a, b, c, d, e, f) |
10 |
| - |
11 |
| - # Tables.jl interface |
12 |
| - select = [:a, :b, :e] |
13 |
| - newnames = select |
14 |
| - s = TableTransforms.TableSelection(t, newnames, select) |
15 |
| - @test Tables.istable(s) == true |
16 |
| - @test Tables.columnaccess(s) == true |
17 |
| - @test Tables.rowaccess(s) == true |
18 |
| - @test Tables.columns(s) === s |
19 |
| - @test Tables.rows(s) == TableTransforms.SelectionRows(s) |
20 |
| - @test Tables.columnnames(s) == [:a, :b, :e] |
21 |
| - @test Tables.schema(s).names == (:a, :b, :e) |
22 |
| - @test Tables.schema(s).types == (Float64, Float64, Float64) |
23 |
| - @test Tables.materializer(s) == Tables.materializer(t) |
24 |
| - |
25 |
| - # getcolumn |
26 |
| - cols = Tables.columns(t) |
27 |
| - @test Tables.getcolumn(s, :a) == Tables.getcolumn(cols, :a) |
28 |
| - @test Tables.getcolumn(s, 1) == Tables.getcolumn(cols, 1) |
29 |
| - @test Tables.getcolumn(s, 3) == Tables.getcolumn(cols, :e) |
30 |
| - |
31 |
| - # selectin with renaming |
32 |
| - select = [:c, :d, :f] |
33 |
| - newnames = [:x, :y, :z] |
34 |
| - s = TableTransforms.TableSelection(t, newnames, select) |
35 |
| - @test Tables.columnnames(s) == [:x, :y, :z] |
36 |
| - @test Tables.getcolumn(s, :x) == t.c |
37 |
| - @test Tables.getcolumn(s, :y) == t.d |
38 |
| - @test Tables.getcolumn(s, :z) == t.f |
39 |
| - @test Tables.getcolumn(s, 1) == t.c |
40 |
| - @test Tables.getcolumn(s, 2) == t.d |
41 |
| - @test Tables.getcolumn(s, 3) == t.f |
42 |
| - |
43 |
| - # row table |
44 |
| - select = [:a, :b, :e] |
45 |
| - newnames = select |
46 |
| - rt = Tables.rowtable(t) |
47 |
| - s = TableTransforms.TableSelection(rt, newnames, select) |
48 |
| - cols = Tables.columns(rt) |
49 |
| - @test Tables.getcolumn(s, :a) == Tables.getcolumn(cols, :a) |
50 |
| - @test Tables.getcolumn(s, 1) == Tables.getcolumn(cols, 1) |
51 |
| - @test Tables.getcolumn(s, 3) == Tables.getcolumn(cols, :e) |
52 |
| - |
53 |
| - # throws |
54 |
| - @test_throws AssertionError TableTransforms.TableSelection(t, [:a, :b, :z], [:a, :b, :z]) |
55 |
| - @test_throws AssertionError TableTransforms.TableSelection(t, [:x, :y, :z], [:c, :d, :k]) |
56 |
| - s = TableTransforms.TableSelection(t, [:a, :b, :e], [:a, :b, :e]) |
57 |
| - @test_throws ErrorException Tables.getcolumn(s, :f) |
58 |
| - @test_throws ErrorException Tables.getcolumn(s, 4) |
59 |
| - s = TableTransforms.TableSelection(t, [:x, :y, :z], [:c, :d, :f]) |
60 |
| - @test_throws ErrorException Tables.getcolumn(s, :c) |
61 |
| - @test_throws ErrorException Tables.getcolumn(s, 4) |
62 |
| - @test_throws ErrorException Tables.getcolumn(s, -2) |
63 |
| - end |
64 |
| - |
65 |
| - @testset "SelectionRow" begin |
66 |
| - x = [2, 1, 7, 5, 2] |
67 |
| - y = [7, 6, 8, 1, 3] |
68 |
| - z = [3, 4, 6, 4, 7] |
69 |
| - table = Table(; x, y, z) |
70 |
| - |
71 |
| - select = [:x, :z] |
72 |
| - newnames = select |
73 |
| - s = TableTransforms.TableSelection(table, newnames, select) |
74 |
| - m = Tables.matrix(s) |
75 |
| - |
76 |
| - # Tables.jl row interface |
77 |
| - srow = TableTransforms.SelectionRow(s, 1) |
78 |
| - @test Tables.columnnames(srow) == Tables.columnnames(s) |
79 |
| - @test Tables.getcolumn(srow, 1) == 2 |
80 |
| - @test Tables.getcolumn(srow, 2) == 3 |
81 |
| - @test Tables.getcolumn(srow, :x) == 2 |
82 |
| - @test Tables.getcolumn(srow, :z) == 3 |
83 |
| - |
84 |
| - # Iteration interface |
85 |
| - for (i, row) in enumerate(eachrow(m)) |
86 |
| - srow = TableTransforms.SelectionRow(s, i) |
87 |
| - @test length(srow) == length(row) |
88 |
| - @test collect(srow) == collect(row) |
89 |
| - for (sr, r) in zip(srow, row) |
90 |
| - @test sr == r |
91 |
| - end |
92 |
| - end |
93 |
| - |
94 |
| - # Indexing interface |
95 |
| - for (i, row) in enumerate(eachrow(m)) |
96 |
| - srow = TableTransforms.SelectionRow(s, i) |
97 |
| - @test firstindex(srow) == 1 |
98 |
| - @test lastindex(srow) == lastindex(row) |
99 |
| - for j in eachindex(srow) |
100 |
| - @test srow[j] == row[j] |
101 |
| - end |
102 |
| - end |
103 |
| - end |
104 |
| - |
105 |
| - @testset "SelectionRows" begin |
106 |
| - a = rand(10) |
107 |
| - b = rand(10) |
108 |
| - c = rand(10) |
109 |
| - d = rand(10) |
110 |
| - t = Table(; a, b, c, d) |
111 |
| - |
112 |
| - select = [:a, :d] |
113 |
| - newnames = select |
114 |
| - s = TableTransforms.TableSelection(t, newnames, select) |
115 |
| - srows = TableTransforms.SelectionRows(s) |
116 |
| - |
117 |
| - # Tables.jl interface |
118 |
| - @test Tables.istable(srows) == true |
119 |
| - @test Tables.rowaccess(srows) == true |
120 |
| - @test Tables.columnaccess(srows) == true |
121 |
| - @test Tables.rows(srows) === srows |
122 |
| - @test Tables.columns(srows) === Tables.columns(srows.selection) |
123 |
| - @test Tables.columnnames(srows) == Tables.columnnames(srows.selection) |
124 |
| - @test Tables.schema(srows) == Tables.schema(srows.selection) |
125 |
| - @test Tables.materializer(srows) == Tables.materializer(srows.selection) |
126 |
| - |
127 |
| - # getcolumn |
128 |
| - cols = Tables.columns(srows) |
129 |
| - @test Tables.getcolumn(srows, :a) == Tables.getcolumn(cols, :a) |
130 |
| - @test Tables.getcolumn(srows, 1) == Tables.getcolumn(cols, 1) |
131 |
| - @test Tables.getcolumn(srows, 2) == Tables.getcolumn(cols, :d) |
132 |
| - |
133 |
| - # Iteration interface |
134 |
| - @test length(srows) == TableTransforms._nrows(s.cols) |
135 |
| - @test eltype(srows) == typeof(TableTransforms.SelectionRow(s, 1)) |
136 |
| - for (i, srow) in enumerate(srows) |
137 |
| - @test srow == TableTransforms.SelectionRow(s, i) |
138 |
| - end |
139 |
| - |
140 |
| - # Indexing interface |
141 |
| - @test firstindex(srows) == 1 |
142 |
| - @test lastindex(srows) == TableTransforms._nrows(s.cols) |
143 |
| - for i in eachindex(srows) |
144 |
| - @test srows[i] == TableTransforms.SelectionRow(s, i) |
145 |
| - end |
146 |
| - end |
| 1 | +@testset "TableSelection" begin |
| 2 | + a = rand(10) |
| 3 | + b = rand(10) |
| 4 | + c = rand(10) |
| 5 | + d = rand(10) |
| 6 | + e = rand(10) |
| 7 | + f = rand(10) |
| 8 | + t = Table(; a, b, c, d, e, f) |
| 9 | + |
| 10 | + # Tables.jl interface |
| 11 | + select = [:a, :b, :e] |
| 12 | + newnames = select |
| 13 | + s = TableTransforms.TableSelection(t, newnames, select) |
| 14 | + @test Tables.istable(s) == true |
| 15 | + @test Tables.columnaccess(s) == true |
| 16 | + @test Tables.rowaccess(s) == false |
| 17 | + @test Tables.columns(s) === s |
| 18 | + @test Tables.columnnames(s) == [:a, :b, :e] |
| 19 | + @test Tables.schema(s).names == (:a, :b, :e) |
| 20 | + @test Tables.schema(s).types == (Float64, Float64, Float64) |
| 21 | + @test Tables.materializer(s) == Tables.materializer(t) |
| 22 | + |
| 23 | + # getcolumn |
| 24 | + cols = Tables.columns(t) |
| 25 | + @test Tables.getcolumn(s, :a) == Tables.getcolumn(cols, :a) |
| 26 | + @test Tables.getcolumn(s, 1) == Tables.getcolumn(cols, 1) |
| 27 | + @test Tables.getcolumn(s, 3) == Tables.getcolumn(cols, :e) |
| 28 | + |
| 29 | + # selectin with renaming |
| 30 | + select = [:c, :d, :f] |
| 31 | + newnames = [:x, :y, :z] |
| 32 | + s = TableTransforms.TableSelection(t, newnames, select) |
| 33 | + @test Tables.columnnames(s) == [:x, :y, :z] |
| 34 | + @test Tables.getcolumn(s, :x) == t.c |
| 35 | + @test Tables.getcolumn(s, :y) == t.d |
| 36 | + @test Tables.getcolumn(s, :z) == t.f |
| 37 | + @test Tables.getcolumn(s, 1) == t.c |
| 38 | + @test Tables.getcolumn(s, 2) == t.d |
| 39 | + @test Tables.getcolumn(s, 3) == t.f |
| 40 | + |
| 41 | + # row table |
| 42 | + select = [:a, :b, :e] |
| 43 | + newnames = select |
| 44 | + rt = Tables.rowtable(t) |
| 45 | + s = TableTransforms.TableSelection(rt, newnames, select) |
| 46 | + cols = Tables.columns(rt) |
| 47 | + @test Tables.getcolumn(s, :a) == Tables.getcolumn(cols, :a) |
| 48 | + @test Tables.getcolumn(s, 1) == Tables.getcolumn(cols, 1) |
| 49 | + @test Tables.getcolumn(s, 3) == Tables.getcolumn(cols, :e) |
| 50 | + |
| 51 | + # throws |
| 52 | + @test_throws AssertionError TableTransforms.TableSelection(t, [:a, :b, :z], [:a, :b, :z]) |
| 53 | + @test_throws AssertionError TableTransforms.TableSelection(t, [:x, :y, :z], [:c, :d, :k]) |
| 54 | + s = TableTransforms.TableSelection(t, [:a, :b, :e], [:a, :b, :e]) |
| 55 | + @test_throws ErrorException Tables.getcolumn(s, :f) |
| 56 | + @test_throws ErrorException Tables.getcolumn(s, 4) |
| 57 | + s = TableTransforms.TableSelection(t, [:x, :y, :z], [:c, :d, :f]) |
| 58 | + @test_throws ErrorException Tables.getcolumn(s, :c) |
| 59 | + @test_throws ErrorException Tables.getcolumn(s, 4) |
| 60 | + @test_throws ErrorException Tables.getcolumn(s, -2) |
147 | 61 | end
|
0 commit comments