|
29 | 29 | @testset "tables" begin
|
30 | 30 | using Tables
|
31 | 31 |
|
32 |
| - R = wrapdims(rand(2,3), 11:12, 21:23) |
33 |
| - N = wrapdims(rand(2,3), a=[11, 12], b=[21, 22, 23.0]) |
34 |
| - |
35 |
| - @test keys(first(Tables.rows(R))) == (:dim_1, :dim_2, :value) |
36 |
| - @test keys(first(Tables.rows(N))) == (:a, :b, :value) |
37 |
| - |
38 |
| - @test Tables.columns(N).a == [11, 12, 11, 12, 11, 12] |
39 |
| - |
| 32 | + @testset "source" begin |
| 33 | + R = wrapdims(rand(2,3), 11:12, 21:23) |
| 34 | + N = wrapdims(rand(2,3), a=[11, 12], b=[21, 22, 23.0]) |
| 35 | + |
| 36 | + @test keys(first(Tables.rows(R))) == (:dim_1, :dim_2, :value) |
| 37 | + @test keys(first(Tables.rows(N))) == (:a, :b, :value) |
| 38 | + |
| 39 | + @test Tables.columns(N).a == [11, 12, 11, 12, 11, 12] |
| 40 | + end |
| 41 | + @testset "sink" begin |
| 42 | + A = KeyedArray(rand(24, 11, 3); time = 0:23, loc = -5:5, id = ["a", "b", "c"]) |
| 43 | + table = Tables.columntable(A) |
| 44 | + |
| 45 | + # Test fully constructing from a table |
| 46 | + # Common when working with adhoc data |
| 47 | + B = wrapdims(table, :value, :time, :loc, :id) |
| 48 | + @test B == A |
| 49 | + |
| 50 | + # Test wrapping of key vectors, and wrong order: |
| 51 | + U = wrapdims(table, UniqueVector, :value, :id, :time, :loc) |
| 52 | + @test axiskeys(U, :time) isa UniqueVector |
| 53 | + @test U(time=3, id="b") == A(time=3, id="b") |
| 54 | + |
| 55 | + # Test populating an existing array (e.g., expected data based on calculated targets/offsets) |
| 56 | + C = KeyedArray( |
| 57 | + zeros(Float64, size(A)); |
| 58 | + time = unique(table.time), |
| 59 | + loc = unique(table.loc), |
| 60 | + id = unique(table.id), |
| 61 | + ) |
| 62 | + @test C != A |
| 63 | + AxisKeys.populate!(C, table, :value) |
| 64 | + @test C == A |
| 65 | + |
| 66 | + # Constructing a NamedDimsArray with different default value and table type |
| 67 | + # Partial populating |
| 68 | + r_table = Tables.rowtable(A) |
| 69 | + n = length(r_table) |
| 70 | + idx = rand(Bool, n) |
| 71 | + D = wrapdims(r_table[idx], :value, :time, :loc, :id; default=missing) |
| 72 | + # dimnames should still match, but we'll have missing values |
| 73 | + @test dimnames(D) == dimnames(A) |
| 74 | + @test any(ismissing, D) |
| 75 | + |
| 76 | + # BTW, this is why it's a method of wrapdims, not KeyedArray: |
| 77 | + # @code_warntype wrapdims(table, :value, :time, :loc, :id) # ::Any |
| 78 | + # @code_warntype wrapdims(r_table[idx], :value, :time, :loc, :id; default=missing) |
| 79 | + |
| 80 | + # Construction with invalid columns error as expected, but the specific error is |
| 81 | + # dependent on the table type. |
| 82 | + # ERROR: ArgumentError: wrong number of names, got (:q, :time, :loc, :id) with ndims(A) == 1 |
| 83 | + @test_throws ArgumentError wrapdims(Tables.rowtable(A), :q, :time, :loc, :id) |
| 84 | + # ERROR: ArgumentError: wrong number of names, got (:value, :p, :loc, :id) with ndims(A) == 1 |
| 85 | + @test_throws ArgumentError wrapdims(Tables.rowtable(A), :value, :p, :loc, :id) |
| 86 | + # ERROR: type NamedTuple has no field q |
| 87 | + @test_throws ErrorException wrapdims(Tables.columntable(A), :q, :time, :loc, :id) |
| 88 | + # ERROR: type NamedTuple has no field p |
| 89 | + @test_throws ErrorException wrapdims(Tables.columntable(A), :value, :p, :loc, :id) |
| 90 | + |
| 91 | + # Construction with duplicates |
| 92 | + # ERROR: ArgumentError: Key (Date("2019-01-01"), -5) is not unique |
| 93 | + @test_throws ArgumentError wrapdims(table, :value, :time, :loc) |
| 94 | + @test wrapdims(r_table, :value, :time, :loc; force=true) == C(:, :, Key("c")) |
| 95 | + end |
40 | 96 | end
|
41 | 97 | @testset "stack" begin
|
42 | 98 | using LazyStack
|
|
0 commit comments