Skip to content

Commit

Permalink
make it optional whether to include blank values in linked data exten…
Browse files Browse the repository at this point in the history
…ded context
  • Loading branch information
elrayle committed Jun 7, 2019
1 parent 62d167d commit a6c6c9f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
7 changes: 7 additions & 0 deletions app/models/qa/linked_data/config/context_property_map.rb
Expand Up @@ -37,6 +37,7 @@ def initialize(property_map = {}, prefixes = {})
@ldpath = Qa::LinkedData::Config::Helper.fetch_required(property_map, :ldpath, false)
@selectable = Qa::LinkedData::Config::Helper.fetch_boolean(property_map, :selectable, false)
@drillable = Qa::LinkedData::Config::Helper.fetch_boolean(property_map, :drillable, false)
@optional = Qa::LinkedData::Config::Helper.fetch_boolean(property_map, :optional, false)
@expansion_label_ldpath = Qa::LinkedData::Config::Helper.fetch(property_map, :expansion_label_ldpath, nil)
@expansion_id_ldpath = Qa::LinkedData::Config::Helper.fetch(property_map, :expansion_id_ldpath, nil)
@prefixes = prefixes
Expand All @@ -54,6 +55,12 @@ def drillable?
@drillable
end

# Should this property always be included in the extended context or is it optional (i.e. only shown if it has a value)
# @return [Boolean] true if this property is optional and will only be included in extended context if it has a value; otherwise, false
def optional?
@optional
end

def group?
group_id.present?
end
Expand Down
4 changes: 4 additions & 0 deletions app/services/qa/linked_data/mapper/context_mapper_service.rb
Expand Up @@ -37,7 +37,11 @@ def populate_property_map(context_map, property_map, graph, subject_uri)
values = Qa::LinkedData::Config::ContextPropertyMap::VALUE_ON_ERROR
error = e.message
end
return {} if values.blank? && property_map.optional?
property_info(values, error, context_map, property_map)
end

def property_info(values, error, context_map, property_map)
property_info = {}
property_info["group"] = context_map.group_label(property_map.group_id) if property_map.group?
property_info["property"] = property_map.label
Expand Down
32 changes: 30 additions & 2 deletions spec/services/linked_data/mapper/context_mapper_service_spec.rb
Expand Up @@ -7,17 +7,19 @@
let(:context_map) { instance_double(Qa::LinkedData::Config::ContextMap) }
let(:subject_uri) { instance_double(RDF::URI) }

let(:context_properties) { [birth_date_property_map, death_date_property_map, occupation_property_map] }
let(:context_properties) { [birth_date_property_map, death_date_property_map, occupation_property_map, missing_property_map] }

let(:birth_date_property_map) { instance_double(Qa::LinkedData::Config::ContextPropertyMap) }
let(:death_date_property_map) { instance_double(Qa::LinkedData::Config::ContextPropertyMap) }
let(:occupation_property_map) { instance_double(Qa::LinkedData::Config::ContextPropertyMap) }
let(:missing_property_map) { instance_double(Qa::LinkedData::Config::ContextPropertyMap) }

let(:group_id) { 'dates' }

let(:birth_date_values) { ['10/15/1943'] }
let(:death_date_values) { ['12/17/2018'] }
let(:occupation_values) { ['Actress', 'Director', 'Producer'] }
let(:missing_values) { [] }

before do
allow(context_map).to receive(:properties).and_return(context_properties)
Expand All @@ -29,20 +31,31 @@
allow(birth_date_property_map).to receive(:selectable?).and_return(false)
allow(birth_date_property_map).to receive(:drillable?).and_return(false)
allow(birth_date_property_map).to receive(:expand_uri?).and_return(false)
allow(birth_date_property_map).to receive(:optional?).and_return(false)

allow(death_date_property_map).to receive(:label).and_return('Death')
allow(death_date_property_map).to receive(:values).with(graph, subject_uri).and_return(death_date_values)
allow(death_date_property_map).to receive(:group?).and_return(false)
allow(death_date_property_map).to receive(:selectable?).and_return(false)
allow(death_date_property_map).to receive(:drillable?).and_return(false)
allow(death_date_property_map).to receive(:expand_uri?).and_return(false)
allow(death_date_property_map).to receive(:optional?).and_return(false)

allow(occupation_property_map).to receive(:label).and_return('Occupation')
allow(occupation_property_map).to receive(:values).with(graph, subject_uri).and_return(occupation_values)
allow(occupation_property_map).to receive(:group?).and_return(false)
allow(occupation_property_map).to receive(:selectable?).and_return(false)
allow(occupation_property_map).to receive(:drillable?).and_return(false)
allow(occupation_property_map).to receive(:expand_uri?).and_return(false)
allow(occupation_property_map).to receive(:optional?).and_return(false)

allow(missing_property_map).to receive(:label).and_return('Property with NO values')
allow(missing_property_map).to receive(:values).with(graph, subject_uri).and_return(missing_values)
allow(missing_property_map).to receive(:group?).and_return(false)
allow(missing_property_map).to receive(:selectable?).and_return(false)
allow(missing_property_map).to receive(:drillable?).and_return(false)
allow(missing_property_map).to receive(:expand_uri?).and_return(false)
allow(missing_property_map).to receive(:optional?).and_return(true)
end

describe '.map_context' do
Expand Down Expand Up @@ -115,6 +128,21 @@
end
end

context 'when optional? is false' do
before { allow(missing_property_map).to receive(:optional?).and_return(false) }
it 'includes property with blank values' do
result = find_property_to_test(subject, 'Property with NO values')
expect(result['values']).to eq []
end
end

context 'when optional? is true' do
before { allow(missing_property_map).to receive(:optional?).and_return(true) }
it 'property with blank values is not added to results' do
expect { find_property_to_test(subject, 'Property with NO values') }.to raise_error StandardError, "property 'Property with NO values' not found"
end
end

context 'when error occurs' do
let(:cause) { I18n.t('qa.linked_data.ldpath.parse_error') }
before { allow(occupation_property_map).to receive(:values).with(graph, subject_uri).and_raise(cause) }
Expand All @@ -132,6 +160,6 @@ def find_property_to_test(results, label)
next unless r['property'] == label
return r
end
raise "property (#{label}) to test not found"
raise StandardError, "property '#{label}' not found"
end
end

0 comments on commit a6c6c9f

Please sign in to comment.