Browse Source

Add tests

main
Victor Roest 2 years ago
parent
commit
95794cce47
Signed by: 0x76 GPG Key ID: A3923C699D1A3BDA
  1. 34
      test/dps/poem_test.exs
  2. 9
      test/dps_web/controllers/poem_controller_test.exs

34
test/dps/poem_test.exs

@ -6,27 +6,24 @@ defmodule Dps.PoemTest do
title: "Some Title",
epigraph: "Some Epi",
content: "Some Content",
author_id: nil
author_id: nil,
translator_id: nil
}
def poem_fixture(attrs \\ %{}) do
{:ok, author} = Author.create_author(%{name: "Some Name"})
if get_in(attrs, [:author_id]) do
{:ok, poem} =
attrs
|> Enum.into(@create_attrs)
|> Poem.create_poem()
author_id = case attrs do
%{author_id: id} -> id
_ -> author.id
end
poem
else
{:ok, poem} =
attrs
|> Enum.into(%{@create_attrs | author_id: author.id})
|> Poem.create_poem()
{:ok, poem} =
attrs
|> Enum.into(%{@create_attrs | author_id: author_id})
|> Poem.create_poem()
poem
end
poem
end
test "list_poems/0 lists all poems" do
@ -58,4 +55,13 @@ defmodule Dps.PoemTest do
assert p.content == cached.content
assert p.epigraph == cached.epigraph
end
test "that adding a translator works" do
{:ok, translator} = Author.create_author(%{name: "Some Translator"})
p = poem_fixture(%{translator_id: translator.id})
poem = Poem.get_poem(p.id)
assert poem.translator_id == translator.id
end
end

9
test/dps_web/controllers/poem_controller_test.exs

@ -5,7 +5,8 @@ defmodule DpsWeb.PoemControllerTest do
title: "Some Title",
epigraph: "Some Epi",
content: "Some Content",
author_id: 1
author_id: 1,
translator_id: nil
}
@invalid_attrs %{title: nil, epigraph: nil, content: nil, author_id: nil}
@ -41,12 +42,13 @@ defmodule DpsWeb.PoemControllerTest do
test "redirects to show when data is valid", %{conn: conn} do
{:ok, new_author} = Dps.Author.create_author(%{name: random_string()})
{:ok, new_translator} = Dps.Author.create_author(%{name: random_string()})
conn = login(conn)
conn =
post(conn, Routes.poem_path(conn, :create),
poem: %{@create_attrs | author_id: new_author.id}
poem: %{@create_attrs | author_id: new_author.id, translator_id: new_translator.id}
)
assert %{id: id} = redirected_params(conn)
@ -61,6 +63,9 @@ defmodule DpsWeb.PoemControllerTest do
_ -> true
end)
|> assert
assert html =~ new_translator.name
assert html =~ new_author.name
end
end

Loading…
Cancel
Save