This repository has been archived by the owner on Apr 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
photo_spec.rb
239 lines (201 loc) · 8.6 KB
/
photo_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
require File.dirname(__FILE__) + '/../spec_helper'
module PhotoSpecHelper
def valid_photo_attributes
{
:flickr_id => '325946210',
:title => "Last time's the charm",
:square_source_url => 'http://farm1.static.flickr.com/134/325946210_3d6af571cb_s.jpg',
:thumb_source_url => 'http://farm1.static.flickr.com/134/325946210_3d6af571cb_t.jpg',
:medium_source_url => 'http://farm1.static.flickr.com/134/325946210_3d6af571cb.jpg',
:fullsize_source_url => 'http://farm1.static.flickr.com/134/325946210_3d6af571cb_o.jpg'
}
end
def all_photo_attributes
{
:description => "I hunkered down every night in the library for four weeks to write my last essay. It was about wheether J. S. Mill, for his utilitarianism, can be criticised as an advocate of individualism.",
:taken_at => 100.days.ago.beginning_of_day,
:flickr_updated_at => 99.days.ago.beginning_of_day
}.merge(valid_photo_attributes)
end
def flickr_photo_attributes
{
:title => 'Hey',
:description => 'Some people',
:taken_at => 10.days.ago.beginning_of_day,
:flickr_updated_at => 9.days.ago.beginning_of_day,
:square_source_url => 'http://farm1.static.flickr.com/134/325946210_3d6af571cb_s.jpg',
:thumb_source_url => 'http://farm1.static.flickr.com/134/325946210_3d6af571cb_t.jpg',
:medium_source_url => 'http://farm1.static.flickr.com/134/325946210_3d6af571cb.jpg',
:fullsize_source_url => 'http://farm1.static.flickr.com/134/325946210_3d6af571cb_o.jpg'
}
end
def stub_attributes(obj, attrs)
obj.stub!(:attributes).and_return(attrs)
attrs.each_pair do |key,val|
obj.stub!(key).and_return(val)
end
end
end
describe "a photo matching a flickr photo", :shared => true do
it "should have a flickr_id matching the flickr photo's id" do
@photo.flickr_id.should == @flickr_photo.id
end
it "should have a title matching the flickr photo's title" do
@photo.title.should == @flickr_photo.title
end
it "should have a description matching the flickr photo's description" do
@photo.description.should == @flickr_photo.description
end
it "should have a flickr_updated_at timestamp matching the flickr photo's updated_at" do
@photo.flickr_updated_at.should == @flickr_photo.flickr_updated_at
end
it "should have a thumbnail source url matching the flickr photo's thumbnail source url" do
@photo.thumb_source_url.should == @flickr_photo.thumb_source_url
end
it "should have a medium source url matching the flickr photo's medium source url" do
@photo.medium_source_url.should == @flickr_photo.medium_source_url
end
it "should have a fullsize source url matching the flickr photo's fullsize source url" do
@photo.fullsize_source_url.should == @flickr_photo.fullsize_source_url
end
end
describe Photo do
include PhotoSpecHelper
before(:each) do
@photo = Photo.new
end
describe "every photo" do
it "should be valid with flickr photo id, thumb, medium, and fullsize source urls, and title" do
@photo.attributes = valid_photo_attributes
@photo.should be_valid
end
it "should be invalid without flickr photo id" do
@photo.attributes = valid_photo_attributes.except(:flickr_id)
@photo.should_not be_valid
end
it "should be invalid without a thumbnail photo source url" do
@photo.attributes = valid_photo_attributes.except(:thumb_source_url)
@photo.should_not be_valid
end
it "should be invalid without a medium photo source url" do
@photo.attributes = valid_photo_attributes.except(:medium_source_url)
@photo.should_not be_valid
end
it "should be invalid without a fullsize photo source url" do
@photo.attributes = valid_photo_attributes.except(:fullsize_source_url)
@photo.should_not be_valid
end
it "should be invalid without title" do
@photo.attributes = valid_photo_attributes.except(:title)
@photo.should_not be_valid
@photo.should have(1).errors
end
end
describe "when created" do
before(:each) do
@photo.attributes = valid_photo_attributes
@photo.save!
end
it "should be active when created" do
@photo.should be_active
end
end
describe "when creating from a flickr photo" do
before(:each) do
@flickr_photo = mock(FlickrPhoto, :id => '12345')
stub_attributes(@flickr_photo, flickr_photo_attributes)
end
describe "and initialializing" do
before(:each) do
@photo = Photo.new_from_flickr(@flickr_photo)
end
it_should_behave_like "a photo matching a flickr photo"
end
describe "and creating" do
before(:each) do
@photo = Photo.create_from_flickr(@flickr_photo)
end
it_should_behave_like "a photo matching a flickr photo"
end
end
describe "when updating from flickr" do
before(:each) do
# setup_attributes(@photo, all_photo_attributes.with(:flickr_id => '12345'))
@photo.attributes = all_photo_attributes.with(:flickr_id => '12345')
@flickr_photo = mock(FlickrPhoto, :id => '12345')
stub_attributes(@flickr_photo, flickr_photo_attributes)
end
describe "before the update" do
it "should have a flickr_id matching the flickr photo's id" do
@photo.flickr_id.should == @flickr_photo.id
end
it "should have a title that does not match the flickr photo's title" do
@photo.title.should_not == @flickr_photo.title
end
it "should have a description that does not match the flickr photo's description" do
@photo.description.should_not == @flickr_photo.description
end
it "should have source urls that match the flickr photo's source urls" do
@photo.thumb_source_url.should == @flickr_photo.thumb_source_url
@photo.medium_source_url.should == @flickr_photo.medium_source_url
@photo.fullsize_source_url.should == @flickr_photo.fullsize_source_url
end
end
describe "after the update" do
before(:each) do
FlickrPhoto.stub!(:new).and_return(@flickr_photo)
@photo.update_from_flickr
end
it_should_behave_like "a photo matching a flickr photo"
end
end
describe "when synchronising with flickr" do
before(:each) do
@photo = Photo.new(flickr_photo_attributes.with(:flickr_id => '123', :flickr_updated_at => 2.days.ago.beginning_of_day))
Photo.stub!(:find).with(:all).and_return([@photo])
@flickr_photo = mock(FlickrPhoto, :id => '123')
stub_attributes(@flickr_photo, flickr_photo_attributes.with(:flickr_updated_at => 2.days.ago.beginning_of_day))
FlickrPhoto.stub!(:find_all_by_user_and_tag).and_return([@flickr_photo])
FlickrPhoto.stub!(:new).with(@flickr_photo.id).and_return(@flickr_photo)
end
describe "when the photos are all up to date" do
it "should do nothing" do
@local_photo_1.should_receive(:save).exactly(0).times
@local_photo_2.should_receive(:save).exactly(0).times
Photo.sync_with_flickr_by_user_and_tag('foo', 'bar')
end
end
describe "when the photos have been updated on flickr" do
before(:each) do
@photo.description = 'previous desc'
@photo.flickr_updated_at = 3.days.ago.beginning_of_day
end
it "should update any values that are altered in the flickr photo" do
@photo.stub!(:save).and_return(true)
lambda {
Photo.sync_with_flickr_by_user_and_tag('foo', 'bar')
}.should change(@photo, :description).from('previous desc').to(@flickr_photo.description)
end
it "should update the photo from flickr" do
@photo.stub!(:save).and_return(true)
@photo.should_receive(:update_from_flickr)
Photo.sync_with_flickr_by_user_and_tag('foo', 'bar')
end
it "should save the photo" do
@photo.should_receive(:save)
Photo.sync_with_flickr_by_user_and_tag('foo', 'bar')
end
end
describe "when new photos have been posted to flickr" do
before(:each) do
@new_flickr_photo = mock(FlickrPhoto, :id => '456')
stub_attributes(@new_flickr_photo, flickr_photo_attributes)
FlickrPhoto.stub!(:find_all_by_user_and_tag).and_return([@new_flickr_photo, @flickr_photo])
end
it "should create the new photos from flickr" do
Photo.should_receive(:create_from_flickr).with(@new_flickr_photo)
Photo.sync_with_flickr_by_user_and_tag('foo', 'bar')
end
end
end
end