@@ -4,7 +4,7 @@ defmodule Ecto.Integration.ConstraintsTest do
44 import Ecto.Migrator , only: [ up: 4 ]
55 alias Ecto.Integration.PoolRepo
66
7- defmodule ConstraintTableMigration do
7+ defmodule ConstraintMigration do
88 use Ecto.Migration
99
1010 @ table table ( :constraints_test )
@@ -15,16 +15,7 @@ defmodule Ecto.Integration.ConstraintsTest do
1515 add :from , :integer
1616 add :to , :integer
1717 end
18- end
19- end
20-
21- defmodule CheckConstraintMigration do
22- use Ecto.Migration
23-
24- @ table table ( :constraints_test )
25-
26- def change do
27- create constraint ( @ table . name , :positive_price , check: "[price] > 0" )
18+ create constraint ( @ table . name , :cannot_overlap , check: "[from] < [to]" )
2819 end
2920 end
3021
@@ -43,74 +34,34 @@ defmodule Ecto.Integration.ConstraintsTest do
4334 setup_all do
4435 ExUnit.CaptureLog . capture_log ( fn ->
4536 num = @ base_migration + System . unique_integer ( [ :positive ] )
46- up ( PoolRepo , num , ConstraintTableMigration , log: false )
37+ up ( PoolRepo , num , ConstraintMigration , log: false )
4738 end )
4839
4940 :ok
5041 end
5142
52- @ tag :create_constraint
5343 test "check constraint" do
54- num = @ base_migration + System . unique_integer ( [ :positive ] )
44+ changeset = Ecto.Changeset . change ( % Constraint { } , from: 0 , to: 10 )
45+ { :ok , _ } = PoolRepo . insert ( changeset )
5546
56- ExUnit.CaptureLog . capture_log ( fn ->
57- :ok = up ( PoolRepo , num , CheckConstraintMigration , log: false )
58- end )
47+ non_overlapping_changeset = Ecto.Changeset . change ( % Constraint { } , from: 11 , to: 12 )
48+ { :ok , _ } = PoolRepo . insert ( non_overlapping_changeset )
5949
60- # When the changeset doesn't expect the db error
61- changeset = Ecto.Changeset . change ( % Constraint { } , price: - 10 )
50+ overlapping_changeset = Ecto.Changeset . change ( % Constraint { } , from: 1900 , to: 12 )
6251
6352 exception =
64- assert_raise Ecto.ConstraintError ,
65- ~r/ constraint error when attempting to insert struct/ ,
66- fn -> PoolRepo . insert ( changeset ) end
67-
68- assert exception . message =~ "\" positive_price\" (check_constraint)"
53+ assert_raise Ecto.ConstraintError , ~r/ constraint error when attempting to insert struct/ , fn ->
54+ PoolRepo . insert ( overlapping_changeset )
55+ end
56+ assert exception . message =~ "\" cannot_overlap\" (check_constraint)"
6957 assert exception . message =~ "The changeset has not defined any constraint."
7058 assert exception . message =~ "call `check_constraint/3`"
7159
72- # When the changeset does expect the db error, but doesn't give a custom message
7360 { :error , changeset } =
74- changeset
75- |> Ecto.Changeset . check_constraint ( :price , name: :positive_price )
61+ overlapping_changeset
62+ |> Ecto.Changeset . check_constraint ( :from , name: :cannot_overlap )
7663 |> PoolRepo . insert ( )
77-
78- assert changeset . errors == [
79- price: { "is invalid" , [ constraint: :check , constraint_name: "positive_price" ] }
80- ]
81-
82- assert changeset . data . __meta__ . state == :built
83-
84- # When the changeset does expect the db error and gives a custom message
85- changeset = Ecto.Changeset . change ( % Constraint { } , price: - 10 )
86-
87- { :error , changeset } =
88- changeset
89- |> Ecto.Changeset . check_constraint ( :price ,
90- name: :positive_price ,
91- message: "price must be greater than 0"
92- )
93- |> PoolRepo . insert ( )
94-
95- assert changeset . errors == [
96- price:
97- { "price must be greater than 0" ,
98- [ constraint: :check , constraint_name: "positive_price" ] }
99- ]
100-
64+ assert changeset . errors == [ from: { "is invalid" , [ constraint: :check , constraint_name: "cannot_overlap" ] } ]
10165 assert changeset . data . __meta__ . state == :built
102-
103- # When the change does not violate the check constraint
104- changeset = Ecto.Changeset . change ( % Constraint { } , price: 10 , from: 100 , to: 200 )
105-
106- { :ok , result } =
107- changeset
108- |> Ecto.Changeset . check_constraint ( :price ,
109- name: :positive_price ,
110- message: "price must be greater than 0"
111- )
112- |> PoolRepo . insert ( )
113-
114- assert is_integer ( result . id )
11566 end
116- end
67+ end
0 commit comments