Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c58750
commit 3233ada
Showing
2 changed files
with
157 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
defmodule Mix.Tasks.Absinthe.Schema.JsonTest do | ||
use Absinthe.Case, async: true | ||
|
||
alias Mix.Tasks.Absinthe.Schema.Json, as: Task | ||
|
||
defmodule TestSchema do | ||
use Absinthe.Schema | ||
|
||
query do | ||
field :item, :item | ||
end | ||
|
||
object :item do | ||
description "A Basic Type" | ||
field :id, :id | ||
field :name, :string | ||
end | ||
end | ||
|
||
defmodule TestEncoder do | ||
def encode!(_map, opts) do | ||
pretty_flag = Keyword.get(opts, :pretty, false) | ||
pretty_string = if pretty_flag, do: "pretty", else: "ugly" | ||
"test-encoder-#{pretty_string}" | ||
end | ||
end | ||
|
||
@test_schema "Mix.Tasks.Absinthe.Schema.JsonTest.TestSchema" | ||
@test_encoder "Mix.Tasks.Absinthe.Schema.JsonTest.TestEncoder" | ||
|
||
describe "absinthe.schema.json" do | ||
test "parses options" do | ||
argv = ["output.json", "--schema", @test_schema, "--json-codec", @test_encoder, "--pretty"] | ||
|
||
opts = Task.parse_options(argv) | ||
|
||
assert opts.filename == "output.json" | ||
assert opts.json_codec == TestEncoder | ||
assert opts.pretty == true | ||
assert opts.schema == TestSchema | ||
end | ||
|
||
test "provides default options" do | ||
argv = ["--schema", @test_schema] | ||
|
||
opts = Task.parse_options(argv) | ||
|
||
assert opts.filename == "./schema.json" | ||
assert opts.json_codec == Jason | ||
assert opts.pretty == false | ||
assert opts.schema == TestSchema | ||
end | ||
|
||
test "fails if no schema arg is provided" do | ||
argv = [] | ||
catch_error(Task.parse_options(argv)) | ||
end | ||
|
||
test "fails if codec hasn't been loaded" do | ||
argv = ["--schema", @test_schema, "--json-codec", "UnloadedCodec"] | ||
opts = Task.parse_options(argv) | ||
catch_error(Task.generate_schema(opts)) | ||
end | ||
|
||
test "can use a custom codec" do | ||
argv = ["--schema", @test_schema, "--json-codec", @test_encoder, "--pretty"] | ||
|
||
opts = Task.parse_options(argv) | ||
{:ok, pretty_content} = Task.generate_schema(opts) | ||
{:ok, ugly_content} = Task.generate_schema(%{opts | pretty: false}) | ||
|
||
assert pretty_content == "test-encoder-pretty" | ||
assert ugly_content == "test-encoder-ugly" | ||
end | ||
end | ||
end |