testing, and also support for org-babel-detangle

main
Ryan Rix 2020-08-09 21:11:17 -07:00
parent 402354838d
commit 401d049b02
10 changed files with 181 additions and 130 deletions

6
.gitignore vendored
View File

@ -1,3 +1,4 @@
# [[file:phoenix.org::*Project =.gitignore= file][Project =.gitignore= file:1]]
# elixir and mix outputs
/_build/
/cover/
@ -17,7 +18,9 @@ lipu_kasi-*.tar
# The directory NPM downloads your dependencies sources to.
/assets/node_modules/
/priv/static/
# Project =.gitignore= file:1 ends here
# [[file:phoenix.org::*Project =.gitignore= file][Project =.gitignore= file:2]]
/mix.exs
# /mix.lock is checked in.
.formatter.exs
@ -38,7 +41,10 @@ lipu_kasi-*.tar
/lib/lipu_kasi_web/templates/page/
/lib/lipu_kasi_web/templates/layout/*.html.eex
/test/support/
# Assets
/assets/js/app.js
/assets/js/foundation.js
/assets/js/sw.js
# Project =.gitignore= file:2 ends here

View File

@ -9,6 +9,7 @@ Lipu Kasi is [[https://en.wikipedia.org/wiki/Tokipona][Tokipona]] for Plant Diar
- [[file:lipu_kasi_web.org][§Lipu Kasi Phoenix Web Frontend]]
- [[file:aesthetics.org][§The Visual Identity of Lipu Kasi]]
- [[file:page_controller.org][§Controller for static Lipu Kasi pages]]
- [[file:testing.org][§Lipu Kasi Test Support]]
* Building

View File

@ -1 +1,5 @@
(load-file "~/org/cce/packaging.el")
(require 'elixir-mode)
(require 'ob)
(add-to-list 'org-babel-default-header-args '(:comments . "link"))

View File

@ -10,7 +10,9 @@ It uses the helpers from [[file:lipu_kasi_web.org][LipuKasiWeb]].
defmodule LipuKasiWeb.PageController do
use LipuKasiWeb, :controller
<<controller-index>>
def index(conn, _params) do
render(conn, "index.html")
end
end
#+end_src
@ -75,3 +77,16 @@ This is the default template. Someday I'll have something more interesting to sa
#+end_src
* Tests
This is the default test, it's important for me to explain test well, I have the power of text and interlinking and I want to show that I can tell compelling stories with this programming methodology. These tests could and should be interspersed in the functionality itself.
#+begin_src elixir :tangle test/lipu_kasi_web/controllers/page_controller_test.exs :mkdirp yes
defmodule LipuKasiWeb.PageControllerTest do
use LipuKasiWeb.ConnCase
test "GET /", %{conn: conn} do
conn = get(conn, "/")
assert html_response(conn, 200) =~ "Welcome to Phoenix!"
end
end
#+end_src

View File

@ -8,7 +8,7 @@ This project is intended to be developed as a [[file:../literate_programming.org
The repo will contain docs and assets and a build script that will call up an [[file:../Emacs.org][§Emacs]] that will "tangle" the files out and build the application. It will *not* contain the compiled code checked in, the docs must be considered the source of truth. We'll have facilities to "de-tangle" the source code eventually to aide contribution from folks who aren't running Emacs. For now, though, I'm gonna be a bastard 😄
This starts with a .gitignore, =phx.new= provides one that I will expand.
This starts with a .gitignore, =phx.new= provides one that I will expand. When updating this, the order of operation is to delete the file from the index, update this, tangle it, and then commit. it's a bit of a mess, maybe detangling is better.
#+begin_src conf :tangle .gitignore
# elixir and mix outputs
@ -55,6 +55,8 @@ Files which are provided from tangles will need to be managed here:
/lib/lipu_kasi_web/templates/page/
/lib/lipu_kasi_web/templates/layout/*.html.eex
/test/support/
# Assets
/assets/js/app.js
/assets/js/foundation.js

View File

@ -1,37 +0,0 @@
defmodule LipuKasiWeb.ChannelCase do
@moduledoc """
This module defines the test case to be used by
channel tests.
Such tests rely on `Phoenix.ChannelTest` and also
import other functionality to make it easier
to build common data structures and query the data layer.
Finally, if the test case interacts with the database,
it cannot be async. For this reason, every test runs
inside a transaction which is reset at the beginning
of the test unless the test case is marked as async.
"""
use ExUnit.CaseTemplate
using do
quote do
# Import conveniences for testing with channels
use Phoenix.ChannelTest
# The default endpoint for testing
@endpoint LipuKasiWeb.Endpoint
end
end
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(LipuKasi.Repo)
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(LipuKasi.Repo, {:shared, self()})
end
:ok
end
end

View File

@ -1,38 +0,0 @@
defmodule LipuKasiWeb.ConnCase do
@moduledoc """
This module defines the test case to be used by
tests that require setting up a connection.
Such tests rely on `Phoenix.ConnTest` and also
import other functionality to make it easier
to build common data structures and query the data layer.
Finally, if the test case interacts with the database,
it cannot be async. For this reason, every test runs
inside a transaction which is reset at the beginning
of the test unless the test case is marked as async.
"""
use ExUnit.CaseTemplate
using do
quote do
# Import conveniences for testing with connections
use Phoenix.ConnTest
alias LipuKasiWeb.Router.Helpers, as: Routes
# The default endpoint for testing
@endpoint LipuKasiWeb.Endpoint
end
end
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(LipuKasi.Repo)
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(LipuKasi.Repo, {:shared, self()})
end
{:ok, conn: Phoenix.ConnTest.build_conn()}
end
end

View File

@ -1,53 +0,0 @@
defmodule LipuKasi.DataCase do
@moduledoc """
This module defines the setup for tests requiring
access to the application's data layer.
You may define functions here to be used as helpers in
your tests.
Finally, if the test case interacts with the database,
it cannot be async. For this reason, every test runs
inside a transaction which is reset at the beginning
of the test unless the test case is marked as async.
"""
use ExUnit.CaseTemplate
using do
quote do
alias LipuKasi.Repo
import Ecto
import Ecto.Changeset
import Ecto.Query
import LipuKasi.DataCase
end
end
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(LipuKasi.Repo)
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(LipuKasi.Repo, {:shared, self()})
end
:ok
end
@doc """
A helper that transforms changeset errors into a map of messages.
assert {:error, changeset} = Accounts.create_user(%{password: "short"})
assert "password is too short" in errors_on(changeset).password
assert %{password: ["password is too short"]} = errors_on(changeset)
"""
def errors_on(changeset) do
Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
Regex.replace(~r"%{(\w+)}", message, fn _, key ->
opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
end)
end)
end
end

View File

@ -1,2 +1,4 @@
# [[file:../testing.org][No heading:1]]
ExUnit.start()
Ecto.Adapters.SQL.Sandbox.mode(LipuKasi.Repo, :manual)
# No heading:1 ends here

149
testing.org Normal file
View File

@ -0,0 +1,149 @@
#+TITLE: Lipu Kasi Test Support
#+begin_src elixir :tangle test/test_helper.exs
ExUnit.start()
Ecto.Adapters.SQL.Sandbox.mode(LipuKasi.Repo, :manual)
#+end_src
* Support for Connection Cases
#+begin_src elixir :tangle test/support/conn_case.ex :mkdirp yes
defmodule LipuKasiWeb.ConnCase do
@moduledoc """
This module defines the test case to be used by
tests that require setting up a connection.
Such tests rely on `Phoenix.ConnTest` and also
import other functionality to make it easier
to build common data structures and query the data layer.
Finally, if the test case interacts with the database,
it cannot be async. For this reason, every test runs
inside a transaction which is reset at the beginning
of the test unless the test case is marked as async.
"""
use ExUnit.CaseTemplate
using do
quote do
# Import conveniences for testing with connections
use Phoenix.ConnTest
alias LipuKasiWeb.Router.Helpers, as: Routes
# The default endpoint for testing
@endpoint LipuKasiWeb.Endpoint
end
end
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(LipuKasi.Repo)
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(LipuKasi.Repo, {:shared, self()})
end
{:ok, conn: Phoenix.ConnTest.build_conn()}
end
end
#+end_src
* Support for Model Tests
#+begin_src elixir :tangle test/support/data_case.ex :mkdirp yes
defmodule LipuKasi.DataCase do
@moduledoc """
This module defines the setup for tests requiring
access to the application's data layer.
You may define functions here to be used as helpers in
your tests.
Finally, if the test case interacts with the database,
it cannot be async. For this reason, every test runs
inside a transaction which is reset at the beginning
of the test unless the test case is marked as async.
"""
use ExUnit.CaseTemplate
using do
quote do
alias LipuKasi.Repo
import Ecto
import Ecto.Changeset
import Ecto.Query
import LipuKasi.DataCase
end
end
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(LipuKasi.Repo)
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(LipuKasi.Repo, {:shared, self()})
end
:ok
end
@doc """
A helper that transforms changeset errors into a map of messages.
assert {:error, changeset} = Accounts.create_user(%{password: "short"})
assert "password is too short" in errors_on(changeset).password
assert %{password: ["password is too short"]} = errors_on(changeset)
"""
def errors_on(changeset) do
Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
Regex.replace(~r"%{(\w+)}", message, fn _, key ->
opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
end)
end)
end
end
#+end_src
* Support for testing channels
#+begin_src elixir :tangle test/support/channel_case.ex :mkdirp yes
defmodule LipuKasiWeb.ChannelCase do
@moduledoc """
This module defines the test case to be used by
channel tests.
Such tests rely on `Phoenix.ChannelTest` and also
import other functionality to make it easier
to build common data structures and query the data layer.
Finally, if the test case interacts with the database,
it cannot be async. For this reason, every test runs
inside a transaction which is reset at the beginning
of the test unless the test case is marked as async.
"""
use ExUnit.CaseTemplate
using do
quote do
# Import conveniences for testing with channels
use Phoenix.ChannelTest
# The default endpoint for testing
@endpoint LipuKasiWeb.Endpoint
end
end
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(LipuKasi.Repo)
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(LipuKasi.Repo, {:shared, self()})
end
:ok
end
end
#+end_src