Skip to content

Commit 2750fae

Browse files
committed
dev(e2e): inputs tag migration
1 parent 24318fc commit 2750fae

9 files changed

Lines changed: 83 additions & 21 deletions

File tree

e2e/lib/e2e/accounts/admin.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ defmodule E2e.Accounts.Admin do
1414
field :terms, :boolean, default: false
1515
field :level, :integer, default: 1
1616
field :currency, :string
17-
field :tags, :string
17+
field :tags, {:array, :string}
1818

1919
timestamps(type: :utc_datetime)
2020
end
@@ -37,5 +37,14 @@ defmodule E2e.Accounts.Admin do
3737
|> validate_inclusion(:country, Ecto.Enum.values(E2e.Accounts.Admin, :country))
3838
|> validate_number(:level, greater_than_or_equal_to: 1, less_than_or_equal_to: 5)
3939
|> validate_inclusion(:currency, @currencies)
40+
|> validate_tags_present()
41+
end
42+
43+
defp validate_tags_present(changeset) do
44+
validate_change(changeset, :tags, fn :tags, tags ->
45+
tags = if is_list(tags), do: Enum.reject(tags, &(&1 == "")), else: []
46+
47+
if tags == [], do: [tags: "can't be blank"], else: []
48+
end)
4049
end
4150
end

e2e/lib/e2e/accounts/user.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ defmodule E2e.Accounts.User do
1414
field :terms, :boolean, default: false
1515
field :level, :integer, default: 1
1616
field :currency, :string
17-
field :tags, :string
17+
field :tags, {:array, :string}
1818

1919
timestamps(type: :utc_datetime)
2020
end
@@ -36,5 +36,14 @@ defmodule E2e.Accounts.User do
3636
|> validate_acceptance(:terms)
3737
|> validate_number(:level, greater_than_or_equal_to: 1, less_than_or_equal_to: 5)
3838
|> validate_inclusion(:currency, @currencies)
39+
|> validate_tags_present()
40+
end
41+
42+
defp validate_tags_present(changeset) do
43+
validate_change(changeset, :tags, fn :tags, tags ->
44+
tags = if is_list(tags), do: Enum.reject(tags, &(&1 == "")), else: []
45+
46+
if tags == [], do: [tags: "can't be blank"], else: []
47+
end)
3948
end
4049
end

e2e/lib/e2e_web/live/admin_live/form.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ defmodule E2eWeb.AdminLive.Form do
6565
6666
<.form
6767
for={@form}
68+
id={@form.id}
6869
phx-change="validate"
6970
phx-submit="save"
7071
>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
defmodule E2e.Repo.Migrations.ChangeTagsToStringArray do
2+
use Ecto.Migration
3+
4+
def up do
5+
execute("ALTER TABLE users ALTER COLUMN tags DROP DEFAULT")
6+
execute("ALTER TABLE admins ALTER COLUMN tags DROP DEFAULT")
7+
8+
execute("""
9+
ALTER TABLE users
10+
ALTER COLUMN tags TYPE varchar(255)[]
11+
USING string_to_array(tags, ',')::varchar(255)[]
12+
""")
13+
14+
execute("""
15+
ALTER TABLE admins
16+
ALTER COLUMN tags TYPE varchar(255)[]
17+
USING string_to_array(tags, ',')::varchar(255)[]
18+
""")
19+
20+
execute("ALTER TABLE users ALTER COLUMN tags SET DEFAULT '{alpha,beta}'")
21+
execute("ALTER TABLE admins ALTER COLUMN tags SET DEFAULT '{alpha,beta}'")
22+
end
23+
24+
def down do
25+
execute("ALTER TABLE users ALTER COLUMN tags DROP DEFAULT")
26+
execute("ALTER TABLE admins ALTER COLUMN tags DROP DEFAULT")
27+
28+
execute("""
29+
ALTER TABLE users
30+
ALTER COLUMN tags TYPE varchar(255)
31+
USING array_to_string(tags, ',')
32+
""")
33+
34+
execute("""
35+
ALTER TABLE admins
36+
ALTER COLUMN tags TYPE varchar(255)
37+
USING array_to_string(tags, ',')
38+
""")
39+
40+
execute("ALTER TABLE users ALTER COLUMN tags SET DEFAULT 'alpha,beta'")
41+
execute("ALTER TABLE admins ALTER COLUMN tags SET DEFAULT 'alpha,beta'")
42+
end
43+
end

e2e/priv/repo/seeds/user_admin_seed.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ user_attrs = %{
3030
terms: true,
3131
level: 5,
3232
currency: "eur",
33-
tags: "alpha,beta"
33+
tags: ["alpha", "beta"]
3434
}
3535

3636
admin_attrs = %{
@@ -41,7 +41,7 @@ admin_attrs = %{
4141
terms: true,
4242
level: 5,
4343
currency: "eur",
44-
tags: "alpha,beta"
44+
tags: ["alpha", "beta"]
4545
}
4646

4747
{:ok, _} = Accounts.create_user(user_attrs)

e2e/test/e2e/accounts_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ defmodule E2e.AccountsTest do
3838
terms: true,
3939
level: 5,
4040
currency: "eur",
41-
tags: "alpha,beta"
41+
tags: ["alpha", "beta"]
4242
}
4343

4444
assert {:ok, %User{} = user} = Accounts.create_user(valid_attrs)
@@ -48,7 +48,7 @@ defmodule E2e.AccountsTest do
4848
assert user.terms == true
4949
assert user.level == 5
5050
assert user.currency == "eur"
51-
assert user.tags == "alpha,beta"
51+
assert user.tags == ["alpha", "beta"]
5252
end
5353

5454
test "create_user/1 with invalid data returns error changeset" do
@@ -159,7 +159,7 @@ defmodule E2e.AccountsTest do
159159
terms: true,
160160
level: 5,
161161
currency: "eur",
162-
tags: "alpha,beta"
162+
tags: ["alpha", "beta"]
163163
}
164164

165165
assert {:ok, %Admin{} = admin} = Accounts.create_admin(valid_attrs)
@@ -169,7 +169,7 @@ defmodule E2e.AccountsTest do
169169
assert admin.terms == true
170170
assert admin.level == 5
171171
assert admin.currency == "eur"
172-
assert admin.tags == "alpha,beta"
172+
assert admin.tags == ["alpha", "beta"]
173173
end
174174

175175
test "create_admin/1 with invalid data returns error changeset" do

e2e/test/e2e_web/controllers/user_controller_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule E2eWeb.UserControllerTest do
1111
terms: true,
1212
level: 5,
1313
currency: "eur",
14-
tags: "alpha,beta"
14+
tags: ["alpha", "beta"]
1515
}
1616
@update_attrs %{
1717
name: "some updated name",
@@ -21,7 +21,7 @@ defmodule E2eWeb.UserControllerTest do
2121
terms: true,
2222
level: 3,
2323
currency: "usd",
24-
tags: "gamma,delta"
24+
tags: ["gamma", "delta"]
2525
}
2626
@invalid_attrs %{
2727
name: nil,

e2e/test/e2e_web/live/admin_live_test.exs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ defmodule E2eWeb.AdminLiveTest do
1313
terms: true,
1414
level: 5,
1515
currency: "eur",
16-
tags: "alpha,beta"
16+
tags: ["alpha", "beta"]
1717
}
1818
@update_attrs %{
1919
name: "some updated name",
@@ -22,7 +22,7 @@ defmodule E2eWeb.AdminLiveTest do
2222
terms: true,
2323
level: 3,
2424
currency: "usd",
25-
tags: "gamma,delta"
25+
tags: ["gamma", "delta"]
2626
}
2727
@invalid_attrs %{
2828
name: "",
@@ -32,7 +32,7 @@ defmodule E2eWeb.AdminLiveTest do
3232
terms: false,
3333
level: 1,
3434
currency: "",
35-
tags: ""
35+
tags: [""]
3636
}
3737
@invalid_attrs_edit %{
3838
name: "",
@@ -41,7 +41,7 @@ defmodule E2eWeb.AdminLiveTest do
4141
terms: false,
4242
level: 5,
4343
currency: "eur",
44-
tags: "alpha,beta"
44+
tags: ["alpha", "beta"]
4545
}
4646

4747
defp create_admin(_) do
@@ -215,7 +215,7 @@ defmodule E2eWeb.AdminLiveTest do
215215
"terms" => "false",
216216
"level" => "42",
217217
"currency" => "eur",
218-
"tags" => "alpha,beta"
218+
"tags" => ["alpha", "beta"]
219219
}
220220

221221
html = render_change(form_live, "validate", %{"admin" => attrs})
@@ -236,7 +236,7 @@ defmodule E2eWeb.AdminLiveTest do
236236
"name" => "h",
237237
"country" => "",
238238
"currency" => "",
239-
"tags" => "",
239+
"tags" => [""],
240240
"birth_date" => "",
241241
"signature" => "",
242242
"terms" => "false",
@@ -260,7 +260,7 @@ defmodule E2eWeb.AdminLiveTest do
260260
"name" => "updated",
261261
"country" => "fra",
262262
"currency" => "eur",
263-
"tags" => "alpha,beta",
263+
"tags" => ["alpha", "beta"],
264264
"birth_date" => "",
265265
"signature" => "",
266266
"terms" => "false",
@@ -291,10 +291,10 @@ defmodule E2eWeb.AdminLiveTest do
291291
~r/<input\b(?=[^>]*\btype="text")(?=[^>]*\bname="admin\[currency\]")[^>]*\bdata-part="hidden-input"/
292292

293293
assert html =~
294-
~r/<input\b(?=[^>]*\btype="text")(?=[^>]*\bname="admin\[tags\]")[^>]*\bdata-part="value-input"/
294+
~r/<input\b(?=[^>]*\btype="hidden")(?=[^>]*\bname="admin\[tags\]\[\]")[^>]*\bdata-part="array-input"/
295295

296296
refute html =~
297-
~r/<input\b(?=[^>]*\btype="hidden")(?=[^>]*\bname="admin\[(country|currency|tags)\]")/
297+
~r/<input\b(?=[^>]*\btype="hidden")(?=[^>]*\bname="admin\[(country|currency)\]")/
298298
end
299299
end
300300
end

e2e/test/support/fixtures/accounts_fixtures.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ defmodule E2e.AccountsFixtures do
1818
terms: true,
1919
level: 5,
2020
currency: "eur",
21-
tags: "alpha,beta"
21+
tags: ["alpha", "beta"]
2222
})
2323
|> E2e.Accounts.create_user()
2424

@@ -39,7 +39,7 @@ defmodule E2e.AccountsFixtures do
3939
terms: true,
4040
level: 5,
4141
currency: "eur",
42-
tags: "alpha,beta"
42+
tags: ["alpha", "beta"]
4343
})
4444
|> E2e.Accounts.create_admin()
4545

0 commit comments

Comments
 (0)