init commit
This commit is contained in:
48
test/diffuser/generator/prompt_request_genserver_test.exs
Normal file
48
test/diffuser/generator/prompt_request_genserver_test.exs
Normal file
@@ -0,0 +1,48 @@
|
||||
defmodule Diffuser.Generator.PromptRequestTest do
|
||||
use Diffuser.DataCase
|
||||
import Diffuser.GeneratorFixtures
|
||||
alias Diffuser.Generator
|
||||
alias Diffuser.Generator.PromptRequest
|
||||
alias DiffuserWeb.Endpoint
|
||||
|
||||
import Phoenix.ChannelTest
|
||||
|
||||
# The default endpoint for testing
|
||||
# @endpoint DiffuserWeb.Endpoint
|
||||
|
||||
# TODO: Write mock that calls python script that only sleeps for 300 milliseconds
|
||||
test "starts a new prompt and can query status while it's running" do
|
||||
prompt_request = prompt_request_fixture()
|
||||
|
||||
assert {:ok, _pid} =
|
||||
Diffuser.Generator.PromptRequestGenserver.new(%{prompt_request: prompt_request})
|
||||
|
||||
:timer.sleep(1000)
|
||||
|
||||
assert %{status: "in_progress"} = Generator.get_prompt_request!(prompt_request.id)
|
||||
|
||||
:timer.sleep(3000)
|
||||
|
||||
assert %{status: "finished"} = Diffuser.Generator.get_prompt_request!(prompt_request.id)
|
||||
end
|
||||
|
||||
test "properly updates when prompt is finished via websocket" do
|
||||
%{id: id} = prompt_request = prompt_request_fixture()
|
||||
|
||||
Endpoint.subscribe("request:#{id}")
|
||||
|
||||
Diffuser.Generator.PromptRequestGenserver.new(%{prompt_request: prompt_request})
|
||||
|
||||
:timer.sleep(1000)
|
||||
|
||||
assert_broadcast("request", %{
|
||||
prompt_request: %PromptRequest{id: ^id}
|
||||
})
|
||||
|
||||
:timer.sleep(3000)
|
||||
|
||||
assert_broadcast("request", %{
|
||||
prompt_request: %PromptRequest{id: ^id}
|
||||
})
|
||||
end
|
||||
end
|
92
test/diffuser/generator_test.exs
Normal file
92
test/diffuser/generator_test.exs
Normal file
@@ -0,0 +1,92 @@
|
||||
defmodule Diffuser.GeneratorTest do
|
||||
use Diffuser.DataCase
|
||||
|
||||
alias Diffuser.Generator
|
||||
alias Diffuser.Test.FileHelper
|
||||
alias Diffuser.Repo
|
||||
|
||||
describe "prompt_requests" do
|
||||
alias Diffuser.Generator.PromptRequest
|
||||
|
||||
import Diffuser.GeneratorFixtures
|
||||
|
||||
@invalid_attrs %{prompt: nil}
|
||||
|
||||
test "list_prompt_requests/0 returns all prompt_requests" do
|
||||
prompt_request = prompt_request_fixture()
|
||||
assert Generator.list_prompt_requests() == [prompt_request]
|
||||
end
|
||||
|
||||
test "get_prompt_request!/1 returns the prompt_request with given id" do
|
||||
prompt_request = prompt_request_fixture()
|
||||
assert Generator.get_prompt_request!(prompt_request.id) == prompt_request
|
||||
end
|
||||
|
||||
test "create_prompt_request/1 with valid data creates a prompt_request" do
|
||||
valid_attrs = %{prompt: "some prompt"}
|
||||
|
||||
assert {:ok, %PromptRequest{} = prompt_request} =
|
||||
Generator.create_prompt_request(valid_attrs)
|
||||
|
||||
assert prompt_request.prompt == "some prompt"
|
||||
end
|
||||
|
||||
test "create_prompt_request/1 with invalid data returns error changeset" do
|
||||
assert {:error, %Ecto.Changeset{}} = Generator.create_prompt_request(@invalid_attrs)
|
||||
end
|
||||
|
||||
test "update_prompt_request/2 with valid data updates the prompt_request" do
|
||||
prompt_request = prompt_request_fixture()
|
||||
|
||||
update_attrs = %{
|
||||
prompt: "some updated prompt"
|
||||
}
|
||||
|
||||
assert {:ok, %PromptRequest{} = prompt_request} =
|
||||
Generator.update_prompt_request(prompt_request, update_attrs)
|
||||
|
||||
assert prompt_request.prompt == "some updated prompt"
|
||||
end
|
||||
|
||||
test "update_prompt_request/2 with invalid data returns error changeset" do
|
||||
prompt_request = prompt_request_fixture()
|
||||
|
||||
assert {:error, %Ecto.Changeset{}} =
|
||||
Generator.update_prompt_request(prompt_request, @invalid_attrs)
|
||||
|
||||
assert prompt_request == Generator.get_prompt_request!(prompt_request.id)
|
||||
end
|
||||
|
||||
test "delete_prompt_request/1 deletes the prompt_request" do
|
||||
prompt_request = prompt_request_fixture()
|
||||
assert {:ok, %PromptRequest{}} = Generator.delete_prompt_request(prompt_request)
|
||||
assert_raise Ecto.NoResultsError, fn -> Generator.get_prompt_request!(prompt_request.id) end
|
||||
end
|
||||
|
||||
test "change_prompt_request/1 returns a prompt_request changeset" do
|
||||
prompt_request = prompt_request_fixture()
|
||||
assert %Ecto.Changeset{} = Generator.change_prompt_request(prompt_request)
|
||||
end
|
||||
|
||||
test "begin_prompt_request/1 returns pid of a prompt request generator" do
|
||||
prompt_request = prompt_request_fixture()
|
||||
assert {:ok, _pid} = Generator.begin_prompt_request(prompt_request)
|
||||
end
|
||||
|
||||
test "create_prompt_request_results/2 properly associates images to prompt request" do
|
||||
%{id: prompt_request_id} = prompt_request_fixture()
|
||||
|
||||
images = [
|
||||
%{file_name: "cat.jpg", binary: FileHelper.binary_image()},
|
||||
%{file_name: "dog.jpg", binary: FileHelper.binary_image()},
|
||||
%{file_name: "horse.jpg", binary: FileHelper.binary_image()}
|
||||
]
|
||||
|
||||
Generator.create_prompt_request_results(prompt_request_id, images)
|
||||
|
||||
%PromptRequest{images: images} = Generator.get_prompt_request!(prompt_request_id)
|
||||
assert images |> length() > 0
|
||||
# assert images |> Enum.all?
|
||||
end
|
||||
end
|
||||
end
|
8
test/diffuser_web/controllers/page_controller_test.exs
Normal file
8
test/diffuser_web/controllers/page_controller_test.exs
Normal file
@@ -0,0 +1,8 @@
|
||||
defmodule DiffuserWeb.PageControllerTest do
|
||||
use DiffuserWeb.ConnCase
|
||||
|
||||
test "GET /", %{conn: conn} do
|
||||
conn = get(conn, "/")
|
||||
assert html_response(conn, 200) =~ "Welcome to Phoenix!"
|
||||
end
|
||||
end
|
110
test/diffuser_web/live/prompt_request_live_test.exs
Normal file
110
test/diffuser_web/live/prompt_request_live_test.exs
Normal file
@@ -0,0 +1,110 @@
|
||||
defmodule DiffuserWeb.PromptRequestLiveTest do
|
||||
use DiffuserWeb.ConnCase
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
import Diffuser.GeneratorFixtures
|
||||
|
||||
@create_attrs %{prompt: "some prompt"}
|
||||
@update_attrs %{prompt: "some updated prompt"}
|
||||
@invalid_attrs %{prompt: nil}
|
||||
|
||||
defp create_prompt_request(_) do
|
||||
prompt_request = prompt_request_fixture()
|
||||
%{prompt_request: prompt_request}
|
||||
end
|
||||
|
||||
describe "Index" do
|
||||
setup [:create_prompt_request]
|
||||
|
||||
test "lists all prompt_requests", %{conn: conn, prompt_request: prompt_request} do
|
||||
{:ok, _index_live, html} = live(conn, Routes.prompt_request_index_path(conn, :index))
|
||||
|
||||
assert html =~ "Listing Prompt requests"
|
||||
assert html =~ prompt_request.prompt
|
||||
end
|
||||
|
||||
test "saves new prompt_request", %{conn: conn} do
|
||||
{:ok, index_live, _html} = live(conn, Routes.prompt_request_index_path(conn, :index))
|
||||
|
||||
assert index_live |> element("a", "New Prompt request") |> render_click() =~
|
||||
"New Prompt request"
|
||||
|
||||
assert_patch(index_live, Routes.prompt_request_index_path(conn, :new))
|
||||
|
||||
assert index_live
|
||||
|> form("#prompt_request-form", prompt_request: @invalid_attrs)
|
||||
|> render_change() =~ "can't be blank"
|
||||
|
||||
{:ok, _, html} =
|
||||
index_live
|
||||
|> form("#prompt_request-form", prompt_request: @create_attrs)
|
||||
|> render_submit()
|
||||
|> follow_redirect(conn, Routes.prompt_request_index_path(conn, :index))
|
||||
|
||||
assert html =~ "Prompt request created successfully"
|
||||
assert html =~ "some prompt"
|
||||
end
|
||||
|
||||
test "updates prompt_request in listing", %{conn: conn, prompt_request: prompt_request} do
|
||||
{:ok, index_live, _html} = live(conn, Routes.prompt_request_index_path(conn, :index))
|
||||
|
||||
assert index_live |> element("#prompt_request-#{prompt_request.id} a", "Edit") |> render_click() =~
|
||||
"Edit Prompt request"
|
||||
|
||||
assert_patch(index_live, Routes.prompt_request_index_path(conn, :edit, prompt_request))
|
||||
|
||||
assert index_live
|
||||
|> form("#prompt_request-form", prompt_request: @invalid_attrs)
|
||||
|> render_change() =~ "can't be blank"
|
||||
|
||||
{:ok, _, html} =
|
||||
index_live
|
||||
|> form("#prompt_request-form", prompt_request: @update_attrs)
|
||||
|> render_submit()
|
||||
|> follow_redirect(conn, Routes.prompt_request_index_path(conn, :index))
|
||||
|
||||
assert html =~ "Prompt request updated successfully"
|
||||
assert html =~ "some updated prompt"
|
||||
end
|
||||
|
||||
test "deletes prompt_request in listing", %{conn: conn, prompt_request: prompt_request} do
|
||||
{:ok, index_live, _html} = live(conn, Routes.prompt_request_index_path(conn, :index))
|
||||
|
||||
assert index_live |> element("#prompt_request-#{prompt_request.id} a", "Delete") |> render_click()
|
||||
refute has_element?(index_live, "#prompt_request-#{prompt_request.id}")
|
||||
end
|
||||
end
|
||||
|
||||
describe "Show" do
|
||||
setup [:create_prompt_request]
|
||||
|
||||
test "displays prompt_request", %{conn: conn, prompt_request: prompt_request} do
|
||||
{:ok, _show_live, html} = live(conn, Routes.prompt_request_show_path(conn, :show, prompt_request))
|
||||
|
||||
assert html =~ "Show Prompt request"
|
||||
assert html =~ prompt_request.prompt
|
||||
end
|
||||
|
||||
test "updates prompt_request within modal", %{conn: conn, prompt_request: prompt_request} do
|
||||
{:ok, show_live, _html} = live(conn, Routes.prompt_request_show_path(conn, :show, prompt_request))
|
||||
|
||||
assert show_live |> element("a", "Edit") |> render_click() =~
|
||||
"Edit Prompt request"
|
||||
|
||||
assert_patch(show_live, Routes.prompt_request_show_path(conn, :edit, prompt_request))
|
||||
|
||||
assert show_live
|
||||
|> form("#prompt_request-form", prompt_request: @invalid_attrs)
|
||||
|> render_change() =~ "can't be blank"
|
||||
|
||||
{:ok, _, html} =
|
||||
show_live
|
||||
|> form("#prompt_request-form", prompt_request: @update_attrs)
|
||||
|> render_submit()
|
||||
|> follow_redirect(conn, Routes.prompt_request_show_path(conn, :show, prompt_request))
|
||||
|
||||
assert html =~ "Prompt request updated successfully"
|
||||
assert html =~ "some updated prompt"
|
||||
end
|
||||
end
|
||||
end
|
14
test/diffuser_web/views/error_view_test.exs
Normal file
14
test/diffuser_web/views/error_view_test.exs
Normal file
@@ -0,0 +1,14 @@
|
||||
defmodule DiffuserWeb.ErrorViewTest do
|
||||
use DiffuserWeb.ConnCase, async: true
|
||||
|
||||
# Bring render/3 and render_to_string/3 for testing custom views
|
||||
import Phoenix.View
|
||||
|
||||
test "renders 404.html" do
|
||||
assert render_to_string(DiffuserWeb.ErrorView, "404.html", []) == "Not Found"
|
||||
end
|
||||
|
||||
test "renders 500.html" do
|
||||
assert render_to_string(DiffuserWeb.ErrorView, "500.html", []) == "Internal Server Error"
|
||||
end
|
||||
end
|
8
test/diffuser_web/views/layout_view_test.exs
Normal file
8
test/diffuser_web/views/layout_view_test.exs
Normal file
@@ -0,0 +1,8 @@
|
||||
defmodule DiffuserWeb.LayoutViewTest do
|
||||
use DiffuserWeb.ConnCase, async: true
|
||||
|
||||
# When testing helpers, you may want to import Phoenix.HTML and
|
||||
# use functions such as safe_to_string() to convert the helper
|
||||
# result into an HTML string.
|
||||
# import Phoenix.HTML
|
||||
end
|
3
test/diffuser_web/views/page_view_test.exs
Normal file
3
test/diffuser_web/views/page_view_test.exs
Normal file
@@ -0,0 +1,3 @@
|
||||
defmodule DiffuserWeb.PageViewTest do
|
||||
use DiffuserWeb.ConnCase, async: true
|
||||
end
|
BIN
test/fixtures/image.png
vendored
Normal file
BIN
test/fixtures/image.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.4 KiB |
38
test/support/conn_case.ex
Normal file
38
test/support/conn_case.ex
Normal file
@@ -0,0 +1,38 @@
|
||||
defmodule DiffuserWeb.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 DiffuserWeb.ConnCase, async: true`, although
|
||||
this option is not recommended for other databases.
|
||||
"""
|
||||
|
||||
use ExUnit.CaseTemplate
|
||||
|
||||
using do
|
||||
quote do
|
||||
# Import conveniences for testing with connections
|
||||
import Plug.Conn
|
||||
import Phoenix.ConnTest
|
||||
import DiffuserWeb.ConnCase
|
||||
|
||||
alias DiffuserWeb.Router.Helpers, as: Routes
|
||||
|
||||
# The default endpoint for testing
|
||||
@endpoint DiffuserWeb.Endpoint
|
||||
end
|
||||
end
|
||||
|
||||
setup tags do
|
||||
Diffuser.DataCase.setup_sandbox(tags)
|
||||
{:ok, conn: Phoenix.ConnTest.build_conn()}
|
||||
end
|
||||
end
|
58
test/support/data_case.ex
Normal file
58
test/support/data_case.ex
Normal file
@@ -0,0 +1,58 @@
|
||||
defmodule Diffuser.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 Diffuser.DataCase, async: true`, although
|
||||
this option is not recommended for other databases.
|
||||
"""
|
||||
|
||||
use ExUnit.CaseTemplate
|
||||
|
||||
using do
|
||||
quote do
|
||||
alias Diffuser.Repo
|
||||
|
||||
import Ecto
|
||||
import Ecto.Changeset
|
||||
import Ecto.Query
|
||||
import Diffuser.DataCase
|
||||
end
|
||||
end
|
||||
|
||||
setup tags do
|
||||
Diffuser.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!(Diffuser.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
|
14
test/support/file_helper.ex
Normal file
14
test/support/file_helper.ex
Normal file
@@ -0,0 +1,14 @@
|
||||
defmodule Diffuser.Test.FileHelper do
|
||||
def base64_image() do
|
||||
binary_image() |> Base.encode64()
|
||||
end
|
||||
|
||||
def binary_image() do
|
||||
"#{__DIR__}/../fixtures/image.png"
|
||||
|> File.read!()
|
||||
end
|
||||
|
||||
def base64_image(_), do: {:ok, image: base64_image()}
|
||||
|
||||
def image_path(), do: "#{__DIR__}/../fixtures/image.png"
|
||||
end
|
20
test/support/fixtures/generator_fixtures.ex
Normal file
20
test/support/fixtures/generator_fixtures.ex
Normal file
@@ -0,0 +1,20 @@
|
||||
defmodule Diffuser.GeneratorFixtures do
|
||||
@moduledoc """
|
||||
This module defines test helpers for creating
|
||||
entities via the `Diffuser.Generator` context.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Generate a prompt_request.
|
||||
"""
|
||||
def prompt_request_fixture(attrs \\ %{}) do
|
||||
{:ok, prompt_request} =
|
||||
attrs
|
||||
|> Enum.into(%{
|
||||
prompt: "some prompt"
|
||||
})
|
||||
|> Diffuser.Generator.create_prompt_request()
|
||||
|
||||
prompt_request
|
||||
end
|
||||
end
|
2
test/test_helper.exs
Normal file
2
test/test_helper.exs
Normal file
@@ -0,0 +1,2 @@
|
||||
ExUnit.start()
|
||||
Ecto.Adapters.SQL.Sandbox.mode(Diffuser.Repo, :manual)
|
Reference in New Issue
Block a user