implement notes

This commit is contained in:
2022-11-17 22:38:52 -05:00
parent 0aa7df0b37
commit 1a508a42ef
21 changed files with 1096 additions and 574 deletions

View File

@ -1,71 +1,113 @@
defmodule Memex.NotesTest do
use Memex.DataCase
alias Memex.Notes
import Memex.NotesFixtures
alias Memex.{Notes, Notes.Note}
@moduletag :notes_test
@invalid_attrs %{content: nil, tag: nil, title: nil, visibility: nil}
describe "notes" do
alias Memex.Notes.Note
import Memex.NotesFixtures
@invalid_attrs %{content: nil, tag: nil, title: nil, visibility: nil}
test "list_notes/0 returns all notes" do
note = note_fixture()
assert Notes.list_notes() == [note]
setup do
[user: user_fixture()]
end
test "get_note!/1 returns the note with given id" do
note = note_fixture()
assert Notes.get_note!(note.id) == note
test "list_notes/1 returns all notes for a user", %{user: user} do
note_a = note_fixture(%{title: "a", visibility: :public}, user)
note_b = note_fixture(%{title: "b", visibility: :unlisted}, user)
note_c = note_fixture(%{title: "c", visibility: :private}, user)
assert Notes.list_notes(user) == [note_a, note_b, note_c]
end
test "create_note/1 with valid data creates a note" do
valid_attrs = %{content: "some content", tag: [], title: "some title", visibility: :public}
test "list_public_notes/0 returns public notes", %{user: user} do
public_note = note_fixture(%{visibility: :public}, user)
note_fixture(%{visibility: :unlisted}, user)
note_fixture(%{visibility: :private}, user)
assert Notes.list_public_notes() == [public_note]
end
assert {:ok, %Note{} = note} = Notes.create_note(valid_attrs)
test "get_note!/1 returns the note with given id", %{user: user} do
note = note_fixture(%{visibility: :public}, user)
assert Notes.get_note!(note.id, user) == note
note = note_fixture(%{visibility: :unlisted}, user)
assert Notes.get_note!(note.id, user) == note
note = note_fixture(%{visibility: :private}, user)
assert Notes.get_note!(note.id, user) == note
end
test "get_note!/1 only returns unlisted or public notes for other users", %{user: user} do
another_user = user_fixture()
note = note_fixture(%{visibility: :public}, another_user)
assert Notes.get_note!(note.id, user) == note
note = note_fixture(%{visibility: :unlisted}, another_user)
assert Notes.get_note!(note.id, user) == note
note = note_fixture(%{visibility: :private}, another_user)
assert_raise Ecto.NoResultsError, fn ->
Notes.get_note!(note.id, user)
end
end
test "create_note/1 with valid data creates a note", %{user: user} do
valid_attrs = %{
"content" => "some content",
"tags_string" => "tag1,tag2",
"title" => "some title",
"visibility" => :public
}
assert {:ok, %Note{} = note} = Notes.create_note(valid_attrs, user)
assert note.content == "some content"
assert note.tag == []
assert note.tags == ["tag1", "tag2"]
assert note.title == "some title"
assert note.visibility == :public
end
test "create_note/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Notes.create_note(@invalid_attrs)
test "create_note/1 with invalid data returns error changeset", %{user: user} do
assert {:error, %Ecto.Changeset{}} = Notes.create_note(@invalid_attrs, user)
end
test "update_note/2 with valid data updates the note" do
note = note_fixture()
test "update_note/2 with valid data updates the note", %{user: user} do
note = note_fixture(user)
update_attrs = %{
content: "some updated content",
tag: [],
title: "some updated title",
visibility: :private
"content" => "some updated content",
"tags_string" => "tag1,tag2",
"title" => "some updated title",
"visibility" => :private
}
assert {:ok, %Note{} = note} = Notes.update_note(note, update_attrs)
assert {:ok, %Note{} = note} = Notes.update_note(note, update_attrs, user)
assert note.content == "some updated content"
assert note.tag == []
assert note.tags == ["tag1", "tag2"]
assert note.title == "some updated title"
assert note.visibility == :private
end
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)
test "update_note/2 with invalid data returns error changeset", %{user: user} do
note = note_fixture(user)
assert {:error, %Ecto.Changeset{}} = Notes.update_note(note, @invalid_attrs, user)
assert note == Notes.get_note!(note.id, user)
end
test "delete_note/1 deletes the note" do
note = note_fixture()
assert {:ok, %Note{}} = Notes.delete_note(note)
assert_raise Ecto.NoResultsError, fn -> Notes.get_note!(note.id) end
test "delete_note/1 deletes the note", %{user: user} do
note = note_fixture(user)
assert {:ok, %Note{}} = Notes.delete_note(note, user)
assert_raise Ecto.NoResultsError, fn -> Notes.get_note!(note.id, user) end
end
test "change_note/1 returns a note changeset" do
note = note_fixture()
assert %Ecto.Changeset{} = Notes.change_note(note)
test "delete_note/1 deletes the note for an admin user", %{user: user} do
admin_user = admin_fixture()
note = note_fixture(user)
assert {:ok, %Note{}} = Notes.delete_note(note, admin_user)
assert_raise Ecto.NoResultsError, fn -> Notes.get_note!(note.id, user) end
end
test "change_note/1 returns a note changeset", %{user: user} do
note = note_fixture(user)
assert %Ecto.Changeset{} = Notes.change_note(note, user)
end
end
end

View File

@ -4,14 +4,24 @@ defmodule MemexWeb.ContextLiveTest do
import Phoenix.LiveViewTest
import Memex.ContextsFixtures
@create_attrs %{content: "some content", tag: [], title: "some title", visibility: :public}
@update_attrs %{
content: "some updated content",
tag: [],
title: "some updated title",
visibility: :private
@create_attrs %{
"content" => "some content",
"tags_string" => "tag1",
"title" => "some title",
"visibility" => :public
}
@update_attrs %{
"content" => "some updated content",
"tags_string" => "tag1,tag2",
"title" => "some updated title",
"visibility" => :private
}
@invalid_attrs %{
"content" => nil,
"tags_string" => "",
"title" => nil,
"visibility" => nil
}
@invalid_attrs %{content: nil, tag: [], title: nil, visibility: nil}
defp create_context(_) do
context = context_fixture()

View File

@ -4,22 +4,31 @@ defmodule MemexWeb.NoteLiveTest do
import Phoenix.LiveViewTest
import Memex.NotesFixtures
@create_attrs %{content: "some content", tag: [], title: "some title", visibility: :public}
@update_attrs %{
content: "some updated content",
tag: [],
title: "some updated title",
visibility: :private
@create_attrs %{
"content" => "some content",
"tags_string" => "tag1",
"title" => "some title",
"visibility" => :public
}
@update_attrs %{
"content" => "some updated content",
"tags_string" => "tag1,tag2",
"title" => "some updated title",
"visibility" => :private
}
@invalid_attrs %{
"content" => nil,
"tags_string" => "",
"title" => nil,
"visibility" => nil
}
@invalid_attrs %{content: nil, tag: [], title: nil, visibility: nil}
defp create_note(_) do
note = note_fixture()
%{note: note}
defp create_note(%{user: user}) do
[note: note_fixture(user)]
end
describe "Index" do
setup [:create_note]
setup [:register_and_log_in_user, :create_note]
test "lists all notes", %{conn: conn, note: note} do
{:ok, _index_live, html} = live(conn, Routes.note_index_path(conn, :index))
@ -53,7 +62,7 @@ defmodule MemexWeb.NoteLiveTest do
test "updates note in listing", %{conn: conn, note: note} do
{:ok, index_live, _html} = live(conn, Routes.note_index_path(conn, :index))
assert index_live |> element("#note-#{note.id} a", "Edit") |> render_click() =~
assert index_live |> element("[data-qa=\"note-edit-#{note.id}\"]") |> render_click() =~
"edit"
assert_patch(index_live, Routes.note_index_path(conn, :edit, note))
@ -75,7 +84,7 @@ defmodule MemexWeb.NoteLiveTest do
test "deletes note in listing", %{conn: conn, note: note} do
{:ok, index_live, _html} = live(conn, Routes.note_index_path(conn, :index))
assert index_live |> element("#note-#{note.id} a", "Delete") |> render_click()
assert index_live |> element("[data-qa=\"delete-note-#{note.id}\"]") |> render_click()
refute has_element?(index_live, "#note-#{note.id}")
end
end

View File

@ -3,20 +3,23 @@ defmodule Memex.NotesFixtures do
This module defines test helpers for creating
entities via the `Memex.Notes` context.
"""
alias Memex.{Accounts.User, Notes, Notes.Note}
@doc """
Generate a note.
"""
def note_fixture(attrs \\ %{}) do
@spec note_fixture(User.t()) :: Note.t()
@spec note_fixture(attrs :: map(), User.t()) :: Note.t()
def note_fixture(attrs \\ %{}, user) do
{:ok, note} =
attrs
|> Enum.into(%{
content: "some content",
tag: [],
title: "some title",
visibility: :public
visibility: :private
})
|> Memex.Notes.create_note()
|> Notes.create_note(user)
note
end