-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathfactory.ex
More file actions
294 lines (265 loc) · 8.34 KB
/
Copy pathfactory.ex
File metadata and controls
294 lines (265 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
defmodule Algora.Factory do
@moduledoc false
use ExMachina.Ecto, repo: Algora.Repo
alias Algora.Accounts.User
alias Algora.Reviews.Review
alias Algora.Workspace.Installation
def identity_factory do
%Algora.Accounts.Identity{
id: Nanoid.generate(),
provider: "github",
provider_id: sequence(:provider_id, &"#{&1 + offset(:identity)}"),
provider_token: sequence(:provider_token, &"token#{&1}"),
provider_email: sequence(:provider_email, &"identity#{&1}@example.com"),
provider_login: sequence(:provider_login, &"identity#{&1}")
}
end
def user_factory do
%User{
id: Nanoid.generate(),
type: :individual,
email: sequence(:email, &"erlich#{&1}@example.com"),
display_name: "Erlich Bachman",
handle: sequence(:handle, &"erlich#{&1}"),
bio: "Founder of Aviato, Incubator extraordinaire",
avatar_url: "https://algora.io/asset/storage/v1/object/public/mock/erich.jpg",
location: "Palo Alto, CA",
country: "US",
timezone: "America/Los_Angeles",
tech_stack: ["HTML"],
hourly_rate_min: Money.new!(100, :USD),
hourly_rate_max: Money.new!(150, :USD),
hours_per_week: 40,
website_url: "https://aviato.com",
twitter_url: "https://twitter.com/erich",
github_url: "https://github.com/erich",
linkedin_url: "https://linkedin.com/in/erich",
provider: "github",
provider_id: sequence(:provider_id, &"#{&1 + offset(:user)}"),
provider_login: sequence(:provider_login, &"erlich#{&1}")
}
end
def organization_factory do
%User{
id: Nanoid.generate(),
type: :organization,
email: sequence(:email, &"piedpiper#{&1}@example.com"),
display_name: "Pied Piper",
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",
og_title: "Pied Piper | Middle-Out Compression Platform",
og_image_url: "https://algora.io/asset/storage/v1/object/public/mock/piedpiper-banner.jpg",
location: "Palo Alto, CA",
country: "US",
timezone: "America/Los_Angeles",
stargazers_count: 2481,
domain: "piedpiper.com",
tech_stack: ["C++", "Java", "Python", "JavaScript"],
hourly_rate_min: Money.new!(100, :USD),
hourly_rate_max: Money.new!(150, :USD),
hours_per_week: 40,
featured: true,
fee_pct: 19,
activated: true,
website_url: "https://piedpiper.com",
twitter_url: "https://twitter.com/piedpiper",
github_url: "https://github.com/piedpiper",
discord_url: "https://discord.gg/piedpiper",
slack_url: "https://piedpiper.slack.com",
provider: "github",
provider_login: "piedpiper",
provider_id: sequence(:provider_id, &"#{&1 + offset(:organization)}")
}
end
def member_factory do
%Algora.Organizations.Member{
id: Nanoid.generate(),
role: :admin
}
end
def customer_factory do
%Algora.Payments.Customer{
id: Nanoid.generate(),
provider: "stripe",
provider_id: sequence(:cus, &"cus_#{&1}"),
provider_meta: %{},
name: "Pied Piper"
}
end
def payment_method_factory do
%Algora.Payments.PaymentMethod{
id: Nanoid.generate(),
provider: "stripe",
provider_id: sequence(:pm, &"pm_#{&1}"),
provider_customer_id: sequence(:cus, &"cus_#{&1}")
}
end
def account_factory do
%Algora.Payments.Account{
id: Nanoid.generate(),
provider: "stripe",
provider_id: sequence(:acct, &"acct_#{&1}"),
provider_meta: %{},
name: "Kevin 'The Carver'",
details_submitted: true,
charges_enabled: true,
payouts_enabled: true,
service_agreement: "recipient",
country: "US",
type: :express,
stale: false
}
end
def contract_factory do
id = Nanoid.generate()
%Algora.Contracts.Contract{
id: id,
original_contract_id: id,
status: :active,
hourly_rate_min: Money.new!(125, :USD),
hourly_rate_max: Money.new!(175, :USD),
hourly_rate: Money.new!(150, :USD),
hours_per_week: 40,
sequence_number: 1,
start_date: days_from_now(0),
end_date: days_from_now(7),
activities: []
}
end
def transaction_factory do
%Algora.Payments.Transaction{
id: Nanoid.generate(),
group_id: Nanoid.generate()
}
end
def timesheet_factory do
%Algora.Contracts.Timesheet{
id: Nanoid.generate(),
hours_worked: 40
}
end
def thread_factory do
%Algora.Chat.Thread{
id: Nanoid.generate(),
title: "Lorem ipsum dolor sit amet"
}
end
def participant_factory do
%Algora.Chat.Participant{
id: Nanoid.generate(),
last_read_at: DateTime.utc_now()
}
end
def message_factory do
%Algora.Chat.Message{
id: Nanoid.generate(),
content: "What's up?"
}
end
def repository_factory do
%Algora.Workspace.Repository{
id: Nanoid.generate(),
provider: "github",
provider_id: sequence(:provider_id, &"#{&1 + offset(:repository)}"),
name: "middle-out",
url: "https://github.com/piedpiper/middle-out",
og_image_url: "https://algora.io/asset/storage/v1/object/public/mock/piedpiper-banner.jpg",
provider_meta: %{}
}
end
def ticket_factory do
%Algora.Workspace.Ticket{
id: Nanoid.generate(),
provider: "github",
provider_id: sequence(:provider_id, &"#{&1 + offset(:ticket)}"),
type: :issue,
title: "Optimize compression algorithm for large files",
description: "We need to improve performance when handling files over 1GB",
number: sequence(:number, &"#{&1}"),
url: sequence(:url, &"https://github.com/piedpiper/middle-out/issues/#{&1}"),
provider_meta: %{}
}
end
def bounty_factory do
%Algora.Bounties.Bounty{
id: Nanoid.generate(),
status: :open,
amount: Money.new!(100, :USD)
}
end
def attempt_factory do
%Algora.Bounties.Attempt{
id: Nanoid.generate(),
status: :active,
warnings_count: 0
}
end
def claim_factory do
id = Nanoid.generate()
%Algora.Bounties.Claim{
id: id,
group_id: id,
type: :pull_request,
status: :pending,
url: sequence(:url, &"https://github.com/piedpiper/middle-out/pull/#{&1}")
}
end
def review_factory do
%Review{
id: Nanoid.generate(),
rating: Review.max_rating(),
content: "Great developer who writes clean code, communicates well, and always delivers on time!"
}
end
def installation_factory do
%Installation{
id: Nanoid.generate(),
provider: "github",
provider_id: sequence(:provider_id, &"#{&1 + offset(:installation)}"),
provider_user_id: sequence(:provider_user_id, &"#{&1 + offset(:installation)}"),
provider_meta: %{
"account" => %{"avatar_url" => "https://algora.io/asset/storage/v1/object/public/mock/piedpiper-logo.png"},
"repository_selection" => "selected"
},
avatar_url: "https://algora.io/asset/storage/v1/object/public/mock/piedpiper-logo.png",
repository_selection: "selected"
}
end
# Convenience API
def insert!(factory_name, attributes \\ [])
def insert!(factory_name, attributes) when factory_name in [:user, :organization] do
user = insert(:user, attributes)
%{user | name: user.display_name || user.handle}
end
def insert!(factory_name, attributes) do
insert(factory_name, attributes)
end
def upsert!(factory_name, conflict_target, attributes \\ []) do
insert(
factory_name,
attributes,
on_conflict: {:replace_all_except, [:id, :name]},
conflict_target: conflict_target,
returning: true
)
end
def days_from_now(days_offset, time \\ ~T[00:00:00.000000]) do
DateTime.new!(
Date.add(Date.utc_today(), days_offset),
time,
"Etc/UTC"
)
end
defp offset(factory_name) do
# Convert each character to its ASCII value, multiply by position to ensure
# different orderings of same letters produce different results
factory_name
|> Atom.to_string()
|> String.to_charlist()
|> Enum.with_index(1)
|> Enum.reduce(0, fn {char, index}, acc -> acc + char * index end)
|> Kernel.*(1_000_000)
end
end