|
| 1 | +defmodule SWAPIWeb.GraphQL.PersonQueriesTest do |
| 2 | + use SWAPIWeb.ConnCase |
| 3 | + |
| 4 | + import Ecto.Changeset |
| 5 | + |
| 6 | + import SWAPI.FilmsFixtures |
| 7 | + import SWAPI.PeopleFixtures |
| 8 | + import SWAPI.PlanetsFixtures |
| 9 | + import SWAPI.SpeciesFixtures |
| 10 | + import SWAPI.StarshipsFixtures |
| 11 | + import SWAPI.VehiclesFixtures |
| 12 | + |
| 13 | + alias SWAPI.Repo |
| 14 | + alias SWAPI.Schemas.Person |
| 15 | + |
| 16 | + setup do |
| 17 | + person = |
| 18 | + person_fixture() |
| 19 | + |> Person.changeset(%{name: "Luke Skywalker"}) |
| 20 | + |> put_assoc(:homeworld, planet_fixture()) |
| 21 | + |> put_assoc(:films, [film_fixture()]) |
| 22 | + |> put_assoc(:species, [species_fixture()]) |
| 23 | + |> put_assoc(:starships, [starship_fixture()]) |
| 24 | + |> put_assoc(:vehicles, [vehicle_fixture()]) |
| 25 | + |> Repo.update!() |
| 26 | + |
| 27 | + {:ok, %{person: person}} |
| 28 | + end |
| 29 | + |
| 30 | + describe "allPeople" do |
| 31 | + test "returns all people", %{conn: conn, person: person1} do |
| 32 | + person2 = person_fixture() |
| 33 | + |
| 34 | + query = """ |
| 35 | + query { |
| 36 | + allPeople { |
| 37 | + id |
| 38 | + } |
| 39 | + } |
| 40 | + """ |
| 41 | + |
| 42 | + conn = get(conn, "/graphql", query: query) |
| 43 | + |
| 44 | + assert %{ |
| 45 | + "data" => %{ |
| 46 | + "allPeople" => [ |
| 47 | + %{"id" => person1_id}, |
| 48 | + %{"id" => person2_id} |
| 49 | + ] |
| 50 | + } |
| 51 | + } = json_response(conn, 200) |
| 52 | + |
| 53 | + assert ^person1_id = "#{person1.id}" |
| 54 | + assert ^person2_id = "#{person2.id}" |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + describe "searchPeople" do |
| 59 | + test "returns matching people", %{conn: conn, person: person} do |
| 60 | + person_fixture(%{name: "Han Solo"}) |
| 61 | + |
| 62 | + query = """ |
| 63 | + query { |
| 64 | + searchPeople(searchTerms: ["Luke"]) { |
| 65 | + id |
| 66 | + } |
| 67 | + } |
| 68 | + """ |
| 69 | + |
| 70 | + conn = get(conn, "/graphql", query: query) |
| 71 | + |
| 72 | + assert %{ |
| 73 | + "data" => %{ |
| 74 | + "searchPeople" => [ |
| 75 | + %{"id" => person_id} |
| 76 | + ] |
| 77 | + } |
| 78 | + } = json_response(conn, 200) |
| 79 | + |
| 80 | + assert ^person_id = "#{person.id}" |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + describe "person" do |
| 85 | + test "returns person when it exists", %{conn: conn, person: person} do |
| 86 | + query = """ |
| 87 | + query { |
| 88 | + person(id: #{person.id}) { |
| 89 | + id |
| 90 | + } |
| 91 | + } |
| 92 | + """ |
| 93 | + |
| 94 | + conn = get(conn, "/graphql", query: query) |
| 95 | + |
| 96 | + assert %{ |
| 97 | + "data" => %{ |
| 98 | + "person" => %{"id" => person_id} |
| 99 | + } |
| 100 | + } = json_response(conn, 200) |
| 101 | + |
| 102 | + assert ^person_id = "#{person.id}" |
| 103 | + end |
| 104 | + |
| 105 | + test "loads nested fields", %{conn: conn, person: person} do |
| 106 | + query = """ |
| 107 | + query { |
| 108 | + person(id: #{person.id}) { |
| 109 | + id |
| 110 | + homeworld { |
| 111 | + id |
| 112 | + } |
| 113 | + films { |
| 114 | + id |
| 115 | + } |
| 116 | + species { |
| 117 | + id |
| 118 | + } |
| 119 | + starships { |
| 120 | + id |
| 121 | + } |
| 122 | + vehicles { |
| 123 | + id |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + """ |
| 128 | + |
| 129 | + conn = get(conn, "/graphql", query: query) |
| 130 | + |
| 131 | + assert %{ |
| 132 | + "data" => %{ |
| 133 | + "person" => %{ |
| 134 | + "id" => person_id, |
| 135 | + "homeworld" => %{"id" => homeworld_id}, |
| 136 | + "films" => [%{"id" => film_id}], |
| 137 | + "species" => [%{"id" => species_id}], |
| 138 | + "starships" => [%{"id" => starship_id}], |
| 139 | + "vehicles" => [%{"id" => vehicle_id}] |
| 140 | + } |
| 141 | + } |
| 142 | + } = json_response(conn, 200) |
| 143 | + |
| 144 | + assert ^person_id = "#{person.id}" |
| 145 | + assert ^homeworld_id = "#{person.homeworld.id}" |
| 146 | + assert ^film_id = "#{List.first(person.films).id}" |
| 147 | + assert ^species_id = "#{List.first(person.species).id}" |
| 148 | + assert ^starship_id = "#{List.first(person.starships).id}" |
| 149 | + assert ^vehicle_id = "#{List.first(person.vehicles).id}" |
| 150 | + end |
| 151 | + |
| 152 | + test "handles recursive nesting", %{conn: conn, person: person} do |
| 153 | + query = """ |
| 154 | + query { |
| 155 | + person(id: #{person.id}) { |
| 156 | + id |
| 157 | + films { |
| 158 | + id |
| 159 | + characters { |
| 160 | + id |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + """ |
| 166 | + |
| 167 | + conn = get(conn, "/graphql", query: query) |
| 168 | + |
| 169 | + assert %{ |
| 170 | + "data" => %{ |
| 171 | + "person" => %{ |
| 172 | + "id" => person_id, |
| 173 | + "films" => [%{"id" => film_id, "characters" => [%{"id" => person_id}]}] |
| 174 | + } |
| 175 | + } |
| 176 | + } = json_response(conn, 200) |
| 177 | + |
| 178 | + assert ^person_id = "#{person.id}" |
| 179 | + assert ^film_id = "#{List.first(person.films).id}" |
| 180 | + end |
| 181 | + end |
| 182 | +end |
0 commit comments