-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Errors now return constraint violations. 0.5.0
- Loading branch information
Jae Bach Hardie
committed
Mar 16, 2017
1 parent
b4e9500
commit 81c9234
Showing
5 changed files
with
92 additions
and
7 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
defmodule Mssqlex.ConstraintsTest do | ||
use ExUnit.Case, async: true | ||
|
||
setup_all do | ||
{:ok, pid} = Mssqlex.start_link([]) | ||
Mssqlex.query!(pid, "DROP DATABASE IF EXISTS constraints_test;", []) | ||
{:ok, _, _} = Mssqlex.query(pid, "CREATE DATABASE constraints_test;", []) | ||
|
||
{:ok, [pid: pid]} | ||
end | ||
|
||
test "Unique constraint", %{pid: pid} do | ||
table_name = "constraints_test.dbo.uniq" | ||
Mssqlex.query!(pid, """ | ||
CREATE TABLE #{table_name} | ||
(id int CONSTRAINT id_unique UNIQUE) | ||
""", []) | ||
Mssqlex.query!(pid, "INSERT INTO #{table_name} VALUES (?)", [42]) | ||
error = assert_raise Mssqlex.Error, fn -> | ||
Mssqlex.query!(pid, "INSERT INTO #{table_name} VALUES (?)", [42]) | ||
end | ||
assert error.constraint_violations == [unique: "id_unique"] | ||
end | ||
|
||
test "Foreign Key constraint", %{pid: pid} do | ||
assoc_table_name = "constraints_test.dbo.assoc" | ||
table_name = "constraints_test.dbo.fk" | ||
Mssqlex.query!(pid, """ | ||
CREATE TABLE #{assoc_table_name} | ||
(id int CONSTRAINT id_pk PRIMARY KEY) | ||
""", []) | ||
Mssqlex.query!(pid, """ | ||
CREATE TABLE #{table_name} | ||
(id int CONSTRAINT id_foreign FOREIGN KEY REFERENCES #{assoc_table_name}) | ||
""", []) | ||
Mssqlex.query!(pid, "INSERT INTO #{assoc_table_name} VALUES (?)", [42]) | ||
error = assert_raise Mssqlex.Error, fn -> | ||
Mssqlex.query!(pid, "INSERT INTO #{table_name} VALUES (?)", [12]) | ||
end | ||
assert error.constraint_violations == [foreign_key: "id_foreign"] | ||
end | ||
|
||
test "Check constraint", %{pid: pid} do | ||
table_name = "constraints_test.dbo.chk" | ||
Mssqlex.query!(pid, """ | ||
CREATE TABLE #{table_name} | ||
(id int CONSTRAINT id_check CHECK (id = 1)) | ||
""", []) | ||
error = assert_raise Mssqlex.Error, fn -> | ||
Mssqlex.query!(pid, "INSERT INTO #{table_name} VALUES (?)", [42]) | ||
end | ||
assert error.constraint_violations == [check: "id_check"] | ||
end | ||
|
||
@tag skip: "Database doesn't support this" | ||
test "Multiple constraints", %{pid: pid} do | ||
table_name = "constraints_test.dbo.mult" | ||
Mssqlex.query!(pid, """ | ||
CREATE TABLE #{table_name} | ||
(id int CONSTRAINT id_unique UNIQUE, | ||
foo int CONSTRAINT foo_check CHECK (foo = 3)) | ||
""", []) | ||
Mssqlex.query!(pid, "INSERT INTO #{table_name} VALUES (?, ?)", [42, 3]) | ||
error = assert_raise Mssqlex.Error, fn -> | ||
Mssqlex.query!(pid, "INSERT INTO #{table_name} VALUES (?, ?)", [42, 5]) | ||
end | ||
assert error.constraint_violations == [unique: "id_unique", check: "foo_check"] | ||
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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
defmodule Mssqlex.TypesTest do | ||
use ExUnit.Case, async: true | ||
@moduletag :only | ||
|
||
alias Mssqlex.Result | ||
|
||
|