add contexts
This commit is contained in:
		
							
								
								
									
										104
									
								
								lib/memex/contexts.ex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										104
									
								
								lib/memex/contexts.ex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,104 @@ | |||||||
|  | defmodule Memex.Contexts do | ||||||
|  |   @moduledoc """ | ||||||
|  |   The Contexts context. | ||||||
|  |   """ | ||||||
|  |  | ||||||
|  |   import Ecto.Query, warn: false | ||||||
|  |   alias Memex.Repo | ||||||
|  |  | ||||||
|  |   alias Memex.Contexts.Context | ||||||
|  |  | ||||||
|  |   @doc """ | ||||||
|  |   Returns the list of contexts. | ||||||
|  |  | ||||||
|  |   ## Examples | ||||||
|  |  | ||||||
|  |       iex> list_contexts() | ||||||
|  |       [%Context{}, ...] | ||||||
|  |  | ||||||
|  |   """ | ||||||
|  |   def list_contexts do | ||||||
|  |     Repo.all(Context) | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   @doc """ | ||||||
|  |   Gets a single context. | ||||||
|  |  | ||||||
|  |   Raises `Ecto.NoResultsError` if the Context does not exist. | ||||||
|  |  | ||||||
|  |   ## Examples | ||||||
|  |  | ||||||
|  |       iex> get_context!(123) | ||||||
|  |       %Context{} | ||||||
|  |  | ||||||
|  |       iex> get_context!(456) | ||||||
|  |       ** (Ecto.NoResultsError) | ||||||
|  |  | ||||||
|  |   """ | ||||||
|  |   def get_context!(id), do: Repo.get!(Context, id) | ||||||
|  |  | ||||||
|  |   @doc """ | ||||||
|  |   Creates a context. | ||||||
|  |  | ||||||
|  |   ## Examples | ||||||
|  |  | ||||||
|  |       iex> create_context(%{field: value}) | ||||||
|  |       {:ok, %Context{}} | ||||||
|  |  | ||||||
|  |       iex> create_context(%{field: bad_value}) | ||||||
|  |       {:error, %Ecto.Changeset{}} | ||||||
|  |  | ||||||
|  |   """ | ||||||
|  |   def create_context(attrs \\ %{}) do | ||||||
|  |     %Context{} | ||||||
|  |     |> Context.changeset(attrs) | ||||||
|  |     |> Repo.insert() | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   @doc """ | ||||||
|  |   Updates a context. | ||||||
|  |  | ||||||
|  |   ## Examples | ||||||
|  |  | ||||||
|  |       iex> update_context(context, %{field: new_value}) | ||||||
|  |       {:ok, %Context{}} | ||||||
|  |  | ||||||
|  |       iex> update_context(context, %{field: bad_value}) | ||||||
|  |       {:error, %Ecto.Changeset{}} | ||||||
|  |  | ||||||
|  |   """ | ||||||
|  |   def update_context(%Context{} = context, attrs) do | ||||||
|  |     context | ||||||
|  |     |> Context.changeset(attrs) | ||||||
|  |     |> Repo.update() | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   @doc """ | ||||||
|  |   Deletes a context. | ||||||
|  |  | ||||||
|  |   ## Examples | ||||||
|  |  | ||||||
|  |       iex> delete_context(context) | ||||||
|  |       {:ok, %Context{}} | ||||||
|  |  | ||||||
|  |       iex> delete_context(context) | ||||||
|  |       {:error, %Ecto.Changeset{}} | ||||||
|  |  | ||||||
|  |   """ | ||||||
|  |   def delete_context(%Context{} = context) do | ||||||
|  |     Repo.delete(context) | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   @doc """ | ||||||
|  |   Returns an `%Ecto.Changeset{}` for tracking context changes. | ||||||
|  |  | ||||||
|  |   ## Examples | ||||||
|  |  | ||||||
|  |       iex> change_context(context) | ||||||
|  |       %Ecto.Changeset{data: %Context{}} | ||||||
|  |  | ||||||
|  |   """ | ||||||
|  |   def change_context(%Context{} = context, attrs \\ %{}) do | ||||||
|  |     Context.changeset(context, attrs) | ||||||
|  |   end | ||||||
|  | end | ||||||
							
								
								
									
										22
									
								
								lib/memex/contexts/context.ex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								lib/memex/contexts/context.ex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | |||||||
|  | defmodule Memex.Contexts.Context do | ||||||
|  |   use Ecto.Schema | ||||||
|  |   import Ecto.Changeset | ||||||
|  |  | ||||||
|  |   @primary_key {:id, :binary_id, autogenerate: true} | ||||||
|  |   @foreign_key_type :binary_id | ||||||
|  |   schema "contexts" do | ||||||
|  |     field :content, :string | ||||||
|  |     field :tag, {:array, :string} | ||||||
|  |     field :title, :string | ||||||
|  |     field :visibility, Ecto.Enum, values: [:public, :private, :unlisted] | ||||||
|  |  | ||||||
|  |     timestamps() | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   @doc false | ||||||
|  |   def changeset(context, attrs) do | ||||||
|  |     context | ||||||
|  |     |> cast(attrs, [:title, :content, :tag, :visibility]) | ||||||
|  |     |> validate_required([:title, :content, :tag, :visibility]) | ||||||
|  |   end | ||||||
|  | end | ||||||
							
								
								
									
										55
									
								
								lib/memex_web/live/context_live/form_component.ex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								lib/memex_web/live/context_live/form_component.ex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,55 @@ | |||||||
|  | defmodule MemexWeb.ContextLive.FormComponent do | ||||||
|  |   use MemexWeb, :live_component | ||||||
|  |  | ||||||
|  |   alias Memex.Contexts | ||||||
|  |  | ||||||
|  |   @impl true | ||||||
|  |   def update(%{context: context} = assigns, socket) do | ||||||
|  |     changeset = Contexts.change_context(context) | ||||||
|  |  | ||||||
|  |     {:ok, | ||||||
|  |      socket | ||||||
|  |      |> assign(assigns) | ||||||
|  |      |> assign(:changeset, changeset)} | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   @impl true | ||||||
|  |   def handle_event("validate", %{"context" => context_params}, socket) do | ||||||
|  |     changeset = | ||||||
|  |       socket.assigns.context | ||||||
|  |       |> Contexts.change_context(context_params) | ||||||
|  |       |> Map.put(:action, :validate) | ||||||
|  |  | ||||||
|  |     {:noreply, assign(socket, :changeset, changeset)} | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   def handle_event("save", %{"context" => context_params}, socket) do | ||||||
|  |     save_context(socket, socket.assigns.action, context_params) | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   defp save_context(socket, :edit, context_params) do | ||||||
|  |     case Contexts.update_context(socket.assigns.context, context_params) do | ||||||
|  |       {:ok, _context} -> | ||||||
|  |         {:noreply, | ||||||
|  |          socket | ||||||
|  |          |> put_flash(:info, "Context updated successfully") | ||||||
|  |          |> push_redirect(to: socket.assigns.return_to)} | ||||||
|  |  | ||||||
|  |       {:error, %Ecto.Changeset{} = changeset} -> | ||||||
|  |         {:noreply, assign(socket, :changeset, changeset)} | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   defp save_context(socket, :new, context_params) do | ||||||
|  |     case Contexts.create_context(context_params) do | ||||||
|  |       {:ok, _context} -> | ||||||
|  |         {:noreply, | ||||||
|  |          socket | ||||||
|  |          |> put_flash(:info, "Context created successfully") | ||||||
|  |          |> push_redirect(to: socket.assigns.return_to)} | ||||||
|  |  | ||||||
|  |       {:error, %Ecto.Changeset{} = changeset} -> | ||||||
|  |         {:noreply, assign(socket, changeset: changeset)} | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | end | ||||||
							
								
								
									
										32
									
								
								lib/memex_web/live/context_live/form_component.html.heex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								lib/memex_web/live/context_live/form_component.html.heex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | |||||||
|  | <div> | ||||||
|  |   <h2><%= @title %></h2> | ||||||
|  |  | ||||||
|  |   <.form | ||||||
|  |     let={f} | ||||||
|  |     for={@changeset} | ||||||
|  |     id="context-form" | ||||||
|  |     phx-target={@myself} | ||||||
|  |     phx-change="validate" | ||||||
|  |     phx-submit="save"> | ||||||
|  |    | ||||||
|  |     <%= label f, :title %> | ||||||
|  |     <%= text_input f, :title %> | ||||||
|  |     <%= error_tag f, :title %> | ||||||
|  |    | ||||||
|  |     <%= label f, :content %> | ||||||
|  |     <%= textarea f, :content %> | ||||||
|  |     <%= error_tag f, :content %> | ||||||
|  |    | ||||||
|  |     <%= label f, :tag %> | ||||||
|  |     <%= multiple_select f, :tag, ["Option 1": "option1", "Option 2": "option2"] %> | ||||||
|  |     <%= error_tag f, :tag %> | ||||||
|  |    | ||||||
|  |     <%= label f, :visibility %> | ||||||
|  |     <%= select f, :visibility, Ecto.Enum.values(Memex.Contexts.Context, :visibility), prompt: "Choose a value" %> | ||||||
|  |     <%= error_tag f, :visibility %> | ||||||
|  |    | ||||||
|  |     <div> | ||||||
|  |       <%= submit "Save", phx_disable_with: "Saving..." %> | ||||||
|  |     </div> | ||||||
|  |   </.form> | ||||||
|  | </div> | ||||||
							
								
								
									
										46
									
								
								lib/memex_web/live/context_live/index.ex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								lib/memex_web/live/context_live/index.ex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | |||||||
|  | defmodule MemexWeb.ContextLive.Index do | ||||||
|  |   use MemexWeb, :live_view | ||||||
|  |  | ||||||
|  |   alias Memex.Contexts | ||||||
|  |   alias Memex.Contexts.Context | ||||||
|  |  | ||||||
|  |   @impl true | ||||||
|  |   def mount(_params, _session, socket) do | ||||||
|  |     {:ok, assign(socket, :contexts, list_contexts())} | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   @impl true | ||||||
|  |   def handle_params(params, _url, socket) do | ||||||
|  |     {:noreply, apply_action(socket, socket.assigns.live_action, params)} | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   defp apply_action(socket, :edit, %{"id" => id}) do | ||||||
|  |     socket | ||||||
|  |     |> assign(:page_title, "Edit Context") | ||||||
|  |     |> assign(:context, Contexts.get_context!(id)) | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   defp apply_action(socket, :new, _params) do | ||||||
|  |     socket | ||||||
|  |     |> assign(:page_title, "New Context") | ||||||
|  |     |> assign(:context, %Context{}) | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   defp apply_action(socket, :index, _params) do | ||||||
|  |     socket | ||||||
|  |     |> assign(:page_title, "Listing Contexts") | ||||||
|  |     |> assign(:context, nil) | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   @impl true | ||||||
|  |   def handle_event("delete", %{"id" => id}, socket) do | ||||||
|  |     context = Contexts.get_context!(id) | ||||||
|  |     {:ok, _} = Contexts.delete_context(context) | ||||||
|  |  | ||||||
|  |     {:noreply, assign(socket, :contexts, list_contexts())} | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   defp list_contexts do | ||||||
|  |     Contexts.list_contexts() | ||||||
|  |   end | ||||||
|  | end | ||||||
							
								
								
									
										45
									
								
								lib/memex_web/live/context_live/index.html.heex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								lib/memex_web/live/context_live/index.html.heex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,45 @@ | |||||||
|  | <h1>Listing Contexts</h1> | ||||||
|  |  | ||||||
|  | <%= if @live_action in [:new, :edit] do %> | ||||||
|  |   <.modal return_to={Routes.context_index_path(@socket, :index)}> | ||||||
|  |     <.live_component | ||||||
|  |       module={MemexWeb.ContextLive.FormComponent} | ||||||
|  |       id={@context.id || :new} | ||||||
|  |       title={@page_title} | ||||||
|  |       action={@live_action} | ||||||
|  |       context={@context} | ||||||
|  |       return_to={Routes.context_index_path(@socket, :index)} | ||||||
|  |     /> | ||||||
|  |   </.modal> | ||||||
|  | <% end %> | ||||||
|  |  | ||||||
|  | <table> | ||||||
|  |   <thead> | ||||||
|  |     <tr> | ||||||
|  |       <th>Title</th> | ||||||
|  |       <th>Content</th> | ||||||
|  |       <th>Tag</th> | ||||||
|  |       <th>Visibility</th> | ||||||
|  |  | ||||||
|  |       <th></th> | ||||||
|  |     </tr> | ||||||
|  |   </thead> | ||||||
|  |   <tbody id="contexts"> | ||||||
|  |     <%= for context <- @contexts do %> | ||||||
|  |       <tr id={"context-#{context.id}"}> | ||||||
|  |         <td><%= context.title %></td> | ||||||
|  |         <td><%= context.content %></td> | ||||||
|  |         <td><%= context.tag %></td> | ||||||
|  |         <td><%= context.visibility %></td> | ||||||
|  |  | ||||||
|  |         <td> | ||||||
|  |           <span><%= live_redirect "Show", to: Routes.context_show_path(@socket, :show, context) %></span> | ||||||
|  |           <span><%= live_patch "Edit", to: Routes.context_index_path(@socket, :edit, context) %></span> | ||||||
|  |           <span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: context.id, data: [confirm: "Are you sure?"] %></span> | ||||||
|  |         </td> | ||||||
|  |       </tr> | ||||||
|  |     <% end %> | ||||||
|  |   </tbody> | ||||||
|  | </table> | ||||||
|  |  | ||||||
|  | <span><%= live_patch "New Context", to: Routes.context_index_path(@socket, :new) %></span> | ||||||
							
								
								
									
										21
									
								
								lib/memex_web/live/context_live/show.ex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								lib/memex_web/live/context_live/show.ex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | |||||||
|  | defmodule MemexWeb.ContextLive.Show do | ||||||
|  |   use MemexWeb, :live_view | ||||||
|  |  | ||||||
|  |   alias Memex.Contexts | ||||||
|  |  | ||||||
|  |   @impl true | ||||||
|  |   def mount(_params, _session, socket) do | ||||||
|  |     {:ok, socket} | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   @impl true | ||||||
|  |   def handle_params(%{"id" => id}, _, socket) do | ||||||
|  |     {:noreply, | ||||||
|  |      socket | ||||||
|  |      |> assign(:page_title, page_title(socket.assigns.live_action)) | ||||||
|  |      |> assign(:context, Contexts.get_context!(id))} | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   defp page_title(:show), do: "Show Context" | ||||||
|  |   defp page_title(:edit), do: "Edit Context" | ||||||
|  | end | ||||||
							
								
								
									
										41
									
								
								lib/memex_web/live/context_live/show.html.heex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								lib/memex_web/live/context_live/show.html.heex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,41 @@ | |||||||
|  | <h1>Show Context</h1> | ||||||
|  |  | ||||||
|  | <%= if @live_action in [:edit] do %> | ||||||
|  |   <.modal return_to={Routes.context_show_path(@socket, :show, @context)}> | ||||||
|  |     <.live_component | ||||||
|  |       module={MemexWeb.ContextLive.FormComponent} | ||||||
|  |       id={@context.id} | ||||||
|  |       title={@page_title} | ||||||
|  |       action={@live_action} | ||||||
|  |       context={@context} | ||||||
|  |       return_to={Routes.context_show_path(@socket, :show, @context)} | ||||||
|  |     /> | ||||||
|  |   </.modal> | ||||||
|  | <% end %> | ||||||
|  |  | ||||||
|  | <ul> | ||||||
|  |  | ||||||
|  |   <li> | ||||||
|  |     <strong>Title:</strong> | ||||||
|  |     <%= @context.title %> | ||||||
|  |   </li> | ||||||
|  |  | ||||||
|  |   <li> | ||||||
|  |     <strong>Content:</strong> | ||||||
|  |     <%= @context.content %> | ||||||
|  |   </li> | ||||||
|  |  | ||||||
|  |   <li> | ||||||
|  |     <strong>Tag:</strong> | ||||||
|  |     <%= @context.tag %> | ||||||
|  |   </li> | ||||||
|  |  | ||||||
|  |   <li> | ||||||
|  |     <strong>Visibility:</strong> | ||||||
|  |     <%= @context.visibility %> | ||||||
|  |   </li> | ||||||
|  |  | ||||||
|  | </ul> | ||||||
|  |  | ||||||
|  | <span><%= live_patch "Edit", to: Routes.context_show_path(@socket, :edit, @context), class: "button" %></span> | | ||||||
|  | <span><%= live_redirect "Back", to: Routes.context_index_path(@socket, :index) %></span> | ||||||
| @@ -59,6 +59,10 @@ defmodule MemexWeb.Router do | |||||||
|  |  | ||||||
|       live "/notes", NoteLive.Index, :index |       live "/notes", NoteLive.Index, :index | ||||||
|       live "/notes/:id", NoteLive.Show, :show |       live "/notes/:id", NoteLive.Show, :show | ||||||
|  |  | ||||||
|  |       live "/contexts", ContextLive.Index, :index | ||||||
|  |       live "/contexts/:id", ContextLive.Show, :show | ||||||
|  |  | ||||||
|     end |     end | ||||||
|  |  | ||||||
|     scope "/", MemexWeb do |     scope "/", MemexWeb do | ||||||
| @@ -68,6 +72,10 @@ defmodule MemexWeb.Router do | |||||||
|       live "/notes/:id/edit", NoteLive.Index, :edit |       live "/notes/:id/edit", NoteLive.Index, :edit | ||||||
|       live "/notes/:id/show/edit", NoteLive.Show, :edit |       live "/notes/:id/show/edit", NoteLive.Show, :edit | ||||||
|  |  | ||||||
|  |       live "/contexts/new", ContextLive.Index, :new | ||||||
|  |       live "/contexts/:id/edit", ContextLive.Index, :edit | ||||||
|  |       live "/contexts/:id/show/edit", ContextLive.Show, :edit | ||||||
|  |  | ||||||
|       get "/users/settings", UserSettingsController, :edit |       get "/users/settings", UserSettingsController, :edit | ||||||
|       put "/users/settings", UserSettingsController, :update |       put "/users/settings", UserSettingsController, :update | ||||||
|       delete "/users/settings/:id", UserSettingsController, :delete |       delete "/users/settings/:id", UserSettingsController, :delete | ||||||
|   | |||||||
							
								
								
									
										15
									
								
								priv/repo/migrations/20220726001039_create_contexts.exs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								priv/repo/migrations/20220726001039_create_contexts.exs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | |||||||
|  | defmodule Memex.Repo.Migrations.CreateContexts do | ||||||
|  |   use Ecto.Migration | ||||||
|  |  | ||||||
|  |   def change do | ||||||
|  |     create table(:contexts, primary_key: false) do | ||||||
|  |       add :id, :binary_id, primary_key: true | ||||||
|  |       add :title, :string | ||||||
|  |       add :content, :text | ||||||
|  |       add :tag, {:array, :string} | ||||||
|  |       add :visibility, :string | ||||||
|  |  | ||||||
|  |       timestamps() | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | end | ||||||
							
								
								
									
										65
									
								
								test/memex/contexts_test.exs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								test/memex/contexts_test.exs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,65 @@ | |||||||
|  | defmodule Memex.ContextsTest do | ||||||
|  |   use Memex.DataCase | ||||||
|  |  | ||||||
|  |   alias Memex.Contexts | ||||||
|  |  | ||||||
|  |   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] | ||||||
|  |     end | ||||||
|  |  | ||||||
|  |     test "get_context!/1 returns the context with given id" do | ||||||
|  |       context = context_fixture() | ||||||
|  |       assert Contexts.get_context!(context.id) == context | ||||||
|  |     end | ||||||
|  |  | ||||||
|  |     test "create_context/1 with valid data creates a context" do | ||||||
|  |       valid_attrs = %{content: "some content", tag: [], title: "some title", visibility: :public} | ||||||
|  |  | ||||||
|  |       assert {:ok, %Context{} = context} = Contexts.create_context(valid_attrs) | ||||||
|  |       assert context.content == "some content" | ||||||
|  |       assert context.tag == [] | ||||||
|  |       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) | ||||||
|  |     end | ||||||
|  |  | ||||||
|  |     test "update_context/2 with valid data updates the context" do | ||||||
|  |       context = context_fixture() | ||||||
|  |       update_attrs = %{content: "some updated content", tag: [], title: "some updated title", visibility: :private} | ||||||
|  |  | ||||||
|  |       assert {:ok, %Context{} = context} = Contexts.update_context(context, update_attrs) | ||||||
|  |       assert context.content == "some updated content" | ||||||
|  |       assert context.tag == [] | ||||||
|  |       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) | ||||||
|  |     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 | ||||||
|  |     end | ||||||
|  |  | ||||||
|  |     test "change_context/1 returns a context changeset" do | ||||||
|  |       context = context_fixture() | ||||||
|  |       assert %Ecto.Changeset{} = Contexts.change_context(context) | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | end | ||||||
							
								
								
									
										110
									
								
								test/memex_web/live/context_live_test.exs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										110
									
								
								test/memex_web/live/context_live_test.exs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,110 @@ | |||||||
|  | defmodule MemexWeb.ContextLiveTest do | ||||||
|  |   use MemexWeb.ConnCase | ||||||
|  |  | ||||||
|  |   import Phoenix.LiveViewTest | ||||||
|  |   import Memex.ContextsFixtures | ||||||
|  |  | ||||||
|  |   @create_attrs %{content: "some content", tag: [], title: "some title", visibility: :public} | ||||||
|  |   @update_attrs %{content: "some updated content", tag: [], title: "some updated title", visibility: :private} | ||||||
|  |   @invalid_attrs %{content: nil, tag: [], title: nil, visibility: nil} | ||||||
|  |  | ||||||
|  |   defp create_context(_) do | ||||||
|  |     context = context_fixture() | ||||||
|  |     %{context: context} | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   describe "Index" do | ||||||
|  |     setup [: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 =~ context.content | ||||||
|  |     end | ||||||
|  |  | ||||||
|  |     test "saves new context", %{conn: conn} do | ||||||
|  |       {:ok, index_live, _html} = live(conn, Routes.context_index_path(conn, :index)) | ||||||
|  |  | ||||||
|  |       assert index_live |> element("a", "New Context") |> render_click() =~ | ||||||
|  |                "New Context" | ||||||
|  |  | ||||||
|  |       assert_patch(index_live, Routes.context_index_path(conn, :new)) | ||||||
|  |  | ||||||
|  |       assert index_live | ||||||
|  |              |> form("#context-form", context: @invalid_attrs) | ||||||
|  |              |> render_change() =~ "can't be blank" | ||||||
|  |  | ||||||
|  |       {:ok, _, html} = | ||||||
|  |         index_live | ||||||
|  |         |> form("#context-form", context: @create_attrs) | ||||||
|  |         |> render_submit() | ||||||
|  |         |> follow_redirect(conn, Routes.context_index_path(conn, :index)) | ||||||
|  |  | ||||||
|  |       assert html =~ "Context created successfully" | ||||||
|  |       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_patch(index_live, Routes.context_index_path(conn, :edit, context)) | ||||||
|  |  | ||||||
|  |       assert index_live | ||||||
|  |              |> form("#context-form", context: @invalid_attrs) | ||||||
|  |              |> render_change() =~ "can't be blank" | ||||||
|  |  | ||||||
|  |       {:ok, _, html} = | ||||||
|  |         index_live | ||||||
|  |         |> form("#context-form", context: @update_attrs) | ||||||
|  |         |> render_submit() | ||||||
|  |         |> follow_redirect(conn, Routes.context_index_path(conn, :index)) | ||||||
|  |  | ||||||
|  |       assert html =~ "Context updated successfully" | ||||||
|  |       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() | ||||||
|  |       refute has_element?(index_live, "#context-#{context.id}") | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   describe "Show" do | ||||||
|  |     setup [:create_context] | ||||||
|  |  | ||||||
|  |     test "displays context", %{conn: conn, context: context} do | ||||||
|  |       {:ok, _show_live, html} = live(conn, Routes.context_show_path(conn, :show, context)) | ||||||
|  |  | ||||||
|  |       assert html =~ "Show Context" | ||||||
|  |       assert html =~ context.content | ||||||
|  |     end | ||||||
|  |  | ||||||
|  |     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_patch(show_live, Routes.context_show_path(conn, :edit, context)) | ||||||
|  |  | ||||||
|  |       assert show_live | ||||||
|  |              |> form("#context-form", context: @invalid_attrs) | ||||||
|  |              |> render_change() =~ "can't be blank" | ||||||
|  |  | ||||||
|  |       {:ok, _, html} = | ||||||
|  |         show_live | ||||||
|  |         |> form("#context-form", context: @update_attrs) | ||||||
|  |         |> render_submit() | ||||||
|  |         |> follow_redirect(conn, Routes.context_show_path(conn, :show, context)) | ||||||
|  |  | ||||||
|  |       assert html =~ "Context updated successfully" | ||||||
|  |       assert html =~ "some updated content" | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | end | ||||||
							
								
								
									
										23
									
								
								test/support/fixtures/contexts_fixtures.ex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								test/support/fixtures/contexts_fixtures.ex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | |||||||
|  | defmodule Memex.ContextsFixtures do | ||||||
|  |   @moduledoc """ | ||||||
|  |   This module defines test helpers for creating | ||||||
|  |   entities via the `Memex.Contexts` context. | ||||||
|  |   """ | ||||||
|  |  | ||||||
|  |   @doc """ | ||||||
|  |   Generate a context. | ||||||
|  |   """ | ||||||
|  |   def context_fixture(attrs \\ %{}) do | ||||||
|  |     {:ok, context} = | ||||||
|  |       attrs | ||||||
|  |       |> Enum.into(%{ | ||||||
|  |         content: "some content", | ||||||
|  |         tag: [], | ||||||
|  |         title: "some title", | ||||||
|  |         visibility: :public | ||||||
|  |       }) | ||||||
|  |       |> Memex.Contexts.create_context() | ||||||
|  |  | ||||||
|  |     context | ||||||
|  |   end | ||||||
|  | end | ||||||
		Reference in New Issue
	
	Block a user