require 'rubygems' require 'test/unit' require 'pp' require 'youtube_g' class TestClient < Test::Unit::TestCase def setup @client = YouTubeG::Client.new end def test_should_respond_to_a_basic_query response = @client.videos_by(:query => "penguin") assert_equal "http://gdata.youtube.com/feeds/api/videos?start-index=1&max-results=25&vq=penguin", response.feed_id assert_equal 25, response.max_result_count assert_equal 25, response.videos.length assert_equal 1, response.offset assert(response.total_result_count > 100) assert_instance_of Time, response.updated_at response.videos.each { |v| assert_valid_video v } end def test_should_get_videos_for_multiword_metasearch_query response = @client.videos_by(:query => 'christina ricci') assert_equal "http://gdata.youtube.com/feeds/api/videos?start-index=1&max-results=25&vq=christina+ricci", response.feed_id assert_equal 25, response.max_result_count assert_equal 25, response.videos.length assert_equal 1, response.offset assert(response.total_result_count > 100) assert_instance_of Time, response.updated_at response.videos.each { |v| assert_valid_video v } end def test_should_handle_video_not_yet_viewed response = @client.videos_by(:query => "YnqHZDh_t2Q") assert_equal 1, response.videos.length response.videos.each { |v| assert_valid_video v } end # TODO: this doesn't work because the returned feed is in an unknown format # def test_should_get_video_for_search_by_video_id # response = @client.videos_by(:video_id => "T7YazwP8GtY") # response.videos.each { |v| assert_valid_video v } # end def test_should_get_videos_for_one_tag response = @client.videos_by(:tags => ['panther']) response.videos.each { |v| assert_valid_video v } end def test_should_get_videos_for_multiple_tags response = @client.videos_by(:tags => ['tiger', 'leopard']) response.videos.each { |v| assert_valid_video v } end def test_should_get_videos_for_one_category response = @client.videos_by(:categories => [:news]) response.videos.each { |v| assert_valid_video v } end def test_should_get_videos_for_multiple_categories response = @client.videos_by(:categories => [:news, :sports]) response.videos.each { |v| assert_valid_video v } end # TODO: Need to do more specific checking in these tests # Currently, if a URL is valid, and videos are found, the test passes regardless of search criteria def test_should_get_videos_for_categories_and_tags response = @client.videos_by(:categories => [:news, :sports], :tags => ['soccer', 'football']) response.videos.each { |v| assert_valid_video v } end def test_should_get_most_viewed_videos response = @client.videos_by(:most_viewed) response.videos.each { |v| assert_valid_video v } end def test_should_get_top_rated_videos_for_today response = @client.videos_by(:top_rated, :time => :today) response.videos.each { |v| assert_valid_video v } end def test_should_get_videos_for_categories_and_tags_with_category_boolean_operators response = @client.videos_by(:categories => { :either => [:news, :sports], :exclude => [:comedy] }, :tags => { :include => ['football'], :exclude => ['soccer'] }) response.videos.each { |v| assert_valid_video v } end def test_should_get_videos_for_categories_and_tags_with_tag_boolean_operators response = @client.videos_by(:categories => { :either => [:news, :sports], :exclude => [:comedy] }, :tags => { :either => ['football', 'soccer', 'polo'] }) response.videos.each { |v| assert_valid_video v } end def test_should_get_videos_by_user response = @client.videos_by(:user => 'liz') response.videos.each { |v| assert_valid_video v } end # HTTP 403 Error # def test_should_get_favorite_videos_by_user # response = @client.videos_by(:favorites, :user => 'liz') # response.videos.each { |v| assert_valid_video v } # end def test_should_get_videos_for_query_search_with_categories_excluded response = @client.videos_by(:query => 'bench press', :categories => { :exclude => [:comedy, :entertainment] }, :max_results => 10) assert_equal "\n", response.videos.first.embed_html response.videos.each { |v| assert_valid_video v } end def test_should_be_able_to_pass_in_logger @client = YouTubeG::Client.new(Logger.new(STDOUT)) assert_not_nil @client.logger end def test_should_create_logger_if_not_passed_in @client = YouTubeG::Client.new assert_not_nil @client.logger end def test_should_determine_if_nonembeddable_video_is_embeddable response = @client.videos_by(:query => "avril lavigne girlfriend") video = response.videos.first assert !video.can_embed? end def test_should_determine_if_embeddable_video_is_embeddable response = @client.videos_by(:query => "strongbad") video = response.videos.first assert video.can_embed? end def test_should_retrieve_video_by_id video = @client.video_by("http://gdata.youtube.com/feeds/videos/EkF4JD2rO3Q") assert_valid_video video video = @client.video_by("EkF4JD2rO3Q") assert_valid_video video end private def assert_valid_video (video) # pp video # check general attributes assert_instance_of YouTubeG::Model::Video, video assert_instance_of Fixnum, video.duration assert(video.duration > 0) #assert_match(/^