Skip to content

Commit

Permalink
Add user tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Abshier committed Oct 6, 2021
1 parent 410d094 commit 6cb2b65
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 30 deletions.
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ defmodule UserPreferences.MixProject do
{: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"},
{:ecto_shorts, "~> 1.0"},
{:ex_mock, "~> 0.1.1", only: :test},
{:postgrex, ">= 0.0.0"},
{:phoenix, "~> 1.6.0"},
{:phoenix_ecto, "~> 4.4"},
{:phoenix_html, "~> 3.0"},
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:phoenix_live_view, "~> 0.16.0"},
Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
"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"},
"ex_mock": {:hex, :ex_mock, "0.1.1", "b123b42b942bbdfce2f978aaa1fe1c28ceda324cacd2d8f881e74b548bec6d9c", [:mix], [{:meck, "~> 0.8.2", [hex: :meck, repo: "hexpm", optional: false]}], "hexpm", "3e1d52efaee5adecc49cb96c7d75c4382e22abedee4e3e4ed849b54f45f6baab"},
"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"},
"gettext": {:hex, :gettext, "0.18.2", "7df3ea191bb56c0309c00a783334b288d08a879f53a7014341284635850a6e55", [:mix], [], "hexpm", "f9f537b13d4fdd30f3039d33cb80144c3aa1f8d9698e47d7bcbcc8df93b1f5c5"},
"html_entities": {:hex, :html_entities, "0.5.2", "9e47e70598da7de2a9ff6af8758399251db6dbb7eebe2b013f2bbd2515895c3c", [:mix], [], "hexpm", "c53ba390403485615623b9531e97696f076ed415e8d8058b1dbaa28181f4fdcc"},
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
"meck": {:hex, :meck, "0.8.13", "ffedb39f99b0b99703b8601c6f17c7f76313ee12de6b646e671e3188401f7866", [:rebar3], [], "hexpm", "d34f013c156db51ad57cc556891b9720e6a1c1df5fe2e15af999c84d6cebeb1a"},
"mime": {:hex, :mime, "2.0.1", "0de4c81303fe07806ebc2494d5321ce8fb4df106e34dd5f9d787b637ebadc256", [:mix], [], "hexpm", "7a86b920d2aedce5fb6280ac8261ac1a739ae6c1a1ad38f5eadf910063008942"},
"nimble_parsec": {:hex, :nimble_parsec, "1.1.0", "3a6fca1550363552e54c216debb6a9e95bd8d32348938e13de5eda962c0d7f89", [:mix], [], "hexpm", "08eb32d66b706e913ff748f11694b17981c0b04a33ef470e33e11b3d3ac8f54b"},
"phoenix": {:hex, :phoenix, "1.6.0", "7b85023f7ddef9a5c70909a51cc37c8b868b474d853f90f4280efd26b0e7cce5", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 1.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "52ffdd31f2daeb399b2e1eb57d468f99a1ad6eee5d8ea19d2353492f06c9fc96"},
Expand Down
5 changes: 0 additions & 5 deletions test/gql_preferences/repo.ex

This file was deleted.

2 changes: 1 addition & 1 deletion test/gql_preferences_web/activity_monitor_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule UserPreferences.ActivityMonitorTest do
defmodule UserPreferencesTest.ActivityMonitorTest do
use ExUnit.Case, async: true
alias UserPreferencesWeb.Schema

Expand Down
52 changes: 30 additions & 22 deletions test/gql_preferences_web/create_user_test.exs
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
defmodule UserPreferences.ActivityMonitorTest do
defmodule UserPreferencesTest.CreateUserTest 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"
@create_user """
mutation createUser(
$name: String,
$email: String,
$likesEmails: Boolean,
$likesPhoneCalls: Boolean
) {
createUser(
name: $name,
email: $email,
preferences: {
likesEmails: $likesEmails,
likesPhoneCalls: $likesPhoneCalls
}
) {
id
name
email
preferences {
likesEmails
likesPhoneCalls
}
}
}
"""

assert {:ok, %{data: %{"resolverHits" => count}, errors: errors}} =
Absinthe.run(@get_resolver_hits, Schema, variables: %{"key" => key})
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(UserPreferences.Repo)
end

assert is_nil(count) === true
assert List.first(errors).message === "Requested key: #{key} is invalid"
end
describe "@createUser" do
end
end
66 changes: 66 additions & 0 deletions test/gql_preferences_web/find_by_id_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
defmodule UserPreferencesTest.FindUserTest do
use ExUnit.Case, async: true
alias UserPreferences.{Repo, User, Preferences}
alias UserPreferencesWeb.{Schema}
import ExMock

@user """
query findById($id: ID){
user(id: $id){
name
email
id
preferences{
likesEmails
likesPhoneCalls
}
}
}
"""

setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo)
end

describe "@findById" do
test "returns a user" do
pref_mock = %{
likes_emails: true,
likes_phone_calls: false
}

user_mock = %{
id: "55",
name: "TestGuy",
email: "[email protected]"
}

with_mock Repo,
get: fn User, _ -> user_mock end,
get_by: fn Preferences, _ -> pref_mock end do
assert {:ok, res} = Absinthe.run(@user, Schema, variables: %{"id" => 55})

assert Map.equal?(res, %{
data: %{
"user" => %{
"id" => "55",
"name" => "TestGuy",
"email" => "[email protected]",
"preferences" => %{
"likesEmails" => true,
"likesPhoneCalls" => false
}
}
}
})
end
end

test "returns nil if no user is found" do
user_id = 1_234_456_789

assert {:ok, %{data: %{"user" => nil}}} =
Absinthe.run(@user, Schema, variables: %{"id" => user_id})
end
end
end

0 comments on commit 6cb2b65

Please sign in to comment.