Skip to content

Commit 1b1bd9d

Browse files
committed
Add controller tests
1 parent db9f415 commit 1b1bd9d

6 files changed

Lines changed: 430 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
defmodule SWAPIWeb.FilmControllerTest do
2+
use SWAPIWeb.ConnCase
3+
4+
import SWAPI.FilmsFixtures
5+
6+
setup %{conn: conn} do
7+
{:ok, conn: put_req_header(conn, "accept", "application/json")}
8+
end
9+
10+
describe "index" do
11+
test "returns empty list when there are no films", %{conn: conn} do
12+
conn = get(conn, ~p"/api/films")
13+
14+
assert json_response(conn, 200) == %{
15+
"next" => nil,
16+
"previous" => nil,
17+
"results" => [],
18+
"count" => 0
19+
}
20+
end
21+
22+
test "returns all films", %{conn: conn} do
23+
for _ <- 1..3 do
24+
film_fixture()
25+
end
26+
27+
conn = get(conn, ~p"/api/films")
28+
29+
assert %{
30+
"next" => _,
31+
"previous" => _,
32+
"results" => [_, _, _],
33+
"count" => 3
34+
} = json_response(conn, 200)
35+
end
36+
37+
test "returns matched films when searching", %{conn: conn} do
38+
for i <- 1..3 do
39+
film_fixture(%{
40+
title: "Film #{i}"
41+
})
42+
end
43+
44+
conn = get(conn, ~p"/api/films?search=\"Film 1\"")
45+
46+
assert %{
47+
"next" => nil,
48+
"previous" => nil,
49+
"results" => [%{"id" => 1}],
50+
"count" => 1
51+
} = json_response(conn, 200)
52+
end
53+
end
54+
55+
describe "show" do
56+
setup [:create_film]
57+
58+
test "returns a single film", %{conn: conn, film: film} do
59+
conn = get(conn, ~p"/api/films/1")
60+
result = json_response(conn, 200)
61+
62+
assert String.ends_with?(result["url"], "/api/films/1")
63+
assert result["title"] == film.title
64+
end
65+
end
66+
67+
defp create_film(_) do
68+
film = film_fixture()
69+
%{film: film}
70+
end
71+
end
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
defmodule SWAPIWeb.PersonControllerTest do
2+
use SWAPIWeb.ConnCase
3+
4+
import SWAPI.PeopleFixtures
5+
6+
setup %{conn: conn} do
7+
{:ok, conn: put_req_header(conn, "accept", "application/json")}
8+
end
9+
10+
describe "index" do
11+
test "returns empty list when there are no people", %{conn: conn} do
12+
conn = get(conn, ~p"/api/people")
13+
14+
assert json_response(conn, 200) == %{
15+
"next" => nil,
16+
"previous" => nil,
17+
"results" => [],
18+
"count" => 0
19+
}
20+
end
21+
22+
test "returns all people", %{conn: conn} do
23+
for _ <- 1..3 do
24+
person_fixture()
25+
end
26+
27+
conn = get(conn, ~p"/api/people")
28+
29+
assert %{
30+
"next" => _,
31+
"previous" => _,
32+
"results" => [_, _, _],
33+
"count" => 3
34+
} = json_response(conn, 200)
35+
end
36+
37+
test "returns matched people when searching", %{conn: conn} do
38+
for i <- 1..3 do
39+
person_fixture(%{
40+
name: "Person #{i}"
41+
})
42+
end
43+
44+
conn = get(conn, ~p"/api/people?search=\"Person 1\"")
45+
46+
assert %{
47+
"next" => nil,
48+
"previous" => nil,
49+
"results" => [%{"id" => 1}],
50+
"count" => 1
51+
} = json_response(conn, 200)
52+
end
53+
end
54+
55+
describe "show" do
56+
setup [:create_person]
57+
58+
test "returns a single person", %{conn: conn, person: person} do
59+
conn = get(conn, ~p"/api/people/1")
60+
result = json_response(conn, 200)
61+
62+
assert String.ends_with?(result["url"], "/api/people/1")
63+
assert result["name"] == person.name
64+
end
65+
end
66+
67+
defp create_person(_) do
68+
person = person_fixture()
69+
%{person: person}
70+
end
71+
end
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
defmodule SWAPIWeb.PlanetControllerTest do
2+
use SWAPIWeb.ConnCase
3+
4+
import SWAPI.PlanetsFixtures
5+
6+
setup %{conn: conn} do
7+
{:ok, conn: put_req_header(conn, "accept", "application/json")}
8+
end
9+
10+
describe "index" do
11+
test "returns empty list when there are no planets", %{conn: conn} do
12+
conn = get(conn, ~p"/api/planets")
13+
14+
assert json_response(conn, 200) == %{
15+
"next" => nil,
16+
"previous" => nil,
17+
"results" => [],
18+
"count" => 0
19+
}
20+
end
21+
22+
test "returns all planets", %{conn: conn} do
23+
for _ <- 1..3 do
24+
planet_fixture()
25+
end
26+
27+
conn = get(conn, ~p"/api/planets")
28+
29+
assert %{
30+
"next" => _,
31+
"previous" => _,
32+
"results" => [_, _, _],
33+
"count" => 3
34+
} = json_response(conn, 200)
35+
end
36+
37+
test "returns matched planets when searching", %{conn: conn} do
38+
for i <- 1..3 do
39+
planet_fixture(%{
40+
name: "Planet #{i}"
41+
})
42+
end
43+
44+
conn = get(conn, ~p"/api/planets?search=\"Planet 1\"")
45+
46+
assert %{
47+
"next" => nil,
48+
"previous" => nil,
49+
"results" => [%{"id" => 1}],
50+
"count" => 1
51+
} = json_response(conn, 200)
52+
end
53+
end
54+
55+
describe "show" do
56+
setup [:create_planet]
57+
58+
test "returns a single planet", %{conn: conn, planet: planet} do
59+
conn = get(conn, ~p"/api/planets/1")
60+
result = json_response(conn, 200)
61+
62+
assert String.ends_with?(result["url"], "/api/planets/1")
63+
assert result["name"] == planet.name
64+
end
65+
end
66+
67+
defp create_planet(_) do
68+
planet = planet_fixture()
69+
%{planet: planet}
70+
end
71+
end
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
defmodule SWAPIWeb.SpeciesControllerTest do
2+
use SWAPIWeb.ConnCase
3+
4+
import SWAPI.SpeciesFixtures
5+
6+
setup %{conn: conn} do
7+
{:ok, conn: put_req_header(conn, "accept", "application/json")}
8+
end
9+
10+
describe "index" do
11+
test "returns empty list when there are no species", %{conn: conn} do
12+
conn = get(conn, ~p"/api/species")
13+
14+
assert json_response(conn, 200) == %{
15+
"next" => nil,
16+
"previous" => nil,
17+
"results" => [],
18+
"count" => 0
19+
}
20+
end
21+
22+
test "returns all species", %{conn: conn} do
23+
for _ <- 1..3 do
24+
species_fixture()
25+
end
26+
27+
conn = get(conn, ~p"/api/species")
28+
29+
assert %{
30+
"next" => _,
31+
"previous" => _,
32+
"results" => [_, _, _],
33+
"count" => 3
34+
} = json_response(conn, 200)
35+
end
36+
37+
test "returns matched species when searching", %{conn: conn} do
38+
for i <- 1..3 do
39+
species_fixture(%{
40+
name: "Species #{i}"
41+
})
42+
end
43+
44+
conn = get(conn, ~p"/api/species?search=\"Species 1\"")
45+
46+
assert %{
47+
"next" => nil,
48+
"previous" => nil,
49+
"results" => [%{"id" => 1}],
50+
"count" => 1
51+
} = json_response(conn, 200)
52+
end
53+
end
54+
55+
describe "show" do
56+
setup [:create_species]
57+
58+
test "returns a single species", %{conn: conn, species: species} do
59+
conn = get(conn, ~p"/api/species/1")
60+
result = json_response(conn, 200)
61+
62+
assert String.ends_with?(result["url"], "/api/species/1")
63+
assert result["name"] == species.name
64+
end
65+
end
66+
67+
defp create_species(_) do
68+
species = species_fixture()
69+
%{species: species}
70+
end
71+
end
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
defmodule SWAPIWeb.StarshipControllerTest do
2+
use SWAPIWeb.ConnCase
3+
4+
import SWAPI.StarshipsFixtures
5+
6+
setup %{conn: conn} do
7+
{:ok, conn: put_req_header(conn, "accept", "application/json")}
8+
end
9+
10+
describe "index" do
11+
test "returns empty list when there are no starships", %{conn: conn} do
12+
conn = get(conn, ~p"/api/starships")
13+
14+
assert json_response(conn, 200) == %{
15+
"next" => nil,
16+
"previous" => nil,
17+
"results" => [],
18+
"count" => 0
19+
}
20+
end
21+
22+
test "returns all starships", %{conn: conn} do
23+
for _ <- 1..3 do
24+
starship_fixture()
25+
end
26+
27+
conn = get(conn, ~p"/api/starships")
28+
29+
assert %{
30+
"next" => _,
31+
"previous" => _,
32+
"results" => [_, _, _],
33+
"count" => 3
34+
} = json_response(conn, 200)
35+
end
36+
37+
test "returns matched starships when searching", %{conn: conn} do
38+
for i <- 1..3 do
39+
starship_fixture(%{
40+
transport: %{
41+
name: "Starship #{i}"
42+
}
43+
})
44+
end
45+
46+
conn = get(conn, ~p"/api/starships?search=\"Starship 1\"")
47+
48+
assert %{
49+
"next" => nil,
50+
"previous" => nil,
51+
"results" => [%{"id" => 1}],
52+
"count" => 1
53+
} = json_response(conn, 200)
54+
end
55+
end
56+
57+
describe "show" do
58+
setup [:create_starship]
59+
60+
test "returns a single starship", %{conn: conn, starship: starship} do
61+
conn = get(conn, ~p"/api/starships/1")
62+
result = json_response(conn, 200)
63+
64+
assert String.ends_with?(result["url"], "/api/starships/1")
65+
assert result["name"] == starship.transport.name
66+
end
67+
end
68+
69+
defp create_starship(_) do
70+
starship = starship_fixture()
71+
%{starship: starship}
72+
end
73+
end

0 commit comments

Comments
 (0)