PageRenderTime 28ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/gems/youtube-g-0.4.9.9/test/test_client.rb

https://github.com/palladius/lovd-by-less
Ruby | 262 lines | 186 code | 52 blank | 24 comment | 2 complexity | c3fd0734c931789c6cb54bc5e62a6986 MD5 | raw file
  1. require 'rubygems'
  2. require 'test/unit'
  3. require 'pp'
  4. require 'youtube_g'
  5. class TestClient < Test::Unit::TestCase
  6. def setup
  7. @client = YouTubeG::Client.new
  8. end
  9. def test_should_respond_to_a_basic_query
  10. response = @client.videos_by(:query => "penguin")
  11. assert_equal "http://gdata.youtube.com/feeds/api/videos", response.feed_id
  12. assert_equal 25, response.max_result_count
  13. assert_equal 25, response.videos.length
  14. assert_equal 1, response.offset
  15. assert(response.total_result_count > 100)
  16. assert_instance_of Time, response.updated_at
  17. response.videos.each { |v| assert_valid_video v }
  18. end
  19. def test_should_respond_to_a_basic_query_with_offset_and_max_results
  20. response = @client.videos_by(:query => "penguin", :offset => 15, :max_results => 30)
  21. assert_equal "http://gdata.youtube.com/feeds/api/videos", response.feed_id
  22. assert_equal 30, response.max_result_count
  23. assert_equal 30, response.videos.length
  24. assert_equal 15, response.offset
  25. assert(response.total_result_count > 100)
  26. assert_instance_of Time, response.updated_at
  27. response.videos.each { |v| assert_valid_video v }
  28. end
  29. def test_should_respond_to_a_basic_query_with_paging
  30. response = @client.videos_by(:query => "penguin")
  31. assert_equal "http://gdata.youtube.com/feeds/api/videos", response.feed_id
  32. assert_equal 25, response.max_result_count
  33. assert_equal 1, response.offset
  34. response = @client.videos_by(:query => "penguin", :page => 2)
  35. assert_equal "http://gdata.youtube.com/feeds/api/videos", response.feed_id
  36. assert_equal 25, response.max_result_count
  37. assert_equal 26, response.offset
  38. response2 = @client.videos_by(:query => "penguin", :page => 3)
  39. assert_equal "http://gdata.youtube.com/feeds/api/videos", response2.feed_id
  40. assert_equal 25, response2.max_result_count
  41. assert_equal 51, response2.offset
  42. end
  43. def test_should_get_videos_for_multiword_metasearch_query
  44. response = @client.videos_by(:query => 'christina ricci')
  45. assert_equal "http://gdata.youtube.com/feeds/api/videos", response.feed_id
  46. assert_equal 25, response.max_result_count
  47. assert_equal 25, response.videos.length
  48. assert_equal 1, response.offset
  49. assert(response.total_result_count > 100)
  50. assert_instance_of Time, response.updated_at
  51. response.videos.each { |v| assert_valid_video v }
  52. end
  53. def test_should_handle_video_not_yet_viewed
  54. response = @client.videos_by(:query => "YnqHZDh_t2Q")
  55. assert_equal 1, response.videos.length
  56. response.videos.each { |v| assert_valid_video v }
  57. end
  58. # TODO: this doesn't work because the returned feed is in an unknown format
  59. # def test_should_get_video_for_search_by_video_id
  60. # response = @client.videos_by(:video_id => "T7YazwP8GtY")
  61. # response.videos.each { |v| assert_valid_video v }
  62. # end
  63. def test_should_get_videos_for_one_tag
  64. response = @client.videos_by(:tags => ['panther'])
  65. response.videos.each { |v| assert_valid_video v }
  66. end
  67. def test_should_get_videos_for_multiple_tags
  68. response = @client.videos_by(:tags => ['tiger', 'leopard'])
  69. response.videos.each { |v| assert_valid_video v }
  70. end
  71. def test_should_get_videos_for_one_category
  72. response = @client.videos_by(:categories => [:news])
  73. response.videos.each { |v| assert_valid_video v }
  74. end
  75. def test_should_get_videos_for_multiple_categories
  76. response = @client.videos_by(:categories => [:news, :sports])
  77. response.videos.each { |v| assert_valid_video v }
  78. end
  79. # TODO: Need to do more specific checking in these tests
  80. # Currently, if a URL is valid, and videos are found, the test passes regardless of search criteria
  81. def test_should_get_videos_for_categories_and_tags
  82. response = @client.videos_by(:categories => [:news, :sports], :tags => ['soccer', 'football'])
  83. response.videos.each { |v| assert_valid_video v }
  84. end
  85. def test_should_get_most_viewed_videos
  86. response = @client.videos_by(:most_viewed)
  87. response.videos.each { |v| assert_valid_video v }
  88. end
  89. def test_should_get_top_rated_videos_for_today
  90. response = @client.videos_by(:top_rated, :time => :today)
  91. response.videos.each { |v| assert_valid_video v }
  92. end
  93. def test_should_get_videos_for_categories_and_tags_with_category_boolean_operators
  94. response = @client.videos_by(:categories => { :either => [:news, :sports], :exclude => [:comedy] },
  95. :tags => { :include => ['football'], :exclude => ['soccer'] })
  96. response.videos.each { |v| assert_valid_video v }
  97. end
  98. def test_should_get_videos_for_categories_and_tags_with_tag_boolean_operators
  99. response = @client.videos_by(:categories => { :either => [:news, :sports], :exclude => [:comedy] },
  100. :tags => { :either => ['football', 'soccer', 'polo'] })
  101. response.videos.each { |v| assert_valid_video v }
  102. end
  103. def test_should_get_videos_by_user
  104. response = @client.videos_by(:user => 'liz')
  105. response.videos.each { |v| assert_valid_video v }
  106. end
  107. def test_should_get_videos_by_user_with_pagination_and_ordering
  108. response = @client.videos_by(:user => 'liz', :page => 2, :per_page => '2', :order_by => 'published')
  109. response.videos.each { |v| assert_valid_video v }
  110. assert_equal 3, response.offset
  111. assert_equal 2, response.max_result_count
  112. end
  113. # HTTP 403 Error
  114. # def test_should_get_favorite_videos_by_user
  115. # response = @client.videos_by(:favorites, :user => 'liz')
  116. # response.videos.each { |v| assert_valid_video v }
  117. # end
  118. def test_should_get_videos_for_query_search_with_categories_excluded
  119. video = @client.video_by("EkF4JD2rO3Q")
  120. assert_equal "<object width=\"425\" height=\"350\">\n <param name=\"movie\" value=\"http://www.youtube.com/v/EkF4JD2rO3Q\"></param>\n <param name=\"wmode\" value=\"transparent\"></param>\n <embed src=\"http://www.youtube.com/v/EkF4JD2rO3Q\" type=\"application/x-shockwave-flash\" \n wmode=\"transparent\" width=\"425\" height=\"350\"></embed>\n</object>\n", video.embed_html
  121. assert_valid_video video
  122. end
  123. def test_should_disable_debug_if_debug_is_set_to_false
  124. @client = YouTubeG::Client.new
  125. assert_nil @client.logger
  126. end
  127. def test_should_enable_logger_if_debug_is_true
  128. @client = YouTubeG::Client.new(true)
  129. assert_not_nil @client.logger
  130. end
  131. def test_should_determine_if_nonembeddable_video_is_embeddable
  132. response = @client.videos_by(:query => "avril lavigne girlfriend")
  133. video = response.videos.first
  134. assert !video.embeddable?
  135. end
  136. def test_should_determine_if_embeddable_video_is_embeddable
  137. response = @client.videos_by(:query => "strongbad")
  138. video = response.videos.first
  139. assert video.embeddable?
  140. end
  141. def test_should_retrieve_video_by_id
  142. video = @client.video_by("http://gdata.youtube.com/feeds/videos/EkF4JD2rO3Q")
  143. assert_valid_video video
  144. video = @client.video_by("EkF4JD2rO3Q")
  145. assert_valid_video video
  146. end
  147. private
  148. def assert_valid_video (video)
  149. # pp video
  150. # check general attributes
  151. assert_instance_of YouTubeG::Model::Video, video
  152. assert_instance_of Fixnum, video.duration
  153. assert(video.duration > 0)
  154. #assert_match(/^<div style=.*?<\/div>/m, video.html_content)
  155. assert_instance_of String, video.html_content
  156. # validate media content records
  157. video.media_content.each do |media_content|
  158. # http://www.youtube.com/v/IHVaXG1thXM
  159. assert_valid_url media_content.url
  160. assert(media_content.duration > 0)
  161. assert_instance_of YouTubeG::Model::Video::Format, media_content.format
  162. assert_instance_of String, media_content.mime_type
  163. assert_match(/^[^\/]+\/[^\/]+$/, media_content.mime_type)
  164. end
  165. default_content = video.default_media_content
  166. if default_content
  167. assert_instance_of YouTubeG::Model::Content, default_content
  168. assert default_content.is_default?
  169. end
  170. # validate keywords
  171. video.keywords.each { |kw| assert_instance_of(String, kw) }
  172. # http://www.youtube.com/watch?v=IHVaXG1thXM
  173. assert_valid_url video.player_url
  174. assert_instance_of Time, video.published_at
  175. # validate optionally-present rating
  176. if video.rating
  177. assert_instance_of YouTubeG::Model::Rating, video.rating
  178. assert_instance_of Float, video.rating.average
  179. assert_instance_of Fixnum, video.rating.max
  180. assert_instance_of Fixnum, video.rating.min
  181. assert_instance_of Fixnum, video.rating.rater_count
  182. end
  183. # validate thumbnails
  184. assert(video.thumbnails.size > 0)
  185. assert_not_nil video.title
  186. assert_instance_of String, video.title
  187. assert(video.title.length > 0)
  188. assert_instance_of Time, video.updated_at
  189. # http://gdata.youtube.com/feeds/videos/IHVaXG1thXM
  190. assert_valid_url video.video_id
  191. assert_instance_of Fixnum, video.view_count
  192. # validate author
  193. assert_instance_of YouTubeG::Model::Author, video.author
  194. assert_instance_of String, video.author.name
  195. assert(video.author.name.length > 0)
  196. assert_valid_url video.author.uri
  197. # validate categories
  198. video.categories.each do |cat|
  199. assert_instance_of YouTubeG::Model::Category, cat
  200. assert_instance_of String, cat.label
  201. assert_instance_of String, cat.term
  202. end
  203. end
  204. def assert_valid_url (url)
  205. URI::parse(url)
  206. return true
  207. rescue
  208. return false
  209. end
  210. end