-
Notifications
You must be signed in to change notification settings - Fork 519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix/add tests for compile time default value evaluation #1026
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,31 @@ | ||
defmodule Elixir.Absinthe.Integration.Execution.Variables.DefaultValueTest do | ||
use Absinthe.Case, async: true | ||
|
||
@query """ | ||
@times_query """ | ||
query ($mult: Int = 6) { | ||
times(base: 4, multiplier: $mult) | ||
} | ||
""" | ||
|
||
test "scenario #1" do | ||
@default_value_query """ | ||
query { | ||
microsecond | ||
} | ||
""" | ||
|
||
test "query field arg default_value and resolve execution" do | ||
assert {:ok, %{data: %{"times" => 24}}} == | ||
Absinthe.run(@query, Absinthe.Fixtures.TimesSchema, []) | ||
Absinthe.run(@times_query, Absinthe.Fixtures.TimesSchema, []) | ||
end | ||
|
||
test "query field default is evaluated only once" do | ||
{:ok, %{data: %{"microsecond" => first_current_microsecond}}} = | ||
Absinthe.run(@default_value_query, Absinthe.Fixtures.DefaultValueSchema, []) | ||
|
||
{:ok, %{data: %{"microsecond" => second_current_microsecond}}} = | ||
Absinthe.run(@default_value_query, Absinthe.Fixtures.DefaultValueSchema, []) | ||
|
||
# If the code to grab the default_value was executed twice, this would be different | ||
assert first_current_microsecond == second_current_microsecond | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,7 @@ defmodule Absinthe.Type.ObjectTest do | |
end | ||
end | ||
|
||
describe "input object keyword description evaluation" do | ||
describe "object keyword description evaluation" do | ||
Absinthe.Fixtures.FunctionEvaluationHelpers.function_evaluation_test_params() | ||
|> Enum.each(fn %{ | ||
test_label: test_label, | ||
|
@@ -83,7 +83,7 @@ defmodule Absinthe.Type.ObjectTest do | |
end) | ||
end | ||
|
||
describe "input object field keyword description evaluation" do | ||
describe "object field keyword description evaluation" do | ||
Absinthe.Fixtures.FunctionEvaluationHelpers.function_evaluation_test_params() | ||
|> Enum.each(fn %{ | ||
test_label: test_label, | ||
|
@@ -100,7 +100,7 @@ defmodule Absinthe.Type.ObjectTest do | |
end) | ||
end | ||
|
||
describe "input object field attribute description evaluation" do | ||
describe "object field attribute description evaluation" do | ||
Absinthe.Fixtures.FunctionEvaluationHelpers.function_evaluation_test_params() | ||
|> Absinthe.Fixtures.FunctionEvaluationHelpers.filter_test_params_for_description_attribute() | ||
|> Enum.each(fn %{ | ||
|
@@ -115,7 +115,7 @@ defmodule Absinthe.Type.ObjectTest do | |
end) | ||
end | ||
|
||
describe "input object field macro description evaluation" do | ||
describe "object field macro description evaluation" do | ||
Absinthe.Fixtures.FunctionEvaluationHelpers.function_evaluation_test_params() | ||
|> Enum.each(fn %{ | ||
test_label: test_label, | ||
|
@@ -129,4 +129,19 @@ defmodule Absinthe.Type.ObjectTest do | |
end | ||
end) | ||
end | ||
|
||
# TODO default_value is always nil???? | ||
describe "object field default_value evaluation" do | ||
Absinthe.Fixtures.FunctionEvaluationHelpers.function_evaluation_test_params() | ||
|> Enum.each(fn %{ | ||
test_label: test_label, | ||
expected_value: expected_value | ||
} -> | ||
test "for #{test_label} (evaluates default_value to '#{expected_value}')" do | ||
type = Object.TestSchemaFieldsAndArgsDescription.__absinthe_type__(:field_default_value) | ||
|
||
assert type.fields[unquote(test_label)].default_value == unquote(expected_value) | ||
end | ||
end) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the input object's field's default_value is always nil here. Not sure if intentional or not. But i have removed this test in ffb7a0c for the time being |
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,51 @@ | ||
defmodule Absinthe.Type.QueryTest do | ||
use Absinthe.Case, async: true | ||
|
||
defmodule TestSchema do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved to fixtures |
||
use Absinthe.Schema | ||
@module_attribute "goodbye" | ||
alias Absinthe.Fixtures.Query | ||
|
||
defmodule NestedModule do | ||
def nested_function(arg1) do | ||
arg1 | ||
end | ||
end | ||
|
||
def test_function(arg1) do | ||
arg1 | ||
end | ||
|
||
query do | ||
field :normal_string, :string do | ||
arg :arg_example, :string, description: "string" | ||
end | ||
|
||
field :local_function_call, :string do | ||
arg :arg_example, :string, description: test_function("red") | ||
end | ||
|
||
field :function_call_using_absolute_path_to_current_module, :string do | ||
arg :arg_example, :string, | ||
description: Absinthe.Type.QueryTest.TestSchema.test_function("red") | ||
end | ||
|
||
field :standard_library_function, :string do | ||
arg :arg_example, :string, description: String.replace("red", "e", "a") | ||
end | ||
|
||
field :function_in_nested_module, :string do | ||
arg :arg_example, :string, description: NestedModule.nested_function("hello") | ||
end | ||
describe "query field arg keyword description evaluation" do | ||
Absinthe.Fixtures.FunctionEvaluationHelpers.function_evaluation_test_params() | ||
|> Enum.each(fn %{ | ||
test_label: test_label, | ||
expected_value: expected_value | ||
} -> | ||
test "for #{test_label} (evaluates description to '#{expected_value}')" do | ||
type = Query.TestSchemaFieldArgDescription.__absinthe_type__("RootQueryType") | ||
|
||
field :external_module_function_call, :string do | ||
arg :arg_example, :string, | ||
description: Absinthe.Fixtures.FunctionEvaluationHelpers.external_function("hello") | ||
assert type.fields[unquote(test_label)].args.arg_example.description == | ||
unquote(expected_value) | ||
end | ||
end) | ||
end | ||
|
||
field :module_attribute_string_concat, :string do | ||
arg :arg_example, :string, description: "hello " <> @module_attribute | ||
end | ||
describe "query field arg default_value evaluation" do | ||
Absinthe.Fixtures.FunctionEvaluationHelpers.function_evaluation_test_params() | ||
|> Enum.each(fn %{ | ||
test_label: test_label, | ||
expected_value: expected_value | ||
} -> | ||
test "for #{test_label} (evaluates default_value to '#{expected_value}')" do | ||
type = Query.TestSchemaFieldArgDefaultValue.__absinthe_type__("RootQueryType") | ||
field = type.fields[unquote(test_label)] | ||
|
||
field :interpolation_of_module_attribute, :string do | ||
arg :arg_example, :string, description: "hello #{@module_attribute}" | ||
assert field.args.arg_example.default_value == unquote(expected_value) | ||
end | ||
end | ||
end) | ||
end | ||
|
||
describe "query field arg keyword description evaluation" do | ||
describe "query field arg default_value evaluation with import_fields" do | ||
Absinthe.Fixtures.FunctionEvaluationHelpers.function_evaluation_test_params() | ||
|> Enum.each(fn %{ | ||
test_label: test_label, | ||
expected_value: expected_value | ||
} -> | ||
test "for #{test_label} (evaluates description to '#{expected_value}')" do | ||
type = TestSchema.__absinthe_type__("RootQueryType") | ||
test "for #{test_label} (evaluates default_value to '#{expected_value}')" do | ||
type = | ||
Query.TestSchemaFieldArgDefaultValueWithImportFields.__absinthe_type__("RootQueryType") | ||
|
||
assert type.fields[unquote(test_label)].args.arg_example.description == | ||
unquote(expected_value) | ||
field = type.fields[unquote(test_label)] | ||
|
||
assert field.args.arg_example.default_value == unquote(expected_value) | ||
end | ||
end) | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
defmodule Absinthe.Fixtures.DefaultValueSchema do | ||
use Absinthe.Schema | ||
use Absinthe.Fixture | ||
|
||
# Note: More examples in Absinthe.Fixtures.Query.TestSchemaFieldArgDefaultValueWithImportFields | ||
|
||
query do | ||
field :microsecond, :integer, default_value: DateTime.utc_now().microsecond |> elem(0) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seemed to not work, and i'm guess it was supposed to do the evaluation. Clearly it didn't so just removed and used the
wrap_in_unquote
function. Tests are passing anyway