diff --git a/test/runtests.jl b/test/runtests.jl index aa65798..7c3651f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -12,7 +12,7 @@ import JSON3 import OrderedCollections import ZipFile -const TEST_SUITE_URL = "https://github.com/json-schema-org/JSON-Schema-Test-Suite/archive/2.0.0.zip" +const TEST_SUITE_URL = "https://github.com/json-schema-org/JSON-Schema-Test-Suite/archive/23.1.0.zip" const SCHEMA_TEST_DIR = let dest_dir = mktempdir() @@ -26,7 +26,7 @@ const SCHEMA_TEST_DIR = let write(filename, read(f, String)) end end - joinpath(dest_dir, "test-suite", "JSON-Schema-Test-Suite-2.0.0", "tests") + joinpath(dest_dir, "test-suite", "JSON-Schema-Test-Suite-23.1.0", "tests") end const LOCAL_TEST_DIR = mktempdir(SCHEMA_TEST_DIR) @@ -131,55 +131,31 @@ write( }]""", ) -@testset "Draft 4/6/7" begin - # Note(odow): I didn't want to use a mutable reference like this for the web-server. - # The obvious thing to do is to start a new server for each value of `draft_folder`, - # however, shutting down the webserver asynchronously doesn't play well with - # testsets, and I spent far too long trying to figure out what was going on. - # This is a simple hack until someone who knows more about this comes along... - GLOBAL_TEST_DIR = Ref{String}("") - server = HTTP.Sockets.listen(HTTP.ip"127.0.0.1", 1234) - HTTP.serve!("127.0.0.1", 1234; server = server) do req - # Make sure to strip first character (`/`) from the target, otherwise it - # will infer as a file in the root directory. - file = joinpath(GLOBAL_TEST_DIR[], "../../remotes", req.target[2:end]) - return HTTP.Response(200, read(file, String)) - end - @testset "$(draft_folder)" for draft_folder in [ - "draft4", - "draft6", - "draft7", - basename(abspath(LOCAL_TEST_DIR)), - ] - test_dir = joinpath(SCHEMA_TEST_DIR, draft_folder) - GLOBAL_TEST_DIR[] = test_dir - @testset "$(file)" for file in filter( - n -> endswith(n, ".json"), - readdir(test_dir), - ) - file_path = joinpath(test_dir, file) - @testset "$(schema["description"])" for schema in - JSON.parsefile(file_path) - spec = JSONSchema.Schema( - schema["schema"]; - parent_dir = schema["schema"] isa Bool ? abspath(".") : - dirname(file_path), - ) - @testset "$(test["description"])" for test in schema["tests"] - @test isvalid(spec, test["data"]) == test["valid"] - end +is_json(n) = endswith(n, ".json") + +function test_draft_directory(server, dir, json_parse_fn::Function) + @testset "$(file)" for file in filter(is_json, readdir(dir)) + if file == "unknownKeyword.json" + # This is an optional test, and to be honest, it is pretty minor. It + # relates to how we handle $id if the user includes part of a schema + # that we don't know how to parse. As a low priority action item, we + # could come back to this. + continue + end + file_path = joinpath(dir, file) + @testset "$(tests["description"])" for tests in json_parse_fn(file_path) + is_bool = tests["schema"] isa Bool + parent_dir = ifelse(is_bool, abspath("."), dirname(file_path)) + schema = JSONSchema.Schema(tests["schema"]; parent_dir) + @testset "$(test["description"])" for test in tests["tests"] + @test isvalid(schema, test["data"]) == test["valid"] end end end - close(server) + return end -@testset "Draft 4/6 JSON3 parsing" begin - # Note(odow): I didn't want to use a mutable reference like this for the web-server. - # The obvious thing to do is to start a new server for each value of `draft_folder`, - # however, shutting down the webserver asynchronously doesn't play well with - # testsets, and I spent far too long trying to figure out what was going on. - # This is a simple hack until someone who knows more about this comes along... +@testset "JSON-Schema-Test-Suite" begin GLOBAL_TEST_DIR = Ref{String}("") server = HTTP.Sockets.listen(HTTP.ip"127.0.0.1", 1234) HTTP.serve!("127.0.0.1", 1234; server = server) do req @@ -188,30 +164,18 @@ end file = joinpath(GLOBAL_TEST_DIR[], "../../remotes", req.target[2:end]) return HTTP.Response(200, read(file, String)) end - @testset "$(draft_folder)" for draft_folder in [ + @testset "$dir" for dir in [ "draft4", "draft6", "draft7", basename(abspath(LOCAL_TEST_DIR)), ] - test_dir = joinpath(SCHEMA_TEST_DIR, draft_folder) - GLOBAL_TEST_DIR[] = test_dir - @testset "$(file)" for file in filter( - n -> endswith(n, ".json"), - readdir(test_dir), - ) - file_path = joinpath(test_dir, file) - @testset "$(schema["description"])" for schema in - JSON3.read(file_path) - spec = JSONSchema.Schema( - schema[:schema]; - parent_dir = schema[:schema] isa Bool ? abspath(".") : - dirname(file_path), - ) - @testset "$(test["description"])" for test in schema[:tests] - @test isnothing(validate(spec, test[:data])) == test[:valid] - end - end + GLOBAL_TEST_DIR[] = joinpath(SCHEMA_TEST_DIR, dir) + @testset "JSON" begin + test_draft_directory(server, GLOBAL_TEST_DIR[], JSON.parsefile) + end + @testset "JSON3" begin + test_draft_directory(server, GLOBAL_TEST_DIR[], JSON3.read) end end close(server)