@@ -16,24 +16,6 @@ defmodule Ecto.Integration.ConstraintsTest do
16
16
add :to , :integer
17
17
end
18
18
create constraint ( @ table . name , :cannot_overlap , check: "[from] < [to]" )
19
- create constraint ( @ table . name , "positive_price" , check: "[price] > 0" )
20
- end
21
- end
22
-
23
- defmodule ConstraintMigration2 do
24
- use Ecto.Migration
25
-
26
- def change do
27
- opts = [ with: "NOCHECK" , check: "[from] < 200" ]
28
- create constraint ( :constraints_test , "from_max" , opts )
29
- end
30
- end
31
-
32
- defmodule ConstraintMigration3 do
33
- use Ecto.Migration
34
-
35
- def change do
36
- drop constraint ( :constraints_test , "from_max" )
37
19
end
38
20
end
39
21
@@ -58,7 +40,7 @@ defmodule Ecto.Integration.ConstraintsTest do
58
40
:ok
59
41
end
60
42
61
- test "creating, using, and dropping an exclusion constraint" do
43
+ test "check constraint" do
62
44
changeset = Ecto.Changeset . change ( % Constraint { } , from: 0 , to: 10 )
63
45
{ :ok , _ } = PoolRepo . insert ( changeset )
64
46
@@ -75,84 +57,11 @@ defmodule Ecto.Integration.ConstraintsTest do
75
57
assert exception . message =~ "The changeset has not defined any constraint."
76
58
assert exception . message =~ "call `check_constraint/3`"
77
59
78
- # Seems like below `Ecto.Changeset.check_constraint(:from)` is not valid for some reason,
79
- # constrint name is mandatory and ecto raises ArgumentError
80
-
81
- # message = ~r/constraint error when attempting to insert struct/
82
- # exception =
83
- # assert_raise Ecto.ConstraintError, message, fn ->
84
- # overlapping_changeset
85
- # |> Ecto.Changeset.check_constraint(:from)
86
- # |> PoolRepo.insert()
87
- # end
88
- # assert exception.message =~ "cannot_overlap (check_constraint)"
89
-
90
60
{ :error , changeset } =
91
61
overlapping_changeset
92
62
|> Ecto.Changeset . check_constraint ( :from , name: :cannot_overlap )
93
63
|> PoolRepo . insert ( )
94
64
assert changeset . errors == [ from: { "is invalid" , [ constraint: :check , constraint_name: "cannot_overlap" ] } ]
95
65
assert changeset . data . __meta__ . state == :built
96
-
97
- ExUnit.CaptureLog . capture_log ( fn ->
98
- # migrate over existing data, it should pass since `with: NOCHECK` is set
99
- num = @ base_migration + System . unique_integer ( [ :positive ] )
100
- assert :ok == up ( PoolRepo , num , ConstraintMigration2 , log: false )
101
- end )
102
-
103
- # from is greated than max allowed by database, so check constrint should
104
- # forbid insert
105
- from_max_changeset = Ecto.Changeset . change ( % Constraint { } , from: 300 , to: 400 )
106
-
107
- exception =
108
- assert_raise Ecto.ConstraintError , ~r/ constraint error when attempting to insert struct/ , fn ->
109
- PoolRepo . insert ( from_max_changeset )
110
- end
111
- assert exception . message =~ "from_max (check_constraint)"
112
- assert exception . message =~ "The changeset has not defined any constraint."
113
- assert exception . message =~ "call `check_constraint/3`"
114
-
115
- ExUnit.CaptureLog . capture_log ( fn ->
116
- num = @ base_migration + System . unique_integer ( [ :positive ] )
117
- assert :ok == up ( PoolRepo , num , ConstraintMigration3 , log: false )
118
- end )
119
- end
120
-
121
- test "creating, using, and dropping a check constraint" do
122
- # When the changeset doesn't expect the db error
123
- changeset = Ecto.Changeset . change ( % Constraint { } , price: - 10 )
124
- exception =
125
- assert_raise Ecto.ConstraintError , ~r/ constraint error when attempting to insert struct/ , fn ->
126
- PoolRepo . insert ( changeset )
127
- end
128
-
129
- assert exception . message =~ "positive_price (check_constraint)"
130
- assert exception . message =~ "The changeset has not defined any constraint."
131
- assert exception . message =~ "call `check_constraint/3`"
132
-
133
- # When the changeset does expect the db error, but doesn't give a custom message
134
- { :error , changeset } =
135
- changeset
136
- |> Ecto.Changeset . check_constraint ( :price , name: :positive_price )
137
- |> PoolRepo . insert ( )
138
- assert changeset . errors == [ price: { "is invalid" , [ constraint: :check , constraint_name: "positive_price" ] } ]
139
- assert changeset . data . __meta__ . state == :built
140
-
141
- # When the changeset does expect the db error and gives a custom message
142
- changeset = Ecto.Changeset . change ( % Constraint { } , price: - 10 )
143
- { :error , changeset } =
144
- changeset
145
- |> Ecto.Changeset . check_constraint ( :price , name: :positive_price , message: "price must be greater than 0" )
146
- |> PoolRepo . insert ( )
147
- assert changeset . errors == [ price: { "price must be greater than 0" , [ constraint: :check , constraint_name: "positive_price" ] } ]
148
- assert changeset . data . __meta__ . state == :built
149
-
150
- # When the change does not violate the check constraint
151
- changeset = Ecto.Changeset . change ( % Constraint { } , price: 10 , from: 100 , to: 200 )
152
- { :ok , changeset } =
153
- changeset
154
- |> Ecto.Changeset . check_constraint ( :price , name: :positive_price , message: "price must be greater than 0" )
155
- |> PoolRepo . insert ( )
156
- assert is_integer ( changeset . id )
157
66
end
158
67
end
0 commit comments