init commit

This commit is contained in:
2024-07-13 15:12:19 -04:00
commit 8668405143
61 changed files with 3123 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
defmodule HereIAm.DevicesTest do
use HereIAm.DataCase
alias HereIAm.Devices
describe "devices" do
alias HereIAm.Devices.Device
import HereIAm.DevicesFixtures
@invalid_attrs %{ip_address: nil, audio: nil}
test "list_devices/0 returns all devices" do
device = device_fixture()
assert Devices.list_devices() == [device]
end
test "get_device!/1 returns the device with given id" do
device = device_fixture()
assert Devices.get_device!(device.id) == device
end
test "create_device/1 with valid data creates a device" do
valid_attrs = %{ip_address: "some ip_address", audio: "some audio"}
assert {:ok, %Device{} = device} = Devices.create_device(valid_attrs)
assert device.ip_address == "some ip_address"
assert device.audio == "some audio"
end
test "create_device/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Devices.create_device(@invalid_attrs)
end
test "update_device/2 with valid data updates the device" do
device = device_fixture()
update_attrs = %{ip_address: "some updated ip_address", audio: "some updated audio"}
assert {:ok, %Device{} = device} = Devices.update_device(device, update_attrs)
assert device.ip_address == "some updated ip_address"
assert device.audio == "some updated audio"
end
test "update_device/2 with invalid data returns error changeset" do
device = device_fixture()
assert {:error, %Ecto.Changeset{}} = Devices.update_device(device, @invalid_attrs)
assert device == Devices.get_device!(device.id)
end
test "delete_device/1 deletes the device" do
device = device_fixture()
assert {:ok, %Device{}} = Devices.delete_device(device)
assert_raise Ecto.NoResultsError, fn -> Devices.get_device!(device.id) end
end
test "change_device/1 returns a device changeset" do
device = device_fixture()
assert %Ecto.Changeset{} = Devices.change_device(device)
end
end
end

View File

@@ -0,0 +1,14 @@
defmodule HereIAmWeb.ErrorHTMLTest do
use HereIAmWeb.ConnCase, async: true
# Bring render_to_string/4 for testing custom views
import Phoenix.Template
test "renders 404.html" do
assert render_to_string(HereIAmWeb.ErrorHTML, "404", "html", []) == "Not Found"
end
test "renders 500.html" do
assert render_to_string(HereIAmWeb.ErrorHTML, "500", "html", []) == "Internal Server Error"
end
end

View File

@@ -0,0 +1,12 @@
defmodule HereIAmWeb.ErrorJSONTest do
use HereIAmWeb.ConnCase, async: true
test "renders 404" do
assert HereIAmWeb.ErrorJSON.render("404.json", %{}) == %{errors: %{detail: "Not Found"}}
end
test "renders 500" do
assert HereIAmWeb.ErrorJSON.render("500.json", %{}) ==
%{errors: %{detail: "Internal Server Error"}}
end
end

View File

@@ -0,0 +1,8 @@
defmodule HereIAmWeb.PageControllerTest do
use HereIAmWeb.ConnCase
test "GET /", %{conn: conn} do
conn = get(conn, ~p"/")
assert html_response(conn, 200) =~ "Peace of mind from prototype to production"
end
end

View File

@@ -0,0 +1,113 @@
defmodule HereIAmWeb.DeviceLiveTest do
use HereIAmWeb.ConnCase
import Phoenix.LiveViewTest
import HereIAm.DevicesFixtures
@create_attrs %{ip_address: "some ip_address", audio: "some audio"}
@update_attrs %{ip_address: "some updated ip_address", audio: "some updated audio"}
@invalid_attrs %{ip_address: nil, audio: nil}
defp create_device(_) do
device = device_fixture()
%{device: device}
end
describe "Index" do
setup [:create_device]
test "lists all devices", %{conn: conn, device: device} do
{:ok, _index_live, html} = live(conn, ~p"/devices")
assert html =~ "Listing Devices"
assert html =~ device.ip_address
end
test "saves new device", %{conn: conn} do
{:ok, index_live, _html} = live(conn, ~p"/devices")
assert index_live |> element("a", "New Device") |> render_click() =~
"New Device"
assert_patch(index_live, ~p"/devices/new")
assert index_live
|> form("#device-form", device: @invalid_attrs)
|> render_change() =~ "can't be blank"
assert index_live
|> form("#device-form", device: @create_attrs)
|> render_submit()
assert_patch(index_live, ~p"/devices")
html = render(index_live)
assert html =~ "Device created successfully"
assert html =~ "some ip_address"
end
test "updates device in listing", %{conn: conn, device: device} do
{:ok, index_live, _html} = live(conn, ~p"/devices")
assert index_live |> element("#devices-#{device.id} a", "Edit") |> render_click() =~
"Edit Device"
assert_patch(index_live, ~p"/devices/#{device}/edit")
assert index_live
|> form("#device-form", device: @invalid_attrs)
|> render_change() =~ "can't be blank"
assert index_live
|> form("#device-form", device: @update_attrs)
|> render_submit()
assert_patch(index_live, ~p"/devices")
html = render(index_live)
assert html =~ "Device updated successfully"
assert html =~ "some updated ip_address"
end
test "deletes device in listing", %{conn: conn, device: device} do
{:ok, index_live, _html} = live(conn, ~p"/devices")
assert index_live |> element("#devices-#{device.id} a", "Delete") |> render_click()
refute has_element?(index_live, "#devices-#{device.id}")
end
end
describe "Show" do
setup [:create_device]
test "displays device", %{conn: conn, device: device} do
{:ok, _show_live, html} = live(conn, ~p"/devices/#{device}")
assert html =~ "Show Device"
assert html =~ device.ip_address
end
test "updates device within modal", %{conn: conn, device: device} do
{:ok, show_live, _html} = live(conn, ~p"/devices/#{device}")
assert show_live |> element("a", "Edit") |> render_click() =~
"Edit Device"
assert_patch(show_live, ~p"/devices/#{device}/show/edit")
assert show_live
|> form("#device-form", device: @invalid_attrs)
|> render_change() =~ "can't be blank"
assert show_live
|> form("#device-form", device: @update_attrs)
|> render_submit()
assert_patch(show_live, ~p"/devices/#{device}")
html = render(show_live)
assert html =~ "Device updated successfully"
assert html =~ "some updated ip_address"
end
end
end

38
test/support/conn_case.ex Normal file
View File

@@ -0,0 +1,38 @@
defmodule HereIAmWeb.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,
we enable the SQL sandbox, so changes done to the database
are reverted at the end of every test. If you are using
PostgreSQL, you can even run database tests asynchronously
by setting `use HereIAmWeb.ConnCase, async: true`, although
this option is not recommended for other databases.
"""
use ExUnit.CaseTemplate
using do
quote do
# The default endpoint for testing
@endpoint HereIAmWeb.Endpoint
use HereIAmWeb, :verified_routes
# Import conveniences for testing with connections
import Plug.Conn
import Phoenix.ConnTest
import HereIAmWeb.ConnCase
end
end
setup tags do
HereIAm.DataCase.setup_sandbox(tags)
{:ok, conn: Phoenix.ConnTest.build_conn()}
end
end

58
test/support/data_case.ex Normal file
View File

@@ -0,0 +1,58 @@
defmodule HereIAm.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,
we enable the SQL sandbox, so changes done to the database
are reverted at the end of every test. If you are using
PostgreSQL, you can even run database tests asynchronously
by setting `use HereIAm.DataCase, async: true`, although
this option is not recommended for other databases.
"""
use ExUnit.CaseTemplate
using do
quote do
alias HereIAm.Repo
import Ecto
import Ecto.Changeset
import Ecto.Query
import HereIAm.DataCase
end
end
setup tags do
HereIAm.DataCase.setup_sandbox(tags)
:ok
end
@doc """
Sets up the sandbox based on the test tags.
"""
def setup_sandbox(tags) do
pid = Ecto.Adapters.SQL.Sandbox.start_owner!(HereIAm.Repo, shared: not tags[:async])
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
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

@@ -0,0 +1,21 @@
defmodule HereIAm.DevicesFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `HereIAm.Devices` context.
"""
@doc """
Generate a device.
"""
def device_fixture(attrs \\ %{}) do
{:ok, device} =
attrs
|> Enum.into(%{
audio: "some audio",
ip_address: "some ip_address"
})
|> HereIAm.Devices.create_device()
device
end
end

2
test/test_helper.exs Normal file
View File

@@ -0,0 +1,2 @@
ExUnit.start()
Ecto.Adapters.SQL.Sandbox.mode(HereIAm.Repo, :manual)