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

replace legacy spec

ulferts701622b559f1
diff --git a/spec/models/custom_value_spec.rb b/spec/models/custom_value_spec.rb
index d1cb8c64bda..211e775d4e1 100644
--- a/spec/models/custom_value_spec.rb
+++ b/spec/models/custom_value_spec.rb
@@ -46,24 +46,24 @@ describe CustomValue do
let(:format) { 'bool' }
let(:value) { true }
- context 'is true' do
+ context 'when it is true' do
it { expect(subject.typed_value).to eql(value) }
end
- context 'is false' do
+ context 'when it is false' do
let(:value) { false }
it { expect(subject.typed_value).to eql(value) }
end
- context 'is nil' do
+ context 'when it is nil' do
let(:value) { nil }
it { expect(subject.typed_value).to eql(value) }
end
end
- describe 'integer custom value' do
+ describe 'string custom value' do
let(:format) { 'string' }
let(:value) { 'This is a string!' }
@@ -90,7 +90,7 @@ describe CustomValue do
it { expect(subject.typed_value).to eql(value) }
- context 'date format', with_settings: { date_format: '%Y/%m/%d' } do
+ context 'for a date format', with_settings: { date_format: '%Y/%m/%d' } do
it { expect(subject.formatted_value).to eq('2016/12/01') }
end
end
@@ -107,9 +107,320 @@ describe CustomValue do
end
end
+ describe '#valid?' do
+ let(:custom_field) { build_stubbed(:custom_field, field_format:, is_required:, min_length:, max_length:, regexp:) }
+ let(:custom_value) { described_class.new(custom_field:, value:) }
+ let(:is_required) { false }
+ let(:min_length) { 0 }
+ let(:max_length) { 0 }
+ let(:regexp) { nil }
+
+ context 'for a data custom field' do
+ let(:field_format) { 'date' }
+
+ context 'with a valid date' do
+ let(:value) { '1975-07-14' }
+
+ it 'is valid' do
+ expect(custom_value)
+ .to be_valid
+ end
+ end
+
+ context 'with some non date string' do
+ let(:value) { 'abc' }
+
+ it 'is invalid' do
+ expect(custom_value)
+ .not_to be_valid
+ end
+ end
+ end
+
+ context 'for a string custom field' do
+ let(:field_format) { 'string' }
+
+ context 'with some string' do
+ let(:value) { 'abc' }
+
+ it 'is invalid' do
+ expect(custom_value)
+ .to be_valid
+ end
+ end
+
+ context 'with a nil value' do
+ let(:value) { nil }
+
+ it 'is valid' do
+ expect(custom_value)
+ .to be_valid
+ end
+ end
+
+ context 'with an empty value' do
+ let(:value) { '' }
+
+ it 'is valid' do
+ expect(custom_value)
+ .to be_valid
+ end
+ end
+
+ context 'with a nil value when required' do
+ let(:value) { nil }
+ let(:is_required) { true }
+
+ it 'is invalid' do
+ expect(custom_value)
+ .not_to be_valid
+ end
+ end
+
+ context 'with an empty value when required' do
+ let(:value) { '' }
+ let(:is_required) { true }
+
+ it 'is invalid' do
+ expect(custom_value)
+ .not_to be_valid
+ end
+ end
+
+ context 'with an empty value when having a min_length' do
+ let(:value) { '' }
+ let(:min_length) { 1 }
+
+ it 'is valid' do
+ expect(custom_value)
+ .to be_valid
+ end
+ end
+
+ context 'with too short a value when having a min_length' do
+ let(:value) { 'a' }
+ let(:min_length) { 2 }
+
+ it 'is invalid' do
+ expect(custom_value)
+ .not_to be_valid
+ end
+ end
+
+ context 'with too long a value when having a max_length' do
+ let(:value) { 'a' * 6 }
+ let(:max_length) { 5 }
+
+ it 'is invalid' do
+ expect(custom_value)
+ .not_to be_valid
+ end
+ end
+
+ context 'with a value of the correct length when having a max_length and a min_value' do
+ let(:value) { 'a' * 4 }
+ let(:min_length) { 4 }
+ let(:max_length) { 4 }
+
+ it 'is invalid' do
+ expect(custom_value)
+ .to be_valid
+ end
+ end
+
+ context 'with an empty value when having a regexp' do
+ let(:value) { '' }
+ let(:regexp) { '^[A-Z0-9]*$' }
+
+ it 'is valid' do
+ expect(custom_value)
+ .to be_valid
+ end
+ end
+
+ context 'with a not matching value when having a regexp' do
+ let(:value) { 'a' }
+ let(:regexp) { '^[A-Z0-9]*$' }
+
+ it 'is invalid' do
+ expect(custom_value)
+ .not_to be_valid
+ end
+ end
+
+ context 'with a matching value when having a regexp' do
+ let(:value) { 'A' }
+ let(:regexp) { '^[A-Z0-9]*$' }
+
+ it 'is valid' do
+ expect(custom_value)
+ .to be_valid
+ end
+ end
+ end
+
+ context 'for a list custom field' do
+ let(:custom_option1) { build_stubbed(:custom_option, value: 'value1') }
+ let(:custom_option2) { build_stubbed(:custom_option, value: 'value1') }
+ let(:custom_field) { build_stubbed(:custom_field, field_format: 'list', custom_options: [custom_option1, custom_option2]) }
+
+ context 'with a value from the list' do
+ let(:value) { custom_option1.id }
+
+ it 'is valid' do
+ expect(custom_value)
+ .to be_valid
+ end
+ end
+
+ context 'with some string' do
+ let(:value) { 'abc' }
+
+ it 'is invalid' do
+ expect(custom_value)
+ .not_to be_valid
+ end
+ end
+
+ context 'with nil string' do
+ let(:value) { nil }
+
+ it 'is invalid' do
+ expect(custom_value)
+ .to be_valid
+ end
+ end
+ end
+
+ context 'for an int custom field' do
+ let(:field_format) { 'int' }
+
+ context 'with a valid int string' do
+ let(:value) { '123' }
+
+ it 'is valid' do
+ expect(custom_value)
+ .to be_valid
+ end
+ end
+
+ context 'with a valid negative int string' do
+ let(:value) { '-123' }
+
+ it 'is valid' do
+ expect(custom_value)
+ .to be_valid
+ end
+ end
+
+ context 'with a valid positive int string' do
+ let(:value) { '+123' }
+
+ it 'is valid' do
+ expect(custom_value)
+ .to be_valid
+ end
+ end
+
+ context 'with some non int string' do
+ let(:value) { 'abc' }
+
+ it 'is invalid' do
+ expect(custom_value)
+ .not_to be_valid
+ end
+ end
+
+ context 'with a float string' do
+ let(:value) { '5.5' }
+
+ it 'is invalid' do
+ expect(custom_value)
+ .not_to be_valid
+ end
+ end
+
+ context 'with an empty string' do
+ let(:value) { '' }
+
+ it 'is valid' do
+ expect(custom_value)
+ .to be_valid
+ end
+ end
+ end
+
+ context 'for a float custom field' do
+ let(:field_format) { 'float' }
+
+ context 'with a valid float string' do
+ let(:value) { '123.5' }
+
+ it 'is valid' do
+ expect(custom_value)
+ .to be_valid
+ end
+ end
+
+ context 'with a valid negative float string' do
+ let(:value) { '-123.5' }
+
+ it 'is valid' do
+ expect(custom_value)
+ .to be_valid
+ end
+ end
+
+ context 'with a valid positive float string' do
+ let(:value) { '+123.5' }
+
+ it 'is valid' do
+ expect(custom_value)
+ .to be_valid
+ end
+ end
+
+ context 'with some non float string' do
+ let(:value) { 'abc' }
+
+ it 'is invalid' do
+ expect(custom_value)
+ .not_to be_valid
+ end
+ end
+
+ context 'with an int string' do
+ let(:value) { '5' }
+
+ it 'is valid' do
+ expect(custom_value)
+ .to be_valid
+ end
+ end
+
+ context 'with an empty string' do
+ let(:value) { '' }
+
+ it 'is valid' do
+ expect(custom_value)
+ .to be_valid
+ end
+ end
+
+ context 'with a mixed string' do
+ let(:value) { '6.5a' }
+
+ it 'is invalid' do
+ expect(custom_value)
+ .not_to be_valid
+ end
+ end
+ end
+ end
+
describe 'value/value=' do
let(:custom_value) { build_stubbed(:custom_value) }
- let(:strategy_double) { double('strategy_double') }
+ let(:strategy_double) { instance_double(CustomValue::FormatStrategy) }
it 'calls the strategy for parsing and uses that value' do
original_value = 'original value'
diff --git a/spec_legacy/unit/custom_value_spec.rb b/spec_legacy/unit/custom_value_spec.rb
deleted file mode 100644
index 4feaceaf1d4..00000000000
--- a/spec_legacy/unit/custom_value_spec.rb
+++ /dev/null
@@ -1,133 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2022 the OpenProject GmbH
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2013 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See COPYRIGHT and LICENSE files for more details.
-#++
-require_relative '../legacy_spec_helper'
-
-describe CustomValue, type: :model do
- it 'strings field validation with blank value' do
- f = CustomField.new(field_format: 'string')
- v = CustomValue.new(custom_field: f)
-
- v.value = nil
- assert v.valid?
- v.value = ''
- assert v.valid?
-
- f.is_required = true
- v.value = nil
- assert !v.valid?
- v.value = ''
- assert !v.valid?
- end
-
- it 'strings field validation with min and max lengths' do
- f = CustomField.new(field_format: 'string', min_length: 2, max_length: 5)
- v = CustomValue.new(custom_field: f, value: '')
- assert v.valid?
- v.value = 'a'
- assert !v.valid?
- v.value = 'a' * 2
- assert v.valid?
- v.value = 'a' * 6
- assert !v.valid?
- end
-
- it 'strings field validation with regexp' do
- f = CustomField.new(field_format: 'string', regexp: '^[A-Z0-9]*$')
- v = CustomValue.new(custom_field: f, value: '')
- assert v.valid?
- v.value = 'abc'
- assert !v.valid?
- v.value = 'ABC'
- assert v.valid?
- end
-
- it 'dates field validation' do
- f = CustomField.new(field_format: 'date')
- v = CustomValue.new(custom_field: f, value: '')
- assert v.valid?
- v.value = 'abc'
- assert !v.valid?
- v.value = '1975-07-14'
- assert v.valid?
- end
-
- it 'lists field validation' do
- f = CustomField.create(field_format: 'list', possible_values: ['value1', 'value2'])
- v = CustomValue.new(custom_field: f, value: '')
- assert v.valid?
- v.value = 'abc'
- assert !v.valid?
- v.value = f.custom_options.first.id
- assert v.valid?
- end
-
- it 'ints field validation' do
- f = CustomField.new(field_format: 'int')
- v = CustomValue.new(custom_field: f, value: '')
- assert v.valid?
- v.value = 'abc'
- assert !v.valid?
- v.value = '123'
- assert v.valid?
- v.value = '+123'
- assert v.valid?
- v.value = '-123'
- assert v.valid?
- end
-
- it 'floats field validation' do
- user = create :user
- # There are cases, where the custom-value-table is not cleared completely,
- # therefore making double sure, that we have a clean slate before we start
- CustomField.destroy_all
- create :float_user_custom_field, name: 'Money'
- v = CustomValue.new(customized: user, custom_field: UserCustomField.find_by(name: 'Money'))
- v.value = '11.2'
- assert v.save
- v.value = ''
- assert v.save
- v.value = '-6.250'
- assert v.save
- v.value = '6a'
- assert !v.save
- end
-
- it 'stis polymorphic association' do
- # Rails uses top level sti class for polymorphic association. See #3978.
- user = create :user
- custom_field = create :user_custom_field, field_format: 'string'
- custom_value = create :principal_custom_value,
- custom_field: custom_field,
- customized: user,
- value: '01 23 45 67 89'
- user.reload
-
- assert !user.custom_values.empty?
- assert !custom_value.customized.nil?
- end
-end