|
| 1 | +defmodule Ecto.Integration.ConstraintsTest do |
| 2 | + use ExUnit.Case, async: true |
| 3 | + |
| 4 | + import Ecto.Migrator, only: [up: 4] |
| 5 | + alias Ecto.Integration.PoolRepo |
| 6 | + |
| 7 | + defmodule ConstraintMigration do |
| 8 | + use Ecto.Migration |
| 9 | + |
| 10 | + @table table(:constraints_test) |
| 11 | + |
| 12 | + def change do |
| 13 | + create @table do |
| 14 | + add :price, :integer |
| 15 | + add :from, :integer |
| 16 | + add :to, :integer |
| 17 | + end |
| 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 | + end |
| 38 | + end |
| 39 | + |
| 40 | + defmodule Constraint do |
| 41 | + use Ecto.Integration.Schema |
| 42 | + |
| 43 | + schema "constraints_test" do |
| 44 | + field :price, :integer |
| 45 | + field :from, :integer |
| 46 | + field :to, :integer |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + @base_migration 2_000_000 |
| 51 | + |
| 52 | + setup_all do |
| 53 | + ExUnit.CaptureLog.capture_log(fn -> |
| 54 | + num = @base_migration + System.unique_integer([:positive]) |
| 55 | + up(PoolRepo, num, ConstraintMigration, log: false) |
| 56 | + end) |
| 57 | + |
| 58 | + :ok |
| 59 | + end |
| 60 | + |
| 61 | + test "creating, using, and dropping an exclusion constraint" do |
| 62 | + changeset = Ecto.Changeset.change(%Constraint{}, from: 0, to: 10) |
| 63 | + {:ok, _} = PoolRepo.insert(changeset) |
| 64 | + |
| 65 | + non_overlapping_changeset = Ecto.Changeset.change(%Constraint{}, from: 11, to: 12) |
| 66 | + {:ok, _} = PoolRepo.insert(non_overlapping_changeset) |
| 67 | + |
| 68 | + overlapping_changeset = Ecto.Changeset.change(%Constraint{}, from: 1900, to: 12) |
| 69 | + |
| 70 | + exception = |
| 71 | + assert_raise Ecto.ConstraintError, ~r/constraint error when attempting to insert struct/, fn -> |
| 72 | + PoolRepo.insert(overlapping_changeset) |
| 73 | + end |
| 74 | + assert exception.message =~ "cannot_overlap (check_constraint)" |
| 75 | + assert exception.message =~ "The changeset has not defined any constraint." |
| 76 | + assert exception.message =~ "call `check_constraint/3`" |
| 77 | + |
| 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 | + {:error, changeset} = |
| 91 | + overlapping_changeset |
| 92 | + |> Ecto.Changeset.check_constraint(:from, name: :cannot_overlap) |
| 93 | + |> PoolRepo.insert() |
| 94 | + assert changeset.errors == [from: {"is invalid", [constraint: :check, constraint_name: "cannot_overlap"]}] |
| 95 | + 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 | + end |
| 158 | +end |
0 commit comments