Add Articles
mix phx.gen.live Articles Article articles title:string\ /Crucial/git/phoenix-liveview-book content:text url:string language:string\ date:utc_datetime author_id:references:authors
This commit is contained in:
67
test/outlook/articles_test.exs
Normal file
67
test/outlook/articles_test.exs
Normal file
@ -0,0 +1,67 @@
|
||||
defmodule Outlook.ArticlesTest do
|
||||
use Outlook.DataCase
|
||||
|
||||
alias Outlook.Articles
|
||||
|
||||
describe "articles" do
|
||||
alias Outlook.Articles.Article
|
||||
|
||||
import Outlook.ArticlesFixtures
|
||||
|
||||
@invalid_attrs %{content: nil, date: nil, language: nil, title: nil, url: nil}
|
||||
|
||||
test "list_articles/0 returns all articles" do
|
||||
article = article_fixture()
|
||||
assert Articles.list_articles() == [article]
|
||||
end
|
||||
|
||||
test "get_article!/1 returns the article with given id" do
|
||||
article = article_fixture()
|
||||
assert Articles.get_article!(article.id) == article
|
||||
end
|
||||
|
||||
test "create_article/1 with valid data creates a article" do
|
||||
valid_attrs = %{content: "some content", date: ~U[2022-12-25 16:16:00Z], language: "some language", title: "some title", url: "some url"}
|
||||
|
||||
assert {:ok, %Article{} = article} = Articles.create_article(valid_attrs)
|
||||
assert article.content == "some content"
|
||||
assert article.date == ~U[2022-12-25 16:16:00Z]
|
||||
assert article.language == "some language"
|
||||
assert article.title == "some title"
|
||||
assert article.url == "some url"
|
||||
end
|
||||
|
||||
test "create_article/1 with invalid data returns error changeset" do
|
||||
assert {:error, %Ecto.Changeset{}} = Articles.create_article(@invalid_attrs)
|
||||
end
|
||||
|
||||
test "update_article/2 with valid data updates the article" do
|
||||
article = article_fixture()
|
||||
update_attrs = %{content: "some updated content", date: ~U[2022-12-26 16:16:00Z], language: "some updated language", title: "some updated title", url: "some updated url"}
|
||||
|
||||
assert {:ok, %Article{} = article} = Articles.update_article(article, update_attrs)
|
||||
assert article.content == "some updated content"
|
||||
assert article.date == ~U[2022-12-26 16:16:00Z]
|
||||
assert article.language == "some updated language"
|
||||
assert article.title == "some updated title"
|
||||
assert article.url == "some updated url"
|
||||
end
|
||||
|
||||
test "update_article/2 with invalid data returns error changeset" do
|
||||
article = article_fixture()
|
||||
assert {:error, %Ecto.Changeset{}} = Articles.update_article(article, @invalid_attrs)
|
||||
assert article == Articles.get_article!(article.id)
|
||||
end
|
||||
|
||||
test "delete_article/1 deletes the article" do
|
||||
article = article_fixture()
|
||||
assert {:ok, %Article{}} = Articles.delete_article(article)
|
||||
assert_raise Ecto.NoResultsError, fn -> Articles.get_article!(article.id) end
|
||||
end
|
||||
|
||||
test "change_article/1 returns a article changeset" do
|
||||
article = article_fixture()
|
||||
assert %Ecto.Changeset{} = Articles.change_article(article)
|
||||
end
|
||||
end
|
||||
end
|
||||
110
test/outlook_web/live/article_live_test.exs
Normal file
110
test/outlook_web/live/article_live_test.exs
Normal file
@ -0,0 +1,110 @@
|
||||
defmodule OutlookWeb.ArticleLiveTest do
|
||||
use OutlookWeb.ConnCase
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
import Outlook.ArticlesFixtures
|
||||
|
||||
@create_attrs %{content: "some content", date: "2022-12-25T16:16:00Z", language: "some language", title: "some title", url: "some url"}
|
||||
@update_attrs %{content: "some updated content", date: "2022-12-26T16:16:00Z", language: "some updated language", title: "some updated title", url: "some updated url"}
|
||||
@invalid_attrs %{content: nil, date: nil, language: nil, title: nil, url: nil}
|
||||
|
||||
defp create_article(_) do
|
||||
article = article_fixture()
|
||||
%{article: article}
|
||||
end
|
||||
|
||||
describe "Index" do
|
||||
setup [:create_article]
|
||||
|
||||
test "lists all articles", %{conn: conn, article: article} do
|
||||
{:ok, _index_live, html} = live(conn, ~p"/articles")
|
||||
|
||||
assert html =~ "Listing Articles"
|
||||
assert html =~ article.content
|
||||
end
|
||||
|
||||
test "saves new article", %{conn: conn} do
|
||||
{:ok, index_live, _html} = live(conn, ~p"/articles")
|
||||
|
||||
assert index_live |> element("a", "New Article") |> render_click() =~
|
||||
"New Article"
|
||||
|
||||
assert_patch(index_live, ~p"/articles/new")
|
||||
|
||||
assert index_live
|
||||
|> form("#article-form", article: @invalid_attrs)
|
||||
|> render_change() =~ "can't be blank"
|
||||
|
||||
{:ok, _, html} =
|
||||
index_live
|
||||
|> form("#article-form", article: @create_attrs)
|
||||
|> render_submit()
|
||||
|> follow_redirect(conn, ~p"/articles")
|
||||
|
||||
assert html =~ "Article created successfully"
|
||||
assert html =~ "some content"
|
||||
end
|
||||
|
||||
test "updates article in listing", %{conn: conn, article: article} do
|
||||
{:ok, index_live, _html} = live(conn, ~p"/articles")
|
||||
|
||||
assert index_live |> element("#articles-#{article.id} a", "Edit") |> render_click() =~
|
||||
"Edit Article"
|
||||
|
||||
assert_patch(index_live, ~p"/articles/#{article}/edit")
|
||||
|
||||
assert index_live
|
||||
|> form("#article-form", article: @invalid_attrs)
|
||||
|> render_change() =~ "can't be blank"
|
||||
|
||||
{:ok, _, html} =
|
||||
index_live
|
||||
|> form("#article-form", article: @update_attrs)
|
||||
|> render_submit()
|
||||
|> follow_redirect(conn, ~p"/articles")
|
||||
|
||||
assert html =~ "Article updated successfully"
|
||||
assert html =~ "some updated content"
|
||||
end
|
||||
|
||||
test "deletes article in listing", %{conn: conn, article: article} do
|
||||
{:ok, index_live, _html} = live(conn, ~p"/articles")
|
||||
|
||||
assert index_live |> element("#articles-#{article.id} a", "Delete") |> render_click()
|
||||
refute has_element?(index_live, "#article-#{article.id}")
|
||||
end
|
||||
end
|
||||
|
||||
describe "Show" do
|
||||
setup [:create_article]
|
||||
|
||||
test "displays article", %{conn: conn, article: article} do
|
||||
{:ok, _show_live, html} = live(conn, ~p"/articles/#{article}")
|
||||
|
||||
assert html =~ "Show Article"
|
||||
assert html =~ article.content
|
||||
end
|
||||
|
||||
test "updates article within modal", %{conn: conn, article: article} do
|
||||
{:ok, show_live, _html} = live(conn, ~p"/articles/#{article}")
|
||||
|
||||
assert show_live |> element("a", "Edit") |> render_click() =~
|
||||
"Edit Article"
|
||||
|
||||
assert_patch(show_live, ~p"/articles/#{article}/show/edit")
|
||||
|
||||
assert show_live
|
||||
|> form("#article-form", article: @invalid_attrs)
|
||||
|> render_change() =~ "can't be blank"
|
||||
|
||||
{:ok, _, html} =
|
||||
show_live
|
||||
|> form("#article-form", article: @update_attrs)
|
||||
|> render_submit()
|
||||
|> follow_redirect(conn, ~p"/articles/#{article}")
|
||||
|
||||
assert html =~ "Article updated successfully"
|
||||
assert html =~ "some updated content"
|
||||
end
|
||||
end
|
||||
end
|
||||
24
test/support/fixtures/articles_fixtures.ex
Normal file
24
test/support/fixtures/articles_fixtures.ex
Normal file
@ -0,0 +1,24 @@
|
||||
defmodule Outlook.ArticlesFixtures do
|
||||
@moduledoc """
|
||||
This module defines test helpers for creating
|
||||
entities via the `Outlook.Articles` context.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Generate a article.
|
||||
"""
|
||||
def article_fixture(attrs \\ %{}) do
|
||||
{:ok, article} =
|
||||
attrs
|
||||
|> Enum.into(%{
|
||||
content: "some content",
|
||||
date: ~U[2022-12-25 16:16:00Z],
|
||||
language: "some language",
|
||||
title: "some title",
|
||||
url: "some url"
|
||||
})
|
||||
|> Outlook.Articles.create_article()
|
||||
|
||||
article
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user