diff --git a/mix.exs b/mix.exs index da986d269..df76888ae 100644 --- a/mix.exs +++ b/mix.exs @@ -76,6 +76,7 @@ defmodule Algora.MixProject do {:styler, "~> 1.2", only: [:dev, :test], runtime: false}, {:typed_ecto_schema, "~> 0.4.1", runtime: false}, {:chameleon, "~> 2.2.0"}, + {:ex_machina, "~> 2.8.0", only: :test}, # ex_aws {:ex_aws, "~> 2.1"}, {:ex_aws_s3, "~> 2.0"}, diff --git a/mix.lock b/mix.lock index 8abc380f2..93c426fb3 100644 --- a/mix.lock +++ b/mix.lock @@ -20,6 +20,7 @@ "ex_cldr": {:hex, :ex_cldr, "2.40.2", "1361d06624d7533fb3a2df59c82c7108b18ef55e884f48d503597fce4ce28d97", [:mix], [{:cldr_utils, "~> 2.28", [hex: :cldr_utils, repo: "hexpm", optional: false]}, {:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:gettext, "~> 0.19", [hex: :gettext, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: true]}], "hexpm", "cd9039ca9a7c61b99c053a16bd2201ebd7d1c87b49499a4c6d761ec14bca4442"}, "ex_cldr_currencies": {:hex, :ex_cldr_currencies, "2.16.3", "1ec6444b5d0c0aabba5a3bc321d73f1c9c751c6add92e7fb7775ccc071d96bd8", [:mix], [{:ex_cldr, "~> 2.38", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "4d1b5f8449fdf0ece6a2e5c7401ad8fcfde77ee6ea480bddc16e266dfa2b570c"}, "ex_cldr_numbers": {:hex, :ex_cldr_numbers, "2.33.4", "ecb06f40fc63f484a53d4ea80e1bdd6860ec44d3032f2b10b17340d34c0a13d5", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:digital_token, "~> 0.3 or ~> 1.0", [hex: :digital_token, repo: "hexpm", optional: false]}, {:ex_cldr, "~> 2.38", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:ex_cldr_currencies, "~> 2.16", [hex: :ex_cldr_currencies, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "d15b7e217e9e60c328e73045e51dc67d7ac5d2997247b833efab2c69b2ed06f5"}, + "ex_machina": {:hex, :ex_machina, "2.8.0", "a0e847b5712065055ec3255840e2c78ef9366634d62390839d4880483be38abe", [: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", "79fe1a9c64c0c1c1fab6c4fa5d871682cb90de5885320c187d117004627a7729"}, "ex_money": {:hex, :ex_money, "5.19.0", "c4c18f097d148f1d02793ac4e2fc63d11422bf40eda5d78031226e0e96982eaa", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ex_cldr_numbers, "~> 2.33", [hex: :ex_cldr_numbers, repo: "hexpm", optional: false]}, {:gringotts, "~> 1.1", [hex: :gringotts, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.0 or ~> 3.0 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:poison, "~> 3.0 or ~> 4.0 or ~> 5.0 or ~> 6.0", [hex: :poison, repo: "hexpm", optional: true]}], "hexpm", "44d296a2ba42d3f03aa76efa8b5618abdd6c6c6bce747b8351406d65249648ea"}, "ex_money_sql": {:hex, :ex_money_sql, "1.11.0", "1b9b2f920d5d9220fa6dd4d8aae258daf562deaed2fb037b38b1f7ba4d0a344c", [:mix], [{:ecto, "~> 3.5", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:ex_money, "~> 5.7", [hex: :ex_money, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.15", [hex: :postgrex, repo: "hexpm", optional: false]}], "hexpm", "629e0541ae9f87122d34650f8c8febbc7349bbc6f881cf7a51b4d0779886107d"}, "expo": {:hex, :expo, "1.1.0", "f7b9ed7fb5745ebe1eeedf3d6f29226c5dd52897ac67c0f8af62a07e661e5c75", [:mix], [], "hexpm", "fbadf93f4700fb44c331362177bdca9eeb8097e8b0ef525c9cc501cb9917c960"}, diff --git a/test/support/factory.ex b/test/support/factory.ex index 376371701..3f4aea0bd 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -1,10 +1,11 @@ defmodule Algora.Factory do @moduledoc false + use ExMachina.Ecto, repo: Algora.Repo + alias Algora.Accounts.User - alias Algora.Repo alias Algora.Reviews.Review - def build(:identity) do + def identity_factory do %Algora.Accounts.Identity{ id: Nanoid.generate(), provider: "github", @@ -12,13 +13,13 @@ defmodule Algora.Factory do } end - def build(:user) do + def user_factory do %User{ id: Nanoid.generate(), type: :individual, - email: "erich@example.com", + email: sequence(:email, &"enrich#{&1}@example.com"), display_name: "Erlich Bachman", - handle: "erich", + handle: sequence(:handle, &"enrich#{&1}"), bio: "Founder of Aviato, Incubator extraordinaire", avatar_url: "https://algora.io/asset/storage/v1/object/public/mock/erich.jpg", location: "Palo Alto, CA", @@ -36,13 +37,13 @@ defmodule Algora.Factory do } end - def build(:organization) do + def organization_factory do %User{ id: Nanoid.generate(), type: :organization, - email: "piedpiper@example.com", + email: sequence(:email, &"piedpiper#{&1}@example.com"), display_name: "Pied Piper", - handle: "piedpiper", + handle: sequence(:handle, &"piedpiper#{&1}"), bio: "Making the world a better place through constructing elegant hierarchies for maximum code re-use and extensibility", avatar_url: "https://algora.io/asset/storage/v1/object/public/mock/piedpiper-logo.png", @@ -68,37 +69,37 @@ defmodule Algora.Factory do } end - def build(:member) do + def member_factory do %Algora.Organizations.Member{ id: Nanoid.generate(), role: :admin } end - def build(:customer) do + def customer_factory do %Algora.Payments.Customer{ id: Nanoid.generate(), provider: "stripe", - provider_id: "cus_1234567890", + provider_id: sequence(:cus, &"cus_#{&1}"), provider_meta: %{}, name: "Pied Piper" } end - def build(:payment_method) do + def payment_method_factory do %Algora.Payments.PaymentMethod{ id: Nanoid.generate(), provider: "stripe", - provider_id: "pm_1234567890", - provider_customer_id: "cus_1234567890" + provider_id: sequence(:pm, &"pm_#{&1}"), + provider_customer_id: sequence(:cus, &"cus_#{&1}") } end - def build(:account) do + def account_factory do %Algora.Payments.Account{ id: Nanoid.generate(), provider: "stripe", - provider_id: "acct_1234567890", + provider_id: sequence(:acct, &"acct_#{&1}"), name: "Kevin 'The Carver'", details_submitted: true, charges_enabled: true, @@ -109,7 +110,7 @@ defmodule Algora.Factory do } end - def build(:contract) do + def contract_factory do id = Nanoid.generate() %Algora.Contracts.Contract{ @@ -126,76 +127,76 @@ defmodule Algora.Factory do } end - def build(:transaction) do + def transaction_factory do %Algora.Payments.Transaction{ id: Nanoid.generate() } end - def build(:timesheet) do + def timesheet_factory do %Algora.Contracts.Timesheet{ id: Nanoid.generate(), hours_worked: 40 } end - def build(:thread) do + def thread_factory do %Algora.Chat.Thread{ id: Nanoid.generate(), title: "Lorem ipsum dolor sit amet" } end - def build(:participant) do + def participant_factory do %Algora.Chat.Participant{ id: Nanoid.generate(), last_read_at: DateTime.utc_now() } end - def build(:message) do + def message_factory do %Algora.Chat.Message{ id: Nanoid.generate(), content: "What's up?" } end - def build(:repository) do + def repository_factory do %Algora.Workspace.Repository{ id: Nanoid.generate(), provider: "github", - provider_id: "#{:rand.uniform(999_999_999)}", + provider_id: sequence(:provider_id, &"#{&1}"), name: "middle-out", url: "https://github.com/piedpiper/middle-out", provider_meta: %{} } end - def build(:ticket) do + def ticket_factory do %Algora.Workspace.Ticket{ id: Nanoid.generate(), provider: "github", - provider_id: "#{:rand.uniform(999_999_999)}", + provider_id: sequence(:provider_id, &"#{&1}"), type: :issue, title: "Optimize compression algorithm for large files", description: "We need to improve performance when handling files over 1GB", - number: :rand.uniform(100), - url: "https://github.com/piedpiper/middle-out/issues/1", + number: sequence(:number, &"#{&1}"), + url: sequence(:url, &"https://github.com/piedpiper/middle-out/issues/#{&1}"), provider_meta: %{} } end - def build(:bounty) do + def bounty_factory do %Algora.Bounties.Bounty{ id: Nanoid.generate() } end - def build(:claim) do + def claim_factory do %Algora.Bounties.Claim{ id: Nanoid.generate(), provider: "github", - provider_id: "#{:rand.uniform(999_999_999)}", + provider_id: sequence(:privider_id, &"#{&1}"), type: :code, status: :pending, title: "Implemented compression optimization", @@ -205,7 +206,7 @@ defmodule Algora.Factory do } end - def build(:review) do + def review_factory do %Review{ id: Nanoid.generate(), rating: Review.max_rating(), @@ -214,18 +215,14 @@ defmodule Algora.Factory do end # Convenience API - def build(factory_name, attributes) do - factory_name |> build() |> struct!(attributes) - end - def insert!(factory_name, attributes \\ []) do - factory_name |> build(attributes) |> Repo.insert!() + insert(factory_name, attributes) end def upsert!(factory_name, conflict_target, attributes \\ []) do - factory_name - |> build(attributes) - |> Repo.insert!( + insert( + factory_name, + attributes, on_conflict: {:replace_all_except, [:id, :name]}, conflict_target: conflict_target, returning: true