Browse Source

test: fix a bunch of tests

main
Inhji Y. 8 months ago
parent
commit
e352fa84d9
  1. 2
      lib/mirage/notes/note.ex
  2. 2
      test/mirage/links_test.exs
  3. 65
      test/mirage/notes_test.exs
  4. 63
      test/mirage/topics_test.exs
  5. 91
      test/mirage_web/controllers/note_controller_test.exs
  6. 6
      test/mirage_web/controllers/topic_controller_test.exs
  7. 14
      test/mirage_web/controllers/user_settings_controller_test.exs
  8. 6
      test/mirage_web/live/link_live_test.exs

2
lib/mirage/notes/note.ex

@ -6,7 +6,7 @@ defmodule Mirage.Notes.Note do
field :title, :string
field :content, :string
field :content_html, :string
field :views, :integer
field :views, :integer, default: 0
field :viewed_at, :naive_datetime
many_to_many :links, Mirage.Links.Link, join_through: "notes_links"

2
test/mirage/links_test.exs

@ -15,7 +15,7 @@ defmodule Mirage.LinksTest do
@invalid_attrs %{title: nil, url: nil}
test "list_notes_links/0 returns all notes_links" do
link = link_fixture()
link = link_fixture() |> Links.preload_link()
assert Links.list_links() == [link]
end

65
test/mirage/notes_test.exs

@ -2,10 +2,10 @@ defmodule Mirage.NotesTest do
use Mirage.DataCase
alias Mirage.Notes
alias Mirage.Notes.Note
alias Mirage.Tags
describe "notes" do
alias Mirage.Notes.Note
@valid_attrs %{content: "some content", title: "some title"}
@at_attrs %{content: "some content", title: "@home"}
@update_attrs %{content: "some updated content"}
@ -40,7 +40,7 @@ defmodule Mirage.NotesTest do
test "list_notes/1 with param :untagged returns all untagged notes" do
note = note_fixture(@valid_attrs)
assert Notes.list_notes(:untagged) == [note]
Notes.Tags.update_tags(note, "foo,bar")
Tags.update_tags(note, "foo,bar")
assert Notes.list_notes(:untagged) == []
end
@ -136,63 +136,4 @@ defmodule Mirage.NotesTest do
assert %Ecto.Changeset{} = Notes.change_note(note)
end
end
describe "topics" do
alias Mirage.Notes.Topic
@valid_attrs %{text: "some text"}
@update_attrs %{text: "some updated text"}
@invalid_attrs %{text: nil}
def topic_fixture(attrs \\ %{}) do
{:ok, topic} =
attrs
|> Enum.into(@valid_attrs)
|> Notes.create_topic()
topic
end
test "list_topics/0 returns all topics" do
topic = topic_fixture() |> Notes.preload_topic()
assert Notes.list_topics() == [topic]
end
test "get_topic!/1 returns the topic with given id" do
topic = topic_fixture()
assert Notes.get_topic!(topic.id) == topic
end
test "create_topic/1 with valid data creates a topic" do
assert {:ok, %Topic{} = topic} = Notes.create_topic(@valid_attrs)
assert topic.text == "some text"
end
test "create_topic/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Notes.create_topic(@invalid_attrs)
end
test "update_topic/2 with valid data updates the topic" do
topic = topic_fixture()
assert {:ok, topic} = Notes.update_topic(topic, @update_attrs)
assert topic.text == "some updated text"
end
test "update_topic/2 with invalid data returns error changeset" do
topic = topic_fixture()
assert {:error, %Ecto.Changeset{}} = Notes.update_topic(topic, @invalid_attrs)
assert topic == Notes.get_topic!(topic.id)
end
test "delete_topic/1 deletes the topic" do
topic = topic_fixture()
assert {:ok, %Topic{}} = Notes.delete_topic(topic)
assert_raise Ecto.NoResultsError, fn -> Notes.get_topic!(topic.id) end
end
test "change_topic/1 returns a topic changeset" do
topic = topic_fixture()
assert %Ecto.Changeset{} = Notes.change_topic(topic)
end
end
end

63
test/mirage/topics_test.exs

@ -0,0 +1,63 @@
defmodule Mirage.TopicsTest do
use Mirage.DataCase
alias Mirage.Topics
alias Mirage.Topics.Topic
describe "topics" do
@valid_attrs %{text: "some text"}
@update_attrs %{text: "some updated text"}
@invalid_attrs %{text: nil}
def topic_fixture(attrs \\ %{}) do
{:ok, topic} =
attrs
|> Enum.into(@valid_attrs)
|> Topics.create_topic()
topic
end
test "list_topics/0 returns all topics" do
topic = topic_fixture() |> Topics.preload_topic()
assert Topics.list_topics() == [topic]
end
test "get_topic!/1 returns the topic with given id" do
topic = topic_fixture()
assert Topics.get_topic!(topic.id) == topic
end
test "create_topic/1 with valid data creates a topic" do
assert {:ok, %Topic{} = topic} = Topics.create_topic(@valid_attrs)
assert topic.text == "some text"
end
test "create_topic/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Topics.create_topic(@invalid_attrs)
end
test "update_topic/2 with valid data updates the topic" do
topic = topic_fixture()
assert {:ok, topic} = Topics.update_topic(topic, @update_attrs)
assert topic.text == "some updated text"
end
test "update_topic/2 with invalid data returns error changeset" do
topic = topic_fixture()
assert {:error, %Ecto.Changeset{}} = Topics.update_topic(topic, @invalid_attrs)
assert topic == Topics.get_topic!(topic.id)
end
test "delete_topic/1 deletes the topic" do
topic = topic_fixture()
assert {:ok, %Topic{}} = Topics.delete_topic(topic)
assert_raise Ecto.NoResultsError, fn -> Topics.get_topic!(topic.id) end
end
test "change_topic/1 returns a topic changeset" do
topic = topic_fixture()
assert %Ecto.Changeset{} = Topics.change_topic(topic)
end
end
end

91
test/mirage_web/controllers/note_controller_test.exs

@ -1,91 +0,0 @@
defmodule MirageWeb.NoteControllerTest do
use MirageWeb.ConnCase
import Mirage.NoteFixtures
setup :register_and_log_in_user
@create_attrs %{content: "some content", title: "some title", topic_string: "some tag"}
@update_attrs %{
content: "some updated content with link [[@NOTE_ID]]",
topic_string: "some other tag"
}
@invalid_attrs %{content: nil}
describe "index" do
test "lists all notes", %{conn: conn} do
conn = get(conn, Routes.note_path(conn, :index))
assert html_response(conn, 200) =~ "Listing Notes"
end
end
describe "new note" do
test "renders form", %{conn: conn} do
conn = get(conn, Routes.note_path(conn, :new))
assert html_response(conn, 200) =~ "New Note"
end
end
describe "create note" do
test "redirects to show when data is valid", %{conn: conn} do
conn = post(conn, Routes.note_path(conn, :create), note: @create_attrs)
assert %{id: id} = redirected_params(conn)
assert redirected_to(conn) == Routes.live_path(conn, MirageWeb.ShowNoteLive, id)
conn = get(conn, Routes.live_path(conn, MirageWeb.ShowNoteLive, id))
assert html_response(conn, 200) =~ "some title"
assert html_response(conn, 200) =~ "some tag"
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, Routes.note_path(conn, :create), note: @invalid_attrs)
assert html_response(conn, 200) =~ "New Note"
end
end
describe "edit note" do
setup [:create_note]
test "renders form for editing chosen note", %{conn: conn, note: note} do
conn = get(conn, Routes.note_path(conn, :edit, note))
assert html_response(conn, 200) =~ "Edit Note"
end
end
describe "update note" do
setup [:create_note]
test "redirects when data is valid", %{conn: conn, note: note} do
attrs =
Map.update!(@update_attrs, :content, fn content ->
String.replace(content, "@NOTE_ID", to_string(note.id))
end)
conn = put(conn, Routes.note_path(conn, :update, note), note: attrs)
assert redirected_to(conn) == Routes.live_path(conn, MirageWeb.ShowNoteLive, note)
conn = get(conn, Routes.live_path(conn, MirageWeb.ShowNoteLive, note))
assert html_response(conn, 200) =~ "some updated content with link"
assert html_response(conn, 200) =~ "some other tag"
refute html_response(conn, 200) =~ "some tag"
end
test "renders errors when data is invalid", %{conn: conn, note: note} do
conn = put(conn, Routes.note_path(conn, :update, note), note: @invalid_attrs)
assert html_response(conn, 200) =~ "Edit Note"
end
end
describe "delete note" do
setup [:create_note]
test "deletes chosen note", %{conn: conn, note: note} do
conn = delete(conn, Routes.note_path(conn, :delete, note))
assert redirected_to(conn) == Routes.note_path(conn, :index)
assert_error_sent 404, fn ->
get(conn, Routes.live_path(conn, MirageWeb.ShowNoteLive, note))
end
end
end
end

6
test/mirage_web/controllers/topic_controller_test.exs

@ -1,7 +1,7 @@
defmodule MirageWeb.TopicControllerTest do
use MirageWeb.ConnCase
alias Mirage.Notes
alias Mirage.Topics
setup :register_and_log_in_user
@ -10,7 +10,7 @@ defmodule MirageWeb.TopicControllerTest do
@invalid_attrs %{text: nil}
def fixture(:topic) do
{:ok, topic} = Notes.create_topic(@create_attrs)
{:ok, topic} = Topics.create_topic(@create_attrs)
topic
end
@ -36,7 +36,7 @@ defmodule MirageWeb.TopicControllerTest do
assert redirected_to(conn) == Routes.topic_path(conn, :show, id)
conn = get(conn, Routes.topic_path(conn, :show, id))
assert html_response(conn, 200) =~ "Topic some text"
assert html_response(conn, 200) =~ "some text"
end
test "renders errors when data is invalid", %{conn: conn} do

14
test/mirage_web/controllers/user_settings_controller_test.exs

@ -6,11 +6,11 @@ defmodule MirageWeb.UserSettingsControllerTest do
setup :register_and_log_in_user
describe "GET /users/settings" do
describe "GET /users/edit" do
test "renders settings page", %{conn: conn} do
conn = get(conn, Routes.user_settings_path(conn, :edit))
response = html_response(conn, 200)
assert response =~ "<h1>Settings</h1>"
assert response =~ "<h1>Edit User</h1>"
end
test "redirects if user is not logged in" do
@ -20,7 +20,7 @@ defmodule MirageWeb.UserSettingsControllerTest do
end
end
describe "PUT /users/settings (change password form)" do
describe "PUT /users/edit (change password form)" do
test "updates the user password and resets tokens", %{conn: conn, user: user} do
new_password_conn =
put(conn, Routes.user_settings_path(conn, :update), %{
@ -50,7 +50,7 @@ defmodule MirageWeb.UserSettingsControllerTest do
})
response = html_response(old_password_conn, 200)
assert response =~ "<h1>Settings</h1>"
assert response =~ "<h1>Edit User</h1>"
assert response =~ "should be at least 12 character(s)"
assert response =~ "does not match password"
assert response =~ "is not valid"
@ -59,7 +59,7 @@ defmodule MirageWeb.UserSettingsControllerTest do
end
end
describe "PUT /users/settings (change email form)" do
describe "PUT /users/edit (change email form)" do
@tag :capture_log
test "updates the user email", %{conn: conn, user: user} do
conn =
@ -83,13 +83,13 @@ defmodule MirageWeb.UserSettingsControllerTest do
})
response = html_response(conn, 200)
assert response =~ "<h1>Settings</h1>"
assert response =~ "<h1>Edit User</h1>"
assert response =~ "must have the @ sign and no spaces"
assert response =~ "is not valid"
end
end
describe "GET /users/settings/confirm_email/:token" do
describe "GET /users/edit/confirm_email/:token" do
setup %{user: user} do
email = unique_user_email()

6
test/mirage_web/live/link_live_test.exs

@ -22,7 +22,7 @@ defmodule MirageWeb.LinkLiveTest do
{:ok, show_live, _html} = live(conn, Routes.link_show_path(conn, :show, link))
assert show_live |> element("a", "Edit") |> render_click() =~
"Edit Note link"
"Edit Link"
assert_patch(show_live, Routes.link_show_path(conn, :edit, link))
@ -47,7 +47,7 @@ defmodule MirageWeb.LinkLiveTest do
test "displays link", %{conn: conn, link: link} do
{:ok, _show_live, html} = live(conn, Routes.link_show_path(conn, :show, link))
assert html =~ "Show Note link"
assert html =~ "Show Link"
assert html =~ link.title
end
@ -55,7 +55,7 @@ defmodule MirageWeb.LinkLiveTest do
{:ok, show_live, _html} = live(conn, Routes.link_show_path(conn, :show, link))
assert show_live |> element("a", "Edit") |> render_click() =~
"Edit Note link"
"Edit Link"
assert_patch(show_live, Routes.link_show_path(conn, :edit, link))

Loading…
Cancel
Save