Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

add test validation for lag with specific text

Behrokh Satarnejad7918c9a16281
diff --git a/app/contracts/relations/base_contract.rb b/app/contracts/relations/base_contract.rb
index 37a4f452ea8..eaf0ebbe408 100644
--- a/app/contracts/relations/base_contract.rb
+++ b/app/contracts/relations/base_contract.rb
@@ -75,9 +75,12 @@ module Relations
end
def validate_lag_numericality
- unless model.lag.nil? || model.lag.between?(0, 2_147_483_647)
- errors.add :lag, :less_than_or_equal_to if model.lag > 2_147_483_647
- errors.add :lag, :greater_than_or_equal_to if model.lag < 0
+ if model.lag.present?
+ if model.lag < 0
+ errors.add :lag, I18n.t(:"activerecord.errors.messages.circular_dependency")
+ elsif model.lag > 2_147_483_647
+ errors.add :lag, I18n.t(:"activerecord.errors.models.relation.attributes.lag.high_value")
+ end
end
end
diff --git a/app/models/relation.rb b/app/models/relation.rb
index 4a5bf201f0a..eb56291542f 100644
--- a/app/models/relation.rb
+++ b/app/models/relation.rb
@@ -103,7 +103,18 @@ class Relation < ApplicationRecord
scope :follows_with_lag,
-> { follows.where("lag > 0") }
- validates :lag, numericality: { allow_nil: true, less_than_or_equal_to: 2_147_483_647, greater_than_or_equal_to: 0 }
+ validates :lag, numericality: {
+ allow_nil: true,
+ less_than_or_equal_to: 2_147_483_647,
+ greater_than_or_equal_to: 0,
+ message: ->(_, data) {
+ if data[:value] < 0
+ I18n.t("activerecord.errors.models.relation.attributes.lag.negative_value")
+ else
+ I18n.t("activerecord.errors.models.relation.attributes.lag.high_value")
+ end
+ }
+ }
validates :to, uniqueness: { scope: :from }
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 73e08a13e6a..a99cb348827 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1307,6 +1307,9 @@ en:
from:
error_not_found: "work package in `from` position not found or not visible"
error_readonly: "an existing relation's `from` link is immutable"
+ lag:
+ high_value: "The value for lag is too high."
+ negative_value: "Lag cannot be negative."
repository:
not_available: "SCM vendor is not available"
not_whitelisted: "is not allowed by the configuration."
diff --git a/spec/models/relation_spec.rb b/spec/models/relation_spec.rb
index f281e18e0c8..32252836b1e 100644
--- a/spec/models/relation_spec.rb
+++ b/spec/models/relation_spec.rb
@@ -38,11 +38,11 @@ RSpec.describe Relation do
it "validates lag numericality" do
relation.lag = -1
expect(relation).not_to be_valid
- expect(relation.errors[:lag]).to include("must be greater than or equal to 0.")
+ expect(relation.errors[:lag]).to include(I18n.t(:"activerecord.errors.models.relation.attributes.lag.negative_value"))
relation.lag = 2_147_483_648
expect(relation).not_to be_valid
- expect(relation.errors[:lag]).to include("must be less than or equal to 2147483647.")
+ expect(relation.errors[:lag]).to include(I18n.t(:"activerecord.errors.models.relation.attributes.lag.high_value"))
relation.lag = 1_000
expect(relation).to be_valid