diff --git a/config/config.exs b/config/config.exs index de360fe..51096d6 100644 --- a/config/config.exs +++ b/config/config.exs @@ -8,7 +8,6 @@ import Config config :birdy_chat, - ecto_repos: [BirdyChat.Repo], generators: [timestamp_type: :utc_datetime] # Configure the endpoint diff --git a/config/dev.exs b/config/dev.exs index 7e10187..5138034 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -1,15 +1,5 @@ import Config -# Configure your database -config :birdy_chat, BirdyChat.Repo, - username: "postgres", - password: "postgres", - hostname: "localhost", - database: "birdy_chat_dev", - stacktrace: true, - show_sensitive_data_on_connection_error: true, - pool_size: 10 - # For development, we disable any cache and enable # debugging and code reloading. # diff --git a/config/runtime.exs b/config/runtime.exs index b411336..26eec65 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -24,23 +24,6 @@ config :birdy_chat, BirdyChatWeb.Endpoint, http: [port: String.to_integer(System.get_env("PORT", "4000"))] if config_env() == :prod do - database_url = - System.get_env("DATABASE_URL") || - raise """ - environment variable DATABASE_URL is missing. - For example: ecto://USER:PASS@HOST/DATABASE - """ - - maybe_ipv6 = if System.get_env("ECTO_IPV6") in ~w(true 1), do: [:inet6], else: [] - - config :birdy_chat, BirdyChat.Repo, - # ssl: true, - url: database_url, - pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"), - # For machines with several cores, consider starting multiple pools of `pool_size` - # pool_count: 4, - socket_options: maybe_ipv6 - # The secret key base is used to sign/encrypt cookies and other secrets. # A default value is used in config/dev.exs and config/test.exs but you # want to use a different value for prod and you most likely don't want diff --git a/config/test.exs b/config/test.exs index 30fda0f..3d83e54 100644 --- a/config/test.exs +++ b/config/test.exs @@ -1,18 +1,5 @@ import Config -# Configure your database -# -# The MIX_TEST_PARTITION environment variable can be used -# to provide built-in test partitioning in CI environment. -# Run `mix help test` for more information. -config :birdy_chat, BirdyChat.Repo, - username: "postgres", - password: "postgres", - hostname: "localhost", - database: "birdy_chat_test#{System.get_env("MIX_TEST_PARTITION")}", - pool: Ecto.Adapters.SQL.Sandbox, - pool_size: System.schedulers_online() * 2 - # We don't run a server during test. If one is required, # you can enable the server option below. config :birdy_chat, BirdyChatWeb.Endpoint, diff --git a/lib/birdy_chat/application.ex b/lib/birdy_chat/application.ex index f53b284..11c9fcb 100644 --- a/lib/birdy_chat/application.ex +++ b/lib/birdy_chat/application.ex @@ -5,14 +5,11 @@ defmodule BirdyChat.Application do use Application - @env Mix.env - @impl true def start(_type, _args) do children = [ BirdyChatWeb.Telemetry, - BirdyChat.Repo, - {BirdyChat.Identity, env: @env}, + BirdyChat.Identity, {DNSCluster, query: Application.get_env(:birdy_chat, :dns_cluster_query) || :ignore}, {Phoenix.PubSub, name: BirdyChat.PubSub}, # Start a worker by calling: BirdyChat.Worker.start_link(arg) diff --git a/lib/birdy_chat/identity.ex b/lib/birdy_chat/identity.ex index caf8c54..080386b 100644 --- a/lib/birdy_chat/identity.ex +++ b/lib/birdy_chat/identity.ex @@ -32,8 +32,7 @@ defmodule BirdyChat.Identity do def parse_peers(peers) do values = String.split(peers, ";") - for value <- values, - [identity, address] = String.split(value, "::"), into: %{} do + for value <- values, [identity, address] = String.split(value, "::"), into: %{} do {identity, address} end end diff --git a/lib/birdy_chat/message.ex b/lib/birdy_chat/message.ex index 43efc56..3c86d43 100644 --- a/lib/birdy_chat/message.ex +++ b/lib/birdy_chat/message.ex @@ -19,28 +19,4 @@ defmodule BirdyChat.Message do {:error, changeset} end end - - def write(%{to: to, from: from, message: message} = msg) do - message_to_write = "#{from}: #{message}\n" - :ok = create_messages_folder!(Application.app_dir(:birdy_chat, ["priv", "messages"])) - path = Application.app_dir(:birdy_chat, ["priv", "messages", "#{to}.txt"]) - result = File.write!(path, message_to_write, [:append]) - {:ok, msg} - end - - def create_messages_folder!(path) do - case File.mkdir(path) do - :ok -> - :ok - - {:error, :eexist} -> - :ok - - {:error, reason} -> - raise File.Error, - reason: reason, - action: "make directory", - path: IO.chardata_to_string(path) - end - end end diff --git a/lib/birdy_chat/message_writer.ex b/lib/birdy_chat/message_writer.ex new file mode 100644 index 0000000..b6f53aa --- /dev/null +++ b/lib/birdy_chat/message_writer.ex @@ -0,0 +1,16 @@ +defmodule BirdyChat.MessageWriter do + @moduledoc false + + @spec write(%{to: String.t(), from: String.t(), message: String.t()}) :: :ok + def write(message) do + path = Application.app_dir(:birdy_chat, ["priv", "messages"]) + write(path, message) + end + + @spec write(String.t(), %{to: String.t(), from: String.t(), message: String.t()}) :: :ok + def write(path, %{to: to, from: from, message: message}) do + message_to_write = "#{from}: #{message}\n" + path = Path.join([path, "#{to}.txt"]) + :ok = File.write!(path, message_to_write, [:append]) + end +end diff --git a/lib/birdy_chat/repo.ex b/lib/birdy_chat/repo.ex deleted file mode 100644 index 4dcf4f0..0000000 --- a/lib/birdy_chat/repo.ex +++ /dev/null @@ -1,5 +0,0 @@ -defmodule BirdyChat.Repo do - use Ecto.Repo, - otp_app: :birdy_chat, - adapter: Ecto.Adapters.Postgres -end diff --git a/lib/birdy_chat_web/api/messages/controller.ex b/lib/birdy_chat_web/api/messages/controller.ex index 7c6531a..79afd64 100644 --- a/lib/birdy_chat_web/api/messages/controller.ex +++ b/lib/birdy_chat_web/api/messages/controller.ex @@ -4,11 +4,11 @@ defmodule BirdyChatWeb.Api.Messages.Controller do def create(conn, params) do case BirdyChat.Message.validate(params) do {:ok, changeset} -> - case BirdyChat.Message.write(changeset.changes) do - {:ok, msg} -> + case BirdyChat.MessageWriter.write(changeset.changes) do + :ok -> conn |> put_status(:created) - |> render(:create, message: msg) + |> render(:create, message: changeset.changes) end {:error, changeset} -> diff --git a/lib/birdy_chat_web/endpoint.ex b/lib/birdy_chat_web/endpoint.ex index 2bbcf36..72a9ccf 100644 --- a/lib/birdy_chat_web/endpoint.ex +++ b/lib/birdy_chat_web/endpoint.ex @@ -33,7 +33,6 @@ defmodule BirdyChatWeb.Endpoint do socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket plug Phoenix.LiveReloader plug Phoenix.CodeReloader - plug Phoenix.Ecto.CheckRepoStatus, otp_app: :birdy_chat end plug Phoenix.LiveDashboard.RequestLogger, diff --git a/mix.exs b/mix.exs index 4235528..10def11 100644 --- a/mix.exs +++ b/mix.exs @@ -42,8 +42,6 @@ defmodule BirdyChat.MixProject do [ {:phoenix, "~> 1.8.3"}, {:phoenix_ecto, "~> 4.5"}, - {:ecto_sql, "~> 3.13"}, - {:postgrex, ">= 0.0.0"}, {:phoenix_html, "~> 4.1"}, {:phoenix_live_reload, "~> 1.2", only: :dev}, {:phoenix_live_view, "~> 1.1.0"}, @@ -67,9 +65,12 @@ defmodule BirdyChat.MixProject do {:dns_cluster, "~> 0.2.0"}, {:bandit, "~> 1.5"}, + # Phoenix sockets client + {:slipstream, "~> 1.0"}, + # Telemetry {:opentelemetry, "~> 1.0"}, - {:opentelemetry_exporter, "~> 1.0"}, + {:opentelemetry_exporter, "~> 1.0"} ] end @@ -81,10 +82,7 @@ defmodule BirdyChat.MixProject do # See the documentation for `Mix` for more info on aliases. defp aliases do [ - setup: ["deps.get", "ecto.setup", "assets.setup", "assets.build"], - "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"], - "ecto.reset": ["ecto.drop", "ecto.setup"], - test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"], + setup: ["deps.get", "assets.setup", "assets.build"], "assets.setup": ["tailwind.install --if-missing", "esbuild.install --if-missing"], "assets.build": ["compile", "tailwind birdy_chat", "esbuild birdy_chat"], "assets.deploy": [ diff --git a/mix.lock b/mix.lock index 642040c..be8d276 100644 --- a/mix.lock +++ b/mix.lock @@ -4,11 +4,9 @@ "cc_precompiler": {:hex, :cc_precompiler, "0.1.11", "8c844d0b9fb98a3edea067f94f616b3f6b29b959b6b3bf25fee94ffe34364768", [:mix], [{:elixir_make, "~> 0.7", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "3427232caf0835f94680e5bcf082408a70b48ad68a5f5c0b02a3bea9f3a075b9"}, "chatterbox": {:hex, :ts_chatterbox, "0.15.1", "5cac4d15dd7ad61fc3c4415ce4826fc563d4643dee897a558ec4ea0b1c835c9c", [:rebar3], [{:hpack, "~> 0.3.0", [hex: :hpack_erl, repo: "hexpm", optional: false]}], "hexpm", "4f75b91451338bc0da5f52f3480fa6ef6e3a2aeecfc33686d6b3d0a0948f31aa"}, "ctx": {:hex, :ctx, "0.6.0", "8ff88b70e6400c4df90142e7f130625b82086077a45364a78d208ed3ed53c7fe", [:rebar3], [], "hexpm", "a14ed2d1b67723dbebbe423b28d7615eb0bdcba6ff28f2d1f1b0a7e1d4aa5fc2"}, - "db_connection": {:hex, :db_connection, "2.9.0", "a6a97c5c958a2d7091a58a9be40caf41ab496b0701d21e1d1abff3fa27a7f371", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "17d502eacaf61829db98facf6f20808ed33da6ccf495354a41e64fe42f9c509c"}, "decimal": {:hex, :decimal, "2.3.0", "3ad6255aa77b4a3c4f818171b12d237500e63525c2fd056699967a3e7ea20f62", [:mix], [], "hexpm", "a4d66355cb29cb47c3cf30e71329e58361cfcb37c34235ef3bf1d7bf3773aeac"}, "dns_cluster": {:hex, :dns_cluster, "0.2.0", "aa8eb46e3bd0326bd67b84790c561733b25c5ba2fe3c7e36f28e88f384ebcb33", [:mix], [], "hexpm", "ba6f1893411c69c01b9e8e8f772062535a4cf70f3f35bcc964a324078d8c8240"}, "ecto": {:hex, :ecto, "3.13.5", "9d4a69700183f33bf97208294768e561f5c7f1ecf417e0fa1006e4a91713a834", [: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", "df9efebf70cf94142739ba357499661ef5dbb559ef902b68ea1f3c1fabce36de"}, - "ecto_sql": {:hex, :ecto_sql, "3.13.4", "b6e9d07557ddba62508a9ce4a484989a5bb5e9a048ae0e695f6d93f095c25d60", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.13.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.7", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.19 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", "2b38cf0749ca4d1c5a8bcbff79bbe15446861ca12a61f9fba604486cb6b62a14"}, "elixir_make": {:hex, :elixir_make, "0.9.0", "6484b3cd8c0cee58f09f05ecaf1a140a8c97670671a6a0e7ab4dc326c3109726", [:mix], [], "hexpm", "db23d4fd8b757462ad02f8aa73431a426fe6671c80b200d9710caf3d1dd0ffdb"}, "esbuild": {:hex, :esbuild, "0.10.0", "b0aa3388a1c23e727c5a3e7427c932d89ee791746b0081bbe56103e9ef3d291f", [:mix], [{:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "468489cda427b974a7cc9f03ace55368a83e1a7be12fba7e30969af78e5f8c70"}, "expo": {:hex, :expo, "1.1.1", "4202e1d2ca6e2b3b63e02f69cfe0a404f77702b041d02b58597c00992b601db5", [:mix], [], "hexpm", "5fb308b9cb359ae200b7e23d37c76978673aa1b06e2b3075d814ce12c5811640"}, @@ -26,6 +24,7 @@ "lazy_html": {:hex, :lazy_html, "0.1.10", "ffe42a0b4e70859cf21a33e12a251e0c76c1dff76391609bd56702a0ef5bc429", [:make, :mix], [{:cc_precompiler, "~> 0.1", [hex: :cc_precompiler, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.9.0", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:fine, "~> 0.1.0", [hex: :fine, repo: "hexpm", optional: false]}], "hexpm", "50f67e5faa09d45a99c1ddf3fac004f051997877dc8974c5797bb5ccd8e27058"}, "mime": {:hex, :mime, "2.0.7", "b8d739037be7cd402aee1ba0306edfdef982687ee7e9859bee6198c1e7e2f128", [:mix], [], "hexpm", "6171188e399ee16023ffc5b76ce445eb6d9672e2e241d2df6050f3c771e80ccd"}, "mint": {:hex, :mint, "1.7.1", "113fdb2b2f3b59e47c7955971854641c61f378549d73e829e1768de90fc1abf1", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1 or ~> 0.2.0 or ~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "fceba0a4d0f24301ddee3024ae116df1c3f4bb7a563a731f45fdfeb9d39a231b"}, + "mint_web_socket": {:hex, :mint_web_socket, "1.0.5", "60354efeb49b1eccf95dfb75f55b08d692e211970fe735a5eb3188b328be2a90", [:mix], [{:mint, ">= 1.4.1 and < 2.0.0-0", [hex: :mint, repo: "hexpm", optional: false]}], "hexpm", "04b35663448fc758f3356cce4d6ac067ca418bbafe6972a3805df984b5f12e61"}, "nimble_options": {:hex, :nimble_options, "1.1.1", "e3a492d54d85fc3fd7c5baf411d9d2852922f66e69476317787a7b2bb000a61b", [:mix], [], "hexpm", "821b2470ca9442c4b6984882fe9bb0389371b8ddec4d45a9504f00a66f650b44"}, "nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"}, "opentelemetry": {:hex, :opentelemetry, "1.7.0", "20d0f12d3d1c398d3670fd44fd1a7c495dd748ab3e5b692a7906662e2fb1a38a", [:rebar3], [{:opentelemetry_api, "~> 1.5.0", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}], "hexpm", "a9173b058c4549bf824cbc2f1d2fa2adc5cdedc22aa3f0f826951187bbd53131"}, @@ -41,8 +40,8 @@ "phoenix_template": {:hex, :phoenix_template, "1.0.4", "e2092c132f3b5e5b2d49c96695342eb36d0ed514c5b252a77048d5969330d639", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "2c0c81f0e5c6753faf5cca2f229c9709919aba34fab866d3bc05060c9c444206"}, "plug": {:hex, :plug, "1.19.1", "09bac17ae7a001a68ae393658aa23c7e38782be5c5c00c80be82901262c394c0", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "560a0017a8f6d5d30146916862aaf9300b7280063651dd7e532b8be168511e62"}, "plug_crypto": {:hex, :plug_crypto, "2.1.1", "19bda8184399cb24afa10be734f84a16ea0a2bc65054e23a62bb10f06bc89491", [:mix], [], "hexpm", "6470bce6ffe41c8bd497612ffde1a7e4af67f36a15eea5f921af71cf3e11247c"}, - "postgrex": {:hex, :postgrex, "0.22.0", "fb027b58b6eab1f6de5396a2abcdaaeb168f9ed4eccbb594e6ac393b02078cbd", [:mix], [{:db_connection, "~> 2.9", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "a68c4261e299597909e03e6f8ff5a13876f5caadaddd0d23af0d0a61afcc5d84"}, "req": {:hex, :req, "0.5.17", "0096ddd5b0ed6f576a03dde4b158a0c727215b15d2795e59e0916c6971066ede", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.17", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 2.0.6 or ~> 2.1", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "0b8bc6ffdfebbc07968e59d3ff96d52f2202d0536f10fef4dc11dc02a2a43e39"}, + "slipstream": {:hex, :slipstream, "1.2.2", "6b07124ac5f62a50327aa38c84edd0284920ac8aba548e04738827838f233ed0", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mint_web_socket, "~> 0.2 or ~> 1.0", [hex: :mint_web_socket, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.1 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ccb873ddb21aadb37c5c7745014febe6da0aa2cef0c4e73e7d08ce11d18aacd0"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"}, "swoosh": {:hex, :swoosh, "1.22.0", "0d65a95f89aedb5011af13295742294e309b4b4aaca556858d81e3b372b58abc", [:mix], [{:bandit, ">= 1.0.0", [hex: :bandit, repo: "hexpm", optional: true]}, {:cowboy, "~> 1.1 or ~> 2.4", [hex: :cowboy, repo: "hexpm", optional: true]}, {:ex_aws, "~> 2.1", [hex: :ex_aws, repo: "hexpm", optional: true]}, {:finch, "~> 0.6", [hex: :finch, repo: "hexpm", optional: true]}, {:gen_smtp, "~> 0.13 or ~> 1.0", [hex: :gen_smtp, repo: "hexpm", optional: true]}, {:hackney, "~> 1.9", [hex: :hackney, repo: "hexpm", optional: true]}, {:idna, "~> 6.0", [hex: :idna, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mail, "~> 0.2", [hex: :mail, repo: "hexpm", optional: true]}, {:mime, "~> 1.1 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mua, "~> 0.2.3", [hex: :mua, repo: "hexpm", optional: true]}, {:multipart, "~> 0.4", [hex: :multipart, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: true]}, {:plug_cowboy, ">= 1.0.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:req, "~> 0.5.10 or ~> 0.6 or ~> 1.0", [hex: :req, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c01ced23d8786d1ee1a03e4c16574290b2ccd6267beb8c81d081c4a34574ef6e"}, "tailwind": {:hex, :tailwind, "0.4.1", "e7bcc222fe96a1e55f948e76d13dd84a1a7653fb051d2a167135db3b4b08d3e9", [:mix], [], "hexpm", "6249d4f9819052911120dbdbe9e532e6bd64ea23476056adb7f730aa25c220d1"}, diff --git a/priv/repo/migrations/.formatter.exs b/priv/repo/migrations/.formatter.exs deleted file mode 100644 index 49f9151..0000000 --- a/priv/repo/migrations/.formatter.exs +++ /dev/null @@ -1,4 +0,0 @@ -[ - import_deps: [:ecto_sql], - inputs: ["*.exs"] -] diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs deleted file mode 100644 index adf49bc..0000000 --- a/priv/repo/seeds.exs +++ /dev/null @@ -1,11 +0,0 @@ -# Script for populating the database. You can run it as: -# -# mix run priv/repo/seeds.exs -# -# Inside the script, you can read and write to any of your -# repositories directly: -# -# BirdyChat.Repo.insert!(%BirdyChat.SomeSchema{}) -# -# We recommend using the bang functions (`insert!`, `update!` -# and so on) as they will fail if something goes wrong. diff --git a/test/birdy_chat/message_writer_test.exs b/test/birdy_chat/message_writer_test.exs new file mode 100644 index 0000000..2962664 --- /dev/null +++ b/test/birdy_chat/message_writer_test.exs @@ -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 diff --git a/test/birdy_chat_web/api/messages_test.exs b/test/birdy_chat_web/api/messages_test.exs index b9d618c..ec2b102 100644 --- a/test/birdy_chat_web/api/messages_test.exs +++ b/test/birdy_chat_web/api/messages_test.exs @@ -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 diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index fa6c882..cc45bdb 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -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 diff --git a/test/support/data_case.ex b/test/support/data_case.ex index c9e1830..627e000 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -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. diff --git a/test/test_helper.exs b/test/test_helper.exs index 6b05416..869559e 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,2 +1 @@ ExUnit.start() -Ecto.Adapters.SQL.Sandbox.mode(BirdyChat.Repo, :manual)