Ensure uniqueness of scrobble rules
This commit is contained in:
@@ -29,5 +29,6 @@ defmodule MusicLibrary.ScrobbleRules.ScrobbleRule do
|
|||||||
scrobble_rule
|
scrobble_rule
|
||||||
|> cast(attrs, [:type, :match_value, :target_musicbrainz_id, :enabled, :description])
|
|> cast(attrs, [:type, :match_value, :target_musicbrainz_id, :enabled, :description])
|
||||||
|> validate_required([:type, :match_value, :target_musicbrainz_id])
|
|> validate_required([:type, :match_value, :target_musicbrainz_id])
|
||||||
|
|> unique_constraint([:type, :match_value], error_key: :match_value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
defmodule MusicLibrary.Repo.Migrations.AddScrobbleRulesConstraints do
|
||||||
|
use Ecto.Migration
|
||||||
|
|
||||||
|
def change do
|
||||||
|
drop index(:scrobble_rules, [:type, :match_value])
|
||||||
|
create unique_index(:scrobble_rules, [:type, :match_value], unique: true)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -95,6 +95,17 @@ defmodule MusicLibrary.ScrobbleRulesTest do
|
|||||||
assert scrobble_rule.description == "Fix Pink Floyd album"
|
assert scrobble_rule.description == "Fix Pink Floyd album"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "create_scrobble_rule/1 doesn't allow duplicates for the same type" do
|
||||||
|
valid_attrs = Map.put(@valid_album_attrs, :type, :album)
|
||||||
|
|
||||||
|
assert {:ok, %ScrobbleRule{} = scrobble_rule} =
|
||||||
|
ScrobbleRules.create_scrobble_rule(valid_attrs)
|
||||||
|
|
||||||
|
assert scrobble_rule.type == :album
|
||||||
|
|
||||||
|
assert {:error, _changeset} = ScrobbleRules.create_scrobble_rule(valid_attrs)
|
||||||
|
end
|
||||||
|
|
||||||
test "create_scrobble_rule/1 with invalid data returns error changeset" do
|
test "create_scrobble_rule/1 with invalid data returns error changeset" do
|
||||||
assert {:error, %Ecto.Changeset{}} = ScrobbleRules.create_scrobble_rule(@invalid_attrs)
|
assert {:error, %Ecto.Changeset{}} = ScrobbleRules.create_scrobble_rule(@invalid_attrs)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user