Skip to content

Commit

Permalink
Add controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrdlicka committed Mar 15, 2024
1 parent db9f415 commit 1b1bd9d
Show file tree
Hide file tree
Showing 6 changed files with 430 additions and 0 deletions.
71 changes: 71 additions & 0 deletions test/swapi_web/controllers/film_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
defmodule SWAPIWeb.FilmControllerTest do
use SWAPIWeb.ConnCase

import SWAPI.FilmsFixtures

setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end

describe "index" do
test "returns empty list when there are no films", %{conn: conn} do
conn = get(conn, ~p"/api/films")

assert json_response(conn, 200) == %{
"next" => nil,
"previous" => nil,
"results" => [],
"count" => 0
}
end

test "returns all films", %{conn: conn} do
for _ <- 1..3 do
film_fixture()
end

conn = get(conn, ~p"/api/films")

assert %{
"next" => _,
"previous" => _,
"results" => [_, _, _],
"count" => 3
} = json_response(conn, 200)
end

test "returns matched films when searching", %{conn: conn} do
for i <- 1..3 do
film_fixture(%{
title: "Film #{i}"
})
end

conn = get(conn, ~p"/api/films?search=\"Film 1\"")

assert %{
"next" => nil,
"previous" => nil,
"results" => [%{"id" => 1}],
"count" => 1
} = json_response(conn, 200)
end
end

describe "show" do
setup [:create_film]

test "returns a single film", %{conn: conn, film: film} do
conn = get(conn, ~p"/api/films/1")
result = json_response(conn, 200)

assert String.ends_with?(result["url"], "/api/films/1")
assert result["title"] == film.title
end
end

defp create_film(_) do
film = film_fixture()
%{film: film}
end
end
71 changes: 71 additions & 0 deletions test/swapi_web/controllers/person_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
defmodule SWAPIWeb.PersonControllerTest do
use SWAPIWeb.ConnCase

import SWAPI.PeopleFixtures

setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end

describe "index" do
test "returns empty list when there are no people", %{conn: conn} do
conn = get(conn, ~p"/api/people")

assert json_response(conn, 200) == %{
"next" => nil,
"previous" => nil,
"results" => [],
"count" => 0
}
end

test "returns all people", %{conn: conn} do
for _ <- 1..3 do
person_fixture()
end

conn = get(conn, ~p"/api/people")

assert %{
"next" => _,
"previous" => _,
"results" => [_, _, _],
"count" => 3
} = json_response(conn, 200)
end

test "returns matched people when searching", %{conn: conn} do
for i <- 1..3 do
person_fixture(%{
name: "Person #{i}"
})
end

conn = get(conn, ~p"/api/people?search=\"Person 1\"")

assert %{
"next" => nil,
"previous" => nil,
"results" => [%{"id" => 1}],
"count" => 1
} = json_response(conn, 200)
end
end

describe "show" do
setup [:create_person]

test "returns a single person", %{conn: conn, person: person} do
conn = get(conn, ~p"/api/people/1")
result = json_response(conn, 200)

assert String.ends_with?(result["url"], "/api/people/1")
assert result["name"] == person.name
end
end

defp create_person(_) do
person = person_fixture()
%{person: person}
end
end
71 changes: 71 additions & 0 deletions test/swapi_web/controllers/planet_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
defmodule SWAPIWeb.PlanetControllerTest do
use SWAPIWeb.ConnCase

import SWAPI.PlanetsFixtures

setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end

describe "index" do
test "returns empty list when there are no planets", %{conn: conn} do
conn = get(conn, ~p"/api/planets")

assert json_response(conn, 200) == %{
"next" => nil,
"previous" => nil,
"results" => [],
"count" => 0
}
end

test "returns all planets", %{conn: conn} do
for _ <- 1..3 do
planet_fixture()
end

conn = get(conn, ~p"/api/planets")

assert %{
"next" => _,
"previous" => _,
"results" => [_, _, _],
"count" => 3
} = json_response(conn, 200)
end

test "returns matched planets when searching", %{conn: conn} do
for i <- 1..3 do
planet_fixture(%{
name: "Planet #{i}"
})
end

conn = get(conn, ~p"/api/planets?search=\"Planet 1\"")

assert %{
"next" => nil,
"previous" => nil,
"results" => [%{"id" => 1}],
"count" => 1
} = json_response(conn, 200)
end
end

describe "show" do
setup [:create_planet]

test "returns a single planet", %{conn: conn, planet: planet} do
conn = get(conn, ~p"/api/planets/1")
result = json_response(conn, 200)

assert String.ends_with?(result["url"], "/api/planets/1")
assert result["name"] == planet.name
end
end

defp create_planet(_) do
planet = planet_fixture()
%{planet: planet}
end
end
71 changes: 71 additions & 0 deletions test/swapi_web/controllers/species_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
defmodule SWAPIWeb.SpeciesControllerTest do
use SWAPIWeb.ConnCase

import SWAPI.SpeciesFixtures

setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end

describe "index" do
test "returns empty list when there are no species", %{conn: conn} do
conn = get(conn, ~p"/api/species")

assert json_response(conn, 200) == %{
"next" => nil,
"previous" => nil,
"results" => [],
"count" => 0
}
end

test "returns all species", %{conn: conn} do
for _ <- 1..3 do
species_fixture()
end

conn = get(conn, ~p"/api/species")

assert %{
"next" => _,
"previous" => _,
"results" => [_, _, _],
"count" => 3
} = json_response(conn, 200)
end

test "returns matched species when searching", %{conn: conn} do
for i <- 1..3 do
species_fixture(%{
name: "Species #{i}"
})
end

conn = get(conn, ~p"/api/species?search=\"Species 1\"")

assert %{
"next" => nil,
"previous" => nil,
"results" => [%{"id" => 1}],
"count" => 1
} = json_response(conn, 200)
end
end

describe "show" do
setup [:create_species]

test "returns a single species", %{conn: conn, species: species} do
conn = get(conn, ~p"/api/species/1")
result = json_response(conn, 200)

assert String.ends_with?(result["url"], "/api/species/1")
assert result["name"] == species.name
end
end

defp create_species(_) do
species = species_fixture()
%{species: species}
end
end
73 changes: 73 additions & 0 deletions test/swapi_web/controllers/starship_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
defmodule SWAPIWeb.StarshipControllerTest do
use SWAPIWeb.ConnCase

import SWAPI.StarshipsFixtures

setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end

describe "index" do
test "returns empty list when there are no starships", %{conn: conn} do
conn = get(conn, ~p"/api/starships")

assert json_response(conn, 200) == %{
"next" => nil,
"previous" => nil,
"results" => [],
"count" => 0
}
end

test "returns all starships", %{conn: conn} do
for _ <- 1..3 do
starship_fixture()
end

conn = get(conn, ~p"/api/starships")

assert %{
"next" => _,
"previous" => _,
"results" => [_, _, _],
"count" => 3
} = json_response(conn, 200)
end

test "returns matched starships when searching", %{conn: conn} do
for i <- 1..3 do
starship_fixture(%{
transport: %{
name: "Starship #{i}"
}
})
end

conn = get(conn, ~p"/api/starships?search=\"Starship 1\"")

assert %{
"next" => nil,
"previous" => nil,
"results" => [%{"id" => 1}],
"count" => 1
} = json_response(conn, 200)
end
end

describe "show" do
setup [:create_starship]

test "returns a single starship", %{conn: conn, starship: starship} do
conn = get(conn, ~p"/api/starships/1")
result = json_response(conn, 200)

assert String.ends_with?(result["url"], "/api/starships/1")
assert result["name"] == starship.transport.name
end
end

defp create_starship(_) do
starship = starship_fixture()
%{starship: starship}
end
end
Loading

0 comments on commit 1b1bd9d

Please sign in to comment.