ruby-tmdb3 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ HTTP/1.1 200 OK
2
+ Server: nginx
3
+ Date: Mon, 23 Aug 2010 16:39:45 GMT
4
+ Content-Type: text/json; charset=utf-8
5
+ Transfer-Encoding: chunked
6
+ Connection: keep-alive
7
+ Keep-Alive: timeout=20
8
+ Status: 200 OK
9
+ Cache-Control: public, max-age=21600
10
+ X-Varnish: 2542104567 2542101419
11
+ Age: 36
12
+ Via: 1.1 varnish
13
+ X-Cache: HIT
14
+
15
+ {"page":1,"results":[{"adult":false,"id":558629,"name":"Vince","profile_path":null},{"adult":false,"id":1905,"name":"Vincent Price","profile_path":"/w9wcf7hWLD2I2K0PojJaqd9vbHL.jpg"},{"adult":false,"id":1925,"name":"Vincent Cassel","profile_path":"/dai7LAxPyohPlIO5WYrKkNqO8Wu.jpg"},{"adult":false,"id":4937,"name":"Vince Vaughn","profile_path":"/vTIHDiSvIm3HU93JDGCstFwNGxI.jpg"},{"adult":false,"id":38231,"name":"June Vincent","profile_path":"/b0SZNSaOzmKQm4UOss6SAxTpktT.jpg"},{"adult":false,"id":7132,"name":"Vincent D'Onofrio","profile_path":"/kmoj1mZVNTGDZjWKKPp1xKftC9Z.jpg"},{"adult":false,"id":3418,"name":"Vincent Schiavelli","profile_path":"/q2fEosVlli2T8k5db7c7xueNTtc.jpg"},{"adult":false,"id":9831,"name":"Vincent Regan","profile_path":"/tiyjPHwmX8dMMfx0F8HUpXyLHeY.jpg"},{"adult":false,"id":7164,"name":"Frank Vincent","profile_path":null},{"adult":false,"id":52646,"name":"Vincent Kartheiser","profile_path":"/ePnbREiwxNeMLNLVIkJWY6ODRyd.jpg"},{"adult":false,"id":14830,"name":"Vincent Gardenia","profile_path":"/g7iwqHaaIKh5lSTW39pXT0OOXVC.jpg"},{"adult":false,"id":54233,"name":"Vincent Rottiers","profile_path":"/iqwHkmc6ip4cbLS7pz6RACCiVCO.jpg"},{"adult":false,"id":1141,"name":"Hélène Vincent","profile_path":null},{"adult":false,"id":84458,"name":"Vincent Miller","profile_path":null},{"adult":false,"id":24595,"name":"Vincenzo Nicoli","profile_path":null},{"adult":false,"id":39152,"name":"Yves Vincent","profile_path":null},{"adult":false,"id":54268,"name":"Vince Gerardis","profile_path":null},{"adult":false,"id":76825,"name":"Vincent Desagnat","profile_path":"/chkXB8vpO0NuR66Wi0tGfVKnuBX.jpg"},{"adult":false,"id":575079,"name":"Vincent E. Toto","profile_path":null},{"adult":false,"id":1983,"name":"Vincent Laresca","profile_path":"/sFvw7EIWb84vzcRFRY2LknRFxvI.jpg"}],"total_pages":28,"total_results":550}
@@ -0,0 +1,10 @@
1
+ begin
2
+ file_path = File.absolute_path('tmdb_api_key.txt', File.dirname(__FILE__))
3
+
4
+ File.open(file_path) do |file|
5
+ Tmdb.api_key = file.read
6
+ end
7
+ rescue Errno::ENOENT => e
8
+ $stderr.puts "You need place your api key in #{file_path}"
9
+ exit
10
+ end
@@ -0,0 +1,21 @@
1
+ module Test::Unit
2
+ # Used to fix a minor minitest/unit incompatibility in flexmock
3
+ AssertionFailedError = Class.new(StandardError)
4
+
5
+ class TestCase
6
+
7
+ def self.test(name, &block)
8
+ test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
9
+ defined = instance_method(test_name) rescue false
10
+ raise "#{test_name} is already defined in #{self}" if defined
11
+ if block_given?
12
+ define_method(test_name, &block)
13
+ else
14
+ define_method(test_name) do
15
+ flunk "No implementation provided for #{name}"
16
+ end
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,46 @@
1
+ def register_api_url_stubs
2
+ unless(TEST_LIVE_API)
3
+
4
+ File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "movie_search.txt")) do |file|
5
+ stub_request(:get, Regexp.new(Tmdb.base_api_url + "search/movie" + ".*")).to_return(file)
6
+ end
7
+
8
+ File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "movie_get_info.txt")) do |file|
9
+ stub_request(:get, Regexp.new(Tmdb.base_api_url + "movie/" + ".*")).to_return(file)
10
+ end
11
+
12
+ File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "movie_imdb_lookup.txt")) do |file|
13
+ stub_request(:get, Regexp.new(Tmdb.base_api_url + "movie/tt" + ".*")).to_return(file)
14
+ end
15
+
16
+ File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "person_get_info.txt")) do |file|
17
+ stub_request(:get, Regexp.new(Tmdb.base_api_url + "person/" + ".*")).to_return(file)
18
+ end
19
+
20
+ File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "person_search.txt")) do |file|
21
+ stub_request(:get, Regexp.new(Tmdb.base_api_url + "search/person" + ".*")).to_return(file)
22
+ end
23
+
24
+ File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "incorrect_api_url.txt")) do |file|
25
+ stub_request(:get, Regexp.new(Tmdb.base_api_url + "Movie.blarg/" + ".*")).to_return(file)
26
+ end
27
+
28
+ File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "example_com.txt")) do |file|
29
+ stub_request(:get, Regexp.new("http://example.com.*")).to_return(file)
30
+ end
31
+
32
+ File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "image.jpg")) do |file|
33
+ stub_request(:get, Regexp.new('http://i[0-9].themoviedb.org/[backdrops|posters|profiles].*')).to_return(file)
34
+ end
35
+
36
+ File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "image.jpg")) do |file|
37
+ stub_request(:get, Regexp.new('http://hwcdn.themoviedb.org/[backdrops|posters|profiles].*')).to_return(file)
38
+ end
39
+
40
+ File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "blank_result.txt")) do |file|
41
+ stub_request(:get, Regexp.new("item_not_found$")).to_return(file)
42
+ end
43
+
44
+ stub_request(:get, 'http://thisisaurlthatdoesntexist.co.nz').to_return(:body => "", :status => 404, :headers => {'content-length' => 0})
45
+ end
46
+ end
@@ -0,0 +1,21 @@
1
+ TEST_LIVE_API = false
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'mocha'
6
+
7
+ unless(TEST_LIVE_API)
8
+ require 'webmock/test_unit'
9
+ include WebMock::API
10
+ end
11
+
12
+ require_files = []
13
+ require_files << File.join(File.dirname(__FILE__), '..', 'lib', 'ruby-tmdb3.rb')
14
+ require_files.concat Dir[File.join(File.dirname(__FILE__), 'setup', '*.rb')]
15
+
16
+ require_files.each do |file|
17
+ require File.expand_path(file)
18
+ end
19
+
20
+ #load(File.join('unit', 'test_direct_require.rb'), true)
21
+ system('ruby ' + File.expand_path(File.join(File.dirname(__FILE__), 'unit', 'test_direct_require.rb')))
@@ -0,0 +1,25 @@
1
+ require "test/unit"
2
+
3
+ require_files = []
4
+ require_files << File.join(File.dirname(__FILE__), "..", "..", "lib", "ruby-tmdb3.rb")
5
+ require_files.concat Dir[File.join(File.dirname(__FILE__), '..', 'setup', '*.rb')]
6
+
7
+ require_files.each do |file|
8
+ require File.expand_path(file)
9
+ end
10
+
11
+ class DirectRequireTest < Test::Unit::TestCase
12
+
13
+ test "TmdbMovie should not raise exception when directly required without using rubygems" do
14
+ assert_nothing_raised do
15
+ TmdbMovie.find(:id => 187)
16
+ end
17
+ end
18
+
19
+ test "TmdbCast should not raise exception when directly required without using rubygems" do
20
+ assert_nothing_raised do
21
+ TmdbCast.find(:id => 287)
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,124 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper.rb'))
2
+
3
+ class TmdbCastTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ register_api_url_stubs
7
+ end
8
+
9
+ test "search that returns no results should create empty array" do
10
+ movie = TmdbCast.find(:name => "item_not_found")
11
+ assert_equal [], movie
12
+ end
13
+
14
+ test "cast data should be able to be dumped and re-loaded" do
15
+ assert_nothing_raised do
16
+ cast = TmdbCast.find(:id => 287)
17
+ TmdbCast.new(cast.raw_data)
18
+ end
19
+ end
20
+
21
+ test "find by id should return full cast data" do
22
+ cast = TmdbCast.find(:id => 287)
23
+ assert_cast_methodized(cast, 287)
24
+ end
25
+
26
+ test "two cast objects with same data should be equal" do
27
+ cast1 = TmdbCast.find(:id => 287, :limit => 1)
28
+ cast2 = TmdbCast.find(:id => 287, :limit => 1)
29
+ assert_equal cast1, cast2
30
+ end
31
+
32
+ test "find by name should return full cast data when :expand_results = true" do
33
+ cast = TmdbCast.find(:name => "Brad Pitt", :expand_results => true)
34
+ cast = cast.first if cast.class == Array
35
+ assert_cast_methodized(cast, 287)
36
+ end
37
+
38
+ test "should raise exception if no arguments supplied to find" do
39
+ assert_raise ArgumentError do
40
+ TmdbCast.find()
41
+ end
42
+ end
43
+
44
+ test "find by id should return a single cast member" do
45
+ assert_kind_of OpenStruct, TmdbCast.find(:id => 287)
46
+ end
47
+
48
+ test "find by name should return an array of cast members" do
49
+ cast_members = TmdbCast.find(:name => "vince")
50
+ assert_kind_of Array, cast_members
51
+ cast_members.each do |actor|
52
+ assert_kind_of OpenStruct, actor
53
+ end
54
+ end
55
+
56
+ test "should raise error if limit is smaller than 1" do
57
+ [0, -1, -100].each do |limit|
58
+ assert_raise ArgumentError do
59
+ TmdbCast.find(:name => "vince", :limit => limit)
60
+ end
61
+ end
62
+ end
63
+
64
+ test "should raise error if limit is not an integer" do
65
+ [1.001, "1.2", "hello", [1,2,3], {:test => "1"}].each do |limit|
66
+ assert_raise ArgumentError do
67
+ TmdbCast.find(:name => "vince", :limit => limit)
68
+ end
69
+ end
70
+ end
71
+
72
+ test "should only return a single item if limit=1" do
73
+ actor = TmdbCast.find(:name => "Vince", :limit => 1)
74
+ assert_kind_of OpenStruct, actor
75
+ end
76
+
77
+ test "should return X items if limit=X" do
78
+ (2..5).each do |x|
79
+ actors = TmdbCast.find(:name => "Vince", :limit => x)
80
+ assert_kind_of Array, actors
81
+ assert_equal x, actors.length
82
+ actors.each do |actor|
83
+ assert_kind_of OpenStruct, actor
84
+ end
85
+ end
86
+ end
87
+
88
+ test "should not pass language to Tmdb.api_call if language is not supplied" do
89
+ Tmdb.expects(:api_call).with("person", {id: "1"}, nil).returns(nil)
90
+ Tmdb.expects(:api_call).with("search/person", {query: "1"}, nil).returns(nil)
91
+ TmdbCast.find(:id => 1, :name => 1)
92
+ end
93
+
94
+ test "should pass through language to Tmdb.api_call when language is supplied" do
95
+ Tmdb.expects(:api_call).with("person", {id: "1"}, "foo").returns(nil)
96
+ Tmdb.expects(:api_call).with("search/person", {query: "1"}, "foo").returns(nil)
97
+ TmdbCast.find(:id => 1, :name => 1, :language => "foo")
98
+ end
99
+
100
+ test "TmdbCast.new should raise error if supplied with raw data for cast member that doesn't exist" do
101
+ Tmdb.expects(:api_call).with("person", {id: "999999999999"}, nil).returns(nil)
102
+ assert_raise ArgumentError do
103
+ TmdbCast.new({"id" => 999999999999}, true)
104
+ end
105
+ end
106
+
107
+ private
108
+
109
+ def assert_cast_methodized(actor, cast_id)
110
+ @cast_data = Tmdb.api_call("person", {id: cast_id.to_s})
111
+ assert_equal @cast_data["adult"], actor.adult
112
+ assert_equal @cast_data["also_known_as"], actor.also_known_as
113
+ assert_equal @cast_data["biography"], actor.biography
114
+ assert_equal @cast_data["birthday"], actor.birthday
115
+ assert_equal @cast_data["biography"], actor.biography
116
+ assert_equal @cast_data["deathday"], actor.deathday
117
+ assert_equal @cast_data["homepage"], actor.homepage
118
+ assert_equal @cast_data["id"], actor.id
119
+ assert_equal @cast_data["name"], actor.name
120
+ assert_equal @cast_data["place_of_birth"], actor.place_of_birth
121
+ assert_equal @cast_data["profile_path"], actor.profile_path
122
+ end
123
+
124
+ end
@@ -0,0 +1,158 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper.rb'))
2
+
3
+ class TmdbMovieTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ register_api_url_stubs
7
+ end
8
+
9
+ test "search that returns no results should create empty array" do
10
+ movie = TmdbMovie.find(:title => "item_not_found")
11
+ assert_equal [], movie
12
+ end
13
+
14
+ test "movie should be able to be dumped and re-loaded" do
15
+ assert_nothing_raised do
16
+ movie = TmdbMovie.find(:id => 187)
17
+ TmdbMovie.new(movie.raw_data)
18
+ end
19
+ end
20
+
21
+ test "find by id should return the full movie data" do
22
+ movie = TmdbMovie.find(:id => 187)
23
+ assert_movie_methodized(movie, 187)
24
+ end
25
+
26
+ test "movies with same data should be seen as equal" do
27
+ movie1 = TmdbMovie.find(:id => 187, :limit => 1)
28
+ movie2 = TmdbMovie.find(:id => 187, :limit => 1)
29
+ assert_equal movie1, movie2
30
+ end
31
+
32
+ test "find by imdb should return the full movie data" do
33
+ movie = TmdbMovie.find(:imdb => "tt0401792")
34
+ assert_movie_methodized(movie, 187)
35
+ end
36
+
37
+ test "find by title should return the full movie data when expand_results set to true" do
38
+ movie = TmdbMovie.find(:title => "Sin City", :limit => 1, :expand_results => true)
39
+ assert_movie_methodized(movie, 187)
40
+ end
41
+
42
+ test "should raise exception if no arguments supplied to find" do
43
+ assert_raise ArgumentError do
44
+ TmdbMovie.find()
45
+ end
46
+ end
47
+
48
+ test "find by id should return a single movie" do
49
+ assert_kind_of OpenStruct, TmdbMovie.find(:id => 187)
50
+ end
51
+
52
+ test "find by imdb should return a single movie" do
53
+ assert_kind_of OpenStruct, TmdbMovie.find(:imdb => "tt0401792")
54
+ end
55
+
56
+ test "find by title should return an array of movies" do
57
+ movies = TmdbMovie.find(:title => "Iron Man")
58
+ assert_kind_of Array, movies
59
+ movies.each do |movie|
60
+ assert_kind_of OpenStruct, movie
61
+ end
62
+ end
63
+
64
+ test "find by title with limit=1 should return a single movie" do
65
+ assert_kind_of OpenStruct, TmdbMovie.find(:title => "Iron Man", :limit => 1)
66
+ end
67
+
68
+ test "find by title with limit=X should return an array of X movies" do
69
+ (2..5).each do |x|
70
+ movies = TmdbMovie.find(:title => "Iron Man", :limit => x)
71
+ assert_kind_of Array, movies
72
+ assert_equal x, movies.length
73
+ movies.each do |movie|
74
+ assert_kind_of OpenStruct, movie
75
+ end
76
+ end
77
+ end
78
+
79
+ test "should raise error if limit is smaller than 1" do
80
+ [0, -1, -100].each do |limit|
81
+ assert_raise ArgumentError do
82
+ TmdbMovie.find(:title => "Iron Man", :limit => limit)
83
+ end
84
+ end
85
+ end
86
+
87
+ test "should raise error if limit is not an integer" do
88
+ [1.001, "1.2", "hello", [1,2,3], {:test => "1"}].each do |limit|
89
+ assert_raise ArgumentError do
90
+ TmdbMovie.find(:title => "Iron Man", :limit => limit)
91
+ end
92
+ end
93
+ end
94
+
95
+ test "find should not pass language to Tmdb.api_call if language is not supplied" do
96
+ Tmdb.expects(:api_call).with("movie", {id: "1"}, nil).twice
97
+ Tmdb.expects(:api_call).with("search/movie", {query: "1"}, nil)
98
+ TmdbMovie.find(:id => 1, :imdb => 1, :title => 1)
99
+ end
100
+
101
+ test "find should pass through language to Tmdb.api_call when language is supplied" do
102
+ Tmdb.expects(:api_call).with("movie", {id: "1"}, "foo").twice
103
+ Tmdb.expects(:api_call).with("search/movie", {query: "1"}, "foo")
104
+ TmdbMovie.find(:id => 1, :imdb => 1, :title => 1, :language => "foo")
105
+ end
106
+
107
+ test "TmdbMovie.new should raise error if supplied with raw data for movie that doesn't exist" do
108
+ Tmdb.expects(:api_call).with("movie", {id: "999999999999"}, nil).returns(nil)
109
+ assert_raise ArgumentError do
110
+ TmdbMovie.new({"id" => 999999999999}, true)
111
+ end
112
+ end
113
+
114
+ private
115
+
116
+ def assert_movie_methodized(movie, movie_id)
117
+ @movie_data = Tmdb.api_call("movie", {id: movie_id.to_s})
118
+ assert_equal @movie_data["adult"], movie.adult
119
+ assert_equal @movie_data["budget"], movie.budget
120
+ assert_equal @movie_data["homepage"], movie.homepage
121
+ assert_equal @movie_data["id"], movie.id
122
+ assert_equal @movie_data["imdb_id"], movie.imdb_id
123
+ assert_equal @movie_data["original_title"], movie.original_title
124
+ assert_equal @movie_data["overview"], movie.overview
125
+ assert_equal @movie_data["popularity"], movie.popularity
126
+ assert_equal @movie_data["poster_path"], movie.poster_path
127
+ assert_equal @movie_data["release_date"], movie.release_date
128
+ assert_equal @movie_data["revenue"], movie.revenue
129
+ assert_equal @movie_data["runtime"], movie.runtime
130
+ assert_equal @movie_data["tagline"], movie.tagline
131
+ assert_equal @movie_data["title"], movie.title
132
+ assert_equal @movie_data["vote_average"], movie.vote_average
133
+ assert_equal @movie_data["vote_count"], movie.vote_count
134
+
135
+ assert_equal @movie_data["belongs_to_collection"]["id"], movie.belongs_to_collection.id
136
+ assert_equal @movie_data["belongs_to_collection"]["name"], movie.belongs_to_collection.name
137
+ assert_equal @movie_data["belongs_to_collection"]["poster_path"], movie.belongs_to_collection.poster_path
138
+ assert_equal @movie_data["belongs_to_collection"]["backdrop_path"], movie.belongs_to_collection.backdrop_path
139
+
140
+ @movie_data["genres"].each_index do |x|
141
+ assert_equal @movie_data["genres"][x]["id"], movie.genres[x].id
142
+ assert_equal @movie_data["genres"][x]["name"], movie.genres[x].name
143
+ end
144
+ @movie_data["production_companies"].each_index do |x|
145
+ assert_equal @movie_data["production_companies"][x]["name"], movie.production_companies[x].name
146
+ assert_equal @movie_data["production_companies"][x]["id"], movie.production_companies[x].id
147
+ end
148
+ @movie_data["production_countries"].each_index do |x|
149
+ assert_equal @movie_data["production_countries"][x]["iso_3166_1"], movie.production_countries[x].iso_3166_1
150
+ assert_equal @movie_data["production_countries"][x]["name"], movie.production_countries[x].name
151
+ end
152
+ @movie_data["spoken_languages"].each_index do |x|
153
+ assert_equal @movie_data["spoken_languages"][x]["iso_639_1"], movie.spoken_languages[x].iso_639_1
154
+ assert_equal @movie_data["spoken_languages"][x]["name"], movie.spoken_languages[x].name
155
+ end
156
+ end
157
+
158
+ end
@@ -0,0 +1,180 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper.rb'))
2
+
3
+ class TmdbTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ register_api_url_stubs
7
+ @@old_default_language = Tmdb.default_language
8
+ end
9
+
10
+ def teardown
11
+ Tmdb.default_language = @@old_default_language
12
+ end
13
+
14
+ test "should allow setting and getting of api_key" do
15
+ old_api_key = Tmdb.api_key
16
+ api_key = "test1234567890"
17
+ Tmdb.api_key = api_key
18
+ assert_equal Tmdb.api_key, api_key
19
+ Tmdb.api_key = old_api_key
20
+ end
21
+
22
+ test "language should default to 'en'" do
23
+ assert_equal "en", Tmdb.default_language
24
+ end
25
+
26
+ test "should allow setting and getting of language" do
27
+ old_language = Tmdb.default_language
28
+ new_language = "blah"
29
+ Tmdb.default_language = new_language
30
+ assert_equal new_language, Tmdb.default_language
31
+ end
32
+
33
+ test "should return base API url" do
34
+ assert_equal "http://api.themoviedb.org/3/", Tmdb.base_api_url
35
+ end
36
+
37
+ test "get url returns a response object" do
38
+ test_response = Tmdb.get_url("http://example.com/")
39
+ assert_equal 200, test_response.code.to_i
40
+ end
41
+
42
+ test "getting nonexistent URL returns response object" do
43
+ test_response = Tmdb.get_url('http://thisisaurlthatdoesntexist.co.nz')
44
+ assert_equal 404, test_response.code.to_i
45
+ end
46
+
47
+ test "API call without explicit language setting should use default language" do
48
+ method = "search/movie"
49
+ data = "hello"
50
+ Tmdb.default_language = "es"
51
+ url = Tmdb.base_api_url + method + '?api_key=' + Tmdb.api_key + '&language=' + Tmdb.default_language + '&query=' + CGI::escape(data.to_s)
52
+ mock_response = stub(:code => "200", :body => '{"page":1,"results":[],"total_pages":0,"total_results":0}')
53
+ Tmdb.expects(:get_url).with(url).returns(mock_response)
54
+ Tmdb.api_call(method, {query: data})
55
+ end
56
+
57
+ test "API call with explicit language setting should override default language" do
58
+ method = "movie"
59
+ data = "hello"
60
+ language = "blah"
61
+ url = Tmdb.base_api_url + method + "?api_key=" + Tmdb.api_key + "&language=" + language + '&query=' + CGI::escape(data.to_s)
62
+ mock_response = stub(:code => "200", :body => '{"page":1,"results":[],"total_pages":0,"total_results":0}')
63
+ Tmdb.expects(:get_url).with(url).returns(mock_response)
64
+ Tmdb.api_call(method, {query: data}, language)
65
+ end
66
+
67
+ test "api_call should raise exception if api_key is not set" do
68
+ old_api_key = Tmdb.api_key
69
+ Tmdb.api_key = ""
70
+ assert_raises ArgumentError do
71
+ Tmdb.api_call('Movie.search', 'Transformers')
72
+ end
73
+ Tmdb.api_key = old_api_key
74
+ end
75
+
76
+ test "should perform search/movie API call and return a Hash with an array of results" do
77
+ movies = Tmdb.api_call("search/movie", {query: "Transformers"})
78
+ assert_kind_of Hash, movies
79
+ assert_kind_of Array, movies["results"]
80
+ movies["results"].each do |movie|
81
+ assert_kind_of Hash, movie
82
+ %w(original_title id).each do |item|
83
+ assert movie[item]
84
+ end
85
+ end
86
+ end
87
+
88
+ test "should perform movie API call and return a single result" do
89
+ result = Tmdb.api_call("movie", {id: "187"})
90
+ assert_kind_of Hash, result
91
+ %w(original_title id).each do |item|
92
+ assert_not_nil result[item]
93
+ end
94
+ end
95
+
96
+ test "should perform Movie.imdbLookup API call and return a single result" do
97
+ result = Tmdb.api_call("movie", {id: "tt0401792"})
98
+ assert_kind_of Hash, result
99
+ %w(original_title id).each do |item|
100
+ assert result[item]
101
+ end
102
+ end
103
+
104
+ test "should perform person API call and return a single result" do
105
+ result = Tmdb.api_call("person", {id: 287})
106
+ assert_kind_of Hash, result
107
+ %w(homepage id name).each do |item|
108
+ assert_not_nil result[item]
109
+ end
110
+ end
111
+
112
+ test "should perform search/person API call and return a Hash with an array of results" do
113
+ people = Tmdb.api_call("search/person", {query: "vince"})
114
+ assert_kind_of Array, people["results"]
115
+ people["results"].each do |person|
116
+ assert_kind_of Hash, person
117
+ %w(id name).each do |item|
118
+ assert_not_nil person[item]
119
+ end
120
+ end
121
+ end
122
+
123
+ test "API call that returns 404 should raise exception" do
124
+ assert_raise ArgumentError do
125
+ movies = Tmdb.api_call('Movie.blarg', 'Transformers')
126
+ end
127
+ end
128
+
129
+ test "API call that finds no results should return nil" do
130
+ movies = Tmdb.api_call('search/movie', {query: "item_not_found"})
131
+ assert_nil movies
132
+ end
133
+
134
+ test "API call cache should not be changed when data altered in the receiving method" do
135
+ person = Tmdb.api_call("person", {id: 287})
136
+ assert_not_nil person[person.keys[0]]
137
+ person[person.keys[0]] = nil
138
+ person = Tmdb.api_call("person", {id: 287})
139
+ assert_not_nil person[person.keys[0]]
140
+ end
141
+
142
+ test "data_to_object should create object from nested data structures" do
143
+ test_data = {
144
+ :test1 => [
145
+ 1,2,3,4
146
+ ],
147
+ :test2 => 1
148
+ }
149
+ test_object = Tmdb.data_to_object(test_data)
150
+ assert_nothing_raised do
151
+ assert_equal [1,2,3,4], test_object.test1
152
+ assert_equal 1, test_object.test2
153
+ end
154
+ end
155
+
156
+ test "data_to_object should include raw_data method that returns original data" do
157
+ test_data = {
158
+ :test1 => [1,2,3]
159
+ }
160
+ test_object = Tmdb.data_to_object(test_data)
161
+ assert_equal test_object.raw_data, test_data
162
+ end
163
+
164
+ test "data_to_object should convert arrays containing images to nicer format" do
165
+ test_data = {
166
+ "backdrops" => [
167
+ {
168
+ "image" => {
169
+ :test => 1
170
+ }
171
+ }
172
+ ]
173
+ }
174
+ test_object = Tmdb.data_to_object(test_data)
175
+ assert_nothing_raised do
176
+ assert_equal 1, test_object.backdrops[0].test
177
+ end
178
+ end
179
+
180
+ end