|
| 1 | + |
| 2 | +@testset "constructor" begin |
| 3 | + @test_throws AssertionError DataStore(10, (:x => rand(10), :y => rand(2, 4))) |
| 4 | + |
| 5 | + @testset "keyword args" begin |
| 6 | + ds = DataStore(10, x = rand(10), y = rand(2, 10)) |
| 7 | + @test size(ds.x) == (10,) |
| 8 | + @test size(ds.y) == (2, 10) |
| 9 | + |
| 10 | + ds = DataStore(x = rand(10), y = rand(2, 10)) |
| 11 | + @test size(ds.x) == (10,) |
| 12 | + @test size(ds.y) == (2, 10) |
| 13 | + end |
| 14 | +end |
| 15 | + |
| 16 | +@testset "getproperty / setproperty!" begin |
| 17 | + x = rand(10) |
| 18 | + ds = DataStore(10, (:x => x, :y => rand(2, 10))) |
| 19 | + @test ds.x == ds[:x] == x |
| 20 | + @test_throws DimensionMismatch ds.z=rand(12) |
| 21 | + ds.z = [1:10;] |
| 22 | + @test ds.z == [1:10;] |
| 23 | + vec = [DataStore(10, (:x => x,)), DataStore(10, (:x => x, :y => rand(2, 10)))] |
| 24 | + @test vec.x == [x, x] |
| 25 | + @test_throws KeyError vec.z |
| 26 | + @test vec._n == [10, 10] |
| 27 | + @test vec._data == [Dict(:x => x), Dict(:x => x, :y => vec[2].y)] |
| 28 | +end |
| 29 | + |
| 30 | +@testset "setindex!" begin |
| 31 | + ds = DataStore(10) |
| 32 | + x = rand(10) |
| 33 | + @test (ds[:x] = x) == x # Tests setindex! |
| 34 | + @test ds.x == ds[:x] == x |
| 35 | +end |
| 36 | + |
| 37 | +@testset "map" begin |
| 38 | + ds = DataStore(10, (:x => rand(10), :y => rand(2, 10))) |
| 39 | + ds2 = map(x -> x .+ 1, ds) |
| 40 | + @test ds2.x == ds.x .+ 1 |
| 41 | + @test ds2.y == ds.y .+ 1 |
| 42 | + |
| 43 | + @test_throws AssertionError ds2=map(x -> [x; x], ds) |
| 44 | +end |
| 45 | + |
| 46 | +@testset "getdata / getn" begin |
| 47 | + ds = DataStore(10, (:x => rand(10), :y => rand(2, 10))) |
| 48 | + @test getdata(ds) == getfield(ds, :_data) |
| 49 | + @test_throws KeyError ds.data |
| 50 | + @test getn(ds) == getfield(ds, :_n) |
| 51 | + @test_throws KeyError ds.n |
| 52 | +end |
| 53 | + |
| 54 | +@testset "cat empty" begin |
| 55 | + ds1 = DataStore(2, (:x => rand(2))) |
| 56 | + ds2 = DataStore(1, (:x => rand(1))) |
| 57 | + dsempty = DataStore(0, (:x => rand(0))) |
| 58 | + |
| 59 | + ds = GNNGraphs.cat_features(ds1, ds2) |
| 60 | + @test getn(ds) == 3 |
| 61 | + ds = GNNGraphs.cat_features(ds1, dsempty) |
| 62 | + @test getn(ds) == 2 |
| 63 | + |
| 64 | + # issue #280 |
| 65 | + g = GNNGraph([1], [2]) |
| 66 | + h = add_edges(g, Int[], Int[]) # adds no edges |
| 67 | + @test getn(g.edata) == 1 |
| 68 | + @test getn(h.edata) == 1 |
| 69 | +end |
| 70 | + |
| 71 | + |
| 72 | +@testset "gradient" begin |
| 73 | + ds = DataStore(10, (:x => rand(10), :y => rand(2, 10))) |
| 74 | + |
| 75 | + f1(ds) = sum(ds.x) |
| 76 | + grad = gradient(f1, ds)[1] |
| 77 | + @test grad._data[:x] ≈ ngradient(f1, ds)[1][:x] |
| 78 | + |
| 79 | + g = rand_graph(5, 2) |
| 80 | + x = rand(2, 5) |
| 81 | + grad = gradient(x -> sum(exp, GNNGraph(g, ndata = x).ndata.x), x)[1] |
| 82 | + @test grad == exp.(x) |
| 83 | +end |
| 84 | + |
| 85 | +@testset "functor" begin |
| 86 | + ds = DataStore(10, (:x => zeros(10), :y => ones(2, 10))) |
| 87 | + p, re = Functors.functor(ds) |
| 88 | + @test p[1] === getn(ds) |
| 89 | + @test p[2] === getdata(ds) |
| 90 | + @test ds == re(p) |
| 91 | + |
| 92 | + ds2 = Functors.fmap(ds) do x |
| 93 | + if x isa AbstractArray |
| 94 | + x .+ 1 |
| 95 | + else |
| 96 | + x |
| 97 | + end |
| 98 | + end |
| 99 | + @test ds isa DataStore |
| 100 | + @test ds2.x == ds.x .+ 1 |
| 101 | +end |
0 commit comments