This repository has been archived by the owner on Sep 12, 2018. It is now read-only.
forked from ruby-rdf/spira
-
Notifications
You must be signed in to change notification settings - Fork 1
/
has_many.spec
177 lines (137 loc) · 4.91 KB
/
has_many.spec
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
require File.dirname(File.expand_path(__FILE__)) + '/spec_helper'
class Posts < RDF::Vocabulary('http://example.org/posts/predicates/')
property :rating
end
describe "has_many" do
before :all do
require 'rdf/ntriples'
class ::Post
include Spira::Resource
type URI.new('http://rdfs.org/sioc/types#Post')
has_many :comments, :predicate => SIOC.has_reply, :type => :Comment
property :title, :predicate => DC.title
property :body, :predicate => SIOC.content
end
class ::Comment
include Spira::Resource
type URI.new('http://rdfs.org/sioc/types#Comment')
property :post, :predicate => SIOC.reply_of, :type => :Post
property :title, :predicate => DC.title
property :body, :predicate => SIOC.content
has_many :ratings, :predicate => Posts.rating, :type => Integer
end
end
context "Comment class basics" do
before :each do
@posts_repository = RDF::Repository.load(fixture('has_many.nt'))
Spira.add_repository(:default, @posts_repository)
@uri = RDF::URI.new('http://example.org/comments/comment1')
@empty_uri = RDF::URI.new('http://example.org/comments/comment0')
@comment = Comment.for @uri
@empty_comment = Comment.for @empty_uri
end
it "should have a ratings method" do
@comment.should respond_to :ratings
end
it "should having a ratings= method" do
@comment.should respond_to :ratings=
end
it "should support is_list?" do
Comment.should respond_to :is_list?
end
it "should report that ratings are a list" do
Comment.is_list?(:ratings).should == true
end
it "should report that bodies are not a list" do
Comment.is_list?(:body).should == false
end
it "should return an empty array of ratings for comments with none" do
@empty_comment.ratings.should == Set.new
end
it "should return a set of ratings for comments with some" do
@comment.ratings.should be_a Set
@comment.ratings.size.should == 3
@comment.ratings.sort.should == [1,3,5]
end
it "should allow setting and saving non-array elements" do
@comment.title = 'test'
@comment.title.should == 'test'
@comment.save!
@comment.title.should == 'test'
end
it "should allow setting on array elements" do
@comment.ratings = [1,2,4]
@comment.save!
@comment.ratings.sort.should == [1,2,4]
end
it "should allow saving array elements" do
@comment.ratings = [1,2,4]
@comment.ratings.sort.should == [1,2,4]
@comment.save!
@comment.ratings.sort.should == [1,2,4]
@comment = Comment.for @uri
@comment.ratings.sort.should == [1,2,4]
end
it "should allow appending to array elements" do
@comment.ratings << 6
@comment.ratings.sort.should == [1,3,5,6]
@comment.save!
@comment.ratings.sort.should == [1,3,5,6]
end
it "should allow saving of appended elements" do
@comment.ratings << 6
@comment.save!
@comment = Comment.for @uri
@comment.ratings.sort.should == [1,3,5,6]
end
end
context "Post class basics" do
before :all do
@posts_repository = RDF::Repository.load(fixture('has_many.nt'))
Spira.add_repository(:default, @posts_repository)
end
before :each do
@uri = RDF::URI.new('http://example.org/posts/post1')
@empty_uri = RDF::URI.new('http://example.org/posts/post0')
@post = Post.for @uri
@empty_post = Post.for @empty_uri
@empty_comment_uri = RDF::URI.new('http://example.org/comments/comment0')
@empty_comment = Comment.for @empty_comment_uri
end
it "should have a comments method" do
@post.should respond_to :comments
end
it "should have a comments= method" do
@post.should respond_to :comments=
end
it "should return an empty array from comments for an object with none" do
@empty_post.comments.should == Set.new
end
it "should return an array of comments for an object with some" do
@post.comments.size.should == 2
@post.comments.each do |comment|
comment.should be_a Comment
end
end
it "should allow setting and saving non-array elements" do
@post.title = "test post title"
@post.save!
@post.title.should == 'test post title'
end
it "should allow setting array elements" do
@post.comments = (@post.comments + [@empty_comment])
@post.comments.size.should == 3
@post.comments.should include @empty_comment
end
it "should allow saving array elements" do
comments = @post.comments + [@empty_comment]
@post.comments = (@post.comments + [@empty_comment])
@post.comments.size.should == 3
@post.save!
@post.comments.size.should == 3
@post.comments.each do |comment|
comments.should include comment
end
end
end
end