Skip to content

Commit

Permalink
Add testing
Browse files Browse the repository at this point in the history
  • Loading branch information
abshierjoel committed Oct 6, 2021
1 parent 48be2a2 commit 39d1fba
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 38 deletions.
2 changes: 1 addition & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 13 additions & 2 deletions lib/gql_preferences/activity_server.ex
Original file line number Diff line number Diff line change
@@ -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))}
Expand Down
1 change: 0 additions & 1 deletion lib/gql_preferences/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ defmodule UserPreferences.Application do
@moduledoc false

use Application
import Supervisor.Spec
alias ActivityServer

@impl true
Expand Down
2 changes: 1 addition & 1 deletion lib/gql_preferences_web/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
31 changes: 31 additions & 0 deletions test/gql_preferences/activity_monitor_test.exs
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions test/gql_preferences/repo.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule UserPreferences.Repo do
use Ecto.Repo,
otp_app: :gql_preferences,
adapter: Ecto.Adapters.SQL.Sandbox
end
8 changes: 0 additions & 8 deletions test/gql_preferences_web/controllers/page_controller_test.exs

This file was deleted.

14 changes: 0 additions & 14 deletions test/gql_preferences_web/views/error_view_test.exs

This file was deleted.

8 changes: 0 additions & 8 deletions test/gql_preferences_web/views/layout_view_test.exs

This file was deleted.

3 changes: 0 additions & 3 deletions test/gql_preferences_web/views/page_view_test.exs

This file was deleted.

25 changes: 25 additions & 0 deletions test/support/repo_case.ex
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 39d1fba

Please sign in to comment.