From 00cd25485e254b74f8489a4a50b5c0b2ac3ef16f Mon Sep 17 00:00:00 2001 From: Martin Frost Date: Thu, 21 May 2015 08:46:07 +0200 Subject: [PATCH] Added encode/decode tests for postgrex extension --- README.md | 16 ++++++++--- test/isn/ean13_test.exs | 0 test/isn/extension_test.exs | 53 +++++++++++++++++++++++++++++++++++++ test/isn/isbn13_test.exs | 7 +++++ test/isn/isbn_test.exs | 0 test/isn/ismn13_test.exs | 0 test/isn/ismn_test.exs | 0 test/isn/issn13_test.exs | 0 test/isn/issn_test.exs | 0 test/isn/upc_test.exs | 0 test/isn_test.exs | 8 ------ test/test_helper.exs | 25 +++++++++++++++++ 12 files changed, 98 insertions(+), 11 deletions(-) create mode 100644 test/isn/ean13_test.exs create mode 100644 test/isn/extension_test.exs create mode 100644 test/isn/isbn13_test.exs create mode 100644 test/isn/isbn_test.exs create mode 100644 test/isn/ismn13_test.exs create mode 100644 test/isn/ismn_test.exs create mode 100644 test/isn/issn13_test.exs create mode 100644 test/isn/issn_test.exs create mode 100644 test/isn/upc_test.exs delete mode 100644 test/isn_test.exs diff --git a/README.md b/README.md index 0b9448b..6414eff 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/test/isn/ean13_test.exs b/test/isn/ean13_test.exs new file mode 100644 index 0000000..e69de29 diff --git a/test/isn/extension_test.exs b/test/isn/extension_test.exs new file mode 100644 index 0000000..5fc7cd2 --- /dev/null +++ b/test/isn/extension_test.exs @@ -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 + diff --git a/test/isn/isbn13_test.exs b/test/isn/isbn13_test.exs new file mode 100644 index 0000000..753965a --- /dev/null +++ b/test/isn/isbn13_test.exs @@ -0,0 +1,7 @@ +defmodule Isn.ISBN13Test do + use ExUnit.Case + + # dump + # cast + # load +end diff --git a/test/isn/isbn_test.exs b/test/isn/isbn_test.exs new file mode 100644 index 0000000..e69de29 diff --git a/test/isn/ismn13_test.exs b/test/isn/ismn13_test.exs new file mode 100644 index 0000000..e69de29 diff --git a/test/isn/ismn_test.exs b/test/isn/ismn_test.exs new file mode 100644 index 0000000..e69de29 diff --git a/test/isn/issn13_test.exs b/test/isn/issn13_test.exs new file mode 100644 index 0000000..e69de29 diff --git a/test/isn/issn_test.exs b/test/isn/issn_test.exs new file mode 100644 index 0000000..e69de29 diff --git a/test/isn/upc_test.exs b/test/isn/upc_test.exs new file mode 100644 index 0000000..e69de29 diff --git a/test/isn_test.exs b/test/isn_test.exs deleted file mode 100644 index ad58314..0000000 --- a/test/isn_test.exs +++ /dev/null @@ -1,8 +0,0 @@ -defmodule IsnTest do - use ExUnit.Case - doctest Isn - - test "the truth" do - assert 1 + 1 == 2 - end -end diff --git a/test/test_helper.exs b/test/test_helper.exs index 869559e..1a1a2fc 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -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