Skip to content

Commit

Permalink
fix & add tests, add earmark markdown rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
fschoenfeldt committed Jul 4, 2024
1 parent ea75dd1 commit 885c018
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 35 deletions.
1 change: 1 addition & 0 deletions .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
~r"/deps/",
~r"/node_modules/",
"lib/fotohaecker_web/live/photo_live",
"lib/fotohaecker_web/live/recipe_live",
"lib/fotohaecker/release.ex",
"lib/mix/tasks"
]
Expand Down
14 changes: 7 additions & 7 deletions lib/fotohaecker_web/live/photo_live/show.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule FotohaeckerWeb.PhotoLive.Show do
alias Fotohaecker.UserManagement
use FotohaeckerWeb, :live_view

alias Fotohaecker.Content
alias Fotohaecker.Content.Photo
alias Fotohaecker.UserManagement

defmodule PhotoNotFoundError do
defexception message: dgettext("errors", "photo not found"), plug_status: 404
Expand All @@ -16,19 +16,19 @@ defmodule FotohaeckerWeb.PhotoLive.Show do
end

@impl true
def handle_params(%{"id" => id}, _what, socket) do
def handle_params(%{"id" => id}, _uri, socket) do
case Content.get_photo(id, [:recipe]) do
%Content.Photo{} = photo ->
nil ->
raise PhotoNotFoundError
{:noreply, socket}

photo ->
{:noreply,
socket
|> assign(:page_title, photo.title)
|> assign(:photo, photo)
# TODO: is this good?
|> assign(:show_delete_photo_confirmation_modal, false)}

nil ->
raise PhotoNotFoundError
{:noreply, socket}
end
end

Expand Down
44 changes: 27 additions & 17 deletions lib/fotohaecker_web/live/recipe_live/show.ex
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
defmodule FotohaeckerWeb.RecipeLive.Show do
use FotohaeckerWeb, :live_view

alias Fotohaecker.Content.Photo
alias Fotohaecker.Content.Recipe
alias Fotohaecker.Content
alias FotohaeckerWeb.IndexLive.Home.PhotoComponent

use FotohaeckerWeb, :live_view
defmodule RecipeNotFoundError do
defexception message: dgettext("errors", "recipe not found"), plug_status: 404
end

def mount(_params, _session, socket) do
{:ok, socket}
end

def handle_params(%{"id" => id}, _uri, socket) do
case Content.get_recipe(id, [
:photos
]) do
nil ->
raise RecipeNotFoundError
{:noreply, socket}

def mount(params, _session, socket) do
# FIXME: error handing 404 when recipe not found
recipe =
Content.get_recipe!(params["id"], [
:photos
])
recipe ->
{:ok, description_as_ast, _whatever} = Earmark.as_html(recipe.description)

{:ok, assign(socket, recipe: recipe)}
{
:noreply,
assign(socket, recipe: recipe, description_as_ast: description_as_ast)
}
end
end

def render(assigns) do
~H"""
<%!-- # FIXME: think about tailwind typography --%>
<div class="space-y-2 p-4">
<article class="prose">
<h1><%= gettext("Recipe") %>: <%= @recipe.title %></h1>
<%!-- # FIXME: render markdown! --%>
<p><%= @recipe.description %></p>
<p><%= Phoenix.HTML.raw(@description_as_ast) %></p>
<h2><%= gettext("Settings") %>:</h2>
<.recipe_settings recipe={@recipe} />
</article>
Expand Down Expand Up @@ -57,8 +71,7 @@ defmodule FotohaeckerWeb.RecipeLive.Show do

defp render_map(map) when is_map(map) do
content =
map
|> Enum.map(fn {key, value} ->
Enum.map_join(map, fn {key, value} ->
"<tr>" <>
"<td>#{key}</td>" <>
"<td>" <>
Expand All @@ -70,21 +83,18 @@ defmodule FotohaeckerWeb.RecipeLive.Show do
"</td>" <>
"</tr>"
end)
|> Enum.join()

"<table>" <> content <> "</table>"
end

defp render_list(list) when is_list(list) do
list
|> Enum.map(fn item ->
Enum.map_join(list, ",", fn item ->
if is_map(item) do
render_map(item)
else
to_string(item)
end
end)
|> Enum.join(", ")
end

# TODO: DRY: dirty
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ defmodule Fotohaecker.MixProject do
{:stripity_stripe, "~> 2.0"},
{:cors_plug, "~> 3.0"},
{:phoenix_swagger, "~> 0.8"},
{:ex_json_schema, "~> 0.5"}
{:ex_json_schema, "~> 0.5"},
{:earmark, "~> 1.4"}
]
end

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"},
"dialyxir": {:hex, :dialyxir, "1.4.3", "edd0124f358f0b9e95bfe53a9fcf806d615d8f838e2202a9f430d59566b6b53b", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "bf2cfb75cd5c5006bec30141b131663299c661a864ec7fbbc72dfa557487a986"},
"doctor": {:hex, :doctor, "0.21.0", "20ef89355c67778e206225fe74913e96141c4d001cb04efdeba1a2a9704f1ab5", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "a227831daa79784eb24cdeedfa403c46a4cb7d0eab0e31232ec654314447e4e0"},
"earmark": {:hex, :earmark, "1.4.46", "8c7287bd3137e99d26ae4643e5b7ef2129a260e3dcf41f251750cb4563c8fb81", [:mix], [], "hexpm", "798d86db3d79964e759ddc0c077d5eb254968ed426399fbf5a62de2b5ff8910a"},
"earmark_parser": {:hex, :earmark_parser, "1.4.39", "424642f8335b05bb9eb611aa1564c148a8ee35c9c8a8bba6e129d51a3e3c6769", [:mix], [], "hexpm", "06553a88d1f1846da9ef066b87b57c6f605552cfbe40d20bd8d59cc6bde41944"},
"ecto": {:hex, :ecto, "3.11.2", "e1d26be989db350a633667c5cda9c3d115ae779b66da567c68c80cfb26a8c9ee", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3c38bca2c6f8d8023f2145326cc8a80100c3ffe4dcbd9842ff867f7fc6156c65"},
"ecto_sql": {:hex, :ecto_sql, "3.11.1", "e9abf28ae27ef3916b43545f9578b4750956ccea444853606472089e7d169470", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.11.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16.0 or ~> 0.17.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ce14063ab3514424276e7e360108ad6c2308f6d88164a076aac8a387e1fea634"},
Expand Down
7 changes: 6 additions & 1 deletion test/fotohaecker/content_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,12 @@ defmodule Fotohaecker.ContentTest do
end

test "create_recipe/1 with valid data creates a recipe" do
valid_attrs = %{title: "some title", user_id: "some user_id", settings: %{}}
valid_attrs = %{
title: "some title",
brand: "some brand",
user_id: "some user_id",
settings: %{}
}

assert {:ok, %Recipe{} = recipe} = Content.create_recipe(valid_attrs)
assert recipe.title == "some title"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ defmodule Fotohaecker.RecipeManagement.JsonRecipeManagement.JsonRecipeImporterTe
}}
] = JsonRecipeImporter.import_recipes_from_directory(test_directory)

assert file_path == "test/support/fixtures/content/recipes/valid/kodak_royal_gold_400.json"
assert title == "Kodak Royal Gold 400"
assert user_id == "auth0|613000000000000000000000"
assert file_path == "test/support/fixtures/content/recipes/valid/valid_recipe.json"
assert title == "Valid Recipe Title"
assert user_id == "auth0|1234567890"

assert settings == %{
"base_simulation" => "Classic Negative",
"highlight" => -1,
"shadow" => 1
"compatible" => ["x_trans_iv", "x_trans_v"]
}
end

Expand Down
4 changes: 3 additions & 1 deletion test/fotohaecker_web/live/photo_live/show_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule FotohaeckerWeb.PhotoLive.ShowTest do

import Phoenix.ConnTest
import Phoenix.LiveViewTest

alias FotohaeckerWeb.PhotoLive.Show.PhotoNotFoundError
@endpoint FotohaeckerWeb.Endpoint

test "disconnected mount works", %{conn: conn} do
Expand Down Expand Up @@ -64,7 +66,7 @@ defmodule FotohaeckerWeb.PhotoLive.ShowTest do
end

test "throws error if photo not found", %{conn: conn} do
assert_raise(FotohaeckerWeb.PhotoLive.Show.PhotoNotFoundError, fn ->
assert_raise(PhotoNotFoundError, fn ->
{:ok, _view, _html} = live(conn, "/fh/en_US/photos/12345")
end)
end
Expand Down
18 changes: 18 additions & 0 deletions test/fotohaecker_web/live/recipe_live/show_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule FotohaeckerWeb.RecipeLive.ShowTest do
use FotohaeckerWeb.ConnCase, async: true

import Phoenix.ConnTest
import Phoenix.LiveViewTest

alias FotohaeckerWeb.RecipeLive.Show.RecipeNotFoundError

@endpoint FotohaeckerWeb.Endpoint

# FIXME: add tests

test "throws error if recipe not found", %{conn: conn} do
assert_raise(RecipeNotFoundError, fn ->
{:ok, _view, _html} = live(conn, "/fh/en_US/recipes/12345")
end)
end
end
7 changes: 4 additions & 3 deletions test/support/fixtures/content_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ defmodule Fotohaecker.ContentFixtures do
{:ok, recipe} =
attrs
|> Enum.into(%{
title: "any title",
brand: "any brand",
user_id: "some user_id",
settings: %{
"compatible" => "xtrans-iv",
"base_simulation" => "Classic Negative",
"highlight" => -1,
"shadow" => 1
},
title: "Kodak Royal Gold 400",
user_id: "some user_id"
}
})
|> Fotohaecker.Content.create_recipe()

Expand Down

0 comments on commit 885c018

Please sign in to comment.