|
| 1 | +defmodule SWAPIWeb.PersonJSONTest do |
| 2 | + use SWAPI.DataCase |
| 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.Schemas.Person |
| 14 | + alias SWAPIWeb.PersonJSON |
| 15 | + |
| 16 | + describe "show" do |
| 17 | + setup do |
| 18 | + person = |
| 19 | + person_fixture() |
| 20 | + |> Person.changeset(%{homeworld_id: planet_fixture().id}) |
| 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 | + test "renders an item", %{person: person} do |
| 31 | + assert %{ |
| 32 | + id: id, |
| 33 | + name: name, |
| 34 | + birth_year: birth_year, |
| 35 | + eye_color: eye_color, |
| 36 | + gender: gender, |
| 37 | + hair_color: hair_color, |
| 38 | + height: height, |
| 39 | + mass: mass, |
| 40 | + skin_color: skin_color, |
| 41 | + homeworld: homeworld_url, |
| 42 | + films: [film_url], |
| 43 | + species: [species_url], |
| 44 | + starships: [starship_url], |
| 45 | + vehicles: [vehicle_url], |
| 46 | + url: url, |
| 47 | + created: created, |
| 48 | + edited: edited |
| 49 | + } = PersonJSON.show(%{person: person}) |
| 50 | + |
| 51 | + assert id == person.id |
| 52 | + assert name == person.name |
| 53 | + assert birth_year == person.birth_year |
| 54 | + assert eye_color == person.eye_color |
| 55 | + assert gender == person.gender |
| 56 | + assert hair_color == person.hair_color |
| 57 | + assert height == person.height |
| 58 | + assert mass == person.mass |
| 59 | + assert skin_color == person.skin_color |
| 60 | + assert created == person.created |
| 61 | + assert edited == person.edited |
| 62 | + |
| 63 | + assert String.ends_with?(url, "/api/people/#{person.id}") |
| 64 | + assert String.ends_with?(homeworld_url, "/api/planets/#{person.homeworld_id}") |
| 65 | + assert String.ends_with?(film_url, "/api/films/#{List.first(person.films).id}") |
| 66 | + assert String.ends_with?(species_url, "/api/species/#{List.first(person.species).id}") |
| 67 | + assert String.ends_with?(starship_url, "/api/starships/#{List.first(person.starships).id}") |
| 68 | + assert String.ends_with?(vehicle_url, "/api/vehicles/#{List.first(person.vehicles).id}") |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + describe "index" do |
| 73 | + setup do |
| 74 | + people = for _ <- 1..10, do: person_fixture() |
| 75 | + meta = %{count: length(people), next: nil, previous: nil} |
| 76 | + conn = %Plug.Conn{request_path: "/api/people", query_params: %{"page" => 1}} |
| 77 | + |
| 78 | + {:ok, %{people: people, meta: meta, conn: conn}} |
| 79 | + end |
| 80 | + |
| 81 | + test "renders a list of items", %{people: people, meta: meta, conn: conn} do |
| 82 | + assert %{ |
| 83 | + count: count, |
| 84 | + next: nil, |
| 85 | + previous: nil, |
| 86 | + results: results |
| 87 | + } = PersonJSON.index(%{people: people, meta: meta, conn: conn}) |
| 88 | + |
| 89 | + assert count == meta.count |
| 90 | + assert is_list(results) |
| 91 | + assert length(results) == length(people) |
| 92 | + end |
| 93 | + |
| 94 | + test "puts a link to next page if there is one", %{people: people, meta: meta, conn: conn} do |
| 95 | + meta = %{meta | next: {:page, 2}} |
| 96 | + |
| 97 | + assert %{ |
| 98 | + count: _count, |
| 99 | + next: next_url, |
| 100 | + previous: nil, |
| 101 | + results: _results |
| 102 | + } = PersonJSON.index(%{people: people, meta: meta, conn: conn}) |
| 103 | + |
| 104 | + assert String.ends_with?(next_url, "/api/people?page=2") |
| 105 | + end |
| 106 | + |
| 107 | + test "puts a link to previous page if there is one", %{people: people, meta: meta, conn: conn} do |
| 108 | + meta = %{meta | previous: {:page, 2}} |
| 109 | + |
| 110 | + assert %{ |
| 111 | + count: _count, |
| 112 | + next: nil, |
| 113 | + previous: previous_url, |
| 114 | + results: _results |
| 115 | + } = PersonJSON.index(%{people: people, meta: meta, conn: conn}) |
| 116 | + |
| 117 | + assert String.ends_with?(previous_url, "/api/people?page=2") |
| 118 | + end |
| 119 | + end |
| 120 | +end |
0 commit comments