Added encode/decode tests for postgrex extension

This commit is contained in:
Martin Frost 2015-05-21 08:46:07 +02:00
parent 4b2fe9d0b8
commit 00cd25485e
12 changed files with 98 additions and 11 deletions

View File

@ -1,4 +1,14 @@
Isn
===
# Isn
** TODO: Add description **
Add support for the `isn` module in Postgrex/Ecto.
This module adds the following ecto types:
* Isn.ISBN
* Isn.ISBN13
* Isn.ISMN
* Isn.ISMN13
* Isn.ISSN
* Isn.ISSN13
* Isn.EAN13
* Isn.UPC

0
test/isn/ean13_test.exs Normal file
View File

View File

@ -0,0 +1,53 @@
defmodule IsnExtensionTest do
use ExUnit.Case, async: true
import Isn.TestHelper
alias Postgrex.Connection, as: P
setup do
{:ok, pid} = P.start_link(
database: "isn_test",
extensions: [{Isn.Extension, {}}])
{:ok, [pid: pid]}
end
test "encode and decode isbn", context do
assert [{"1-937785-58-0"}] =
query(context[:pid], "SELECT $1::isbn", ['1937785580'])
end
test "encode and decode isbn13", context do
assert [{"978-1-937785-58-1"}] =
query(context[:pid], "SELECT $1::isbn13", ['9781937785581'])
end
test "encode and decode ismn", context do
assert [{"M-060-11561-5"}] =
query(context[:pid], "SELECT $1::ismn", ['9790060115615'])
end
test "encode and decode ismn13", context do
assert [{"979-0-060-11561-5"}] =
query(context[:pid], "SELECT $1::ismn13", ['9790060115615'])
end
test "encode and decode issn", context do
assert [{"1436-4522"}] =
query(context[:pid], "SELECT $1::issn", ['14364522'])
end
test "encode and decode issn13", context do
assert [{"977-1436-452-00-8"}] =
query(context[:pid], "SELECT issn13(issn('1436-4522'))", [])
end
test "encode and decode ean13", context do
assert [{"022-035648348-1"}] =
query(context[:pid], "SELECT $1::ean13", ["0220356483481"])
end
test "encode and decode upc", context do
assert [{"220356483481"}] =
query(context[:pid], "SELECT $1::upc", ["220356483481"])
end
end

7
test/isn/isbn13_test.exs Normal file
View File

@ -0,0 +1,7 @@
defmodule Isn.ISBN13Test do
use ExUnit.Case
# dump
# cast
# load
end

0
test/isn/isbn_test.exs Normal file
View File

0
test/isn/ismn13_test.exs Normal file
View File

0
test/isn/ismn_test.exs Normal file
View File

0
test/isn/issn13_test.exs Normal file
View File

0
test/isn/issn_test.exs Normal file
View File

0
test/isn/upc_test.exs Normal file
View File

View File

@ -1,8 +0,0 @@
defmodule IsnTest do
use ExUnit.Case
doctest Isn
test "the truth" do
assert 1 + 1 == 2
end
end

View File

@ -1 +1,26 @@
ExUnit.start()
{:ok, pid} = Postgrex.Connection.start_link(
hostname: "localhost",
database: "postgres"
)
Postgrex.Connection.query!(pid, "DROP DATABASE IF EXISTS isn_test;", [])
Postgrex.Connection.query!(pid, "CREATE DATABASE isn_test;", [])
{:ok, pid} = Postgrex.Connection.start_link(
hostname: "localhost",
database: "isn_test"
)
Postgrex.Connection.query!(pid, "CREATE EXTENSION isn;", [])
defmodule Isn.TestHelper do
defmacro query(pid, statement, params) do
quote bind_quoted: [pid: pid, statement: statement, params: params] do
case Postgrex.Connection.query(pid, statement, params, []) do
{:ok, %Postgrex.Result{rows: nil}} -> :ok
{:ok, %Postgrex.Result{rows: rows}} -> rows
{:error, %Postgrex.Error{} = err} -> err
end
end
end
end