|
17 | 17 | context 'when ldpath_program gets parse error' do
|
18 | 18 | let(:cause) { "undefined method `ascii_tree' for nil:NilClass" }
|
19 | 19 | let(:warning) { I18n.t('qa.linked_data.ldpath.parse_logger_error') }
|
20 |
| - let(:program_code) { "@prefix skos : <http://www.w3.org/2004/02/skos/core#> ;\nproperty = skos:prefLabel ::xsd:string ;" } |
| 20 | + let(:program_code) { "BAD_PROGRAM ;" } |
21 | 21 | let(:log_message) { "WARNING: #{warning}... cause: #{cause}\n ldpath_program=\n#{program_code}" }
|
22 | 22 |
|
23 |
| - before { allow(Ldpath::Program).to receive(:parse).with(anything).and_raise(cause) } |
| 23 | + before do |
| 24 | + allow(described_class).to receive(:ldpath_program_code).with(anything).and_return(program_code) |
| 25 | + allow(Ldpath::Program).to receive(:parse).with(anything).and_raise(cause) |
| 26 | + end |
24 | 27 |
|
25 | 28 | it 'logs error and returns PARSE ERROR as the value' do
|
26 | 29 | expect(Rails.logger).to receive(:warn).with(log_message)
|
|
29 | 32 | end
|
30 | 33 | end
|
31 | 34 |
|
| 35 | + describe '.ldpath_program_code' do |
| 36 | + subject { described_class.ldpath_program_code(ldpath: ldpath, prefixes: prefixes, languages: languages) } |
| 37 | + |
| 38 | + context 'for a ldpath without language pattern' do |
| 39 | + let(:ldpath) { 'dcterms:identifier' } |
| 40 | + let(:languages) { [:fr] } |
| 41 | + let(:prefixes) { { "dcterms" => "http://purl.org/dc/terms/" } } |
| 42 | + it 'generates the simple program code' do |
| 43 | + expected_program = <<-PROGRAM |
| 44 | +@prefix dcterms : <http://purl.org/dc/terms/> \; |
| 45 | +property = dcterms:identifier \; |
| 46 | +PROGRAM |
| 47 | + expect(subject).to eq expected_program |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + context 'for a ldpath with language pattern' do |
| 52 | + let(:ldpath) { 'madsrdf:authoritativeLabel*LANG* ::xsd:string' } |
| 53 | + let(:prefixes) { { "madsrdf" => "http://www.loc.gov/mads/rdf/v1#" } } |
| 54 | + context 'and no languages specified' do |
| 55 | + let(:languages) { nil } |
| 56 | + it 'generates the simple program code' do |
| 57 | + expected_program = <<-PROGRAM |
| 58 | +@prefix madsrdf : <http://www.loc.gov/mads/rdf/v1#> \; |
| 59 | +property = madsrdf:authoritativeLabel ::xsd:string \; |
| 60 | +PROGRAM |
| 61 | + expect(subject).to eq expected_program |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + context 'and one language specified' do |
| 66 | + let(:languages) { [:en] } |
| 67 | + it 'generates a program with the language' do |
| 68 | + expected_program = <<-PROGRAM |
| 69 | +@prefix madsrdf : <http://www.loc.gov/mads/rdf/v1#> \; |
| 70 | +en_property = madsrdf:authoritativeLabel[@en] ::xsd:string \; |
| 71 | +property = madsrdf:authoritativeLabel[@none] ::xsd:string \; |
| 72 | +PROGRAM |
| 73 | + expect(subject).to eq expected_program |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + context 'and multiple languages specified' do |
| 78 | + let(:languages) { [:fr, :de] } |
| 79 | + it 'generates a program with languages' do |
| 80 | + expected_program = <<-PROGRAM |
| 81 | +@prefix madsrdf : <http://www.loc.gov/mads/rdf/v1#> \; |
| 82 | +fr_property = madsrdf:authoritativeLabel[@fr] ::xsd:string \; |
| 83 | +de_property = madsrdf:authoritativeLabel[@de] ::xsd:string \; |
| 84 | +property = madsrdf:authoritativeLabel[@none] ::xsd:string \; |
| 85 | +PROGRAM |
| 86 | + expect(subject).to eq expected_program |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | + end |
| 91 | + |
32 | 92 | describe '.ldpath_evaluate' do
|
33 | 93 | subject { described_class.ldpath_evaluate(program: program, graph: graph, subject_uri: subject_uri) }
|
34 | 94 |
|
35 | 95 | let(:program) { instance_double(Ldpath::Program) }
|
36 | 96 | let(:graph) { instance_double(RDF::Graph) }
|
37 | 97 | let(:subject_uri) { instance_double(RDF::URI) }
|
38 |
| - let(:values) { ['Expanded Label'] } |
39 | 98 |
|
40 | 99 | before do
|
41 |
| - allow(Ldpath::Program).to receive(:parse).with('property = skos:prefLabel ::xsd:string ;').and_return(program) |
42 |
| - allow(program).to receive(:evaluate).with(subject_uri, context: graph, limit_to_context: true).and_return('property' => values) |
| 100 | + allow(Ldpath::Program).to receive(:parse).with(anything).and_return(program) |
43 | 101 | end
|
44 |
| - it 'returns the extracted label' do |
45 |
| - expect(subject).to match_array values |
| 102 | + |
| 103 | + context 'when program does not contain languages' do |
| 104 | + context 'and value is a string' do |
| 105 | + let(:values) { ['value'] } |
| 106 | + before do |
| 107 | + allow(program).to receive(:evaluate) |
| 108 | + .with(subject_uri, context: graph, limit_to_context: true) |
| 109 | + .and_return('property' => values) |
| 110 | + end |
| 111 | + it 'returns the string values as is' do |
| 112 | + expected_values = values.map { |v| RDF::Literal.new(v) } |
| 113 | + expect(subject).to match_array expected_values |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + context 'and value is a URI' do |
| 118 | + let(:values) { [RDF::URI.new('http://example.com/1'), RDF::URI.new('http://example.com/2')] } |
| 119 | + before do |
| 120 | + allow(program).to receive(:evaluate) |
| 121 | + .with(subject_uri, context: graph, limit_to_context: true) |
| 122 | + .and_return('property' => values) |
| 123 | + end |
| 124 | + it 'returns the URIs' do |
| 125 | + expected_values = values |
| 126 | + expect(subject).to match_array expected_values |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + context 'and value is numeric' do |
| 131 | + let(:values) { [23, 14, 55] } |
| 132 | + before do |
| 133 | + allow(program).to receive(:evaluate) |
| 134 | + .with(subject_uri, context: graph, limit_to_context: true) |
| 135 | + .and_return('property' => values) |
| 136 | + end |
| 137 | + it 'returns the URIs' do |
| 138 | + expected_values = values |
| 139 | + expect(subject).to match_array expected_values |
| 140 | + end |
| 141 | + end |
| 142 | + end |
| 143 | + |
| 144 | + context 'when program has languages' do |
| 145 | + context 'and one language specified' do |
| 146 | + let(:en_values) { ['en_value'] } |
| 147 | + let(:untagged_values) { ['untagged_value'] } |
| 148 | + before do |
| 149 | + allow(program).to receive(:evaluate) |
| 150 | + .with(subject_uri, context: graph, limit_to_context: true) |
| 151 | + .and_return('en_property' => en_values, 'property' => untagged_values) |
| 152 | + end |
| 153 | + it 'generates a program with the language' do |
| 154 | + expected_values = |
| 155 | + en_values.map { |v| RDF::Literal.new(v, language: :en) } + |
| 156 | + untagged_values.map { |v| RDF::Literal.new(v) } |
| 157 | + expect(subject).to match_array expected_values |
| 158 | + end |
| 159 | + end |
| 160 | + |
| 161 | + context 'and multiple languages specified' do |
| 162 | + let(:fr_values) { ['fr_value1', 'fr_value2', 'fr_value1'] } |
| 163 | + let(:de_values) { ['de_value'] } |
| 164 | + let(:untagged_values) { ['untagged_value'] } |
| 165 | + before do |
| 166 | + allow(program).to receive(:evaluate) |
| 167 | + .with(subject_uri, context: graph, limit_to_context: true) |
| 168 | + .and_return('fr_property' => fr_values, 'de_property' => de_values, 'property' => untagged_values) |
| 169 | + end |
| 170 | + it 'returns the extracted label' do |
| 171 | + expected_values = |
| 172 | + (fr_values.uniq.map { |v| RDF::Literal.new(v, language: :fr) } + |
| 173 | + de_values.map { |v| RDF::Literal.new(v, language: :de) } + |
| 174 | + untagged_values.map { |v| RDF::Literal.new(v) }).uniq |
| 175 | + expect(subject).to match_array expected_values |
| 176 | + end |
| 177 | + end |
46 | 178 | end
|
47 | 179 |
|
48 | 180 | context 'when ldpath_evaluate gets parse error' do
|
49 | 181 | let(:cause) { "unknown cause" }
|
50 | 182 | let(:warning) { I18n.t('qa.linked_data.ldpath.evaluate_logger_error') }
|
51 | 183 | let(:log_message) { "WARNING: #{warning} (cause: #{cause}" }
|
52 | 184 |
|
53 |
| - before { allow(program).to receive(:evaluate).with(subject_uri, context: graph, limit_to_context: true).and_raise(cause) } |
| 185 | + before { allow(program).to receive(:evaluate).with(subject_uri, context: graph, limit_to_context: true).and_raise(ParseError, cause) } |
54 | 186 |
|
55 | 187 | it 'logs error and returns PARSE ERROR as the value' do
|
56 | 188 | expect(Rails.logger).to receive(:warn).with(log_message)
|
57 |
| - expect { subject.values(graph, subject_uri) }.to raise_error StandardError, I18n.t('qa.linked_data.ldpath.evaluate_error') + "... cause: #{cause}" |
| 189 | + expect { subject }.to raise_error ParseError, I18n.t('qa.linked_data.ldpath.evaluate_error') + "... cause: #{cause}" |
| 190 | + end |
| 191 | + end |
| 192 | + |
| 193 | + context 'when program is empty' do |
| 194 | + let(:program) { nil } |
| 195 | + it 'returns empty array' do |
| 196 | + expect { subject }.to raise_error ArgumentError, "You must specify a program when calling ldpath_evaluate" |
58 | 197 | end
|
59 | 198 | end
|
60 | 199 | end
|
|
0 commit comments