Skip to content

Commit

Permalink
Tests factories and ex machina (#396)
Browse files Browse the repository at this point in the history
* add ex_machina

* added initial factory, reworked account_tests

* cleanup

* browser sessions test - intermediate

* browser_replay_events, users

* conversations, customers

* user_invitations, widgets, tags, google, event_subscriptions

* messages, reporting, slack/auth/conv, api_keys

* cleanup, chat_api_web tests

* fix merge

* Update mix.exs
  • Loading branch information
flmel committed Dec 7, 2020
1 parent 72b0b3e commit 50804b0
Show file tree
Hide file tree
Showing 48 changed files with 1,056 additions and 1,331 deletions.
7 changes: 0 additions & 7 deletions lib/chat_api/conversations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,6 @@ defmodule ChatApi.Conversations do
|> Repo.insert()
end

@spec create_test_conversation(map()) :: {:ok, Conversation.t()} | {:error, Ecto.Changeset.t()}
def create_test_conversation(attrs \\ %{}) do
%Conversation{}
|> Conversation.test_changeset(attrs)
|> Repo.insert()
end

@spec update_conversation(Conversation.t(), map()) ::
{:ok, Conversation.t()} | {:error, Ecto.Changeset.t()}
def update_conversation(%Conversation{} = conversation, attrs) do
Expand Down
7 changes: 0 additions & 7 deletions lib/chat_api/customers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,6 @@ defmodule ChatApi.Customers do
|> Repo.insert()
end

@spec create_test_customer(map()) :: {:ok, Customer.t()} | {:error, Ecto.Changeset.t()}
def create_test_customer(attrs \\ %{}) do
%Customer{}
|> Customer.test_changeset(attrs)
|> Repo.insert()
end

@spec update_customer(Customer.t(), map) :: {:ok, Customer.t()} | {:error, Ecto.Changeset.t()}
@doc """
Updates a customer.
Expand Down
6 changes: 0 additions & 6 deletions lib/chat_api/customers/customer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,4 @@ defmodule ChatApi.Customers.Customer do
:time_zone
])
end

def test_changeset(customer, attrs) do
customer
|> cast(attrs, [:inserted_at])
|> changeset(attrs)
end
end
7 changes: 0 additions & 7 deletions lib/chat_api/messages.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,6 @@ defmodule ChatApi.Messages do
|> Repo.insert()
end

@spec create_test_message(map()) :: {:ok, Message.t()} | {:error, Ecto.Changeset.t()}
def create_test_message(attrs \\ %{}) do
%Message{}
|> Message.test_changeset(attrs)
|> Repo.insert()
end

@spec create_and_fetch!(map()) :: Message.t() | {:error, Ecto.Changeset.t()}
def create_and_fetch!(attrs \\ %{}) do
case create_message(attrs) do
Expand Down
6 changes: 0 additions & 6 deletions lib/chat_api/messages/message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,4 @@ defmodule ChatApi.Messages.Message do
])
|> validate_required([:body, :account_id, :conversation_id])
end

def test_changeset(message, attrs) do
message
|> cast(attrs, [:inserted_at])
|> changeset(attrs)
end
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ defmodule ChatApi.MixProject do
{:mail, "~> 0.2"},
{:phoenix_swagger, "~> 0.8"},
{:ex_json_schema, "~> 0.5"},
{:ex_machina, "~> 2.4", only: [:test]},
{:pow_postgres_store, "~> 1.0.0-rc2"},
{:tzdata, "~> 1.0.5"}
]
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ecto_sql": {:hex, :ecto_sql, "3.4.5", "30161f81b167d561a9a2df4329c10ae05ff36eca7ccc84628f2c8b9fa1e43323", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.4.3", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.3.0 or ~> 0.4.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.0", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "31990c6a3579b36a3c0841d34a94c275e727de8b84f58509da5f1b2032c98ac2"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex_json_schema": {:hex, :ex_json_schema, "0.7.4", "09eb5b0c8184e5702bc89625a9d0c05c7a0a845d382e9f6f406a0fc1c9a8cc3f", [:mix], [], "hexpm", "45c67fa840f0d719a2b5578126dc29bcdc1f92499c0f61bcb8a3bcb5935f9684"},
"ex_machina": {:hex, :ex_machina, "2.4.0", "09a34c5d371bfb5f78399029194a8ff67aff340ebe8ba19040181af35315eabb", [:mix], [{:ecto, "~> 2.2 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_sql, "~> 3.0", [hex: :ecto_sql, repo: "hexpm", optional: true]}], "hexpm", "a20bc9ddc721b33ea913b93666c5d0bdca5cbad7a67540784ae277228832d72c"},
"gen_smtp": {:hex, :gen_smtp, "0.15.0", "9f51960c17769b26833b50df0b96123605a8024738b62db747fece14eb2fbfcc", [:rebar3], [], "hexpm", "29bd14a88030980849c7ed2447b8db6d6c9278a28b11a44cafe41b791205440f"},
"gettext": {:hex, :gettext, "0.18.0", "406d6b9e0e3278162c2ae1de0a60270452c553536772167e2d701f028116f870", [:mix], [], "hexpm", "c3f850be6367ebe1a08616c2158affe4a23231c70391050bf359d5f92f66a571"},
"google_api_gmail": {:hex, :google_api_gmail, "0.13.1", "1ddabc37c5ec9477193611dc3409f7d2e066f9d325024ceb49c1421544226484", [:mix], [{:google_gax, "~> 0.4", [hex: :google_gax, repo: "hexpm", optional: false]}], "hexpm", "9bbd2767b896296e6f88effb2c4e79bb91cce581d2b7c78967c42f619d26af37"},
Expand Down
68 changes: 34 additions & 34 deletions test/chat_api/accounts_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule ChatApi.AccountsTest do
use ChatApi.DataCase, async: true

import ChatApi.Factory
alias ChatApi.{Accounts, WidgetSettings, Users}

describe "accounts" do
Expand All @@ -11,49 +12,56 @@ defmodule ChatApi.AccountsTest do
@invalid_attrs %{company_name: nil}

setup do
account = account_fixture()

{:ok, account: account}
{:ok, account: insert(:account)}
end

test "list_accounts/0 returns all accounts", %{account: account} do
ids = Accounts.list_accounts() |> Enum.map(& &1.id)
assert ids == [account.id]
account_ids = Accounts.list_accounts() |> Enum.map(& &1.id)
assert account_ids == [account.id]
end

test "get_account!/1 returns the account with given id", %{account: account} do
assert Accounts.get_account!(account.id) == account
test "get_account!/1 returns the account with given id",
%{account: account} do
found_account = Accounts.get_account!(account.id)

assert found_account.id === account.id
assert found_account.company_name === account.company_name
end

test "create_account/1 with valid data creates an account and widget_setting" do
test "create_account/1 with valid data creates a account and widget_setting" do
assert {:ok, %Account{} = account} = Accounts.create_account(@valid_attrs)
assert account.company_name == "some company_name"

assert account.company_name != nil
assert %WidgetSettings.WidgetSetting{} = WidgetSettings.get_settings_by_account(account.id)
end

test "create_account/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Accounts.create_account(@invalid_attrs)
end

test "update_account/2 with valid data updates the account", %{account: account} do
test "update_account/2 with valid data updates the account",
%{account: account} do
assert {:ok, %Account{} = account} = Accounts.update_account(account, @update_attrs)
assert account.company_name == "some updated company_name"
end

test "update_account/2 does not update billing information fields", %{account: account} do
test "update_account/2 does not update billing information fields",
%{account: account} do
assert {:ok, %Account{} = account} =
Accounts.update_account(account, %{subscription_plan: "team"})

assert account.subscription_plan != "team"
end

test "update_account/2 with invalid data returns error changeset", %{account: account} do
test "update_account/2 with invalid data returns error changeset",
%{account: account} do
assert {:error, %Ecto.Changeset{}} = Accounts.update_account(account, @invalid_attrs)
assert account == Accounts.get_account!(account.id)

found_account = Accounts.get_account!(account.id)
assert account.updated_at == found_account.updated_at
end

test "update_billing_info/2 updates billing information fields", %{account: account} do
test "update_billing_info/2 updates billing information fields",
%{account: account} do
assert {:ok, %Account{} = account} =
Accounts.update_billing_info(account, %{subscription_plan: "team"})

Expand All @@ -69,7 +77,8 @@ defmodule ChatApi.AccountsTest do
assert %Ecto.Changeset{} = Accounts.change_account(account)
end

test "get_subscription_plan!/1 returns the account subscription plan", %{account: account} do
test "get_subscription_plan!/1 returns the account subscription plan",
%{account: account} do
assert "starter" = Accounts.get_subscription_plan!(account.id)

assert {:ok, %Account{} = account} =
Expand All @@ -78,14 +87,13 @@ defmodule ChatApi.AccountsTest do
assert "team" = Accounts.get_subscription_plan!(account.id)
end

test "count_active_users/1 counts the number of active users on an account", %{
account: account
} do
test "count_active_users/1 counts the number of active users on an account",
%{account: account} do
assert 0 = Accounts.count_active_users(account.id)

user_1 = user_fixture(account)
user_2 = user_fixture(account)
_user_3 = user_fixture(account)
user_1 = insert(:user, account: account)
user_2 = insert(:user, account: account)
_user_3 = insert(:user, account: account)

assert 3 = Accounts.count_active_users(account.id)

Expand All @@ -96,29 +104,21 @@ defmodule ChatApi.AccountsTest do
end

test "has_reached_user_capacity?/1 returns true for accounts on the 'starter' plan with >= 2 users",
%{
account: account
} do
%{account: account} do
assert "starter" = Accounts.get_subscription_plan!(account.id)
refute Accounts.has_reached_user_capacity?(account.id)

for _n <- 1..3 do
user_fixture(account)
end
insert_list(3, :user, account: account)

assert Accounts.has_reached_user_capacity?(account.id)
end

test "has_reached_user_capacity?/1 returns false for accounts on the 'team' plan with >= 2 users",
%{
account: account
} do
%{account: account} do
Accounts.update_billing_info(account, %{subscription_plan: "team"})
refute Accounts.has_reached_user_capacity?(account.id)

for _n <- 1..3 do
user_fixture(account)
end
insert_list(3, :user, account: account)

refute Accounts.has_reached_user_capacity?(account.id)
end
Expand Down
62 changes: 28 additions & 34 deletions test/chat_api/api_keys_test.exs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
defmodule ChatApi.ApiKeysTest do
use ChatApi.DataCase

import ChatApi.Factory
alias ChatApi.ApiKeys

describe "personal_api_keys" do
alias ChatApi.ApiKeys.PersonalApiKey

@valid_attrs %{
label: "some label",
last_used_at: ~U[2010-04-17 14:00:00Z],
value: "some value"
}
@update_attrs %{
label: "some updated label",
last_used_at: ~U[2010-05-18 14:00:00Z],
Expand All @@ -19,44 +15,45 @@ defmodule ChatApi.ApiKeysTest do
@invalid_attrs %{account_id: nil, label: nil, last_used_at: nil, user_id: nil, value: nil}

setup do
account = account_fixture()
user = user_fixture(account)
account = insert(:account)
user = insert(:user, account: account)
personal_api_key = insert(:personal_api_key, account: account, user: user)

{:ok, account: account, user: user}
{:ok, account: account, user: user, personal_api_key: personal_api_key}
end

test "list_personal_api_keys/0 returns all personal_api_keys", %{user: user, account: account} do
personal_api_key = personal_api_key_fixture(user)
test "list_personal_api_keys/0 returns all personal_api_keys",
%{user: user, account: account, personal_api_key: personal_api_key} do
personal_api_key_ids =
ApiKeys.list_personal_api_keys(user.id, account.id)
|> Enum.map(& &1.id)

assert ApiKeys.list_personal_api_keys(user.id, account.id) == [personal_api_key]
assert personal_api_key_ids == [personal_api_key.id]
end

test "get_personal_api_key!/1 returns the personal_api_key with given id", %{user: user} do
personal_api_key = personal_api_key_fixture(user)
assert ApiKeys.get_personal_api_key!(personal_api_key.id) == personal_api_key
test "get_personal_api_key!/1 returns the personal_api_key with given id",
%{personal_api_key: personal_api_key} do
assert Map.take(personal_api_key, [:id]) ==
ApiKeys.get_personal_api_key!(personal_api_key.id)
|> Map.take([:id])
end

test "create_personal_api_key/1 with valid data creates a personal_api_key", %{
user: user,
account: account
} do
attrs = Map.merge(@valid_attrs, %{user_id: user.id, account_id: account.id})
test "create_personal_api_key/1 with valid data creates a personal_api_key",
%{user: user, account: account} do
attrs = params_for(:personal_api_key, account: account, user: user)

assert {:ok, %PersonalApiKey{} = personal_api_key} = ApiKeys.create_personal_api_key(attrs)

assert personal_api_key.user_id == user.id
assert personal_api_key.account_id == account.id
assert personal_api_key.label == "some label"
assert personal_api_key.last_used_at == ~U[2010-04-17 14:00:00Z]
assert personal_api_key.value == "some value"
end

test "create_personal_api_key/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = ApiKeys.create_personal_api_key(@invalid_attrs)
end

test "update_personal_api_key/2 with valid data updates the personal_api_key", %{user: user} do
personal_api_key = personal_api_key_fixture(user)

test "update_personal_api_key/2 with valid data updates the personal_api_key",
%{personal_api_key: personal_api_key} do
assert {:ok, %PersonalApiKey{} = personal_api_key} =
ApiKeys.update_personal_api_key(personal_api_key, @update_attrs)

Expand All @@ -65,26 +62,23 @@ defmodule ChatApi.ApiKeysTest do
assert personal_api_key.value == "some updated value"
end

test "update_personal_api_key/2 with invalid data returns error changeset", %{user: user} do
personal_api_key = personal_api_key_fixture(user)

test "update_personal_api_key/2 with invalid data returns error changeset",
%{personal_api_key: personal_api_key} do
assert {:error, %Ecto.Changeset{}} =
ApiKeys.update_personal_api_key(personal_api_key, @invalid_attrs)

assert personal_api_key == ApiKeys.get_personal_api_key!(personal_api_key.id)
end

test "delete_personal_api_key/1 deletes the personal_api_key", %{user: user} do
personal_api_key = personal_api_key_fixture(user)
test "delete_personal_api_key/1 deletes the personal_api_key",
%{personal_api_key: personal_api_key} do
assert {:ok, %PersonalApiKey{}} = ApiKeys.delete_personal_api_key(personal_api_key)

assert_raise Ecto.NoResultsError, fn ->
ApiKeys.get_personal_api_key!(personal_api_key.id)
end
end

test "change_personal_api_key/1 returns a personal_api_key changeset", %{user: user} do
personal_api_key = personal_api_key_fixture(user)
test "change_personal_api_key/1 returns a personal_api_key changeset",
%{personal_api_key: personal_api_key} do
assert %Ecto.Changeset{} = ApiKeys.change_personal_api_key(personal_api_key)
end
end
Expand Down
Loading

0 comments on commit 50804b0

Please sign in to comment.