Mix format everything

This commit is contained in:
Martin Frost 2018-01-26 17:55:15 +01:00
parent 1e620e0e84
commit 3ba1c322b8
11 changed files with 56 additions and 66 deletions

View File

@ -24,4 +24,3 @@ use Mix.Config
# here (which is why it is important to import them last). # here (which is why it is important to import them last).
# #
# import_config "#{Mix.env}.exs" # import_config "#{Mix.env}.exs"

View File

@ -46,28 +46,27 @@ defmodule ISN do
def init(opts), do: Keyword.get(opts, :decode_copy, :reference) def init(opts), do: Keyword.get(opts, :decode_copy, :reference)
def matching(_), def matching(_), do: Enum.zip(Stream.cycle([:type]), @isn)
do: Enum.zip(Stream.cycle([:type]), @isn)
def format(_), def format(_), do: :text
do: :text
def encode(_opts) do def encode(_opts) do
quote do quote do
thing -> thing ->
[<<IO.iodata_length(thing) :: int32>> | thing] [<<IO.iodata_length(thing)::int32>> | thing]
end end
end end
def decode(:copy) do def decode(:copy) do
quote do quote do
<<len :: int32, thing::binary-size(len)>> -> <<len::int32, thing::binary-size(len)>> ->
:binary.copy(thing) :binary.copy(thing)
end end
end end
def decode(:reference) do def decode(:reference) do
quote do quote do
<<len :: int32, thing::binary-size(len)>> -> <<len::int32, thing::binary-size(len)>> ->
thing thing
end end
end end
@ -77,7 +76,7 @@ end
# postgresql module. # postgresql module.
for module <- ~w(ISBN ISMN ISSN ISBN13 ISMN13 ISSN13 UPC EAN13) do for module <- ~w(ISBN ISMN ISSN ISBN13 ISMN13 ISSN13 UPC EAN13) do
module_name = Module.concat([ISN, module]) module_name = Module.concat([ISN, module])
ecto_type = module |> String.downcase |> String.to_atom ecto_type = module |> String.downcase() |> String.to_atom()
defmodule module_name do defmodule module_name do
@behaviour Ecto.Type @behaviour Ecto.Type

View File

@ -17,10 +17,10 @@ defmodule Mix.Tasks.Isn.Gen.Migration do
@doc false @doc false
def run(args) do def run(args) do
Mix.Task.run "app.start", args Mix.Task.run("app.start", args)
[repo] = parse_repo(args) [repo] = parse_repo(args)
filename = "#{timestamp()}_create_isn_extension.exs" filename = "#{timestamp()}_create_isn_extension.exs"
path = Path.relative_to(migrations_path(repo), Mix.Project.app_path) path = Path.relative_to(migrations_path(repo), Mix.Project.app_path())
file = Path.join(path, filename) file = Path.join(path, filename)
create_directory(path) create_directory(path)
mod = Module.concat([repo, Migrations, @migration_name]) mod = Module.concat([repo, Migrations, @migration_name])
@ -32,9 +32,9 @@ defmodule Mix.Tasks.Isn.Gen.Migration do
"#{y}#{pad(m)}#{pad(d)}#{pad(hh)}#{pad(mm)}#{pad(ss)}" "#{y}#{pad(m)}#{pad(d)}#{pad(hh)}#{pad(mm)}#{pad(ss)}"
end end
defp pad(s), do: s |> to_string() |> String.rjust(2, ?0) defp pad(s), do: s |> to_string() |> String.pad_leading(2, ?0)
embed_template :migration, """ embed_template(:migration, """
defmodule <%= inspect @mod %> do defmodule <%= inspect @mod %> do
use Ecto.Migration use Ecto.Migration
@ -46,5 +46,5 @@ defmodule Mix.Tasks.Isn.Gen.Migration do
execute "DROP EXTENSION IF EXISTS isn;" execute "DROP EXTENSION IF EXISTS isn;"
end end
end end
""" """)
end end

51
mix.exs
View File

@ -4,20 +4,23 @@ defmodule ISN.Mixfile do
@version "2.0.0" @version "2.0.0"
def project do def project do
[app: :isn, [
version: @version, app: :isn,
elixir: "~> 1.0", version: @version,
deps: deps(), elixir: "~> 1.0",
test_paths: ["test"], deps: deps(),
# Hex test_paths: ["test"],
description: description(), # Hex
package: package(), description: description(),
# Docs package: package(),
name: "ISN", # Docs
docs: [source_ref: "v#{@version}", name: "ISN",
source_url: "https://github.com/Frost/isn", docs: [
extras: ["README.md"]] source_ref: "v#{@version}",
] source_url: "https://github.com/Frost/isn",
extras: ["README.md"]
]
]
end end
# Configuration for the OTP application # Configuration for the OTP application
@ -34,16 +37,20 @@ defmodule ISN.Mixfile do
end end
defp package do defp package do
[files: ~w(lib README.md mix.exs), [
maintainers: ["Martin Frost"], files: ~w(lib README.md mix.exs),
licenses: ["Apache 2.0"], maintainers: ["Martin Frost"],
links: %{"GitHub" => "https://github.com/Frost/isn"}] licenses: ["Apache 2.0"],
links: %{"GitHub" => "https://github.com/Frost/isn"}
]
end end
defp deps do defp deps do
[{:postgrex, ">= 0.13.2"}, [
{:ecto, ">= 2.1.4"}, {:postgrex, ">= 0.13.2"},
{:credo, "~> 0.7", only: :dev}, {:ecto, ">= 2.1.4"},
{:ex_doc, "~> 0.15", only: :dev}] {:credo, "~> 0.7", only: :dev},
{:ex_doc, "~> 0.15", only: :dev}
]
end end
end end

View File

@ -16,4 +16,3 @@ defmodule ISN.EAN13Test do
assert ISN.EAN13.dump(@test_ean) == {:ok, @test_ean} assert ISN.EAN13.dump(@test_ean) == {:ok, @test_ean}
end end
end end

View File

@ -16,4 +16,3 @@ defmodule ISN.ISBNTest do
assert ISN.ISBN.dump(@test_isbn) == {:ok, @test_isbn} assert ISN.ISBN.dump(@test_isbn) == {:ok, @test_isbn}
end end
end end

View File

@ -16,4 +16,3 @@ defmodule ISN.ISMNTest do
assert ISN.ISMN.dump(@test_ismn) == {:ok, @test_ismn} assert ISN.ISMN.dump(@test_ismn) == {:ok, @test_ismn}
end end
end end

View File

@ -16,4 +16,3 @@ defmodule ISN.ISSNTest do
assert ISN.ISSN.dump(@test_issn) == {:ok, @test_issn} assert ISN.ISSN.dump(@test_issn) == {:ok, @test_issn}
end end
end end

View File

@ -16,5 +16,3 @@ defmodule ISN.UPCTest do
assert ISN.UPC.dump(@test_upc) == {:ok, @test_upc} assert ISN.UPC.dump(@test_upc) == {:ok, @test_upc}
end end
end end

View File

@ -4,48 +4,40 @@ defmodule ISNTest do
alias Postgrex, as: P alias Postgrex, as: P
setup do setup do
options = Keyword.merge(conn_options(), [extensions: [{ISN, {}}]]) options = Keyword.merge(conn_options(), extensions: [{ISN, {}}])
{:ok, pid} = P.start_link(options) {:ok, pid} = P.start_link(options)
{:ok, [pid: pid]} {:ok, [pid: pid]}
end end
test "encode and decode isbn", context do test "encode and decode isbn", context do
assert [["1-937785-58-0"]] = assert [["1-937785-58-0"]] = query(context[:pid], "SELECT $1::isbn", ['1937785580'])
query(context[:pid], "SELECT $1::isbn", ['1937785580'])
end end
test "encode and decode isbn13", context do test "encode and decode isbn13", context do
assert [["978-1-937785-58-1"]] = assert [["978-1-937785-58-1"]] = query(context[:pid], "SELECT $1::isbn13", ['9781937785581'])
query(context[:pid], "SELECT $1::isbn13", ['9781937785581'])
end end
test "encode and decode ismn", context do test "encode and decode ismn", context do
assert [["M-060-11561-5"]] = assert [["M-060-11561-5"]] = query(context[:pid], "SELECT $1::ismn", ['9790060115615'])
query(context[:pid], "SELECT $1::ismn", ['9790060115615'])
end end
test "encode and decode ismn13", context do test "encode and decode ismn13", context do
assert [["979-0-060-11561-5"]] = assert [["979-0-060-11561-5"]] = query(context[:pid], "SELECT $1::ismn13", ['9790060115615'])
query(context[:pid], "SELECT $1::ismn13", ['9790060115615'])
end end
test "encode and decode issn", context do test "encode and decode issn", context do
assert [["1436-4522"]] = assert [["1436-4522"]] = query(context[:pid], "SELECT $1::issn", ['14364522'])
query(context[:pid], "SELECT $1::issn", ['14364522'])
end end
test "encode and decode issn13", context do test "encode and decode issn13", context do
assert [["977-1436-452-00-8"]] = assert [["977-1436-452-00-8"]] = query(context[:pid], "SELECT issn13(issn('1436-4522'))", [])
query(context[:pid], "SELECT issn13(issn('1436-4522'))", [])
end end
test "encode and decode ean13", context do test "encode and decode ean13", context do
assert [["022-035648348-1"]] = assert [["022-035648348-1"]] = query(context[:pid], "SELECT $1::ean13", ["0220356483481"])
query(context[:pid], "SELECT $1::ean13", ["0220356483481"])
end end
test "encode and decode upc", context do test "encode and decode upc", context do
assert [["220356483481"]] = assert [["220356483481"]] = query(context[:pid], "SELECT $1::upc", ["220356483481"])
query(context[:pid], "SELECT $1::upc", ["220356483481"])
end end
end end

View File

@ -1,8 +1,5 @@
ExUnit.start() ExUnit.start()
Postgrex.Types.define( Postgrex.Types.define(ISN.PostgrexTypes, [ISN], [])
ISN.PostgrexTypes,
[ISN],
[])
defmodule ISN.TestHelper do defmodule ISN.TestHelper do
def conn_options do def conn_options do
@ -10,7 +7,8 @@ defmodule ISN.TestHelper do
sync_connect: true, sync_connect: true,
hostname: "localhost", hostname: "localhost",
types: ISN.PostgrexTypes, types: ISN.PostgrexTypes,
database: "isn_test"] database: "isn_test"
]
db_user = System.get_env("DATABASE_POSTGRESQL_USERNAME") db_user = System.get_env("DATABASE_POSTGRESQL_USERNAME")
db_pass = System.get_env("DATABASE_POSTGRESQL_PASSWORD") db_pass = System.get_env("DATABASE_POSTGRESQL_PASSWORD")
@ -21,6 +19,7 @@ defmodule ISN.TestHelper do
else else
db_options db_options
end end
db_options = db_options =
if db_pass do if db_pass do
Keyword.put(db_options, :password, db_pass) Keyword.put(db_options, :password, db_pass)
@ -42,10 +41,10 @@ defmodule ISN.TestHelper do
end end
end end
db_options = Keyword.merge(ISN.TestHelper.conn_options, [database: "postgres"]) db_options = Keyword.merge(ISN.TestHelper.conn_options(), database: "postgres")
{:ok, pid} = Postgrex.start_link(db_options) {:ok, pid} = Postgrex.start_link(db_options)
Postgrex.query!(pid, "DROP DATABASE IF EXISTS isn_test;", []) Postgrex.query!(pid, "DROP DATABASE IF EXISTS isn_test;", [])
Postgrex.query!(pid, "CREATE DATABASE isn_test;", []) Postgrex.query!(pid, "CREATE DATABASE isn_test;", [])
{:ok, pid} = Postgrex.start_link(ISN.TestHelper.conn_options) {:ok, pid} = Postgrex.start_link(ISN.TestHelper.conn_options())
Postgrex.query!(pid, "CREATE EXTENSION isn;", []) Postgrex.query!(pid, "CREATE EXTENSION isn;", [])