Commit 97e91853 authored by Vitali Tatarintev's avatar Vitali Tatarintev Committed by Douglas Barbosa Alexandre

Replace optional: false with presence validation

The follow up of the following discussion:

https://gitlab.com/gitlab-org/gitlab
/-/merge_requests/24360#note_282247872
parent 704dd1d4
......@@ -9,9 +9,9 @@ class PrometheusAlert < ApplicationRecord
gt: ">"
}.freeze
belongs_to :environment, optional: false, validate: true, inverse_of: :prometheus_alerts
belongs_to :project, optional: false, validate: true, inverse_of: :prometheus_alerts
belongs_to :prometheus_metric, optional: false, validate: true, inverse_of: :prometheus_alerts
belongs_to :environment, validate: true, inverse_of: :prometheus_alerts
belongs_to :project, validate: true, inverse_of: :prometheus_alerts
belongs_to :prometheus_metric, validate: true, inverse_of: :prometheus_alerts
has_many :prometheus_alert_events, inverse_of: :prometheus_alert
has_many :related_issues, through: :prometheus_alert_events
......@@ -19,6 +19,7 @@ class PrometheusAlert < ApplicationRecord
after_save :clear_prometheus_adapter_cache!
after_destroy :clear_prometheus_adapter_cache!
validates :environment, :project, :prometheus_metric, presence: true
validate :require_valid_environment_project!
validate :require_valid_metric_project!
......
......@@ -31,8 +31,8 @@ describe PrometheusAlert do
end
describe 'associations' do
it { is_expected.to belong_to(:project).required }
it { is_expected.to belong_to(:environment).required }
it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:environment) }
end
describe 'project validations' do
......@@ -43,6 +43,10 @@ describe PrometheusAlert do
build(:prometheus_alert, prometheus_metric: metric, environment: environment, project: project)
end
it { is_expected.to validate_presence_of(:environment) }
it { is_expected.to validate_presence_of(:project) }
it { is_expected.to validate_presence_of(:prometheus_metric) }
context 'when environment and metric belongs same project' do
it { is_expected.to be_valid }
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment