Skip to content

Commit 54e61fe

Browse files
authored
use ex_machina for test factories (#22)
1 parent 0237e06 commit 54e61fe

File tree

3 files changed

+39
-40
lines changed

3 files changed

+39
-40
lines changed

mix.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ defmodule Algora.MixProject do
7676
{:styler, "~> 1.2", only: [:dev, :test], runtime: false},
7777
{:typed_ecto_schema, "~> 0.4.1", runtime: false},
7878
{:chameleon, "~> 2.2.0"},
79+
{:ex_machina, "~> 2.8.0", only: :test},
7980
# ex_aws
8081
{:ex_aws, "~> 2.1"},
8182
{:ex_aws_s3, "~> 2.0"},

mix.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"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"},
2525
"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"},
2626
"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"},
27+
"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"},
2728
"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"},
2829
"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"},
2930
"expo": {:hex, :expo, "1.1.0", "f7b9ed7fb5745ebe1eeedf3d6f29226c5dd52897ac67c0f8af62a07e661e5c75", [:mix], [], "hexpm", "fbadf93f4700fb44c331362177bdca9eeb8097e8b0ef525c9cc501cb9917c960"},

test/support/factory.ex

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
defmodule Algora.Factory do
22
@moduledoc false
3+
use ExMachina.Ecto, repo: Algora.Repo
4+
35
alias Algora.Accounts.User
4-
alias Algora.Repo
56
alias Algora.Reviews.Review
67

7-
def build(:identity) do
8+
def identity_factory do
89
%Algora.Accounts.Identity{
910
id: Nanoid.generate(),
1011
provider: "github",
1112
provider_token: ""
1213
}
1314
end
1415

15-
def build(:user) do
16+
def user_factory do
1617
%User{
1718
id: Nanoid.generate(),
1819
type: :individual,
19-
email: "erich@example.com",
20+
email: sequence(:email, &"enrich#{&1}@example.com"),
2021
display_name: "Erlich Bachman",
21-
handle: "erich",
22+
handle: sequence(:handle, &"enrich#{&1}"),
2223
bio: "Founder of Aviato, Incubator extraordinaire",
2324
avatar_url: "https://algora.io/asset/storage/v1/object/public/mock/erich.jpg",
2425
location: "Palo Alto, CA",
@@ -36,13 +37,13 @@ defmodule Algora.Factory do
3637
}
3738
end
3839

39-
def build(:organization) do
40+
def organization_factory do
4041
%User{
4142
id: Nanoid.generate(),
4243
type: :organization,
43-
44+
email: sequence(:email, &"piedpiper#{&1}@example.com"),
4445
display_name: "Pied Piper",
45-
handle: "piedpiper",
46+
handle: sequence(:handle, &"piedpiper#{&1}"),
4647
bio:
4748
"Making the world a better place through constructing elegant hierarchies for maximum code re-use and extensibility",
4849
avatar_url: "https://algora.io/asset/storage/v1/object/public/mock/piedpiper-logo.png",
@@ -68,37 +69,37 @@ defmodule Algora.Factory do
6869
}
6970
end
7071

71-
def build(:member) do
72+
def member_factory do
7273
%Algora.Organizations.Member{
7374
id: Nanoid.generate(),
7475
role: :admin
7576
}
7677
end
7778

78-
def build(:customer) do
79+
def customer_factory do
7980
%Algora.Payments.Customer{
8081
id: Nanoid.generate(),
8182
provider: "stripe",
82-
provider_id: "cus_1234567890",
83+
provider_id: sequence(:cus, &"cus_#{&1}"),
8384
provider_meta: %{},
8485
name: "Pied Piper"
8586
}
8687
end
8788

88-
def build(:payment_method) do
89+
def payment_method_factory do
8990
%Algora.Payments.PaymentMethod{
9091
id: Nanoid.generate(),
9192
provider: "stripe",
92-
provider_id: "pm_1234567890",
93-
provider_customer_id: "cus_1234567890"
93+
provider_id: sequence(:pm, &"pm_#{&1}"),
94+
provider_customer_id: sequence(:cus, &"cus_#{&1}")
9495
}
9596
end
9697

97-
def build(:account) do
98+
def account_factory do
9899
%Algora.Payments.Account{
99100
id: Nanoid.generate(),
100101
provider: "stripe",
101-
provider_id: "acct_1234567890",
102+
provider_id: sequence(:acct, &"acct_#{&1}"),
102103
name: "Kevin 'The Carver'",
103104
details_submitted: true,
104105
charges_enabled: true,
@@ -109,7 +110,7 @@ defmodule Algora.Factory do
109110
}
110111
end
111112

112-
def build(:contract) do
113+
def contract_factory do
113114
id = Nanoid.generate()
114115

115116
%Algora.Contracts.Contract{
@@ -126,76 +127,76 @@ defmodule Algora.Factory do
126127
}
127128
end
128129

129-
def build(:transaction) do
130+
def transaction_factory do
130131
%Algora.Payments.Transaction{
131132
id: Nanoid.generate()
132133
}
133134
end
134135

135-
def build(:timesheet) do
136+
def timesheet_factory do
136137
%Algora.Contracts.Timesheet{
137138
id: Nanoid.generate(),
138139
hours_worked: 40
139140
}
140141
end
141142

142-
def build(:thread) do
143+
def thread_factory do
143144
%Algora.Chat.Thread{
144145
id: Nanoid.generate(),
145146
title: "Lorem ipsum dolor sit amet"
146147
}
147148
end
148149

149-
def build(:participant) do
150+
def participant_factory do
150151
%Algora.Chat.Participant{
151152
id: Nanoid.generate(),
152153
last_read_at: DateTime.utc_now()
153154
}
154155
end
155156

156-
def build(:message) do
157+
def message_factory do
157158
%Algora.Chat.Message{
158159
id: Nanoid.generate(),
159160
content: "What's up?"
160161
}
161162
end
162163

163-
def build(:repository) do
164+
def repository_factory do
164165
%Algora.Workspace.Repository{
165166
id: Nanoid.generate(),
166167
provider: "github",
167-
provider_id: "#{:rand.uniform(999_999_999)}",
168+
provider_id: sequence(:provider_id, &"#{&1}"),
168169
name: "middle-out",
169170
url: "https://github.com/piedpiper/middle-out",
170171
provider_meta: %{}
171172
}
172173
end
173174

174-
def build(:ticket) do
175+
def ticket_factory do
175176
%Algora.Workspace.Ticket{
176177
id: Nanoid.generate(),
177178
provider: "github",
178-
provider_id: "#{:rand.uniform(999_999_999)}",
179+
provider_id: sequence(:provider_id, &"#{&1}"),
179180
type: :issue,
180181
title: "Optimize compression algorithm for large files",
181182
description: "We need to improve performance when handling files over 1GB",
182-
number: :rand.uniform(100),
183-
url: "https://github.com/piedpiper/middle-out/issues/1",
183+
number: sequence(:number, &"#{&1}"),
184+
url: sequence(:url, &"https://github.com/piedpiper/middle-out/issues/#{&1}"),
184185
provider_meta: %{}
185186
}
186187
end
187188

188-
def build(:bounty) do
189+
def bounty_factory do
189190
%Algora.Bounties.Bounty{
190191
id: Nanoid.generate()
191192
}
192193
end
193194

194-
def build(:claim) do
195+
def claim_factory do
195196
%Algora.Bounties.Claim{
196197
id: Nanoid.generate(),
197198
provider: "github",
198-
provider_id: "#{:rand.uniform(999_999_999)}",
199+
provider_id: sequence(:privider_id, &"#{&1}"),
199200
type: :code,
200201
status: :pending,
201202
title: "Implemented compression optimization",
@@ -205,7 +206,7 @@ defmodule Algora.Factory do
205206
}
206207
end
207208

208-
def build(:review) do
209+
def review_factory do
209210
%Review{
210211
id: Nanoid.generate(),
211212
rating: Review.max_rating(),
@@ -214,18 +215,14 @@ defmodule Algora.Factory do
214215
end
215216

216217
# Convenience API
217-
def build(factory_name, attributes) do
218-
factory_name |> build() |> struct!(attributes)
219-
end
220-
221218
def insert!(factory_name, attributes \\ []) do
222-
factory_name |> build(attributes) |> Repo.insert!()
219+
insert(factory_name, attributes)
223220
end
224221

225222
def upsert!(factory_name, conflict_target, attributes \\ []) do
226-
factory_name
227-
|> build(attributes)
228-
|> Repo.insert!(
223+
insert(
224+
factory_name,
225+
attributes,
229226
on_conflict: {:replace_all_except, [:id, :name]},
230227
conflict_target: conflict_target,
231228
returning: true

0 commit comments

Comments
 (0)