-
Notifications
You must be signed in to change notification settings - Fork 14
/
identifiable_spec.rb
182 lines (147 loc) · 4.78 KB
/
identifiable_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# frozen_string_literal: true
require 'spec_helper'
require 'active_model'
describe ActiveTriples::Identifiable do
before do
class ActiveExample; include ActiveTriples::Identifiable; end
end
after do
Object.send(:remove_const, 'ActiveExample')
end
subject { ActiveExample.new }
let(:klass) { ActiveExample }
shared_context 'with data' do
let(:parent) { MyResource.new }
before do
class MyResource
include ActiveTriples::RDFSource
property :relation, predicate: RDF::Vocab::DC.relation, class_name: 'ActiveExample'
end
klass.property :title, predicate: RDF::Vocab::DC.title
klass.property :identifier, predicate: RDF::Vocab::DC.identifier
klass.property :description, predicate: RDF::Vocab::DC.description
subject.resource.title = 'Moomin Valley in November'
subject.resource.identifier = 'moomvember'
subject.resource.description = 'The ninth and final book in the Moomin series by Finnish author Tove Jansson'
parent.relation = subject
end
after do
Object.send(:remove_const, 'MyResource')
end
end
context 'without implementation' do
describe '::from_uri' do
it 'raises a NotImplementedError' do
expect{ klass.from_uri(RDF::URI('http://example.org/blah')) }.to raise_error NotImplementedError
end
end
describe '#to_uri' do
it 'raises a NotImplementedError' do
expect{ subject.to_uri }.to raise_error NotImplementedError
end
end
end
context 'with implementation' do
before do
class ActiveExample
attr_accessor :id
configure base_uri: 'http://example.org/ns/'
def self.property(*args)
prop = args.first
define_method prop.to_s do
resource.get_values(prop)
end
define_method "#{prop.to_s}=" do |*args|
resource.set_value(prop, *args)
end
resource_class.property(*args)
end
end
subject.id = '123'
end
describe '::properties' do
before do
klass.property :title, :predicate => RDF::Vocab::DC.title
end
it 'can be set' do
expect(klass.properties).to include 'title'
end
it 'sets property values' do
subject.title = 'Finn Family Moomintroll'
expect(subject.resource.title).to eq ['Finn Family Moomintroll']
end
it 'appends property values' do
subject.title << 'Finn Family Moomintroll'
expect(subject.resource.title).to eq ['Finn Family Moomintroll']
end
it 'returns correct values in property getters' do
subject.resource.title = 'Finn Family Moomintroll'
expect(subject.title).to eq subject.resource.title
end
context 'with other identifiable classes' do
before do
class ActiveExampleTwo
include ActiveTriples::Identifiable
end
end
after do
Object.send(:remove_const, 'ActiveExampleTwo')
end
it 'does not effect other classes' do
klass.property :identifier, :predicate => RDF::Vocab::DC.identifier
expect(ActiveExampleTwo.properties).to be_empty
end
end
end
describe '::configure' do
it 'allows configuration' do
klass.configure type: RDF::OWL.Thing
expect(subject.resource.type).to eq [RDF::OWL.Thing]
end
end
describe '#parent' do
it 'is nil' do
expect(subject.parent).to be_nil
end
context 'with relationships' do
include_context 'with data'
it 'has a parent' do
expect(parent.relation.first.parent).to eq parent
end
end
end
describe '#parent=' do
before { class MyResource; include ActiveTriples::RDFSource; end }
let(:parent) { MyResource.new }
it 'sets parent' do
expect { subject.parent = parent }
.to change { subject.parent }.from(nil).to(parent)
end
end
describe '#rdf_subject' do
it 'has a subject' do
expect(subject.rdf_subject).to eq 'http://example.org/ns/123'
end
end
describe '#to_uri' do
it 'has a subject' do
expect(subject.rdf_subject).to eq 'http://example.org/ns/123'
end
end
describe "adding it as a property for an AT Resource" do
include_context 'with data'
it "returns the same in-memory object added" do
resource = MyResource.new
resource.relation = subject
expect(resource.relation).to eq [subject]
end
it "can share that object with another resource" do
resource = MyResource.new
resource_2 = MyResource.new
resource.relation = subject
resource_2.relation = resource.relation
expect(resource.relation).to eq resource_2.relation
end
end
end
end