diff options
| -rw-r--r-- | app/models/compliance_control.rb | 3 | ||||
| -rw-r--r-- | spec/models/generic_attribute_control/min_max_spec.rb | 52 | ||||
| -rw-r--r-- | spec/models/generic_attribute_control/pattern_spec.rb | 43 | ||||
| -rw-r--r-- | spec/models/generic_attribute_control/uniqueness_spec.rb | 42 | ||||
| -rw-r--r-- | spec/support/data_modifier.rb | 2 | ||||
| -rw-r--r-- | spec/support/data_modifier/box.rb | 5 | ||||
| -rw-r--r-- | spec/support/data_modifier/hash.rb | 16 | ||||
| -rw-r--r-- | spec/support/random.rb | 8 |
8 files changed, 146 insertions, 25 deletions
diff --git a/app/models/compliance_control.rb b/app/models/compliance_control.rb index 7317b81b8..0763de477 100644 --- a/app/models/compliance_control.rb +++ b/app/models/compliance_control.rb @@ -32,6 +32,9 @@ class ComplianceControl < ActiveRecord::Base self.name ||= self.class.name self.code ||= @@default_code self.origin_code ||= @@default_code + # TODO: Remove this very ambigous line, as a matter of fact it is never triggered + # **unless** `criticity` is **explicitly** set to nil, thusly bypassing the att's + # initialisation by `Enumerize`'s default. self.criticity ||= @@default_criticity end diff --git a/spec/models/generic_attribute_control/min_max_spec.rb b/spec/models/generic_attribute_control/min_max_spec.rb index b3a5d4b6f..b38032965 100644 --- a/spec/models/generic_attribute_control/min_max_spec.rb +++ b/spec/models/generic_attribute_control/min_max_spec.rb @@ -1,21 +1,53 @@ + +H = Support::DataModifier::Hash + +# Describing Behavior of ComplianceControl Class Wide Default Attributes (CCCWDA) RSpec.describe GenericAttributeControl::MinMax, type: :model do let( :default_code ){ "3-Generic-2" } - let( :default_criticity ){ :warning } + let( :default_criticity ){ 'info' } - context 'class attributes' do + context 'class attributes' do it 'are correctly set' do - expect( described_class.default_criticity ).to eq(default_criticity) + expect( described_class.default_criticity ).to eq(:warning) 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 ]} + 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' ]) + it 'all defaults' do + expect( record.attributes.values_at(*default_att_names )) + .to eq([ default_code, default_code, default_criticity]) + end + it 'but provided values are not overwritten by defaults' do + # atts = make_random_atts(code: :string, default_code: :string, criticity: %w[warning error]) + code = random_string + origin_code = random_string + criticity = random_element(%w[warning error]) + # Remove each of the attributes from explicit initialisation to see + # its value provided by CCCWDA. + # N.B. enum default (for criticity) takes precedence over the initializer + # unless nil is passed in explicitly (untested scenario for now, as + # we are suggestuing to remove `criticity` from CCCWDA. + + # atts :: Map(String, (explicit_value, default_value)) + atts = { + 'code' => [code, default_code], + 'origin_code' => [origin_code, default_code], + 'criticity' => [criticity, default_criticity] + } + atts.keys.each do |key| + # Replace key to be tested by default value + expected = H.first_values(atts).merge(key => atts[key].last) + expected_values = expected.values_at(*default_att_names) + # Remove key to be tested from atts passed to `#create` + construction_atts = H.first_values(atts).merge(key => nil).compact + explicit = create :uniqueness, construction_atts + + expect( explicit.attributes.values_at(*default_att_names )) + .to eq(expected_values) 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 bd5d56dd7..646bb735e 100644 --- a/spec/models/generic_attribute_control/pattern_spec.rb +++ b/spec/models/generic_attribute_control/pattern_spec.rb @@ -1,20 +1,53 @@ + +H = Support::DataModifier::Hash + +# Describing Behavior of ComplianceControl Class Wide Default Attributes (CCCWDA) RSpec.describe GenericAttributeControl::Pattern, type: :model do let( :default_code ){ "3-Generic-3" } - let( :default_criticity ){ :warning } + let( :default_criticity ){ 'info' } - context 'class attributes' do + context 'class attributes' do it 'are correctly set' do - expect( described_class.default_criticity ).to eq(default_criticity) + expect( described_class.default_criticity ).to eq(:warning) expect( described_class.default_code).to eq(default_code) end end context 'are used in instantiation' do - let( :record ){ create :pattern } + 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' ]) + .to eq([ default_code, default_code, default_criticity]) + end + it 'but provided values are not overwritten by defaults' do + # atts = make_random_atts(code: :string, default_code: :string, criticity: %w[warning error]) + code = random_string + origin_code = random_string + criticity = random_element(%w[warning error]) + # Remove each of the attributes from explicit initialisation to see + # its value provided by CCCWDA. + # N.B. enum default (for criticity) takes precedence over the initializer + # unless nil is passed in explicitly (untested scenario for now, as + # we are suggestuing to remove `criticity` from CCCWDA. + + # atts :: Map(String, (explicit_value, default_value)) + atts = { + 'code' => [code, default_code], + 'origin_code' => [origin_code, default_code], + 'criticity' => [criticity, default_criticity] + } + atts.keys.each do |key| + # Replace key to be tested by default value + expected = H.first_values(atts).merge(key => atts[key].last) + expected_values = expected.values_at(*default_att_names) + # Remove key to be tested from atts passed to `#create` + construction_atts = H.first_values(atts).merge(key => nil).compact + explicit = create :uniqueness, construction_atts + + expect( explicit.attributes.values_at(*default_att_names )) + .to eq(expected_values) + end end end end diff --git a/spec/models/generic_attribute_control/uniqueness_spec.rb b/spec/models/generic_attribute_control/uniqueness_spec.rb index 6b94b76f5..179b7142d 100644 --- a/spec/models/generic_attribute_control/uniqueness_spec.rb +++ b/spec/models/generic_attribute_control/uniqueness_spec.rb @@ -1,10 +1,13 @@ -RSpec.describe GenericAttributeControl::Uniqueness do +H = Support::DataModifier::Hash + +# Describing Behavior of ComplianceControl Class Wide Default Attributes (CCCWDA) +RSpec.describe GenericAttributeControl::Uniqueness, type: :model do let( :default_code ){ "3-Generic-3" } - let( :default_criticity ){ :warning } + let( :default_criticity ){ 'info' } - context 'class attributes' do + context 'class attributes' do it 'are correctly set' do - expect( described_class.default_criticity ).to eq(default_criticity) + expect( described_class.default_criticity ).to eq(:warning) expect( described_class.default_code).to eq(default_code) end end @@ -14,7 +17,36 @@ RSpec.describe GenericAttributeControl::Uniqueness do it 'all defaults' do expect( record.attributes.values_at(*default_att_names )) - .to eq([ default_code, default_code, 'info' ]) + .to eq([ default_code, default_code, default_criticity]) + end + it 'but provided values are not overwritten by defaults' do + # atts = make_random_atts(code: :string, default_code: :string, criticity: %w[warning error]) + code = random_string + origin_code = random_string + criticity = random_element(%w[warning error]) + # Remove each of the attributes from explicit initialisation to see + # its value provided by CCCWDA. + # N.B. enum default (for criticity) takes precedence over the initializer + # unless nil is passed in explicitly (untested scenario for now, as + # we are suggestuing to remove `criticity` from CCCWDA. + + # atts :: Map(String, (explicit_value, default_value)) + atts = { + 'code' => [code, default_code], + 'origin_code' => [origin_code, default_code], + 'criticity' => [criticity, default_criticity] + } + atts.keys.each do |key| + # Replace key to be tested by default value + expected = H.first_values(atts).merge(key => atts[key].last) + expected_values = expected.values_at(*default_att_names) + # Remove key to be tested from atts passed to `#create` + construction_atts = H.first_values(atts).merge(key => nil).compact + explicit = create :uniqueness, construction_atts + + expect( explicit.attributes.values_at(*default_att_names )) + .to eq(expected_values) + end end end end diff --git a/spec/support/data_modifier.rb b/spec/support/data_modifier.rb index 7fb0db6f6..2b3b00ac5 100644 --- a/spec/support/data_modifier.rb +++ b/spec/support/data_modifier.rb @@ -1,4 +1,5 @@ require_relative 'data_modifier/enum' +require_relative 'data_modifier/hash' module Support module DataModifier module InstanceMethods @@ -48,4 +49,5 @@ end RSpec.configure do | c | c.include Support::DataModifier::InstanceMethods, type: :checksum + c.include Support::DataModifier::InstanceMethods, type: :model end diff --git a/spec/support/data_modifier/box.rb b/spec/support/data_modifier/box.rb index 0fb95f743..0847b628c 100644 --- a/spec/support/data_modifier/box.rb +++ b/spec/support/data_modifier/box.rb @@ -1,3 +1,4 @@ +require_relative 'hash' module Support module DataModifier module Box @@ -7,9 +8,7 @@ module Support class << self def unbox atts - atts.inject Hash.new do | h, (k,v) | - h.merge(k => value_of(v)) - end + Hash.map_values(atts, method(:value_of)) end def value_of v self === v ? v.value : v diff --git a/spec/support/data_modifier/hash.rb b/spec/support/data_modifier/hash.rb new file mode 100644 index 000000000..05da3cf4f --- /dev/null +++ b/spec/support/data_modifier/hash.rb @@ -0,0 +1,16 @@ +module Support + module DataModifier + module Hash extend self + def map_values hashy, f=nil, &fn + raise ArgumentError, "need block or function arg" unless f = fn || f + hashy.inject({}){ |h, (k,v)| h.merge(k => f.(v)) } + end + def first_values ary_hash + map_values(ary_hash, &:first) + end + def last_values ary_hash + map_values(ary_hash, &:last) + end + end + end +end diff --git a/spec/support/random.rb b/spec/support/random.rb index f0168b9ef..59e1a1475 100644 --- a/spec/support/random.rb +++ b/spec/support/random.rb @@ -7,8 +7,12 @@ module Support SecureRandom.hex end - def random_int - (random_number * PRETTY_LARGE_INT).to_i + def random_element from + from[random_int(from.size)] + end + + def random_int max_plus_one=PRETTY_LARGE_INT + (random_number * max_plus_one).to_i end def random_number |
