File: delicious_test.rb

package info (click to toggle)
libwww-delicious-ruby 0.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 320 kB
  • ctags: 366
  • sloc: ruby: 2,178; xml: 106; makefile: 2
file content (369 lines) | stat: -rw-r--r-- 12,080 bytes parent folder | download
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# 
# = WWW::Delicious
#
# Ruby client for del.icio.us API.
# 
#
# Category::    WWW
# Package::     WWW::Delicious
# Author::      Simone Carletti <weppos@weppos.net>
# License::     MIT License
#
#--
# SVN: $Id$
#++


require 'test_helper'


class DeliciousTest < Test::Unit::TestCase
  
  TEST_USERNAME = 'username'
  TEST_PASSWORD = 'password'
  
  
  def setup
    @delicious = instance
  end
  
  
  def test_initialize_should_raise_without_account
    assert_raise(ArgumentError) { WWW::Delicious.new() }
    assert_raise(ArgumentError) { WWW::Delicious.new(TEST_USERNAME) }
  end
  
  def test_initialize_should_set_account_credentials
    assert_equal(TEST_USERNAME, @delicious.username)
    assert_equal(TEST_PASSWORD, @delicious.password)
  end
  
  def test_initialize_should_allow_option_user_agent
    useragent = 'MyClass/1.0 (Foo/Bar +http://foo.com/)'
    delicious = instance(:user_agent => useragent)
    assert_equal(useragent, delicious.user_agent)
  end
  
  def test_initialize_should_default_option_user_agent_unless_option
    useragent = instance.user_agent
    assert_match("Ruby/#{RUBY_VERSION}", useragent)
    assert_match("#{WWW::Delicious::NAME}/#{WWW::Delicious::VERSION}", useragent)
  end
  
  def test_initialize_should_allow_option_base_uri
    base_uri = 'https://ma.gnolia.com/api/mirrord'
    delicious = instance(:base_uri => base_uri)
    assert_equal(URI.parse(base_uri), delicious.base_uri)
  end
  
  def test_initialize_should_default_option_base_uri_unless_option
    base_uri = instance.base_uri
    assert_equal(URI.parse('https://api.del.icio.us'), base_uri)
  end
  
  
  # =========================================================================
  # HTTP Request common checks
  # =========================================================================
  
  def test_request_raises_without_http_client
    @delicious.http_client = nil
    assert_raise(WWW::Delicious::Error) { @delicious.update }
  end

  def test_request_waits_necessary_time_between_requests
    @delicious.expects(:make_request).times(4).returns(load_fixture('/net_response_success.yml'))
    @delicious.valid_account?   # 1st request
    3.times do |time|
      lr = @delicious.instance_variable_get(:@last_request)
      @delicious.valid_account? # N request
      nr = @delicious.instance_variable_get(:@last_request)
      assert((nr - lr) > WWW::Delicious::SECONDS_BEFORE_NEW_REQUEST)
    end
  end
  
  
  def test_valid_account
    @delicious.expects(:make_request).once.returns(load_fixture('/net_response_success.yml'))
    assert(@delicious.valid_account?)
  end
  
  def test_invalid_account
    @delicious.expects(:make_request).once.returns(load_fixture('/net_response_invalid_account.yml'))
    assert(!@delicious.valid_account?)
  end
  
  
  # =========================================================================
  # Update
  # =========================================================================
  
  def test_update
    @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
    assert_equal(@delicious.update, Time.parse("2008-08-02T11:55:35Z"))
  end
  
  def test_update_raises_without_update_root_node
    @delicious.expects(:request).once.returns(mock_response('/response/bundles_all.xml'))
    error = assert_raise(WWW::Delicious::ResponseError) do
      @delicious.update
    end
    assert_match(/`update`/, error.message)
  end  
  
  def test_update_delicious1
    @delicious.expects(:request).once.returns(mock_response('/response/update.delicious1.xml'))
    assert_equal(@delicious.update, Time.parse("2008-03-12T08:41:20Z"))
  end
  
  
  # =========================================================================
  # Bundles
  # =========================================================================
  
  def test_bundles_all
    @delicious.expects(:request).once.returns(mock_response('/response/bundles_all.xml'))
    expected = [ ['music', %w(ipod mp3 music)], ['pc', %w(computer software hardware)] ]
    
    results = @delicious.bundles_all
    assert_instance_of(Array, results)
    assert_equal(2, results.length)
    
    results.each_with_index do |bundle, index|
      assert_instance_of(WWW::Delicious::Bundle, bundle)
      name, tags = expected[index]
      assert_equal(name, bundle.name)
      assert_equal(tags, bundle.tags)
    end
  end
  
  def test_bundles_all_empty
    @delicious.expects(:request).once.returns(mock_response('/response/bundles_all_empty.xml'))
    results = @delicious.bundles_all
    assert_instance_of(Array, results)
    assert_equal(0, results.length)
  end
  
  def test_bundles_all_raises_without_bundles_root_node
    @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
    error = assert_raise(WWW::Delicious::ResponseError) do
      @delicious.bundles_all
    end
    assert_match(/`bundles`/, error.message)
  end
  
  
  def test_bundles_set
    @delicious.expects(:request).once.returns(mock_response('/response/bundles_set.xml'))
    assert(@delicious.bundles_set('name', %w(foo bar)))
  end
  
  def test_bundles_set_raises_with_response_error
    @delicious.expects(:request).once.returns(mock_response('/response/bundles_set_error.xml'))
    error = assert_raise(WWW::Delicious::Error) do
      @delicious.bundles_set('name', %w(foo bar))
    end
    assert_match(/you must supply a bundle name/, error.message)
  end
  
  def test_bundles_set_raises_without_result_root_node
    @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
    error = assert_raise(WWW::Delicious::ResponseError) do
      @delicious.bundles_set('name', %w(foo bar))
    end
    assert_match(/`result`/, error.message)
  end
  
  
  def test_bundles_delete
    @delicious.expects(:request).once.returns(mock_response('/response/bundles_delete.xml'))
    assert(@delicious.bundles_delete('name'))
  end
  
  def test_bundles_delete_raises_without_result_root_node
    @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
    error = assert_raise(WWW::Delicious::ResponseError) do
      @delicious.bundles_delete('name')
    end
    assert_match(/`result`/, error.message)
  end
  
  
  # =========================================================================
  # Tags
  # =========================================================================

  def test_tags_get
    @delicious.expects(:request).once.returns(mock_response('/response/tags_get.xml'))
    expected = [ ['activedesktop', 1], ['business', 14] ]
    
    results = @delicious.tags_get
    assert_instance_of(Array, results)
    assert_equal(2, results.length)
    
    results.each_with_index do |tag, index|
      assert_instance_of(WWW::Delicious::Tag, tag)
      name, count = expected[index]
      assert_equal(name, tag.name)
      assert_equal(count, tag.count)
    end
  end

  def test_tags_get_empty
    @delicious.expects(:request).once.returns(mock_response('/response/tags_get_empty.xml'))
    results = @delicious.tags_get
    assert_instance_of(Array, results)
    assert_equal(0, results.length)
  end
  
  def test_tags_get_raises_without_bundles_root_node
    @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
    error = assert_raise(WWW::Delicious::ResponseError) do
      @delicious.tags_get
    end
    assert_match(/`tags`/, error.message)
  end
  
  
  def test_tags_rename
    @delicious.expects(:request).once.returns(mock_response('/response/tags_rename.xml'))
    assert(@delicious.tags_rename('old', 'new'))
  end
  
  def test_tags_rename_raises_without_result_root_node
    @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
    error = assert_raise(WWW::Delicious::ResponseError) do
      @delicious.tags_rename('foo', 'bar')
    end
    assert_match(/`result`/, error.message)
  end
  
  
  # =========================================================================
  # Posts
  # =========================================================================
  
  def test_posts_get
    @delicious.expects(:request).once.returns(mock_response('/response/posts_get.xml'))
    results = @delicious.posts_get
    assert_instance_of(Array, results)
    assert_equal(3, results.length)
    assert_equal('New to Git? - GitHub', results.first.title)
    assert_equal('.c( whytheluckystiff )o. -- The Fully Upturned Bin', results.last.title)
  end
  
  def test_posts_get_raises_without_posts_root_node
    @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
    error = assert_raise(WWW::Delicious::ResponseError) do
      @delicious.posts_get
    end
    assert_match(/`posts`/, error.message)
  end
  
  
  def test_posts_recent
    @delicious.expects(:request).once.returns(mock_response('/response/posts_recent.xml'))
    results = @delicious.posts_recent
    assert_instance_of(Array, results)
    assert_equal(15, results.length)
    assert_equal('New to Git? - GitHub', results.first.title)
    assert_equal('RichText | Lightview for modal dialogs on Rails', results.last.title)
  end
  
  def test_posts_recent_raises_without_posts_root_node
    @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
    error = assert_raise(WWW::Delicious::ResponseError) do
      @delicious.posts_recent
    end
    assert_match(/`posts`/, error.message)
  end
  
  
  def test_posts_all
    @delicious.expects(:request).once.returns(mock_response('/response/posts_all.xml'))
    results = @delicious.posts_all
    assert_instance_of(Array, results)
    assert_equal(8, results.length)
    assert_equal('New to Git? - GitHub', results.first.title)
    assert_equal('ASP 101 - Object Oriented ASP: Using Classes in Classic ASP', results.last.title)
  end
  
  def test_posts_all_raises_without_posts_root_node
    @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
    error = assert_raise(WWW::Delicious::ResponseError) do
      @delicious.posts_all
    end
    assert_match(/`posts`/, error.message)
  end
  
  
  def test_posts_dates
    @delicious.expects(:request).once.returns(mock_response('/response/posts_dates.xml'))
    results = @delicious.posts_dates
    assert_instance_of(Hash, results)
    assert_equal(10, results.length)
  end
  
  def test_posts_dates_raises_without_dates_root_node
    @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
    error = assert_raise(WWW::Delicious::ResponseError) do
      @delicious.posts_dates
    end
    assert_match(/`dates`/, error.message)
  end
  
  
  def test_posts_add
    params = {:url => 'http://localhost', :title => 'Just a test'}
    @delicious.expects(:request).times(2).returns(mock_response('/response/posts_add.xml'))
    assert(@delicious.posts_add(WWW::Delicious::Post.new(params)))
    assert(@delicious.posts_add(params))
  end
  
  
  def test_posts_delete
    @delicious.expects(:request).once.returns(mock_response('/response/posts_delete.xml'))
    assert(@delicious.posts_delete('test'))
  end
  
  def test_posts_delete_raises_without_result_root_node
    @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
    error = assert_raise(WWW::Delicious::ResponseError) do
      @delicious.posts_delete('test')
    end
    assert_match(/`result`/, error.message)
  end
  
  
  
  protected
  
    # returns a stub instance
    def instance(options = {}, &block)
      WWW::Delicious.new(TEST_USERNAME, TEST_PASSWORD, options, &block)
    end

    def load_testcase(file)
      File.read(TESTCASES_PATH + file)
    end

    def load_fixture(file)
      YAML.load(File.read(FIXTURES_PATH + file))
    end
    
    def mock_response(file_or_content)
      content = case 
        when file_or_content =~ /\.xml$/
          load_testcase(file_or_content)
        when file_or_content =~ /\.yml$/
          load_fixture(file_or_content)
        else
          file_or_content.to_s
      end
      
      response = mock()
      response.expects(:body).at_least(1).returns(content)
      response
    end
  
end