diff options
| author | Robert | 2017-09-29 17:55:58 +0200 |
|---|---|---|
| committer | Robert | 2017-10-02 10:52:07 +0200 |
| commit | f034dc2999a56295178c64921e6f059adc1a7bd9 (patch) | |
| tree | d9cf98cd0df42e7f776a727e09fefb02499a6d59 | |
| parent | ea6537e2f520ca9abba0ca63c68ee42afe08d872 (diff) | |
| download | chouette-core-f034dc2999a56295178c64921e6f059adc1a7bd9.tar.bz2 | |
Refs: #4627@0.75h;
Got some data_modifier support for attribute generation w/w-o defaults for model tests generic_attribute_control
| -rw-r--r-- | spec/factories/compliance_controls.rb | 18 | ||||
| -rw-r--r-- | spec/models/generic_attribute_control/min_max_spec.rb | 19 | ||||
| -rw-r--r-- | spec/models/generic_attribute_control/pattern_spec.rb | 18 | ||||
| -rw-r--r-- | spec/models/generic_attribute_control/uniqueness_spec.rb | 16 | ||||
| -rw-r--r-- | spec/support/data_modifier.rb | 51 | ||||
| -rw-r--r-- | spec/support/data_modifier/bool.rb | 18 | ||||
| -rw-r--r-- | spec/support/data_modifier/box.rb | 22 | ||||
| -rw-r--r-- | spec/support/data_modifier/enum.rb | 19 | ||||
| -rw-r--r-- | spec/support/random.rb | 26 |
9 files changed, 199 insertions, 8 deletions
diff --git a/spec/factories/compliance_controls.rb b/spec/factories/compliance_controls.rb index 83169b13a..b01e49011 100644 --- a/spec/factories/compliance_controls.rb +++ b/spec/factories/compliance_controls.rb @@ -9,4 +9,22 @@ FactoryGirl.define do association :compliance_control_set association :compliance_control_block end + + factory :min_max, class: 'GenericAttributeControl::MinMax' do + sequence(:name) { |n| "MinMax control #{n}" } + association :compliance_control_set + association :compliance_control_block + end + + factory :pattern, class: 'GenericAttributeControl::Pattern' do + sequence(:name) { |n| "Pattern control #{n}" } + association :compliance_control_set + association :compliance_control_block + end + + factory :uniqueness, class: 'GenericAttributeControl::Uniqueness' do + sequence(:name) { |n| "Uniqueness control #{n}" } + association :compliance_control_set + association :compliance_control_block + end end diff --git a/spec/models/generic_attribute_control/min_max_spec.rb b/spec/models/generic_attribute_control/min_max_spec.rb index 5eb8ed74e..b3a5d4b6f 100644 --- a/spec/models/generic_attribute_control/min_max_spec.rb +++ b/spec/models/generic_attribute_control/min_max_spec.rb @@ -1,8 +1,21 @@ -RSpec.describe GenericAttributeControl::MinMax do +RSpec.describe GenericAttributeControl::MinMax, type: :model do + let( :default_code ){ "3-Generic-2" } + let( :default_criticity ){ :warning } + context 'class attributes' do it 'are correctly set' do - expect( described_class.default_criticity ).to eq(:warning) - expect( described_class.default_code).to eq("3-Generic-2") + expect( described_class.default_criticity ).to eq(default_criticity) + expect( described_class.default_code).to eq(default_code) + end + context 'are used in instantiation' do + let( :record ){ create :min_max } + let( :default_att_names ){%w[ code origin_code criticity ]} + + it 'all defaults' do + expect( record.attributes.values_at(*default_att_names )) + .to eq([ default_code, default_code, 'info' ]) + end end end + end diff --git a/spec/models/generic_attribute_control/pattern_spec.rb b/spec/models/generic_attribute_control/pattern_spec.rb index d1f2dcb5f..bd5d56dd7 100644 --- a/spec/models/generic_attribute_control/pattern_spec.rb +++ b/spec/models/generic_attribute_control/pattern_spec.rb @@ -1,8 +1,20 @@ -RSpec.describe GenericAttributeControl::Pattern do +RSpec.describe GenericAttributeControl::Pattern, type: :model do + let( :default_code ){ "3-Generic-3" } + let( :default_criticity ){ :warning } + context 'class attributes' do it 'are correctly set' do - expect( described_class.default_criticity ).to eq( :warning ) - expect( described_class.default_code ).to eq( "3-Generic-3" ) + expect( described_class.default_criticity ).to eq(default_criticity) + expect( described_class.default_code).to eq(default_code) + end + end + context 'are used in instantiation' do + let( :record ){ create :pattern } + let( :default_att_names ){%w[ code origin_code criticity ]} + + it 'all defaults' do + expect( record.attributes.values_at(*default_att_names )) + .to eq([ default_code, default_code, 'info' ]) end end end diff --git a/spec/models/generic_attribute_control/uniqueness_spec.rb b/spec/models/generic_attribute_control/uniqueness_spec.rb index dbf4feff4..6b94b76f5 100644 --- a/spec/models/generic_attribute_control/uniqueness_spec.rb +++ b/spec/models/generic_attribute_control/uniqueness_spec.rb @@ -1,8 +1,20 @@ RSpec.describe GenericAttributeControl::Uniqueness do + let( :default_code ){ "3-Generic-3" } + let( :default_criticity ){ :warning } + context 'class attributes' do it 'are correctly set' do - expect( described_class.default_criticity ).to eq( :warning ) - expect( described_class.default_code ).to eq( "3-Generic-3" ) + expect( described_class.default_criticity ).to eq(default_criticity) + expect( described_class.default_code).to eq(default_code) + end + end + context 'are used in instantiation' do + let( :record ){ create :uniqueness } + let( :default_att_names ){%w[ code origin_code criticity ]} + + it 'all defaults' do + expect( record.attributes.values_at(*default_att_names )) + .to eq([ default_code, default_code, 'info' ]) end end end diff --git a/spec/support/data_modifier.rb b/spec/support/data_modifier.rb new file mode 100644 index 000000000..7fb0db6f6 --- /dev/null +++ b/spec/support/data_modifier.rb @@ -0,0 +1,51 @@ +require_relative 'data_modifier/enum' +module Support + module DataModifier + module InstanceMethods + CannotModify = Class.new RuntimeError + + def advance_values(atts, *keys) + keys.inject(atts){ |h, k| h.merge( k => atts[k].next) } + end + + # return array of atts wich each value modified, unboxing + # values if needed + def modify_atts(base_atts) + base_atts.keys.map do | key | + modify_att base_atts, key + end.compact + end + + private + def modify_att atts, key + atts.merge(key => modify_value(atts[key])) + rescue CannotModify + nil + end + def modify_value value + case value + when String + "#{value}." + when Fixnum + value + 1 + when TrueClass + false + when FalseClass + true + when Float + value * 1.1 + when Date + value + 1.day + when Box + value.next.value + else + raise CannotModify + end + end + end + end +end + +RSpec.configure do | c | + c.include Support::DataModifier::InstanceMethods, type: :checksum +end diff --git a/spec/support/data_modifier/bool.rb b/spec/support/data_modifier/bool.rb new file mode 100644 index 000000000..f4be9bd89 --- /dev/null +++ b/spec/support/data_modifier/bool.rb @@ -0,0 +1,18 @@ +require_relative 'box' + +module Support + module DataModifier + + class BoolBox + include Box + attr_reader :value + + def initialize value + @value = value + end + def next + self.class.new(!value) + end + end + end +end diff --git a/spec/support/data_modifier/box.rb b/spec/support/data_modifier/box.rb new file mode 100644 index 000000000..0fb95f743 --- /dev/null +++ b/spec/support/data_modifier/box.rb @@ -0,0 +1,22 @@ +module Support + module DataModifier + module Box + def next + raise "Need to implement #{__method__} in #{self.class}" + end + + class << self + def unbox atts + atts.inject Hash.new do | h, (k,v) | + h.merge(k => value_of(v)) + end + end + def value_of v + self === v ? v.value : v + end + end + end + + end +end + diff --git a/spec/support/data_modifier/enum.rb b/spec/support/data_modifier/enum.rb new file mode 100644 index 000000000..c8a6fe573 --- /dev/null +++ b/spec/support/data_modifier/enum.rb @@ -0,0 +1,19 @@ +require_relative 'box' + +module Support + module DataModifier + + class EnumBox + include Box + attr_reader :value, :values + + def initialize *enum_values + @values = enum_values + @value = @values.first + end + def next + self.class.new(*(@values[1..-1] << @values.first)) + end + end + end +end diff --git a/spec/support/random.rb b/spec/support/random.rb new file mode 100644 index 000000000..f0168b9ef --- /dev/null +++ b/spec/support/random.rb @@ -0,0 +1,26 @@ +module Support + module Random + + PRETTY_LARGE_INT = 1 << 30 + + def random_hex + SecureRandom.hex + end + + def random_int + (random_number * PRETTY_LARGE_INT).to_i + end + + def random_number + SecureRandom.random_number + end + + def random_string + SecureRandom.urlsafe_base64 + end + end +end + +RSpec.configure do | c | + c.include Support::Random +end |
