Update the Links.create_one to support options for the generator.
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -9,8 +9,28 @@ defmodule LinkShortener.Links do
|
|||||||
|
|
||||||
def new_one(), do: Link.changeset(%Link{}, %{})
|
def new_one(), do: Link.changeset(%Link{}, %{})
|
||||||
|
|
||||||
def create_one(attrs, length \\ 10, generator \\ &LinkGenerator.generate_one/2) do
|
@doc """
|
||||||
generator.(attrs, length)
|
Create a Link with random shorten with the length.
|
||||||
|
|
||||||
|
## Options
|
||||||
|
• :is_atom_based (boolean, true by default) - if a map is
|
||||||
|
atom-based, then a key for shorten will be :shorten, else
|
||||||
|
"shorten".
|
||||||
|
• :length (integer, 10 by default) - the length for
|
||||||
|
the shorten.
|
||||||
|
• :generator (function) - the shorten is generated by the
|
||||||
|
generator function.
|
||||||
|
LinkShortener.Generators.LinkWithRandomShorten.generate_one/2 by
|
||||||
|
default.
|
||||||
|
"""
|
||||||
|
@spec create_one(map(), [is_atom_based: boolean(), length: integer(), generator: function()]) ::
|
||||||
|
[map()]
|
||||||
|
def create_one(attrs, opts \\ []) do
|
||||||
|
is_atom_based = Keyword.get(opts, :is_atom_based, true)
|
||||||
|
length = Keyword.get(opts, :length, 10)
|
||||||
|
generator = Keyword.get(opts, :generator, &LinkGenerator.generate_one/2)
|
||||||
|
|
||||||
|
generator.(attrs, is_atom_based: is_atom_based, length: length)
|
||||||
|> insert_one()
|
|> insert_one()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ defmodule LinkShortener.LinksTest do
|
|||||||
test "create_one/2 with valid data creates a link", %{user: user} do
|
test "create_one/2 with valid data creates a link", %{user: user} do
|
||||||
assert {:ok, %Link{} = link} =
|
assert {:ok, %Link{} = link} =
|
||||||
with_user(@create_generated_attrs, user)
|
with_user(@create_generated_attrs, user)
|
||||||
|> Links.create_one(5)
|
|> Links.create_one(length: 5)
|
||||||
|
|
||||||
assert link.name == "some link name"
|
assert link.name == "some link name"
|
||||||
assert link.url == "https://gitlab.com/KKlochko/link_shortener"
|
assert link.url == "https://gitlab.com/KKlochko/link_shortener"
|
||||||
|
|||||||
Reference in New Issue
Block a user