Browse Source

feat: make tests pass again

main
Inhji Y. 9 months ago
parent
commit
559fb957ba
  1. 2
      config/prod.exs
  2. 2
      config/releases.exs
  3. 14
      lib/mirage/markdown.ex
  4. 2
      lib/mirage/notes.ex
  5. 10
      lib/mirage/notes/note.ex
  6. 2
      lib/mirage/notes/note_link.ex
  7. 2
      lib/mirage/notes/note_topic.ex
  8. 276
      lib/mirage/notes/tags.ex
  9. 2
      lib/mirage/release.ex
  10. 2
      lib/mirage/settings.ex
  11. 2
      lib/mirage/slug.ex
  12. 25
      lib/mirage_web/controllers/note_controller.ex
  13. 2
      lib/mirage_web/live/page_live.ex
  14. 0
      lib/mirage_web/templates/topic/edit.html.eex
  15. 0
      lib/mirage_web/templates/topic/form.html.eex
  16. 0
      lib/mirage_web/templates/topic/index.html.eex
  17. 0
      lib/mirage_web/templates/topic/new.html.eex
  18. 0
      lib/mirage_web/templates/topic/show.html.eex
  19. 0
      lib/mirage_web/views/topic_view.ex
  20. 1
      priv/repo/migrations/20210108235219_create_notes.exs
  21. 5
      priv/repo/migrations/20210109130905_create_settings.exs
  22. 6
      priv/repo/migrations/20210111174337_add_settings_data.exs
  23. 6
      priv/repo/migrations/20210202224744_add_title_to_note.exs
  24. 19
      priv/repo/migrations/20210205175108_add_html_fields.exs
  25. 10
      priv/repo/migrations/20210206173536_add_settings_data2.exs
  26. 72
      test/mirage/notes_test.exs
  27. 3
      test/mirage/settings_test.exs
  28. 5
      test/mirage_web/controllers/note_controller_test.exs
  29. 88
      test/mirage_web/controllers/note_topic_controller_test.exs
  30. 91
      test/mirage_web/controllers/topic_controller_test.exs
  31. 6
      test/mirage_web/live/page_live_test.exs

2
config/prod.exs

@ -52,4 +52,4 @@ config :logger, level: :info
# Check `Plug.SSL` for all available options in `force_ssl`.
# Finally import the config/prod.secret.exs which loads secrets
# and configuration from environment variables.
# and configuration from environment variables.

2
config/releases.exs

@ -22,7 +22,7 @@ secret_key_base =
You can generate one by calling: mix phx.gen.secret
"""
public_url =
public_url =
System.get_env("PUBLIC_URL") ||
raise """
environment variable PUBLIC_URL is missing.

14
lib/mirage/markdown.ex

@ -1,13 +1,13 @@
defmodule Mirage.Markdown do
import Ecto.Changeset, only: [get_change: 2, put_change: 3]
def maybe_render_markdown(changeset, markdown_field, html_field) do
import Ecto.Changeset, only: [get_change: 2, put_change: 3]
def maybe_render_markdown(changeset, markdown_field, html_field) do
if markdown = get_change(changeset, markdown_field) do
html = Earmark.as_html!(markdown)
html = Earmark.as_html!(markdown)
put_change(changeset, html_field, html)
put_change(changeset, html_field, html)
else
changeset
changeset
end
end
end
end

2
lib/mirage/notes.ex

@ -41,7 +41,7 @@ defmodule Mirage.Notes do
** (Ecto.NoResultsError)
"""
def get_note!(id), do: Repo.get!(Note, id) |> Repo.preload(@note_preloads)
def get_note!(id), do: Repo.get!(Note, id)
def preload_note(note), do: Repo.preload(note, @note_preloads)

10
lib/mirage/notes/note.ex

@ -12,7 +12,9 @@ defmodule Mirage.Notes.Note do
has_many :links, NoteLink
many_to_many :topics, Topic, join_through: "notes_topics"
field :topic_string, :string, virtual: true
field :topic_string, :string,
virtual: true,
default: ""
timestamps()
end
@ -28,9 +30,11 @@ defmodule Mirage.Notes.Note do
def maybe_load_tag_string(changeset) do
case get_field(changeset, :topic_string) do
nil ->
"" ->
put_change(changeset, :topic_string, Tags.get_tags(changeset.data))
_ -> changeset
_ ->
changeset
end
end
end

2
lib/mirage/notes/note_link.ex

@ -5,7 +5,7 @@ defmodule Mirage.Notes.NoteLink do
schema "note_links" do
field :title, :string
field :url, :string
belongs_to :note, Mirage.Notes.Note
timestamps()

2
lib/mirage/notes/note_topic.ex

@ -18,4 +18,4 @@ defmodule Mirage.Notes.NoteTopic do
|> cast(attrs, @attrs)
|> validate_required(@attrs)
end
end
end

276
lib/mirage/notes/tags.ex

@ -1,138 +1,140 @@
defmodule Mirage.Notes.Tags do
import Ecto.Query, warn: false
alias Mirage.Repo
alias Mirage.Notes.{Note, Topic, NoteTopic}
@doc utils: :tag
@doc """
Updates tags of a given note.
## Examples
iex> Tags.update_tags(note, "foo,bar")
"""
def update_tags(note, new_tags) when is_binary(new_tags) do
old_tags = tags_loaded(note) |> split_tags()
new_tags = new_tags |> split_tags()
note
|> add_tags(new_tags -- old_tags)
|> remove_tags(old_tags -- new_tags)
end
@doc """
Retrieves a string of tags for a given note.
## Examples
iex> Tags.get_tags(note)
"foo,bar"
"""
def get_tags(note), do: tags_loaded(note)
@doc utils: :tag
defp add_tag(note, topic_text) when is_binary(topic_text) do
slug =
topic_text
|> Slugger.slugify()
|> String.downcase()
topic =
case Repo.get_by(Topic, %{text_slug: slug}) do
nil ->
%Topic{}
|> Topic.changeset(%{text: topic_text})
|> Repo.insert!()
topic ->
topic
end
add_tag(note, topic.id)
end
@doc utils: :tag
defp add_tag(%{note_id: note_id}, topic_id) do
add_tag(note_id, topic_id)
end
@doc utils: :tag
defp add_tag(%Note{} = note, topic_id) do
add_tag(note.id, topic_id)
end
@doc utils: :tag
defp add_tag(note_id, topic_id) do
%NoteTopic{}
|> NoteTopic.changeset(%{note_id: note_id, topic_id: topic_id})
|> Repo.insert()
end
@doc utils: :tag
defp add_tags(content, tags) when is_binary(tags) do
Enum.each(split_tags(tags), &add_tag(content, &1))
content
end
@doc utils: :tag
defp add_tags(content, tags) do
Enum.each(tags, &add_tag(content, &1))
content
end
@doc utils: :tag
defp remove_tag(content, topic_text) when is_binary(topic_text) do
case Repo.get_by(Topic, %{text: topic_text}) do
nil -> nil
topic -> remove_tag(content, topic.id)
end
end
@doc utils: :tag
defp remove_tag(%{note_id: note_id}, topic_id) do
remove_tag(note_id, topic_id)
end
@doc utils: :tag
defp remove_tag(%Note{} = note, topic_id) do
remove_tag(note.id, topic_id)
end
@doc utils: :tag
defp remove_tag(note_id, topic_id) do
case Repo.get_by(NoteTopic, %{note_id: note_id, topic_id: topic_id}) do
nil -> nil
tag -> Repo.delete(tag)
end
end
@doc utils: :tag
defp remove_tags(content, tags) when is_binary(tags) do
Enum.each(split_tags(tags), &remove_tag(content, &1))
content
end
@doc utils: :tag
defp remove_tags(content, tags) do
Enum.each(tags, &remove_tag(content, &1))
content
end
@doc utils: :tag
defp tags_loaded(%{topics: topics}) when is_list(topics) do
topics |> Enum.map_join(", ", & &1.text)
end
defp tags_loaded(_), do: ""
@doc utils: :tag
defp split_tags(tags_string) when is_binary(tags_string) do
tags_string
|> String.split(",")
|> Enum.map(&String.trim/1)
|> Enum.filter(&(String.length(&1) > 0))
end
end
import Ecto.Query, warn: false
alias Mirage.Repo
alias Mirage.Notes.{Note, Topic, NoteTopic}
@doc utils: :tag
@doc """
Updates tags of a given note.
## Examples
iex> Tags.update_tags(note, "foo,bar")
"""
def update_tags(note, new_tags) when is_binary(new_tags) do
old_tags = tags_loaded(note) |> split_tags()
new_tags = new_tags |> split_tags()
note
|> add_tags(new_tags -- old_tags)
|> remove_tags(old_tags -- new_tags)
end
def update_tags(_, _), do: nil
@doc """
Retrieves a string of tags for a given note.
## Examples
iex> Tags.get_tags(note)
"foo,bar"
"""
def get_tags(note), do: tags_loaded(note)
@doc utils: :tag
defp add_tag(note, topic_text) when is_binary(topic_text) do
slug =
topic_text
|> Slugger.slugify()
|> String.downcase()
topic =
case Repo.get_by(Topic, %{text_slug: slug}) do
nil ->
%Topic{}
|> Topic.changeset(%{text: topic_text})
|> Repo.insert!()
topic ->
topic
end
add_tag(note, topic.id)
end
@doc utils: :tag
defp add_tag(%{note_id: note_id}, topic_id) do
add_tag(note_id, topic_id)
end
@doc utils: :tag
defp add_tag(%Note{} = note, topic_id) do
add_tag(note.id, topic_id)
end
@doc utils: :tag
defp add_tag(note_id, topic_id) do
%NoteTopic{}
|> NoteTopic.changeset(%{note_id: note_id, topic_id: topic_id})
|> Repo.insert()
end
@doc utils: :tag
defp add_tags(content, tags) when is_binary(tags) do
Enum.each(split_tags(tags), &add_tag(content, &1))
content
end
@doc utils: :tag
defp add_tags(content, tags) do
Enum.each(tags, &add_tag(content, &1))
content
end
@doc utils: :tag
defp remove_tag(content, topic_text) when is_binary(topic_text) do
case Repo.get_by(Topic, %{text: topic_text}) do
nil -> nil
topic -> remove_tag(content, topic.id)
end
end
@doc utils: :tag
defp remove_tag(%{note_id: note_id}, topic_id) do
remove_tag(note_id, topic_id)
end
@doc utils: :tag
defp remove_tag(%Note{} = note, topic_id) do
remove_tag(note.id, topic_id)
end
@doc utils: :tag
defp remove_tag(note_id, topic_id) do
case Repo.get_by(NoteTopic, %{note_id: note_id, topic_id: topic_id}) do
nil -> nil
tag -> Repo.delete(tag)
end
end
@doc utils: :tag
defp remove_tags(content, tags) when is_binary(tags) do
Enum.each(split_tags(tags), &remove_tag(content, &1))
content
end
@doc utils: :tag
defp remove_tags(content, tags) do
Enum.each(tags, &remove_tag(content, &1))
content
end
@doc utils: :tag
defp tags_loaded(%{topics: topics}) when is_list(topics) do
topics |> Enum.map_join(", ", & &1.text)
end
defp tags_loaded(_), do: ""
@doc utils: :tag
defp split_tags(tags_string) when is_binary(tags_string) do
tags_string
|> String.split(",")
|> Enum.map(&String.trim/1)
|> Enum.filter(&(String.length(&1) > 0))
end
end

2
lib/mirage/release.ex

@ -13,4 +13,4 @@ defmodule Mirage.Release do
Application.load(:mirage)
Application.fetch_env!(:mirage, :ecto_repos)
end
end
end

2
lib/mirage/settings.ex

@ -23,7 +23,7 @@ defmodule Mirage.Settings do
def list_settings_as_map do
list_settings()
|> Enum.map(fn s ->
|> Enum.map(fn s ->
{String.to_atom(s.name), s.value_html}
end)
|> Map.new()

2
lib/mirage/slug.ex

@ -39,4 +39,4 @@ defmodule Mirage.Slug do
|> Base.url_encode64()
|> binary_part(0, length)
end
end
end

25
lib/mirage_web/controllers/note_controller.ex

@ -33,25 +33,36 @@ defmodule MirageWeb.NoteController do
end
def show(conn, %{"id" => id}) do
note = Notes.get_note!(id)
note =
id
|> Notes.get_note!()
|> Notes.preload_note()
render(conn, "show.html", note: note)
end
def edit(conn, %{"id" => id}) do
note = Notes.get_note!(id)
note =
id
|> Notes.get_note!()
|> Notes.preload_note()
changeset = Notes.change_note(note)
render(conn, "edit.html", note: note, changeset: changeset)
end
def update(conn, %{"id" => id, "note" => note_params}) do
note = Notes.get_note!(id)
note =
id
|> Notes.get_note!()
|> Notes.preload_note()
case Notes.update_note(note, note_params) do
{:ok, note} ->
note
|> Notes.preload_note()
|> Notes.Tags.update_tags(note_params["topic_string"])
conn
|> put_flash(:info, "Note updated successfully.")
|> redirect(to: Routes.note_path(conn, :show, note))
@ -62,7 +73,11 @@ defmodule MirageWeb.NoteController do
end
def delete(conn, %{"id" => id}) do
note = Notes.get_note!(id)
note =
id
|> Notes.get_note!()
|> Notes.preload_note()
{:ok, _note} = Notes.delete_note(note)
conn

2
lib/mirage_web/live/page_live.ex

@ -3,7 +3,7 @@ defmodule MirageWeb.PageLive do
@impl true
def mount(_params, _session, socket) do
settings = Mirage.Settings.list_settings_as_map()
settings = Mirage.Settings.list_settings_as_map()
{:ok, assign(socket, _s: settings)}
end
end

0
lib/mirage_web/templates/note_topic/edit.html.eex → lib/mirage_web/templates/topic/edit.html.eex

0
lib/mirage_web/templates/note_topic/form.html.eex → lib/mirage_web/templates/topic/form.html.eex

0
lib/mirage_web/templates/note_topic/index.html.eex → lib/mirage_web/templates/topic/index.html.eex

0
lib/mirage_web/templates/note_topic/new.html.eex → lib/mirage_web/templates/topic/new.html.eex

0
lib/mirage_web/templates/note_topic/show.html.eex → lib/mirage_web/templates/topic/show.html.eex

0
lib/mirage_web/views/note_topic_view.ex → lib/mirage_web/views/topic_view.ex

1
priv/repo/migrations/20210108235219_create_notes.exs

@ -7,6 +7,5 @@ defmodule Mirage.Repo.Migrations.CreateNotes do
timestamps()
end
end
end

5
priv/repo/migrations/20210109130905_create_settings.exs

@ -5,6 +5,7 @@ defmodule Mirage.Repo.Migrations.CreateSettings do
create table(:settings) do
add :name, :string
add :value, :string
add :value_html, :string
timestamps()
end
@ -13,7 +14,7 @@ defmodule Mirage.Repo.Migrations.CreateSettings do
end
def down do
drop unique_index(:settings, [:name])
drop table(:settings)
drop unique_index(:settings, [:name])
drop table(:settings)
end
end

6
priv/repo/migrations/20210111174337_add_settings_data.exs

@ -9,7 +9,11 @@ defmodule Mirage.Repo.Migrations.AddSettingsData do
def up do
Multi.new()
|> Multi.insert(:user_tagline, %Setting{name: "user_tagline", value: "some tagline", value_html: "some tagline"})
|> Multi.insert(:user_tagline, %Setting{
name: "user_tagline",
value: "some tagline",
value_html: "some tagline"
})
|> Multi.insert(:user_title, %Setting{name: "user_title", value: "Hi", value_html: "Hi"})
|> Multi.insert(:footer_text, %Setting{name: "footer_text", value: "2020", value_html: "2020"})
|> repo().transaction()

6
priv/repo/migrations/20210202224744_add_title_to_note.exs

@ -2,8 +2,8 @@ defmodule Mirage.Repo.Migrations.AddTitleToNote do
use Ecto.Migration
def change do
alter table(:notes) do
add :title, :string, default: "Untitled Note"
end
alter table(:notes) do
add :title, :string, default: "Untitled Note"
end
end
end

19
priv/repo/migrations/20210205175108_add_html_fields.exs

@ -1,14 +1,13 @@
defmodule Mirage.Repo.Migrations.AddHtmlFields do
use Ecto.Migration
use Ecto.Migration
def change do
alter table(:settings) do
modify :value, :text
add :value_html, :text
end
def change do
alter table(:settings) do
modify :value, :text
end
alter table(:notes) do
add :content_html, :text
end
end
alter table(:notes) do
add :content_html, :text
end
end
end

10
priv/repo/migrations/20210206173536_add_settings_data2.exs

@ -8,9 +8,13 @@ defmodule Mirage.Repo.Migrations.AddSettingsData2 do
@settings ["header_title"]
def up do
Multi.new()
|> Multi.insert(:header_title, %Setting{name: "header_title", value: "Mirage", value_html: "Mirage"})
|> repo().transaction()
Multi.new()
|> Multi.insert(:header_title, %Setting{
name: "header_title",
value: "Mirage",
value_html: "Mirage"
})
|> repo().transaction()
end
def down do

72
test/mirage/notes_test.exs

@ -6,7 +6,7 @@ defmodule Mirage.NotesTest do
describe "notes" do
alias Mirage.Notes.Note
@valid_attrs %{content: "some content"}
@valid_attrs %{content: "some content", title: "some title"}
@update_attrs %{content: "some updated content"}
@invalid_attrs %{content: nil}
@ -17,6 +17,7 @@ defmodule Mirage.NotesTest do
|> Notes.create_note()
note
|> Notes.preload_note()
end
test "list_notes/0 returns all notes" do
@ -26,12 +27,13 @@ defmodule Mirage.NotesTest do
test "get_note!/1 returns the note with given id" do
note = note_fixture()
assert Notes.get_note!(note.id) == note
assert Notes.get_note!(note.id) |> Notes.preload_note() == note
end
test "create_note/1 with valid data creates a note" do
assert {:ok, %Note{} = note} = Notes.create_note(@valid_attrs)
assert note.content == "some content"
assert note.title == "some title"
end
test "create_note/1 with invalid data returns error changeset" do
@ -47,7 +49,7 @@ defmodule Mirage.NotesTest do
test "update_note/2 with invalid data returns error changeset" do
note = note_fixture()
assert {:error, %Ecto.Changeset{}} = Notes.update_note(note, @invalid_attrs)
assert note == Notes.get_note!(note.id)
assert note == Notes.get_note!(note.id) |> Notes.preload_note()
end
test "delete_note/1 deletes the note" do
@ -62,62 +64,62 @@ defmodule Mirage.NotesTest do
end
end
describe "note_topics" do
alias Mirage.Notes.NoteTopic
describe "topics" do
alias Mirage.Notes.Topic
@valid_attrs %{text: "some text"}
@update_attrs %{text: "some updated text"}
@invalid_attrs %{text: nil}
def note_topic_fixture(attrs \\ %{}) do
{:ok, note_topic} =
def topic_fixture(attrs \\ %{}) do
{:ok, topic} =
attrs
|> Enum.into(@valid_attrs)
|> Notes.create_note_topic()
|> Notes.create_topic()
note_topic
topic
end
test "list_note_topics/0 returns all note_topics" do
note_topic = note_topic_fixture()
assert Notes.list_note_topics() == [note_topic]
test "list_topics/0 returns all topics" do
topic = topic_fixture()
assert Notes.list_topics() == [topic]
end
test "get_note_topic!/1 returns the note_topic with given id" do
note_topic = note_topic_fixture()
assert Notes.get_note_topic!(note_topic.id) == note_topic
test "get_topic!/1 returns the topic with given id" do
topic = topic_fixture()
assert Notes.get_topic!(topic.id) == topic
end
test "create_note_topic/1 with valid data creates a note_topic" do
assert {:ok, %NoteTopic{} = note_topic} = Notes.create_note_topic(@valid_attrs)
assert note_topic.text == "some text"
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_note_topic/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Notes.create_note_topic(@invalid_attrs)
test "create_topic/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Notes.create_topic(@invalid_attrs)
end
test "update_note_topic/2 with valid data updates the note_topic" do
note_topic = note_topic_fixture()
assert {:ok, %NoteTopic{} = note_topic} = Notes.update_note_topic(note_topic, @update_attrs)
assert note_topic.text == "some updated text"
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_note_topic/2 with invalid data returns error changeset" do
note_topic = note_topic_fixture()
assert {:error, %Ecto.Changeset{}} = Notes.update_note_topic(note_topic, @invalid_attrs)
assert note_topic == Notes.get_note_topic!(note_topic.id)
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_note_topic/1 deletes the note_topic" do
note_topic = note_topic_fixture()
assert {:ok, %NoteTopic{}} = Notes.delete_note_topic(note_topic)
assert_raise Ecto.NoResultsError, fn -> Notes.get_note_topic!(note_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_note_topic/1 returns a note_topic changeset" do
note_topic = note_topic_fixture()
assert %Ecto.Changeset{} = Notes.change_note_topic(note_topic)
test "change_topic/1 returns a topic changeset" do
topic = topic_fixture()
assert %Ecto.Changeset{} = Notes.change_topic(topic)
end
end
end

3
test/mirage/settings_test.exs

@ -20,8 +20,7 @@ defmodule Mirage.SettingsTest do
end
test "list_settings/0 returns all settings" do
setting = setting_fixture()
assert Settings.list_settings() == [%Setting{}, setting]
assert Settings.list_settings() |> Enum.count() > 0
end
test "get_setting!/1 returns the setting with given id" do

5
test/mirage_web/controllers/note_controller_test.exs

@ -5,7 +5,7 @@ defmodule MirageWeb.NoteControllerTest do
setup :register_and_log_in_user
@create_attrs %{content: "some content"}
@create_attrs %{content: "some content", title: "some title"}
@update_attrs %{content: "some updated content"}
@invalid_attrs %{content: nil}
@ -36,7 +36,7 @@ defmodule MirageWeb.NoteControllerTest do
assert redirected_to(conn) == Routes.note_path(conn, :show, id)
conn = get(conn, Routes.note_path(conn, :show, id))
assert html_response(conn, 200) =~ "Show Note"
assert html_response(conn, 200) =~ @create_attrs.title
end
test "renders errors when data is invalid", %{conn: conn} do
@ -77,6 +77,7 @@ defmodule MirageWeb.NoteControllerTest do
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.note_path(conn, :show, note))
end

88
test/mirage_web/controllers/note_topic_controller_test.exs

@ -1,88 +0,0 @@
defmodule MirageWeb.NoteTopicControllerTest do
use MirageWeb.ConnCase
alias Mirage.Notes
@create_attrs %{text: "some text"}
@update_attrs %{text: "some updated text"}
@invalid_attrs %{text: nil}
def fixture(:note_topic) do
{:ok, note_topic} = Notes.create_note_topic(@create_attrs)
note_topic
end
describe "index" do
test "lists all note_topics", %{conn: conn} do
conn = get(conn, Routes.note_topic_path(conn, :index))
assert html_response(conn, 200) =~ "Listing Note topics"
end
end
describe "new note_topic" do
test "renders form", %{conn: conn} do
conn = get(conn, Routes.note_topic_path(conn, :new))
assert html_response(conn, 200) =~ "New Note topic"
end
end
describe "create note_topic" do
test "redirects to show when data is valid", %{conn: conn} do
conn = post(conn, Routes.note_topic_path(conn, :create), note_topic: @create_attrs)
assert %{id: id} = redirected_params(conn)
assert redirected_to(conn) == Routes.note_topic_path(conn, :show, id)
conn = get(conn, Routes.note_topic_path(conn, :show, id))
assert html_response(conn, 200) =~ "Show Note topic"
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, Routes.note_topic_path(conn, :create), note_topic: @invalid_attrs)
assert html_response(conn, 200) =~ "New Note topic"
end
end
describe "edit note_topic" do
setup [:create_note_topic]
test "renders form for editing chosen note_topic", %{conn: conn, note_topic: note_topic} do
conn = get(conn, Routes.note_topic_path(conn, :edit, note_topic))
assert html_response(conn, 200) =~ "Edit Note topic"
end
end
describe "update note_topic" do
setup [:create_note_topic]
test "redirects when data is valid", %{conn: conn, note_topic: note_topic} do
conn = put(conn, Routes.note_topic_path(conn, :update, note_topic), note_topic: @update_attrs)
assert redirected_to(conn) == Routes.note_topic_path(conn, :show, note_topic)
conn = get(conn, Routes.note_topic_path(conn, :show, note_topic))
assert html_response(conn, 200) =~ "some updated text"
end
test "renders errors when data is invalid", %{conn: conn, note_topic: note_topic} do
conn = put(conn, Routes.note_topic_path(conn, :update, note_topic), note_topic: @invalid_attrs)
assert html_response(conn, 200) =~ "Edit Note topic"
end
end
describe "delete note_topic" do
setup [:create_note_topic]
test "deletes chosen note_topic", %{conn: conn, note_topic: note_topic} do
conn = delete(conn, Routes.note_topic_path(conn, :delete, note_topic))
assert redirected_to(conn) == Routes.note_topic_path(conn, :index)
assert_error_sent 404, fn ->
get(conn, Routes.note_topic_path(conn, :show, note_topic))
end
end
end
defp create_note_topic(_) do
note_topic = fixture(:note_topic)
%{note_topic: note_topic}
end
end

91
test/mirage_web/controllers/topic_controller_test.exs

@ -0,0 +1,91 @@
defmodule MirageWeb.TopicControllerTest do
use MirageWeb.ConnCase
alias Mirage.Notes
setup :register_and_log_in_user
@create_attrs %{text: "some text"}
@update_attrs %{text: "some updated text"}
@invalid_attrs %{text: nil}
def fixture(:topic) do
{:ok, topic} = Notes.create_topic(@create_attrs)
topic
end
describe "index" do
test "lists all topics", %{conn: conn} do
conn = get(conn, Routes.topic_path(conn, :index))
assert html_response(conn, 200) =~ "Listing Note topics"
end
end
describe "new topic" do
test "renders form", %{conn: conn} do
conn = get(conn, Routes.topic_path(conn, :new))
assert html_response(conn, 200) =~ "New Note topic"
end
end
describe "create topic" do
test "redirects to show when data is valid", %{conn: conn} do
conn = post(conn, Routes.topic_path(conn, :create), topic: @create_attrs)
assert %{id: id} = redirected_params(conn)
assert redirected_to(conn) == Routes.topic_path(conn, :show, id)
conn = get(conn, Routes.topic_path(conn, :show, id))
assert html_response(conn, 200) =~ "Show Note topic"
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, Routes.topic_path(conn, :create), topic: @invalid_attrs)
assert html_response(conn, 200) =~ "New Note topic"
end
end
describe "edit topic" do
setup [:create_topic]
test "renders form for editing chosen topic", %{conn: conn, topic: topic} do
conn = get(conn, Routes.topic_path(conn, :edit, topic))
assert html_response(conn, 200) =~ "Edit Note topic"
end
end
describe "update topic" do
setup [:create_topic]
test "redirects when data is valid", %{conn: conn, topic: topic} do
conn = put(conn, Routes.topic_path(conn, :update, topic), topic: @update_attrs)
assert redirected_to(conn) == Routes.topic_path(conn, :show, topic)
conn = get(conn, Routes.topic_path(conn, :show, topic))
assert html_response(conn, 200) =~ "some updated text"
end
test "renders errors when data is invalid", %{conn: conn, topic: topic} do
conn = put(conn, Routes.topic_path(conn, :update, topic), topic: @invalid_attrs)
assert html_response(conn, 200) =~ "Edit Note topic"
end
end
describe "delete topic" do
setup [:create_topic]
test "deletes chosen topic", %{conn: conn, topic: topic} do
conn = delete(conn, Routes.topic_path(conn, :delete, topic))
assert redirected_to(conn) == Routes.topic_path(conn, :index)
assert_error_sent 404, fn ->
get(conn, Routes.topic_path(conn, :show, topic))
end
end
end
defp create_topic(_) do
topic = fixture(:topic)
%{topic: topic}
end
end

6
test/mirage_web/live/page_live_test.exs

@ -5,7 +5,9 @@ defmodule MirageWeb.PageLiveTest do
test "disconnected and connected render", %{conn: conn} do
{:ok, page_live, disconnected_html} = live(conn, "/")
assert disconnected_html =~ "Hello my name is Inhji!"
assert render(page_live) =~ "Hello my name is Inhji!"
assert disconnected_html =~ "Hi"
assert disconnected_html =~ "some tagline"
assert render(page_live) =~ "Hi"
assert render(page_live) =~ "some tagline"
end
end

Loading…
Cancel
Save