Generate schema and live_view scaffold for records

1. Records are not yet tied to artists
2. The generated edit route, along with related functionality, may be
   deleted as I don't think it's gonna be needed. Leaving it included in
   the commit for reference.
This commit is contained in:
Claudio Ortolina
2024-09-12 14:42:15 +01:00
parent 34111a44ba
commit e08fbe3740
12 changed files with 603 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
defmodule MusicLibrary.RecordsTest do
use MusicLibrary.DataCase
alias MusicLibrary.Records
describe "records" do
alias MusicLibrary.Records.Record
import MusicLibrary.RecordsFixtures
@invalid_attrs %{type: nil, title: nil, image: nil, year: nil, musicbrainz_id: nil, genres: nil}
test "list_records/0 returns all records" do
record = record_fixture()
assert Records.list_records() == [record]
end
test "get_record!/1 returns the record with given id" do
record = record_fixture()
assert Records.get_record!(record.id) == record
end
test "create_record/1 with valid data creates a record" do
valid_attrs = %{type: :album, title: "some title", image: "some image", year: 42, musicbrainz_id: "7488a646-e31f-11e4-aace-600308960662", genres: ["option1", "option2"]}
assert {:ok, %Record{} = record} = Records.create_record(valid_attrs)
assert record.type == :album
assert record.title == "some title"
assert record.image == "some image"
assert record.year == 42
assert record.musicbrainz_id == "7488a646-e31f-11e4-aace-600308960662"
assert record.genres == ["option1", "option2"]
end
test "create_record/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Records.create_record(@invalid_attrs)
end
test "update_record/2 with valid data updates the record" do
record = record_fixture()
update_attrs = %{type: :ep, title: "some updated title", image: "some updated image", year: 43, musicbrainz_id: "7488a646-e31f-11e4-aace-600308960668", genres: ["option1"]}
assert {:ok, %Record{} = record} = Records.update_record(record, update_attrs)
assert record.type == :ep
assert record.title == "some updated title"
assert record.image == "some updated image"
assert record.year == 43
assert record.musicbrainz_id == "7488a646-e31f-11e4-aace-600308960668"
assert record.genres == ["option1"]
end
test "update_record/2 with invalid data returns error changeset" do
record = record_fixture()
assert {:error, %Ecto.Changeset{}} = Records.update_record(record, @invalid_attrs)
assert record == Records.get_record!(record.id)
end
test "delete_record/1 deletes the record" do
record = record_fixture()
assert {:ok, %Record{}} = Records.delete_record(record)
assert_raise Ecto.NoResultsError, fn -> Records.get_record!(record.id) end
end
test "change_record/1 returns a record changeset" do
record = record_fixture()
assert %Ecto.Changeset{} = Records.change_record(record)
end
end
end
@@ -0,0 +1,113 @@
defmodule MusicLibraryWeb.RecordLiveTest do
use MusicLibraryWeb.ConnCase
import Phoenix.LiveViewTest
import MusicLibrary.RecordsFixtures
@create_attrs %{type: :album, title: "some title", image: "some image", year: 42, musicbrainz_id: "7488a646-e31f-11e4-aace-600308960662", genres: ["option1", "option2"]}
@update_attrs %{type: :ep, title: "some updated title", image: "some updated image", year: 43, musicbrainz_id: "7488a646-e31f-11e4-aace-600308960668", genres: ["option1"]}
@invalid_attrs %{type: nil, title: nil, image: nil, year: nil, musicbrainz_id: nil, genres: []}
defp create_record(_) do
record = record_fixture()
%{record: record}
end
describe "Index" do
setup [:create_record]
test "lists all records", %{conn: conn, record: record} do
{:ok, _index_live, html} = live(conn, ~p"/records")
assert html =~ "Listing Records"
assert html =~ record.title
end
test "saves new record", %{conn: conn} do
{:ok, index_live, _html} = live(conn, ~p"/records")
assert index_live |> element("a", "New Record") |> render_click() =~
"New Record"
assert_patch(index_live, ~p"/records/new")
assert index_live
|> form("#record-form", record: @invalid_attrs)
|> render_change() =~ "can't be blank"
assert index_live
|> form("#record-form", record: @create_attrs)
|> render_submit()
assert_patch(index_live, ~p"/records")
html = render(index_live)
assert html =~ "Record created successfully"
assert html =~ "some title"
end
test "updates record in listing", %{conn: conn, record: record} do
{:ok, index_live, _html} = live(conn, ~p"/records")
assert index_live |> element("#records-#{record.id} a", "Edit") |> render_click() =~
"Edit Record"
assert_patch(index_live, ~p"/records/#{record}/edit")
assert index_live
|> form("#record-form", record: @invalid_attrs)
|> render_change() =~ "can't be blank"
assert index_live
|> form("#record-form", record: @update_attrs)
|> render_submit()
assert_patch(index_live, ~p"/records")
html = render(index_live)
assert html =~ "Record updated successfully"
assert html =~ "some updated title"
end
test "deletes record in listing", %{conn: conn, record: record} do
{:ok, index_live, _html} = live(conn, ~p"/records")
assert index_live |> element("#records-#{record.id} a", "Delete") |> render_click()
refute has_element?(index_live, "#records-#{record.id}")
end
end
describe "Show" do
setup [:create_record]
test "displays record", %{conn: conn, record: record} do
{:ok, _show_live, html} = live(conn, ~p"/records/#{record}")
assert html =~ "Show Record"
assert html =~ record.title
end
test "updates record within modal", %{conn: conn, record: record} do
{:ok, show_live, _html} = live(conn, ~p"/records/#{record}")
assert show_live |> element("a", "Edit") |> render_click() =~
"Edit Record"
assert_patch(show_live, ~p"/records/#{record}/show/edit")
assert show_live
|> form("#record-form", record: @invalid_attrs)
|> render_change() =~ "can't be blank"
assert show_live
|> form("#record-form", record: @update_attrs)
|> render_submit()
assert_patch(show_live, ~p"/records/#{record}")
html = render(show_live)
assert html =~ "Record updated successfully"
assert html =~ "some updated title"
end
end
end
+25
View File
@@ -0,0 +1,25 @@
defmodule MusicLibrary.RecordsFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `MusicLibrary.Records` context.
"""
@doc """
Generate a record.
"""
def record_fixture(attrs \\ %{}) do
{:ok, record} =
attrs
|> Enum.into(%{
genres: ["option1", "option2"],
image: "some image",
musicbrainz_id: "7488a646-e31f-11e4-aace-600308960662",
title: "some title",
type: :album,
year: 42
})
|> MusicLibrary.Records.create_record()
record
end
end