/
context_mapper_service_spec.rb
137 lines (116 loc) · 5.84 KB
/
context_mapper_service_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require 'spec_helper'
RSpec.describe Qa::LinkedData::Mapper::ContextMapperService do
subject { described_class.map_context(graph: graph, context_map: context_map, subject_uri: subject_uri) }
let(:graph) { instance_double(RDF::Graph) }
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(: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(:group_id) { 'dates' }
let(:birth_date_values) { ['10/15/1943'] }
let(:death_date_values) { ['12/17/2018'] }
let(:occupation_values) { ['Actress', 'Director', 'Producer'] }
before do
allow(context_map).to receive(:properties).and_return(context_properties)
allow(context_map).to receive(:group_label).with('dates').and_return('Dates')
allow(birth_date_property_map).to receive(:label).and_return('Birth')
allow(birth_date_property_map).to receive(:values).with(graph, subject_uri).and_return(birth_date_values)
allow(birth_date_property_map).to receive(:group?).and_return(false)
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(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(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)
end
describe '.map_context' do
it 'sets the property labels from the property map' do
find_property_to_test(subject, 'Birth')
find_property_to_test(subject, 'Death')
find_property_to_test(subject, 'Occupation')
expect(subject.size).to be 3
end
it 'sets the property values from the graph' do
result = find_property_to_test(subject, 'Birth')
expect(result['values']).to match_array birth_date_values
result = find_property_to_test(subject, 'Death')
expect(result['values']).to match_array death_date_values
result = find_property_to_test(subject, 'Occupation')
expect(result['values']).to match_array occupation_values
end
context 'when group? is false' do
before { allow(birth_date_property_map).to receive(:group?).and_return(false) }
it 'does not include group in results' do
result = find_property_to_test(subject, 'Birth')
expect(result.key?('group')).to be false
end
end
context 'when group? is true' do
before do
allow(birth_date_property_map).to receive(:group?).and_return(true)
allow(birth_date_property_map).to receive(:group_id).and_return('dates')
allow(context_map).to receive(:group_label).with('dates').and_return('Dates')
end
it 'includes group in results' do
result = find_property_to_test(subject, 'Birth')
expect(result['group']).to eq 'Dates'
end
end
context 'when drillable? is false' do
before { allow(death_date_property_map).to receive(:drillable?).and_return(false) }
it 'includes drillable set to false' do
result = find_property_to_test(subject, 'Death')
expect(result['drillable']).to be false
end
end
context 'when drillable? is true' do
before { allow(death_date_property_map).to receive(:drillable?).and_return(true) }
it 'includes drillable set to true' do
result = find_property_to_test(subject, 'Death')
expect(result['drillable']).to be true
end
end
context 'when selectable? is false' do
before { allow(occupation_property_map).to receive(:selectable?).and_return(false) }
it 'includes selectable set to false' do
result = find_property_to_test(subject, 'Occupation')
expect(result['selectable']).to be false
end
end
context 'when selectable? is true' do
before { allow(occupation_property_map).to receive(:selectable?).and_return(true) }
it 'includes selectable set to true' do
result = find_property_to_test(subject, 'Occupation')
expect(result['selectable']).to be true
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) }
it 'includes error message and empty value array' do
result = find_property_to_test(subject, 'Occupation')
expect(result.key?('error')).to be true
expect(result['error']).to eq cause
expect(result['values']).to match_array([])
end
end
end
def find_property_to_test(results, label)
results.each do |r|
next unless r['property'] == label
return r
end
raise "property (#{label}) to test not found"
end
end