Skip to content

Commit db9f415

Browse files
committed
Add view tests
1 parent a7944d2 commit db9f415

6 files changed

Lines changed: 716 additions & 0 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
defmodule SWAPIWeb.FilmJSONTest 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.Film
14+
alias SWAPIWeb.FilmJSON
15+
16+
describe "show" do
17+
setup do
18+
film =
19+
film_fixture()
20+
|> Film.changeset(%{})
21+
|> put_assoc(:species, [species_fixture()])
22+
|> put_assoc(:starships, [starship_fixture()])
23+
|> put_assoc(:vehicles, [vehicle_fixture()])
24+
|> put_assoc(:characters, [person_fixture()])
25+
|> put_assoc(:planets, [planet_fixture()])
26+
|> Repo.update!()
27+
28+
{:ok, %{film: film}}
29+
end
30+
31+
test "renders an item", %{film: film} do
32+
assert %{
33+
id: id,
34+
title: title,
35+
episode_id: episode_id,
36+
opening_crawl: opening_crawl,
37+
director: director,
38+
producer: producer,
39+
release_date: release_date,
40+
species: [species_url],
41+
starships: [starship_url],
42+
vehicles: [vehicle_url],
43+
characters: [person_url],
44+
planets: [planet_url],
45+
url: url,
46+
created: created,
47+
edited: edited
48+
} = FilmJSON.show(%{film: film})
49+
50+
assert id == film.id
51+
assert title == film.title
52+
assert episode_id == film.episode_id
53+
assert opening_crawl == film.opening_crawl
54+
assert director == film.director
55+
assert producer == film.producer
56+
assert release_date == film.release_date
57+
assert created == film.created
58+
assert edited == film.edited
59+
60+
assert String.ends_with?(url, "/api/films/#{film.id}")
61+
assert String.ends_with?(species_url, "/api/species/#{List.first(film.species).id}")
62+
assert String.ends_with?(starship_url, "/api/starships/#{List.first(film.starships).id}")
63+
assert String.ends_with?(vehicle_url, "/api/vehicles/#{List.first(film.vehicles).id}")
64+
assert String.ends_with?(person_url, "/api/people/#{List.first(film.characters).id}")
65+
assert String.ends_with?(planet_url, "/api/planets/#{List.first(film.planets).id}")
66+
end
67+
end
68+
69+
describe "index" do
70+
setup do
71+
films = for _ <- 1..10, do: film_fixture()
72+
meta = %{count: length(films), next: nil, previous: nil}
73+
conn = %Plug.Conn{request_path: "/api/films", query_params: %{"page" => 1}}
74+
75+
{:ok, %{films: films, meta: meta, conn: conn}}
76+
end
77+
78+
test "renders a list of items", %{films: films, meta: meta, conn: conn} do
79+
assert %{
80+
count: count,
81+
next: nil,
82+
previous: nil,
83+
results: results
84+
} = FilmJSON.index(%{films: films, meta: meta, conn: conn})
85+
86+
assert count == meta.count
87+
assert is_list(results)
88+
assert length(results) == length(films)
89+
end
90+
91+
test "puts a link to next page if there is one", %{films: films, meta: meta, conn: conn} do
92+
meta = %{meta | next: {:page, 2}}
93+
94+
assert %{
95+
count: _count,
96+
next: next_url,
97+
previous: nil,
98+
results: _results
99+
} = FilmJSON.index(%{films: films, meta: meta, conn: conn})
100+
101+
assert String.ends_with?(next_url, "/api/films?page=2")
102+
end
103+
104+
test "puts a link to previous page if there is one", %{films: films, meta: meta, conn: conn} do
105+
meta = %{meta | previous: {:page, 2}}
106+
107+
assert %{
108+
count: _count,
109+
next: nil,
110+
previous: previous_url,
111+
results: _results
112+
} = FilmJSON.index(%{films: films, meta: meta, conn: conn})
113+
114+
assert String.ends_with?(previous_url, "/api/films?page=2")
115+
end
116+
end
117+
end
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
defmodule SWAPIWeb.PlanetJSONTest do
2+
use SWAPI.DataCase
3+
4+
import Ecto.Changeset
5+
6+
import SWAPI.FilmsFixtures
7+
import SWAPI.PlanetsFixtures
8+
import SWAPI.PeopleFixtures
9+
10+
alias SWAPI.Schemas.Planet
11+
alias SWAPIWeb.PlanetJSON
12+
13+
describe "show" do
14+
setup do
15+
planet =
16+
planet_fixture()
17+
|> Planet.changeset(%{})
18+
|> put_assoc(:residents, [person_fixture()])
19+
|> put_assoc(:films, [film_fixture()])
20+
|> Repo.update!()
21+
22+
{:ok, %{planet: planet}}
23+
end
24+
25+
test "renders an item", %{planet: planet} do
26+
assert %{
27+
id: id,
28+
name: name,
29+
diameter: diameter,
30+
rotation_period: rotation_period,
31+
orbital_period: orbital_period,
32+
gravity: gravity,
33+
population: population,
34+
climate: climate,
35+
terrain: terrain,
36+
surface_water: surface_water,
37+
residents: [resident_url],
38+
films: [film_url],
39+
url: url,
40+
created: created,
41+
edited: edited
42+
} = PlanetJSON.show(%{planet: planet})
43+
44+
assert id == planet.id
45+
assert name == planet.name
46+
assert diameter == planet.diameter
47+
assert rotation_period == planet.rotation_period
48+
assert orbital_period == planet.orbital_period
49+
assert gravity == planet.gravity
50+
assert population == planet.population
51+
assert climate == planet.climate
52+
assert terrain == planet.terrain
53+
assert surface_water == planet.surface_water
54+
assert created == planet.created
55+
assert edited == planet.edited
56+
57+
assert String.ends_with?(url, "/api/planets/#{planet.id}")
58+
assert String.ends_with?(resident_url, "/api/people/#{List.first(planet.residents).id}")
59+
assert String.ends_with?(film_url, "/api/films/#{List.first(planet.films).id}")
60+
end
61+
end
62+
63+
describe "index" do
64+
setup do
65+
planets = for _ <- 1..10, do: planet_fixture()
66+
meta = %{count: length(planets), next: nil, previous: nil}
67+
conn = %Plug.Conn{request_path: "/api/planets", query_params: %{"page" => 1}}
68+
69+
{:ok, %{planets: planets, meta: meta, conn: conn}}
70+
end
71+
72+
test "renders a list of items", %{planets: planets, meta: meta, conn: conn} do
73+
assert %{
74+
count: count,
75+
next: nil,
76+
previous: nil,
77+
results: results
78+
} = PlanetJSON.index(%{planets: planets, meta: meta, conn: conn})
79+
80+
assert count == meta.count
81+
assert is_list(results)
82+
assert length(results) == length(planets)
83+
end
84+
85+
test "puts a link to next page if there is one", %{planets: planets, meta: meta, conn: conn} do
86+
meta = %{meta | next: {:page, 2}}
87+
88+
assert %{
89+
count: _count,
90+
next: next_url,
91+
previous: nil,
92+
results: _results
93+
} = PlanetJSON.index(%{planets: planets, meta: meta, conn: conn})
94+
95+
assert String.ends_with?(next_url, "/api/planets?page=2")
96+
end
97+
98+
test "puts a link to previous page if there is one", %{
99+
planets: planets,
100+
meta: meta,
101+
conn: conn
102+
} do
103+
meta = %{meta | previous: {:page, 2}}
104+
105+
assert %{
106+
count: _count,
107+
next: nil,
108+
previous: previous_url,
109+
results: _results
110+
} = PlanetJSON.index(%{planets: planets, meta: meta, conn: conn})
111+
112+
assert String.ends_with?(previous_url, "/api/planets?page=2")
113+
end
114+
end
115+
end

0 commit comments

Comments
 (0)