Skip to content

Commit

Permalink
Added test for UserSearch pagination, Made URL param building determe…
Browse files Browse the repository at this point in the history
…nistic by sorting param keys
  • Loading branch information
Layton Wedgeworth committed Aug 16, 2008
1 parent d7c871c commit 7257f25
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/youtube_g/request/base_search.rb
Expand Up @@ -24,7 +24,7 @@ def build_query_params(params) #:nodoc:
# pair for which the value is non-nil
u = '?'
item_count = 0
params.keys.each do |key|
params.keys.sort.each do |key|
value = params[key]
next if value.nil?

Expand Down
5 changes: 3 additions & 2 deletions lib/youtube_g/request/user_search.rb
Expand Up @@ -6,16 +6,17 @@ class UserSearch < BaseSearch #:nodoc:
attr_reader :offset # start-index

def initialize(params, options={})
@max_results, @order_by, @offset = nil
@url = base_url

if params == :favorites
@url << "#{options[:user]}/favorites"
set_instance_variables(options)
elsif params[:user]
@url << "#{params[:user]}/uploads"
set_instance_variables(params)
end

set_instance_variables(params)

@url << build_query_params(to_youtube_params)
end

Expand Down
8 changes: 7 additions & 1 deletion test/test_client.rb
Expand Up @@ -52,7 +52,6 @@ def test_should_respond_to_a_basic_query_with_paging
assert_equal 51, response2.offset
end


def test_should_get_videos_for_multiword_metasearch_query
response = @client.videos_by(:query => 'christina ricci')

Expand Down Expand Up @@ -132,6 +131,13 @@ def test_should_get_videos_by_user
response = @client.videos_by(:user => 'liz')
response.videos.each { |v| assert_valid_video v }
end

def test_should_get_videos_by_user_with_pagination_and_ordering
response = @client.videos_by(:user => 'liz', :page => 2, :per_page => '2', :order_by => 'published')
response.videos.each { |v| assert_valid_video v }
assert_equal 3, response.offset
assert_equal 2, response.max_result_count
end

# HTTP 403 Error
# def test_should_get_favorite_videos_by_user
Expand Down
14 changes: 12 additions & 2 deletions test/test_video_search.rb
Expand Up @@ -74,7 +74,7 @@ def test_should_build_url_for_most_viewed_offset_and_max_results_without_time

def test_should_build_url_for_most_viewed_offset_and_max_results_with_time
request = YouTubeG::Request::StandardSearch.new(:top_rated, :offset => 5, :max_results => 10, :time => :today)
assert_equal "http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?time=today&max-results=10&start-index=5", request.url
assert_equal "http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?max-results=10&start-index=5&time=today", request.url
end

def test_should_raise_exception_for_invalid_type
Expand Down Expand Up @@ -111,7 +111,7 @@ def test_should_build_url_for_query_search_with_categories_excluded
request = YouTubeG::Request::VideoSearch.new(:query => 'bench press',
:categories => { :exclude => [:comedy, :entertainment] },
:max_results => 10)
assert_equal "http://gdata.youtube.com/feeds/api/videos/-/-Comedy/-Entertainment/?vq=bench+press&max-results=10", request.url
assert_equal "http://gdata.youtube.com/feeds/api/videos/-/-Comedy/-Entertainment/?max-results=10&vq=bench+press", request.url
end

# -- User Queries ---------------------------------------------------------------------------------
Expand All @@ -121,8 +121,18 @@ def test_should_build_url_for_videos_by_user
assert_equal "http://gdata.youtube.com/feeds/api/users/liz/uploads", request.url
end

def test_should_build_url_for_videos_by_user_paginate_and_order
request = YouTubeG::Request::UserSearch.new(:user => 'liz', :offset => 20, :max_results => 10, :order_by => 'published')
assert_equal "http://gdata.youtube.com/feeds/api/users/liz/uploads?max-results=10&orderby=published&start-index=20", request.url
end

def test_should_build_url_for_favorite_videos_by_user
request = YouTubeG::Request::UserSearch.new(:favorites, :user => 'liz')
assert_equal "http://gdata.youtube.com/feeds/api/users/liz/favorites", request.url
end

def test_should_build_url_for_favorite_videos_by_user_paginate
request = YouTubeG::Request::UserSearch.new(:favorites, :user => 'liz', :offset => 20, :max_results => 10)
assert_equal "http://gdata.youtube.com/feeds/api/users/liz/favorites?max-results=10&start-index=20", request.url
end
end

0 comments on commit 7257f25

Please sign in to comment.