From 39d1fba6d857b0ec380b4e62e5181e42f30639d3 Mon Sep 17 00:00:00 2001 From: Joel Abshier Date: Wed, 6 Oct 2021 00:50:48 -0500 Subject: [PATCH] Add testing --- config/dev.exs | 2 +- lib/gql_preferences/activity_server.ex | 15 +++++++-- lib/gql_preferences/application.ex | 1 - lib/gql_preferences_web/schema.ex | 2 +- mix.exs | 1 + mix.lock | 2 ++ .../gql_preferences/activity_monitor_test.exs | 31 +++++++++++++++++++ test/gql_preferences/repo.ex | 5 +++ .../controllers/page_controller_test.exs | 8 ----- .../views/error_view_test.exs | 14 --------- .../views/layout_view_test.exs | 8 ----- .../views/page_view_test.exs | 3 -- test/support/repo_case.ex | 25 +++++++++++++++ 13 files changed, 79 insertions(+), 38 deletions(-) create mode 100644 test/gql_preferences/activity_monitor_test.exs create mode 100644 test/gql_preferences/repo.ex delete mode 100644 test/gql_preferences_web/controllers/page_controller_test.exs delete mode 100644 test/gql_preferences_web/views/error_view_test.exs delete mode 100644 test/gql_preferences_web/views/layout_view_test.exs delete mode 100644 test/gql_preferences_web/views/page_view_test.exs create mode 100644 test/support/repo_case.ex diff --git a/config/dev.exs b/config/dev.exs index 0bbe3ef..bfe7361 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -3,7 +3,7 @@ import Config # Configure your database config :gql_preferences, UserPreferences.Repo, username: "postgres", - password: "notpostgres", + password: "postgres", database: "gql_preferences_dev", hostname: "localhost", show_sensitive_data_on_connection_error: true, diff --git a/lib/gql_preferences/activity_server.ex b/lib/gql_preferences/activity_server.ex index 57c07ae..1f0075e 100644 --- a/lib/gql_preferences/activity_server.ex +++ b/lib/gql_preferences/activity_server.ex @@ -1,8 +1,19 @@ defmodule ActivityServer do use GenServer - def start_link(_), do: GenServer.start_link(__MODULE__, %{}, name: ActivityServer) - def init(_), do: {:ok, %{}} + @resolvers [ + "get_user_by_id", + "get_all_users", + "create_user", + "update_user", + "get_preferences", + "update_preferences_by_id" + ] + + def start_link(_), + do: GenServer.start_link(__MODULE__, Map.new(@resolvers, &{&1, 0}), name: ActivityServer) + + def init(args), do: {:ok, args} def handle_cast({:update, key}, state) do {:noreply, Map.update(state, key, 1, &(&1 + 1))} diff --git a/lib/gql_preferences/application.ex b/lib/gql_preferences/application.ex index 90d72b9..3aabf3b 100644 --- a/lib/gql_preferences/application.ex +++ b/lib/gql_preferences/application.ex @@ -4,7 +4,6 @@ defmodule UserPreferences.Application do @moduledoc false use Application - import Supervisor.Spec alias ActivityServer @impl true diff --git a/lib/gql_preferences_web/schema.ex b/lib/gql_preferences_web/schema.ex index 8b64f12..1fe1e88 100644 --- a/lib/gql_preferences_web/schema.ex +++ b/lib/gql_preferences_web/schema.ex @@ -32,7 +32,7 @@ defmodule UserPreferencesWeb.Schema do {:ok, UserPreferences.Repo.get(UserPreferences.User, local_id)} _, _ -> - {:error, "Unknown node"} |> IO.inspect() + {:error, "Unknown node"} end) end end diff --git a/mix.exs b/mix.exs index 7a4ddef..47ef939 100644 --- a/mix.exs +++ b/mix.exs @@ -38,6 +38,7 @@ defmodule UserPreferences.MixProject do {:absinthe_phoenix, "~> 2.0"}, {:absinthe_relay, "~> 1.5"}, {:cors_plug, "~> 2.0"}, + {:dialyxir, "~> 1.1", only: [:dev], runtime: false}, {:phoenix, "~> 1.6.0"}, {:phoenix_ecto, "~> 4.4"}, {:ecto_sql, "~> 3.6"}, diff --git a/mix.lock b/mix.lock index dc5e3c1..1278b2c 100644 --- a/mix.lock +++ b/mix.lock @@ -11,9 +11,11 @@ "cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"}, "db_connection": {:hex, :db_connection, "2.4.0", "d04b1b73795dae60cead94189f1b8a51cc9e1f911c234cc23074017c43c031e5", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ad416c21ad9f61b3103d254a71b63696ecadb6a917b36f563921e0de00d7d7c8"}, "decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"}, + "dialyxir": {:hex, :dialyxir, "1.1.0", "c5aab0d6e71e5522e77beff7ba9e08f8e02bad90dfbeffae60eaf0cb47e29488", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "07ea8e49c45f15264ebe6d5b93799d4dd56a44036cf42d0ad9c960bc266c0b9a"}, "ecto": {:hex, :ecto, "3.7.1", "a20598862351b29f80f285b21ec5297da1181c0442687f9b8329f0445d228892", [:mix], [{:decimal, "~> 1.6 or ~> 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", "d36e5b39fc479e654cffd4dbe1865d9716e4a9b6311faff799b6f90ab81b8638"}, "ecto_shorts": {:hex, :ecto_shorts, "1.0.0", "a127bc8356ec6e834b13d3563feea8b705867d2beb3d52ed233940f4ac896a87", [:mix], [{:ecto_sql, "~> 3.3", [hex: :ecto_sql, repo: "hexpm", optional: false]}], "hexpm", "f7c9c1d5842526525488ead46525c6523f47fe388d31143a816d7e8c23abd828"}, "ecto_sql": {:hex, :ecto_sql, "3.7.0", "2fcaad4ab0c8d76a5afbef078162806adbe709c04160aca58400d5cbbe8eeac6", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.4.0 or ~> 0.5.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a26135dfa1d99bf87a928c464cfa25bba6535a4fe761eefa56077a4febc60f70"}, + "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, "esbuild": {:hex, :esbuild, "0.3.1", "bf6a3783f8677aa93e8e6ee04b79eeceadb29e07255941fab7e50f1e3527f4a8", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "342ccd0eb2c64211326580189389d52cdf0f16f5ca22bc0267a66357e269a14a"}, "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, "floki": {:hex, :floki, "0.31.0", "f05ee8a8e6a3ced4e62beeb2c79a63bc8e12ab98fbaaf6e6a3d9b76b1278e23f", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "b05afa372f5c345a5bf240ac25ea1f0f3d5fcfd7490ac0beeb4a203f9444891e"}, diff --git a/test/gql_preferences/activity_monitor_test.exs b/test/gql_preferences/activity_monitor_test.exs new file mode 100644 index 0000000..708029d --- /dev/null +++ b/test/gql_preferences/activity_monitor_test.exs @@ -0,0 +1,31 @@ +defmodule UserPreferences.ActivityMonitorTest do + use ExUnit.Case, async: true + alias UserPreferencesWeb.Schema + + @get_resolver_hits """ + query getResolverHits($key: String){ + resolverHits(key: $key) + } + """ + + describe "@resolverHits" do + test "returns the count for a resolver" do + key = "get_all_users" + + assert {:ok, %{data: %{"resolverHits" => count}}} = + Absinthe.run(@get_resolver_hits, Schema, variables: %{"key" => key}) + + assert count === 0 + end + + test "returns" do + key = "not_a_key" + + assert {:ok, %{data: %{"resolverHits" => count}, errors: errors}} = + Absinthe.run(@get_resolver_hits, Schema, variables: %{"key" => "not_a_key"}) + + assert is_nil(count) === true + assert List.first(errors).message === "Requested key: not_a_key is invalid" + end + end +end diff --git a/test/gql_preferences/repo.ex b/test/gql_preferences/repo.ex new file mode 100644 index 0000000..a1369f5 --- /dev/null +++ b/test/gql_preferences/repo.ex @@ -0,0 +1,5 @@ +defmodule UserPreferences.Repo do + use Ecto.Repo, + otp_app: :gql_preferences, + adapter: Ecto.Adapters.SQL.Sandbox +end diff --git a/test/gql_preferences_web/controllers/page_controller_test.exs b/test/gql_preferences_web/controllers/page_controller_test.exs deleted file mode 100644 index 61e88c6..0000000 --- a/test/gql_preferences_web/controllers/page_controller_test.exs +++ /dev/null @@ -1,8 +0,0 @@ -defmodule UserPreferencesWeb.PageControllerTest do - use UserPreferencesWeb.ConnCase - - test "GET /", %{conn: conn} do - conn = get(conn, "/") - assert html_response(conn, 200) =~ "Welcome to Phoenix!" - end -end diff --git a/test/gql_preferences_web/views/error_view_test.exs b/test/gql_preferences_web/views/error_view_test.exs deleted file mode 100644 index dc1fe13..0000000 --- a/test/gql_preferences_web/views/error_view_test.exs +++ /dev/null @@ -1,14 +0,0 @@ -defmodule UserPreferencesWeb.ErrorViewTest do - use UserPreferencesWeb.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(UserPreferencesWeb.ErrorView, "404.html", []) == "Not Found" - end - - test "renders 500.html" do - assert render_to_string(UserPreferencesWeb.ErrorView, "500.html", []) == "Internal Server Error" - end -end diff --git a/test/gql_preferences_web/views/layout_view_test.exs b/test/gql_preferences_web/views/layout_view_test.exs deleted file mode 100644 index 2ec40bd..0000000 --- a/test/gql_preferences_web/views/layout_view_test.exs +++ /dev/null @@ -1,8 +0,0 @@ -defmodule UserPreferencesWeb.LayoutViewTest do - use UserPreferencesWeb.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 diff --git a/test/gql_preferences_web/views/page_view_test.exs b/test/gql_preferences_web/views/page_view_test.exs deleted file mode 100644 index 22de789..0000000 --- a/test/gql_preferences_web/views/page_view_test.exs +++ /dev/null @@ -1,3 +0,0 @@ -defmodule UserPreferencesWeb.PageViewTest do - use UserPreferencesWeb.ConnCase, async: true -end diff --git a/test/support/repo_case.ex b/test/support/repo_case.ex new file mode 100644 index 0000000..b753bba --- /dev/null +++ b/test/support/repo_case.ex @@ -0,0 +1,25 @@ +defmodule UserPreferences.RepoCase do + use ExUnit.CaseTemplate + + using do + quote do + alias UserPreferences.Repo + + import Ecto + import Ecto.Query + import UserPreferences.RepoCase + + # and any other stuff + end + end + + setup tags do + :ok = Ecto.Adapters.SQL.Sandbox.checkout(UserPreferences.Repo) + + unless tags[:async] do + Ecto.Adapters.SQL.Sandbox.mode(UserPreferences.Repo, {:shared, self()}) + end + + :ok + end +end