1
0
Fork 0

Add: tests

This commit is contained in:
Aroy-Art 2024-09-05 21:21:36 +02:00
parent 799c0bac65
commit 728382919b
Signed by: Aroy
GPG key ID: 583642324A1D2070
3 changed files with 166 additions and 0 deletions

View file

@ -0,0 +1,61 @@
defmodule NotesApp.NotesTest do
use NotesApp.DataCase
alias NotesApp.Notes
describe "notes" do
alias NotesApp.Notes.Note
import NotesApp.NotesFixtures
@invalid_attrs %{title: nil, content: nil}
test "list_notes/0 returns all notes" do
note = note_fixture()
assert Notes.list_notes() == [note]
end
test "get_note!/1 returns the note with given id" do
note = note_fixture()
assert Notes.get_note!(note.id) == note
end
test "create_note/1 with valid data creates a note" do
valid_attrs = %{title: "some title", content: "some content"}
assert {:ok, %Note{} = note} = Notes.create_note(valid_attrs)
assert note.title == "some title"
assert note.content == "some content"
end
test "create_note/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Notes.create_note(@invalid_attrs)
end
test "update_note/2 with valid data updates the note" do
note = note_fixture()
update_attrs = %{title: "some updated title", content: "some updated content"}
assert {:ok, %Note{} = note} = Notes.update_note(note, update_attrs)
assert note.title == "some updated title"
assert note.content == "some updated content"
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)
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
end
test "change_note/1 returns a note changeset" do
note = note_fixture()
assert %Ecto.Changeset{} = Notes.change_note(note)
end
end
end

View file

@ -0,0 +1,84 @@
defmodule NotesAppWeb.NoteControllerTest do
use NotesAppWeb.ConnCase
import NotesApp.NotesFixtures
@create_attrs %{title: "some title", content: "some content"}
@update_attrs %{title: "some updated title", content: "some updated content"}
@invalid_attrs %{title: nil, content: nil}
describe "index" do
test "lists all notes", %{conn: conn} do
conn = get(conn, ~p"/notes")
assert html_response(conn, 200) =~ "Listing Notes"
end
end
describe "new note" do
test "renders form", %{conn: conn} do
conn = get(conn, ~p"/notes/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, ~p"/notes", note: @create_attrs)
assert %{id: id} = redirected_params(conn)
assert redirected_to(conn) == ~p"/notes/#{id}"
conn = get(conn, ~p"/notes/#{id}")
assert html_response(conn, 200) =~ "Note #{id}"
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, ~p"/notes", 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, ~p"/notes/#{note}/edit")
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
conn = put(conn, ~p"/notes/#{note}", note: @update_attrs)
assert redirected_to(conn) == ~p"/notes/#{note}"
conn = get(conn, ~p"/notes/#{note}")
assert html_response(conn, 200) =~ "some updated title"
end
test "renders errors when data is invalid", %{conn: conn, note: note} do
conn = put(conn, ~p"/notes/#{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, ~p"/notes/#{note}")
assert redirected_to(conn) == ~p"/notes"
assert_error_sent 404, fn ->
get(conn, ~p"/notes/#{note}")
end
end
end
defp create_note(_) do
note = note_fixture()
%{note: note}
end
end

View file

@ -0,0 +1,21 @@
defmodule NotesApp.NotesFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `NotesApp.Notes` context.
"""
@doc """
Generate a note.
"""
def note_fixture(attrs \\ %{}) do
{:ok, note} =
attrs
|> Enum.into(%{
content: "some content",
title: "some title"
})
|> NotesApp.Notes.create_note()
note
end
end