-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify the autogenerate in Ecto.Nanoid, add test
We don't muddle with the app config in here. Need something more explicit.
- Loading branch information
Showing
9 changed files
with
97 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
priv/test_repo/migrations/20180612113352_create_posts_table_with_nanoid.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |