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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe PublicBody, " using tags" do
before do
@public_body = PublicBody.new(:name => 'Aardvark Monitoring Service',
:short_name => 'AMS',
:request_email => 'foo@flourish.org',
:last_edit_editor => 'test',
:last_edit_comment => '')
end
it 'should correctly convert a tag string into tags' do
@public_body.tag_string = 'stilton emmental'
@public_body.tag_string.should == 'stilton emmental'
@public_body.has_tag?('stilton').should be_true
@public_body.has_tag?('emmental').should be_true
@public_body.has_tag?('jarlsberg').should be_false
end
it 'should strip spaces from tag strings' do
@public_body.tag_string = ' chesire lancashire'
@public_body.tag_string.should == 'chesire lancashire'
end
it 'should work with other white space, such as tabs and new lines' do
@public_body.tag_string = "chesire\n\tlancashire"
@public_body.tag_string.should == 'chesire lancashire'
end
it 'changing tags should remove presence of the old ones' do
@public_body.tag_string = 'stilton'
@public_body.tag_string.should == 'stilton'
@public_body.has_tag?('stilton').should be_true
@public_body.has_tag?('jarlsberg').should be_false
@public_body.tag_string = 'jarlsberg'
@public_body.tag_string.should == 'jarlsberg'
@public_body.has_tag?('stilton').should be_false
@public_body.has_tag?('jarlsberg').should be_true
end
it 'should be able to append tags' do
@public_body.tag_string.should == ''
@public_body.add_tag_if_not_already_present('cheddar')
@public_body.tag_string.should == 'cheddar'
@public_body.has_tag?('cheddar').should be_true
end
it 'should ignore repeat tags' do
@public_body.tag_string = 'stilton stilton'
@public_body.tag_string.should == 'stilton'
end
end
describe PublicBody, " using machine tags" do
before do
@public_body = PublicBody.new(:name => 'Aardvark Monitoring Service',
:short_name => 'AMS',
:request_email => 'foo@flourish.org',
:last_edit_editor => 'test',
:last_edit_comment => '')
end
it 'should parse machine tags' do
@public_body.tag_string = 'wondrous cheese:green'
@public_body.tag_string.should == 'wondrous cheese:green'
@public_body.has_tag?('cheese:green').should be_false
@public_body.has_tag?('cheese').should be_true
@public_body.get_tag_values('cheese').should == ['green']
@public_body.get_tag_values('wondrous').should == []
lambda {
@public_body.get_tag_values('notthere').should raise_error(PublicBody::TagNotFound)
}
end
it 'should cope with colons in value' do
@public_body.tag_string = 'url:http://www.flourish.org'
@public_body.tag_string.should == 'url:http://www.flourish.org'
@public_body.has_tag?('url').should be_true
@public_body.get_tag_values('url').should == ['http://www.flourish.org']
end
it 'should allow multiple tags of the same sort' do
@public_body.tag_string = 'url:http://www.theyworkforyou.com/ url:http://www.fixmystreet.com/'
@public_body.has_tag?('url').should be_true
@public_body.get_tag_values('url').should == ['http://www.theyworkforyou.com/', 'http://www.fixmystreet.com/']
end
end
describe PublicBody, "when finding_by_tags" do
fixtures :public_bodies, :public_body_translations
before do
@geraldine = public_bodies(:geraldine_public_body)
@geraldine.tag_string = 'rabbit'
@humpadink = public_bodies(:humpadink_public_body)
@humpadink.tag_string = 'coney:5678 coney:1234'
end
it 'should be able to find bodies by string' do
found = PublicBody.find_by_tag('rabbit')
found.should == [ @geraldine ]
end
it 'should be able to find when there are multiple tags in one body, without returning duplicates' do
found = PublicBody.find_by_tag('coney')
found.should == [ @humpadink ]
end
end
describe PublicBody, " when making up the URL name" do
before do
@public_body = PublicBody.new
end
it 'should remove spaces, and make lower case' do
@public_body.name = 'Some Authority'
@public_body.url_name.should == 'some_authority'
end
it 'should not allow a numeric name' do
@public_body.name = '1234'
@public_body.url_name.should == 'body'
end
end
describe PublicBody, " when saving" do
before do
@public_body = PublicBody.new
end
it "should not be valid without setting some parameters" do
@public_body.should_not be_valid
end
it "should not be valid with misformatted request email" do
@public_body.name = "Testing Public Body"
@public_body.short_name = "TPB"
@public_body.request_email = "requestBOOlocalhost"
@public_body.last_edit_editor = "*test*"
@public_body.last_edit_comment = "This is a test"
@public_body.should_not be_valid
@public_body.should have(1).errors_on(:request_email)
end
it "should save" do
@public_body.name = "Testing Public Body"
@public_body.short_name = "TPB"
@public_body.request_email = "request@localhost"
@public_body.last_edit_editor = "*test*"
@public_body.last_edit_comment = "This is a test"
@public_body.save!
end
it "should update first_letter" do
@public_body.name = "Testing Public Body"
@public_body.short_name = "TPB"
@public_body.request_email = "request@localhost"
@public_body.last_edit_editor = "*test*"
@public_body.last_edit_comment = "This is a test"
@public_body.first_letter.should be_nil
@public_body.save!
@public_body.first_letter.should == 'T'
end
end
describe PublicBody, "when searching" do
fixtures :public_bodies, :public_body_translations, :public_body_versions
it "should find by existing url name" do
body = PublicBody.find_by_url_name_with_historic('dfh')
body.id.should == 3
end
it "should find by historic url name" do
body = PublicBody.find_by_url_name_with_historic('hdink')
body.id.should == 3
body.class.to_s.should == 'PublicBody'
end
it "should cope with not finding any" do
body = PublicBody.find_by_url_name_with_historic('idontexist')
body.should be_nil
end
it "should cope with duplicate historic names" do
body = PublicBody.find_by_url_name_with_historic('dfh')
# create history with short name "mouse" twice in it
body.short_name = 'Mouse'
body.url_name.should == 'mouse'
body.save!
body.request_email = 'dummy@localhost'
body.save!
# but a different name now
body.short_name = 'Stilton'
body.url_name.should == 'stilton'
body.save!
# try and find by it
body = PublicBody.find_by_url_name_with_historic('mouse')
body.id.should == 3
body.class.to_s.should == 'PublicBody'
end
it "should cope with same url_name across multiple locales" do
PublicBody.with_locale(:es) do
# use the unique spanish name to retrieve and edit
body = PublicBody.find_by_url_name_with_historic('etgq')
body.short_name = 'tgq' # Same as english version
body.save!
# now try to retrieve it
body = PublicBody.find_by_url_name_with_historic('tgq')
body.id.should == public_bodies(:geraldine_public_body).id
body.name.should == "El A Geraldine Quango"
end
end
end
describe PublicBody, " when loading CSV files" do
it "should do a dry run successfully" do
original_count = PublicBody.count
csv_contents = load_file_fixture("fake-authority-type.csv")
errors, notes = PublicBody.import_csv(csv_contents, 'fake', true, 'someadmin') # true means dry run
errors.should == []
notes.size.should == 3
notes.should == [
"line 1: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"request_email\":\"north_west_foi@localhost\"\}",
"line 2: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"request_email\":\"scottish_foi@localhost\"\}",
"line 3: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t\{\"request_email\":\"ni_foi@localhost\"\}"
]
PublicBody.count.should == original_count
end
it "should do full run successfully" do
original_count = PublicBody.count
csv_contents = load_file_fixture("fake-authority-type.csv")
errors, notes = PublicBody.import_csv(csv_contents, 'fake', false, 'someadmin') # false means real run
errors.should == []
notes.size.should == 3
notes.should == [
"line 1: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"request_email\":\"north_west_foi@localhost\"\}",
"line 2: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"request_email\":\"scottish_foi@localhost\"\}",
"line 3: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t\{\"request_email\":\"ni_foi@localhost\"\}"
]
PublicBody.count.should == original_count + 3
end
it "should handle a field list and fields out of order" do
original_count = PublicBody.count
csv_contents = load_file_fixture("fake-authority-type-with-field-names.csv")
errors, notes = PublicBody.import_csv(csv_contents, 'fake', true, 'someadmin') # true means dry run
errors.should == []
notes.size.should == 3
notes.should == [
"line 2: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"request_email\":\"north_west_foi@localhost\",\"home_page\":\"http://northwest.org\"\}",
"line 3: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"request_email\":\"scottish_foi@localhost\",\"home_page\":\"http://scottish.org\"\}",
"line 4: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t\{\"request_email\":\"ni_foi@localhost\"\}"
]
PublicBody.count.should == original_count
end
it "should create bodies with names in multiple locales" do
original_count = PublicBody.count
csv_contents = load_file_fixture("fake-authority-type-with-field-names.csv")
errors, notes = PublicBody.import_csv(csv_contents, 'fake', false, 'someadmin', [:en, :es])
errors.should == []
notes.size.should == 6
notes.should == [
"line 2: creating new authority 'North West Fake Authority' (locale: en):\n\t{\"request_email\":\"north_west_foi@localhost\",\"home_page\":\"http://northwest.org\"}",
"line 2: creating new authority 'North West Fake Authority' (locale: es):\n\t{\"name\":\"Autoridad del Nordeste\"}",
"line 3: creating new authority 'Scottish Fake Authority' (locale: en):\n\t{\"request_email\":\"scottish_foi@localhost\",\"home_page\":\"http://scottish.org\"}",
"line 3: creating new authority 'Scottish Fake Authority' (locale: es):\n\t{\"name\":\"Autoridad Escocesa\"}",
"line 4: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t{\"request_email\":\"ni_foi@localhost\"}",
"line 4: creating new authority 'Fake Authority of Northern Ireland' (locale: es):\n\t{\"name\":\"Autoridad Irlandesa\"}"
]
PublicBody.count.should == original_count + 3
# XXX Not sure why trying to do a PublicBody.with_locale fails here. Seems related to
# the way categories are loaded every time from the PublicBody class. For now we just
# test some translation was done.
body = PublicBody.find_by_name('North West Fake Authority')
body.translated_locales.map{|l|l.to_s}.sort.should == ["en", "es"]
end
it "should not fail if a locale is not found in the input file" do
original_count = PublicBody.count
csv_contents = load_file_fixture("fake-authority-type-with-field-names.csv")
errors, notes = PublicBody.import_csv(csv_contents, 'fake', true, 'someadmin', [:en, :xx]) # true means dry run
errors.should == []
notes.size.should == 3
notes.should == [
"line 2: creating new authority 'North West Fake Authority' (locale: en):\n\t{\"request_email\":\"north_west_foi@localhost\",\"home_page\":\"http://northwest.org\"}",
"line 3: creating new authority 'Scottish Fake Authority' (locale: en):\n\t{\"request_email\":\"scottish_foi@localhost\",\"home_page\":\"http://scottish.org\"}",
"line 4: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t{\"request_email\":\"ni_foi@localhost\"}"
]
PublicBody.count.should == original_count
end
end
|