Created
March 18, 2018 17:05
-
-
Save JEG2/e19d1c7ee5546aec023fe300ae0d5852 to your computer and use it in GitHub Desktop.
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
defmodule SQLTest do | |
use ExUnit.Case, async: false | |
alias Replicator.{Config, Database, Connection, TestHelper} | |
defmodule Examples do | |
use Replicator.SQL | |
none(:prepare, "CREATE TABLE examples (id SERIAL, name TEXT)") | |
none(:insert, "INSERT INTO examples (name) VALUES ($name)") | |
one(:find_by_name, "SELECT * FROM examples WHERE name = $name") | |
one(:error_on_string, "SELECT CAST($string::TEXT AS INTEGER)") | |
many(:all, "SELECT * FROM examples") | |
end | |
setup_all do | |
config = %Config{ | |
migration_directory: "test/migrations", | |
database_name: "test_database" | |
} | |
Database.drop(config) | |
Database.create(config) | |
Connection.start_link(config) | |
:ok | |
end | |
setup do | |
TestHelper.reset_connected_database() | |
:ok | |
end | |
test "no result queries can be defined by name" do | |
assert Examples.prepare() == :ok | |
end | |
describe "with example records" do | |
setup do | |
Examples.prepare!() | |
Examples.insert!(name: "One") | |
Examples.insert!(name: "Two") | |
:ok | |
end | |
test "one result queries can be defined by name" do | |
expected = {:ok, %{"name" => "Two", "id" => 2}} | |
example = Examples.find_by_name(name: "Two") | |
assert expected == example | |
assert {:ok, nil} == Examples.find_by_name(name: "Three") | |
end | |
test "bang versions return results or raise errors" do | |
assert nil == Examples.find_by_name!(name: "Three") | |
assert_raise Postgrex.Error, fn -> | |
Examples.error_on_string!(string: "BOOM") | |
end | |
end | |
test "many results queries can be defined by name" do | |
names = | |
Examples.all!() | |
|> Enum.map(fn example -> Map.fetch!(example, "name") end) | |
assert ~w[One Two] == names | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment