work on contexts

This commit is contained in:
2022-11-24 12:44:34 -05:00
parent a0a0697f2d
commit cc11491106
18 changed files with 771 additions and 249 deletions

View File

@ -1,71 +1,113 @@
defmodule Memex.ContextsTest do
use Memex.DataCase
alias Memex.Contexts
import Memex.ContextsFixtures
alias Memex.{Contexts, Contexts.Context}
@moduletag :contexts_test
@invalid_attrs %{content: nil, tag: nil, title: nil, visibility: nil}
describe "contexts" do
alias Memex.Contexts.Context
import Memex.ContextsFixtures
@invalid_attrs %{content: nil, tag: nil, title: nil, visibility: nil}
test "list_contexts/0 returns all contexts" do
context = context_fixture()
assert Contexts.list_contexts() == [context]
setup do
[user: user_fixture()]
end
test "get_context!/1 returns the context with given id" do
context = context_fixture()
assert Contexts.get_context!(context.id) == context
test "list_contexts/1 returns all contexts for a user", %{user: user} do
context_a = context_fixture(%{title: "a", visibility: :public}, user)
context_b = context_fixture(%{title: "b", visibility: :unlisted}, user)
context_c = context_fixture(%{title: "c", visibility: :private}, user)
assert Contexts.list_contexts(user) == [context_a, context_b, context_c]
end
test "create_context/1 with valid data creates a context" do
valid_attrs = %{content: "some content", tag: [], title: "some title", visibility: :public}
test "list_public_contexts/0 returns public contexts", %{user: user} do
public_context = context_fixture(%{visibility: :public}, user)
context_fixture(%{visibility: :unlisted}, user)
context_fixture(%{visibility: :private}, user)
assert Contexts.list_public_contexts() == [public_context]
end
assert {:ok, %Context{} = context} = Contexts.create_context(valid_attrs)
test "get_context!/1 returns the context with given id", %{user: user} do
context = context_fixture(%{visibility: :public}, user)
assert Contexts.get_context!(context.id, user) == context
context = context_fixture(%{visibility: :unlisted}, user)
assert Contexts.get_context!(context.id, user) == context
context = context_fixture(%{visibility: :private}, user)
assert Contexts.get_context!(context.id, user) == context
end
test "get_context!/1 only returns unlisted or public contexts for other users", %{user: user} do
another_user = user_fixture()
context = context_fixture(%{visibility: :public}, another_user)
assert Contexts.get_context!(context.id, user) == context
context = context_fixture(%{visibility: :unlisted}, another_user)
assert Contexts.get_context!(context.id, user) == context
context = context_fixture(%{visibility: :private}, another_user)
assert_raise Ecto.NoResultsError, fn ->
Contexts.get_context!(context.id, user)
end
end
test "create_context/1 with valid data creates a context", %{user: user} do
valid_attrs = %{
"content" => "some content",
"tags_string" => "tag1,tag2",
"title" => "some title",
"visibility" => :public
}
assert {:ok, %Context{} = context} = Contexts.create_context(valid_attrs, user)
assert context.content == "some content"
assert context.tag == []
assert context.tags == ["tag1", "tag2"]
assert context.title == "some title"
assert context.visibility == :public
end
test "create_context/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Contexts.create_context(@invalid_attrs)
test "create_context/1 with invalid data returns error changeset", %{user: user} do
assert {:error, %Ecto.Changeset{}} = Contexts.create_context(@invalid_attrs, user)
end
test "update_context/2 with valid data updates the context" do
context = context_fixture()
test "update_context/2 with valid data updates the context", %{user: user} do
context = context_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, %Context{} = context} = Contexts.update_context(context, update_attrs)
assert {:ok, %Context{} = context} = Contexts.update_context(context, update_attrs, user)
assert context.content == "some updated content"
assert context.tag == []
assert context.tags == ["tag1", "tag2"]
assert context.title == "some updated title"
assert context.visibility == :private
end
test "update_context/2 with invalid data returns error changeset" do
context = context_fixture()
assert {:error, %Ecto.Changeset{}} = Contexts.update_context(context, @invalid_attrs)
assert context == Contexts.get_context!(context.id)
test "update_context/2 with invalid data returns error changeset", %{user: user} do
context = context_fixture(user)
assert {:error, %Ecto.Changeset{}} = Contexts.update_context(context, @invalid_attrs, user)
assert context == Contexts.get_context!(context.id, user)
end
test "delete_context/1 deletes the context" do
context = context_fixture()
assert {:ok, %Context{}} = Contexts.delete_context(context)
assert_raise Ecto.NoResultsError, fn -> Contexts.get_context!(context.id) end
test "delete_context/1 deletes the context", %{user: user} do
context = context_fixture(user)
assert {:ok, %Context{}} = Contexts.delete_context(context, user)
assert_raise Ecto.NoResultsError, fn -> Contexts.get_context!(context.id, user) end
end
test "change_context/1 returns a context changeset" do
context = context_fixture()
assert %Ecto.Changeset{} = Contexts.change_context(context)
test "delete_context/1 deletes the context for an admin user", %{user: user} do
admin_user = admin_fixture()
context = context_fixture(user)
assert {:ok, %Context{}} = Contexts.delete_context(context, admin_user)
assert_raise Ecto.NoResultsError, fn -> Contexts.get_context!(context.id, user) end
end
test "change_context/1 returns a context changeset", %{user: user} do
context = context_fixture(user)
assert %Ecto.Changeset{} = Contexts.change_context(context, user)
end
end
end

View File

@ -23,18 +23,17 @@ defmodule MemexWeb.ContextLiveTest do
"visibility" => nil
}
defp create_context(_) do
context = context_fixture()
%{context: context}
defp create_context(%{user: user}) do
[context: context_fixture(user)]
end
describe "Index" do
setup [:create_context]
setup [:register_and_log_in_user, :create_context]
test "lists all contexts", %{conn: conn, context: context} do
{:ok, _index_live, html} = live(conn, Routes.context_index_path(conn, :index))
assert html =~ "listing contexts"
assert html =~ "contexts"
assert html =~ context.content
end
@ -56,15 +55,15 @@ defmodule MemexWeb.ContextLiveTest do
|> render_submit()
|> follow_redirect(conn, Routes.context_index_path(conn, :index))
assert html =~ "context created successfully"
assert html =~ "#{@create_attrs |> Map.get("title")} created"
assert html =~ "some content"
end
test "updates context in listing", %{conn: conn, context: context} do
{:ok, index_live, _html} = live(conn, Routes.context_index_path(conn, :index))
assert index_live |> element("#context-#{context.id} a", "edit") |> render_click() =~
"edit context"
assert index_live |> element("[data-qa=\"context-edit-#{context.id}\"]") |> render_click() =~
"edit"
assert_patch(index_live, Routes.context_index_path(conn, :edit, context))
@ -78,20 +77,20 @@ defmodule MemexWeb.ContextLiveTest do
|> render_submit()
|> follow_redirect(conn, Routes.context_index_path(conn, :index))
assert html =~ "context updated successfully"
assert html =~ "#{@update_attrs |> Map.get("title")} saved"
assert html =~ "some updated content"
end
test "deletes context in listing", %{conn: conn, context: context} do
{:ok, index_live, _html} = live(conn, Routes.context_index_path(conn, :index))
assert index_live |> element("#context-#{context.id} a", "delete") |> render_click()
assert index_live |> element("[data-qa=\"delete-context-#{context.id}\"]") |> render_click()
refute has_element?(index_live, "#context-#{context.id}")
end
end
describe "show" do
setup [:create_context]
setup [:register_and_log_in_user, :create_context]
test "displays context", %{conn: conn, context: context} do
{:ok, _show_live, html} = live(conn, Routes.context_show_path(conn, :show, context))
@ -103,8 +102,7 @@ defmodule MemexWeb.ContextLiveTest do
test "updates context within modal", %{conn: conn, context: context} do
{:ok, show_live, _html} = live(conn, Routes.context_show_path(conn, :show, context))
assert show_live |> element("a", "edit") |> render_click() =~
"edit context"
assert show_live |> element("a", "edit") |> render_click() =~ "edit"
assert_patch(show_live, Routes.context_show_path(conn, :edit, context))
@ -118,8 +116,20 @@ defmodule MemexWeb.ContextLiveTest do
|> render_submit()
|> follow_redirect(conn, Routes.context_show_path(conn, :show, context))
assert html =~ "context updated successfully"
assert html =~ "#{@update_attrs |> Map.get("title")} saved"
assert html =~ "some updated content"
end
test "deletes context", %{conn: conn, context: context} do
{:ok, show_live, _html} = live(conn, Routes.context_show_path(conn, :show, context))
{:ok, index_live, _html} =
show_live
|> element("[data-qa=\"delete-context-#{context.id}\"]")
|> render_click()
|> follow_redirect(conn, Routes.context_index_path(conn, :index))
refute has_element?(index_live, "#context-#{context.id}")
end
end
end

View File

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