initial book review schema

This commit is contained in:
Daniel Yrovas 2025-01-09 21:43:05 +11:00
parent e218290e08
commit 90414e6b93
Signed by: danielyrovas
SSH key fingerprint: SHA256:1avlGZQpGW038lBkNI5lOS7f0hIPM7ecJen2/P1MCCU
15 changed files with 653 additions and 0 deletions

View file

@ -0,0 +1,24 @@
defmodule Wild.BookReadsFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `Wild.BookReads` context.
"""
@doc """
Generate a book_read.
"""
def book_read_fixture(attrs \\ %{}) do
{:ok, book_read} =
attrs
|> Enum.into(%{
progress: 42,
rating: 42,
read_finish: ~U[2025-01-08 10:31:00Z],
read_start: ~U[2025-01-08 10:31:00Z],
thoughts: "some thoughts"
})
|> Wild.BookReads.create_book_read()
book_read
end
end

View file

@ -0,0 +1,67 @@
defmodule Wild.BookReadsTest do
use Wild.DataCase
alias Wild.BookReads
describe "book_reads" do
alias Wild.BookReads.BookRead
import Wild.BookReadsFixtures
@invalid_attrs %{progress: nil, rating: nil, thoughts: nil, read_start: nil, read_finish: nil}
test "list_book_reads/0 returns all book_reads" do
book_read = book_read_fixture()
assert BookReads.list_book_reads() == [book_read]
end
test "get_book_read!/1 returns the book_read with given id" do
book_read = book_read_fixture()
assert BookReads.get_book_read!(book_read.id) == book_read
end
test "create_book_read/1 with valid data creates a book_read" do
valid_attrs = %{progress: 42, rating: 42, thoughts: "some thoughts", read_start: ~U[2025-01-08 10:31:00Z], read_finish: ~U[2025-01-08 10:31:00Z]}
assert {:ok, %BookRead{} = book_read} = BookReads.create_book_read(valid_attrs)
assert book_read.progress == 42
assert book_read.rating == 42
assert book_read.thoughts == "some thoughts"
assert book_read.read_start == ~U[2025-01-08 10:31:00Z]
assert book_read.read_finish == ~U[2025-01-08 10:31:00Z]
end
test "create_book_read/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = BookReads.create_book_read(@invalid_attrs)
end
test "update_book_read/2 with valid data updates the book_read" do
book_read = book_read_fixture()
update_attrs = %{progress: 43, rating: 43, thoughts: "some updated thoughts", read_start: ~U[2025-01-09 10:31:00Z], read_finish: ~U[2025-01-09 10:31:00Z]}
assert {:ok, %BookRead{} = book_read} = BookReads.update_book_read(book_read, update_attrs)
assert book_read.progress == 43
assert book_read.rating == 43
assert book_read.thoughts == "some updated thoughts"
assert book_read.read_start == ~U[2025-01-09 10:31:00Z]
assert book_read.read_finish == ~U[2025-01-09 10:31:00Z]
end
test "update_book_read/2 with invalid data returns error changeset" do
book_read = book_read_fixture()
assert {:error, %Ecto.Changeset{}} = BookReads.update_book_read(book_read, @invalid_attrs)
assert book_read == BookReads.get_book_read!(book_read.id)
end
test "delete_book_read/1 deletes the book_read" do
book_read = book_read_fixture()
assert {:ok, %BookRead{}} = BookReads.delete_book_read(book_read)
assert_raise Ecto.NoResultsError, fn -> BookReads.get_book_read!(book_read.id) end
end
test "change_book_read/1 returns a book_read changeset" do
book_read = book_read_fixture()
assert %Ecto.Changeset{} = BookReads.change_book_read(book_read)
end
end
end

View file

@ -0,0 +1,113 @@
defmodule WildWeb.BookReadLiveTest do
use WildWeb.ConnCase
import Phoenix.LiveViewTest
import Wild.BookReadsFixtures
@create_attrs %{progress: 42, rating: 42, thoughts: "some thoughts", read_start: "2025-01-08T10:31:00Z", read_finish: "2025-01-08T10:31:00Z"}
@update_attrs %{progress: 43, rating: 43, thoughts: "some updated thoughts", read_start: "2025-01-09T10:31:00Z", read_finish: "2025-01-09T10:31:00Z"}
@invalid_attrs %{progress: nil, rating: nil, thoughts: nil, read_start: nil, read_finish: nil}
defp create_book_read(_) do
book_read = book_read_fixture()
%{book_read: book_read}
end
describe "Index" do
setup [:create_book_read]
test "lists all book_reads", %{conn: conn, book_read: book_read} do
{:ok, _index_live, html} = live(conn, ~p"/book_reads")
assert html =~ "Listing Book reads"
assert html =~ book_read.thoughts
end
test "saves new book_read", %{conn: conn} do
{:ok, index_live, _html} = live(conn, ~p"/book_reads")
assert index_live |> element("a", "New Book read") |> render_click() =~
"New Book read"
assert_patch(index_live, ~p"/book_reads/new")
assert index_live
|> form("#book_read-form", book_read: @invalid_attrs)
|> render_change() =~ "can't be blank"
assert index_live
|> form("#book_read-form", book_read: @create_attrs)
|> render_submit()
assert_patch(index_live, ~p"/book_reads")
html = render(index_live)
assert html =~ "Book read created successfully"
assert html =~ "some thoughts"
end
test "updates book_read in listing", %{conn: conn, book_read: book_read} do
{:ok, index_live, _html} = live(conn, ~p"/book_reads")
assert index_live |> element("#book_reads-#{book_read.id} a", "Edit") |> render_click() =~
"Edit Book read"
assert_patch(index_live, ~p"/book_reads/#{book_read}/edit")
assert index_live
|> form("#book_read-form", book_read: @invalid_attrs)
|> render_change() =~ "can't be blank"
assert index_live
|> form("#book_read-form", book_read: @update_attrs)
|> render_submit()
assert_patch(index_live, ~p"/book_reads")
html = render(index_live)
assert html =~ "Book read updated successfully"
assert html =~ "some updated thoughts"
end
test "deletes book_read in listing", %{conn: conn, book_read: book_read} do
{:ok, index_live, _html} = live(conn, ~p"/book_reads")
assert index_live |> element("#book_reads-#{book_read.id} a", "Delete") |> render_click()
refute has_element?(index_live, "#book_reads-#{book_read.id}")
end
end
describe "Show" do
setup [:create_book_read]
test "displays book_read", %{conn: conn, book_read: book_read} do
{:ok, _show_live, html} = live(conn, ~p"/book_reads/#{book_read}")
assert html =~ "Show Book read"
assert html =~ book_read.thoughts
end
test "updates book_read within modal", %{conn: conn, book_read: book_read} do
{:ok, show_live, _html} = live(conn, ~p"/book_reads/#{book_read}")
assert show_live |> element("a", "Edit") |> render_click() =~
"Edit Book read"
assert_patch(show_live, ~p"/book_reads/#{book_read}/show/edit")
assert show_live
|> form("#book_read-form", book_read: @invalid_attrs)
|> render_change() =~ "can't be blank"
assert show_live
|> form("#book_read-form", book_read: @update_attrs)
|> render_submit()
assert_patch(show_live, ~p"/book_reads/#{book_read}")
html = render(show_live)
assert html =~ "Book read updated successfully"
assert html =~ "some updated thoughts"
end
end
end