|
| 1 | +defmodule Absinthe.Phase.Schema.CoordinatesTest do |
| 2 | + use Absinthe.Case, async: true |
| 3 | + |
| 4 | + import Absinthe.Blueprint.Schema, only: [lookup_type: 2] |
| 5 | + |
| 6 | + alias Absinthe.Phase.Schema.Coordinates |
| 7 | + |
| 8 | + defmodule Schema do |
| 9 | + use Absinthe.Schema |
| 10 | + |
| 11 | + enum :age_demographic do |
| 12 | + value :child |
| 13 | + value :young_adult |
| 14 | + value :adult |
| 15 | + end |
| 16 | + |
| 17 | + enum :status do |
| 18 | + value :living |
| 19 | + value :not_living |
| 20 | + end |
| 21 | + |
| 22 | + object :human_user do |
| 23 | + field :family_name, :string |
| 24 | + field :surname, :string |
| 25 | + field :age_demographic, :age_demographic |
| 26 | + field :status, :status |
| 27 | + end |
| 28 | + |
| 29 | + input_object :name_input do |
| 30 | + field :family_name, :string |
| 31 | + field :surname, :string |
| 32 | + end |
| 33 | + |
| 34 | + input_object :by do |
| 35 | + field :name, :name_input do |
| 36 | + arg(:living, :boolean) |
| 37 | + arg(:not_living, :boolean) |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + query do |
| 42 | + field :get_user, :human_user do |
| 43 | + arg(:by, :by) |
| 44 | + arg(:include_deceased, :boolean) |
| 45 | + end |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + setup_all do |
| 50 | + {:ok, blueprint} = Coordinates.run(Schema.__absinthe_blueprint__()) |
| 51 | + [blueprint: blueprint] |
| 52 | + end |
| 53 | + |
| 54 | + describe "run/2 for object types" do |
| 55 | + test "schema", %{blueprint: %{schema_definitions: [schema_definition]}} do |
| 56 | + assert %{coordinate: "Schema"} = schema_definition |
| 57 | + end |
| 58 | + |
| 59 | + test "single word", %{blueprint: blueprint} do |
| 60 | + assert %{coordinate: "RootQueryType"} = lookup_type(blueprint, :query) |
| 61 | + end |
| 62 | + |
| 63 | + test "multiple words", %{blueprint: blueprint} do |
| 64 | + assert %{coordinate: "HumanUser"} = lookup_type(blueprint, :human_user) |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + describe "run/2 for input types" do |
| 69 | + test "single word", %{blueprint: blueprint} do |
| 70 | + assert %{coordinate: "By"} = lookup_type(blueprint, :by) |
| 71 | + end |
| 72 | + |
| 73 | + test "multiple words", %{blueprint: blueprint} do |
| 74 | + assert %{coordinate: "NameInput"} = lookup_type(blueprint, :name_input) |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + describe "run/2 for enum types" do |
| 79 | + test "single word", %{blueprint: blueprint} do |
| 80 | + assert %{coordinate: "Status"} = lookup_type(blueprint, :status) |
| 81 | + end |
| 82 | + |
| 83 | + test "multiple words", %{blueprint: blueprint} do |
| 84 | + assert %{coordinate: "AgeDemographic"} = lookup_type(blueprint, :age_demographic) |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + describe "run/2 for enum values" do |
| 89 | + test "single word", %{blueprint: blueprint} do |
| 90 | + assert %{coordinate: "AgeDemographic.CHILD"} = |
| 91 | + lookup_enum_value(blueprint, :age_demographic, :child) |
| 92 | + end |
| 93 | + |
| 94 | + test "multiple words", %{blueprint: blueprint} do |
| 95 | + assert %{coordinate: "AgeDemographic.YOUNG_ADULT"} = |
| 96 | + lookup_enum_value(blueprint, :age_demographic, :young_adult) |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + describe "run/2 for fields" do |
| 101 | + test "top level field", %{blueprint: blueprint} do |
| 102 | + assert %{coordinate: "RootQueryType.getUser"} = lookup_field(blueprint, :query, :get_user) |
| 103 | + end |
| 104 | + |
| 105 | + test "single words", %{blueprint: blueprint} do |
| 106 | + assert %{coordinate: "HumanUser.surname"} = |
| 107 | + lookup_field(blueprint, :human_user, :surname) |
| 108 | + end |
| 109 | + |
| 110 | + test "multiple words", %{blueprint: blueprint} do |
| 111 | + assert %{coordinate: "HumanUser.familyName"} = |
| 112 | + lookup_field(blueprint, :human_user, :family_name) |
| 113 | + end |
| 114 | + |
| 115 | + test "single word on input type", %{blueprint: blueprint} do |
| 116 | + assert %{coordinate: "NameInput.surname"} = lookup_field(blueprint, :name_input, :surname) |
| 117 | + end |
| 118 | + |
| 119 | + test "multiple words on input type", %{blueprint: blueprint} do |
| 120 | + assert %{coordinate: "NameInput.familyName"} = |
| 121 | + lookup_field(blueprint, :name_input, :family_name) |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + describe "run/2 for arguments" do |
| 126 | + test "single word", %{blueprint: blueprint} do |
| 127 | + assert %{coordinate: "RootQueryType.getUser(by:)"} = |
| 128 | + lookup_argument(blueprint, :query, :get_user, :by) |
| 129 | + end |
| 130 | + |
| 131 | + test "multiple words", %{blueprint: blueprint} do |
| 132 | + assert %{coordinate: "RootQueryType.getUser(includeDeceased:)"} = |
| 133 | + lookup_argument(blueprint, :query, :get_user, :include_deceased) |
| 134 | + end |
| 135 | + |
| 136 | + test "single word on input field", %{blueprint: blueprint} do |
| 137 | + assert %{coordinate: "By.name(living:)"} = lookup_argument(blueprint, :by, :name, :living) |
| 138 | + end |
| 139 | + |
| 140 | + test "multiple words on input field", %{blueprint: blueprint} do |
| 141 | + assert %{coordinate: "By.name(notLiving:)"} = |
| 142 | + lookup_argument(blueprint, :by, :name, :not_living) |
| 143 | + end |
| 144 | + end |
| 145 | + |
| 146 | + defp lookup_field(blueprint, type, field) do |
| 147 | + blueprint |
| 148 | + |> lookup_type(type) |
| 149 | + |> Map.get(:fields) |
| 150 | + |> Enum.find(&(&1.identifier == field)) |
| 151 | + end |
| 152 | + |
| 153 | + defp lookup_argument(blueprint, type, field, argument) do |
| 154 | + blueprint |
| 155 | + |> lookup_field(type, field) |
| 156 | + |> Map.get(:arguments) |
| 157 | + |> Enum.find(&(&1.identifier == argument)) |
| 158 | + end |
| 159 | + |
| 160 | + defp lookup_enum_value(blueprint, enum, value) do |
| 161 | + blueprint |
| 162 | + |> lookup_type(enum) |
| 163 | + |> Map.get(:values) |
| 164 | + |> Enum.find(&(&1.identifier == value)) |
| 165 | + end |
| 166 | +end |
0 commit comments