Updated source's friendly_name to default to the collection_name (#29)
This commit is contained in:
@@ -59,6 +59,35 @@ defmodule Pinchflat.SourcesTest do
|
||||
assert String.starts_with?(source.collection_id, "some_playlist_id_")
|
||||
end
|
||||
|
||||
test "you can specify a custom friendly_name" do
|
||||
expect(YtDlpRunnerMock, :run, &runner_function_mock/3)
|
||||
|
||||
valid_attrs = %{
|
||||
media_profile_id: media_profile_fixture().id,
|
||||
original_url: "https://www.youtube.com/channel/abc123",
|
||||
collection_type: "channel",
|
||||
friendly_name: "some custom name"
|
||||
}
|
||||
|
||||
assert {:ok, %Source{} = source} = Sources.create_source(valid_attrs)
|
||||
|
||||
assert source.friendly_name == "some custom name"
|
||||
end
|
||||
|
||||
test "friendly name is pulled from collection_name if not specified" do
|
||||
expect(YtDlpRunnerMock, :run, &runner_function_mock/3)
|
||||
|
||||
valid_attrs = %{
|
||||
media_profile_id: media_profile_fixture().id,
|
||||
original_url: "https://www.youtube.com/channel/abc123",
|
||||
collection_type: "channel"
|
||||
}
|
||||
|
||||
assert {:ok, %Source{} = source} = Sources.create_source(valid_attrs)
|
||||
|
||||
assert source.friendly_name == "some channel name"
|
||||
end
|
||||
|
||||
test "creation with invalid data returns error changeset" do
|
||||
assert {:error, %Ecto.Changeset{}} = Sources.create_source(@invalid_source_attrs)
|
||||
end
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
defmodule Pinchflat.Utils.ChangesetUtilsTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
defmodule MockSchema do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
import Pinchflat.Utils.ChangesetUtils
|
||||
|
||||
schema "mock_schemas" do
|
||||
field :title, :string
|
||||
end
|
||||
|
||||
def changeset(data, attrs) do
|
||||
data
|
||||
|> cast(attrs, [:title])
|
||||
|> dynamic_default(:title, fn _ -> "default" end)
|
||||
end
|
||||
end
|
||||
|
||||
describe "dynamic_default/3" do
|
||||
test "sets the default value if the field is nil" do
|
||||
changeset = MockSchema.changeset(%MockSchema{}, %{})
|
||||
|
||||
assert Ecto.Changeset.get_change(changeset, :title) == "default"
|
||||
end
|
||||
|
||||
test "does not set the default value if the field is not nil" do
|
||||
changeset = MockSchema.changeset(%MockSchema{}, %{title: "custom"})
|
||||
|
||||
assert Ecto.Changeset.get_change(changeset, :title) == "custom"
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user