|
1275 | 1275 | n, c = apply(T, t)
|
1276 | 1276 | @test_throws AssertionError revert(T, n, c)
|
1277 | 1277 | end
|
1278 |
| - |
| 1278 | + |
1279 | 1279 | @testset "Levels" begin
|
| 1280 | + a = rand([true, false], 50) |
| 1281 | + b = rand(["y", "n"], 50) |
| 1282 | + c = rand(1:3, 50) |
| 1283 | + t = Table(; a, b, c) |
| 1284 | + |
| 1285 | + T = Levels(2 => ["n", "y", "m"]) |
| 1286 | + n, c = apply(T, t) |
| 1287 | + @test levels(n.b) == ["n", "y", "m"] |
| 1288 | + @test isordered(n.b) == false |
| 1289 | + tₒ = revert(T, n, c) |
| 1290 | + @test tₒ == t |
| 1291 | + |
| 1292 | + T = Levels(:b => ["n", "y", "m"], :c => 1:4, ordered=[:c]) |
| 1293 | + n, c = apply(T, t) |
| 1294 | + @test levels(n.b) == ["n", "y", "m"] |
| 1295 | + @test isordered(n.b) == false |
| 1296 | + @test levels(n.c) == [1, 2, 3, 4] |
| 1297 | + @test isordered(n.c) == true |
| 1298 | + tₒ = revert(T, n, c) |
| 1299 | + @test tₒ == t |
| 1300 | + |
| 1301 | + T = Levels("b" => ["n", "y", "m"], "c" => 1:4, ordered=["b"]) |
| 1302 | + n, c = apply(T, t) |
| 1303 | + @test levels(n.b) == ["n", "y", "m"] |
| 1304 | + @test isordered(n.b) == true |
| 1305 | + @test levels(n.c) == [1, 2, 3, 4] |
| 1306 | + @test isordered(n.c) == false |
| 1307 | + tₒ = revert(T, n, c) |
| 1308 | + @test tₒ == t |
| 1309 | + |
| 1310 | + # categorical columns |
1280 | 1311 | a = categorical(["yes", "no", "no", "no", "yes"])
|
1281 | 1312 | b = categorical([1, 2, 4, 2, 8], ordered=false)
|
1282 | 1313 | c = categorical([1, 2, 1, 2, 1])
|
|
1288 | 1319 | n, c = apply(T, t)
|
1289 | 1320 | @test levels(n.a) == ["yes", "no"]
|
1290 | 1321 | @test levels(n.c) == [1, 2, 4]
|
| 1322 | + @test levels(n.d) == [1, 23, 5, 7] |
1291 | 1323 | @test levels(n.e) == [1, 2, 3, 4, 5]
|
1292 | 1324 | tₒ = revert(T, n, c)
|
1293 | 1325 | @test levels(tₒ.a) == ["no", "yes"]
|
|
1302 | 1334 | @test levels(tₒ.a) == ["no", "yes"]
|
1303 | 1335 | @test levels(tₒ.c) == [1, 2]
|
1304 | 1336 |
|
1305 |
| - T = Levels(:a => ["yes", "no"], :c => [1, 2, 4], :d => [1, 23, 5, 7], ordered=[:b]) |
| 1337 | + T = Levels(:a => ["yes", "no"], :c => [1, 2, 4], :d => [1, 23, 5, 7]) |
1306 | 1338 | n, c = apply(T, t)
|
1307 | 1339 | @test levels(n.a) == ["yes", "no"]
|
1308 |
| - @test isordered(n.b) == true |
1309 | 1340 | @test levels(n.c) == [1, 2, 4]
|
| 1341 | + @test levels(n.d) == [1, 23, 5, 7] |
1310 | 1342 | tₒ = revert(T, n, c)
|
1311 | 1343 | @test levels(tₒ.a) == ["no", "yes"]
|
1312 | 1344 | @test levels(tₒ.c) == [1, 2]
|
1313 |
| - @test isordered(tₒ.b) == false |
1314 | 1345 |
|
1315 |
| - T = Levels("a" => ["yes", "no"], "c" => [1, 2, 4], "e" => 5:-1:1, ordered=["b", "e"]) |
| 1346 | + T = Levels("a" => ["yes", "no"], "c" => [1, 2, 4], "e" => 5:-1:1, ordered=["e"]) |
1316 | 1347 | n, c = apply(T, t)
|
1317 |
| - @test isordered(n.b) == true |
1318 |
| - @test levels(n.e) == [5, 4, 3, 2, 1] |
| 1348 | + @test levels(n.a) == ["yes", "no"] |
1319 | 1349 | @test levels(n.c) == [1, 2, 4]
|
| 1350 | + @test levels(n.e) == [5, 4, 3, 2, 1] |
| 1351 | + @test isordered(n.a) == false |
| 1352 | + @test isordered(n.c) == false |
| 1353 | + @test isordered(n.e) == true |
1320 | 1354 | tₒ = revert(T, n, c)
|
1321 |
| - @test isordered(tₒ.b) == false |
1322 | 1355 | @test levels(tₒ.e) == [1, 2, 3, 4]
|
| 1356 | + @test isordered(tₒ.e) == false |
1323 | 1357 |
|
1324 |
| - T = Levels(:a => ["yes", "no"], :c => [1, 2, 4], :d => [1, 23, 5, 7], ordered=[:a, :b, :d]) |
| 1358 | + T = Levels(:a => ["yes", "no"], :c => [1, 2, 4], :d => [1, 23, 5, 7], ordered=[:a, :d]) |
1325 | 1359 | n, c = apply(T, t)
|
1326 | 1360 | @test levels(n.a) == ["yes", "no"]
|
| 1361 | + @test levels(n.c) == [1, 2, 4] |
| 1362 | + @test levels(n.d) == [1, 23, 5, 7] |
1327 | 1363 | @test isordered(n.a) == true
|
1328 |
| - @test isordered(n.b) == true |
| 1364 | + @test isordered(n.c) == false |
1329 | 1365 | @test isordered(n.d) == true
|
1330 | 1366 | tₒ = revert(T, n, c)
|
1331 | 1367 | @test typeof(tₒ.d) == Vector{Int64}
|
1332 | 1368 | @test isordered(tₒ.a) == false
|
1333 |
| - @test isordered(tₒ.b) == false |
| 1369 | + |
| 1370 | + a = rand([true, false], 50) |
| 1371 | + b = rand(["y", "n"], 50) |
| 1372 | + c = rand(1:3, 50) |
| 1373 | + t = Table(; a, b, c) |
| 1374 | + |
| 1375 | + # throws: Levels without arguments |
| 1376 | + @test_throws ArgumentError Levels() |
| 1377 | + |
| 1378 | + # throws: columns that do not exist in the original table |
| 1379 | + T = Levels(:x => ["n", "y", "m"], :y => 1:4) |
| 1380 | + @test_throws AssertionError apply(T, t) |
| 1381 | + T = Levels("x" => ["n", "y", "m"], "y" => 1:4) |
| 1382 | + @test_throws AssertionError apply(T, t) |
| 1383 | + |
| 1384 | + # throws: invalid ordered column selection |
| 1385 | + T = Levels(:b => ["n", "y", "m"], :c => 1:4, ordered=[:a]) |
| 1386 | + @test_throws AssertionError apply(T, t) |
| 1387 | + T = Levels("b" => ["n", "y", "m"], "c" => 1:4, ordered=["a"]) |
| 1388 | + @test_throws AssertionError apply(T, t) |
| 1389 | + T = Levels("b" => ["n", "y", "m"], "c" => 1:4, ordered=r"xy") |
| 1390 | + @test_throws AssertionError apply(T, t) |
1334 | 1391 | end
|
1335 | 1392 |
|
1336 | 1393 | @testset "Sort" begin
|
|
0 commit comments