Remove ecto repo, add writing to file
This commit is contained in:
parent
386a331956
commit
ce463329f6
20 changed files with 98 additions and 122 deletions
37
test/birdy_chat/message_writer_test.exs
Normal file
37
test/birdy_chat/message_writer_test.exs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
defmodule BirdyChat.MessageWriterTest do
|
||||
use BirdyChat.DataCase, async: true
|
||||
|
||||
describe "write/2" do
|
||||
setup do
|
||||
message = %{to: "123-user", from: "456-user", message: "hello!"}
|
||||
folder = System.tmp_dir!()
|
||||
path = Path.join([folder, "123-user.txt"])
|
||||
|
||||
on_exit(fn -> File.rm!(path) end)
|
||||
|
||||
%{message: message, folder: folder, path: path}
|
||||
end
|
||||
|
||||
test "writes message to a temporary file if a given folder", %{
|
||||
message: message,
|
||||
folder: folder,
|
||||
path: path
|
||||
} do
|
||||
result = BirdyChat.MessageWriter.write(folder, message)
|
||||
assert result == :ok
|
||||
contents = File.read!(path)
|
||||
assert contents == "456-user: hello!\n"
|
||||
end
|
||||
|
||||
test "Appends subsequent messages to a file", %{
|
||||
message: message,
|
||||
folder: folder,
|
||||
path: path
|
||||
} do
|
||||
_result = BirdyChat.MessageWriter.write(folder, message)
|
||||
_result = BirdyChat.MessageWriter.write(folder, message)
|
||||
contents = File.read!(path)
|
||||
assert contents == "456-user: hello!\n456-user: hello!\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -4,6 +4,9 @@ defmodule BirdyChatWeb.Api.MessagesTest do
|
|||
setup %{conn: conn} do
|
||||
url = ~p"/api/messages"
|
||||
|
||||
path = Application.app_dir(:birdy_chat, ["priv", "messages", "test-user.txt"])
|
||||
on_exit(fn -> File.rm(path) end)
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|
|
@ -28,13 +31,39 @@ defmodule BirdyChatWeb.Api.MessagesTest do
|
|||
assert result == expected_result
|
||||
end
|
||||
|
||||
test "returns message and ok when successful", %{conn: conn, url: url} do
|
||||
test "returns message and 201 when successful", %{conn: conn, url: url} do
|
||||
message = %{"from" => "2-user", "to" => "1-user", "message" => "123"}
|
||||
payload = Jason.encode!(message)
|
||||
conn = post(conn, url, payload)
|
||||
assert result = json_response(conn, :created)
|
||||
|
||||
assert result == message
|
||||
end
|
||||
|
||||
test "writes message to file", %{conn: conn, url: url} do
|
||||
message = %{"from" => "2-user", "to" => "test-user", "message" => "123"}
|
||||
payload = Jason.encode!(message)
|
||||
conn = post(conn, url, payload)
|
||||
assert result = json_response(conn, :created)
|
||||
assert result == message
|
||||
|
||||
path = Application.app_dir(:birdy_chat, ["priv", "messages", "test-user.txt"])
|
||||
contents = File.read!(path)
|
||||
assert contents == "2-user: 123\n"
|
||||
end
|
||||
|
||||
test "appends message to file", %{conn: conn, url: url} do
|
||||
message = %{"from" => "2-user", "to" => "test-user", "message" => "123"}
|
||||
payload = Jason.encode!(message)
|
||||
post(conn, url, payload)
|
||||
|
||||
message = %{"from" => "2-user", "to" => "test-user", "message" => "456"}
|
||||
payload = Jason.encode!(message)
|
||||
conn = post(conn, url, payload)
|
||||
assert json_response(conn, :created)
|
||||
|
||||
path = Application.app_dir(:birdy_chat, ["priv", "messages", "test-user.txt"])
|
||||
contents = File.read!(path)
|
||||
assert contents == "2-user: 123\n2-user: 456\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -31,8 +31,7 @@ defmodule BirdyChatWeb.ConnCase do
|
|||
end
|
||||
end
|
||||
|
||||
setup tags do
|
||||
BirdyChat.DataCase.setup_sandbox(tags)
|
||||
setup _tags do
|
||||
{:ok, conn: Phoenix.ConnTest.build_conn()}
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -18,8 +18,6 @@ defmodule BirdyChat.DataCase do
|
|||
|
||||
using do
|
||||
quote do
|
||||
alias BirdyChat.Repo
|
||||
|
||||
import Ecto
|
||||
import Ecto.Changeset
|
||||
import Ecto.Query
|
||||
|
|
@ -27,19 +25,10 @@ defmodule BirdyChat.DataCase do
|
|||
end
|
||||
end
|
||||
|
||||
setup tags do
|
||||
BirdyChat.DataCase.setup_sandbox(tags)
|
||||
setup _tags do
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sets up the sandbox based on the test tags.
|
||||
"""
|
||||
def setup_sandbox(tags) do
|
||||
pid = Ecto.Adapters.SQL.Sandbox.start_owner!(BirdyChat.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.
|
||||
|
||||
|
|
|
|||
|
|
@ -1,2 +1 @@
|
|||
ExUnit.start()
|
||||
Ecto.Adapters.SQL.Sandbox.mode(BirdyChat.Repo, :manual)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue