Skip to content

Commit

Permalink
Simplify the autogenerate in Ecto.Nanoid, add test
Browse files Browse the repository at this point in the history
We don't muddle with the app config in here.
Need something more explicit.
  • Loading branch information
oyeb committed Jun 12, 2018
1 parent 18b8397 commit 83c9392
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Used by "mix format"
[
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]
inputs: ["mix.exs", "{config,lib,test,priv}/**/*.{ex,exs}"]
]
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Changelog

# v0.1.0 BigBang!

New custom `Ecto.Type`
----------------------

`Ecto.Nanoid`, uses [`nanoid`][nanoid] to its magic.

[nanoid]: https://github.com/railsmechanic/nanoid
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

_When UUID is not enough!_

[![codecov](https://codecov.io/gh/oyeb/ecto_identifier/branch/master/graph/badge.svg)](https://codecov.io/gh/oyeb/ecto_identifier)
[![Build Status](https://travis-ci.org/oyeb/ecto_identifier.svg?branch=master)](https://travis-ci.org/oyeb/ecto_identifier)

Provides some custom [`Ecto`][ecto] [type][ecto-type]s to work with identifier
fields that are generated using:

Expand Down Expand Up @@ -50,7 +53,7 @@ iex> %Post{
}
```
> If you pass in a `:number` in the `params`, a nanoid will not be autogenerated
> for you. For more juicy detals see `Ecto.Schema`.
> for you. For more juicy details see `Ecto.Schema`.
#### Why a "type"

Expand All @@ -68,8 +71,8 @@ _Coming shortly folks!_

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `ecto_identifier` to your list of dependencies in `mix.exs`:
The package can be installed by adding `ecto_identifier` to your list of
dependencies in `mix.exs`:

```elixir
def deps do
Expand Down
20 changes: 5 additions & 15 deletions lib/ecto_nanoid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,9 @@ defmodule Ecto.Nanoid do
def dump(value) when is_binary(value), do: {:ok, value}
def dump(_), do: :error

def autogenerate do
case Application.get_env(:ecto_identifier, :nanoid) do
%{size: size, alphabet: alphabet} ->
Nanoid.generate(size, alphabet)

%{alphabet: alphabet} ->
Nanoid.generate(21, alphabet)

%{size: size} ->
Nanoid.generate(size)

_ ->
Nanoid.generate()
end
end
@doc """
Delegates generation of the field to `Nanoid.generate/0`.
"""
@spec autogenerate() :: String.t()
def autogenerate, do: Nanoid.generate()
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ defmodule EctoIdentifier.MixProject do

defp docs do
[
extras: ["README.md", "LICENSE", "CHANGELOG.md"],
extras: ["README.md", "CHANGELOG.md"],
main: "readme",
source_ref: "v#{@version}"
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule EctoIdentifier.TestRepo.Migrations.CreatePostsTableWithNanoid do
use Ecto.Migration

def change do
create(table(:posts_nanoid)) do
add(:number, :string, comments: "the nanoid field")
end
end
end
8 changes: 0 additions & 8 deletions test/ecto_identifier_test.exs

This file was deleted.

55 changes: 55 additions & 0 deletions test/ecto_nanoid_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
defmodule Ecto.NanoidTest do
use ExUnit.Case

alias Ecto.{Changeset, Nanoid}
alias EctoIdentifier.PostWithNanoid
alias EctoIdentifier.TestRepo

test "type/0" do
assert :string == Nanoid.type()
end

test "cast" do
assert {:ok, "pinkfloyd"} = Nanoid.cast("pinkfloyd")
assert :error = Nanoid.cast(:pinkfloyd)
assert :error = Nanoid.cast(123_456)
end

test "dump" do
assert {:ok, "pinkfloyd"} = Nanoid.dump("pinkfloyd")
assert :error = Nanoid.dump(:pinkfloyd)
assert :error = Nanoid.dump(123_456)
end

test "load" do
assert {:ok, "pinkfloyd"} = Nanoid.load("pinkfloyd")
end

describe "autogenerate" do
test "struct doesn't come with an 'number' at creation" do
post = %PostWithNanoid{}
assert is_nil(post.id)
assert is_nil(post.number)
end

test "'number' generated at insertion" do
post = TestRepo.insert!(%PostWithNanoid{})
refute is_nil(post.id)
assert post.number =~ ~r/[~_A-Za-z0-9]{21}/
end

test "'number' not generated if provided in changeset/struct/map/keyword" do
post = TestRepo.insert!(%PostWithNanoid{number: "pinkfloyd"})
refute is_nil(post.id)
assert post.number == "pinkfloyd"

post =
%PostWithNanoid{}
|> Changeset.change(%{number: "pinkfloyd"})
|> TestRepo.insert!()

refute is_nil(post.id)
assert post.number == "pinkfloyd"
end
end
end
10 changes: 10 additions & 0 deletions test/support/post_with_nanoid.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule EctoIdentifier.PostWithNanoid do
@moduledoc false
use Ecto.Schema

alias Ecto.Nanoid

schema("posts_nanoid") do
field(:number, Nanoid, autogenerate: true)
end
end

0 comments on commit 83c9392

Please sign in to comment.