Skip to content

Commit

Permalink
Add specs for tmdb_handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mikevallano committed May 17, 2020
1 parent 754cab7 commit 863ee6a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/tmdb_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ def self.tmdb_handler_update_movie(tmdb_id)
movie_url = "#{BASE_URL}/movie/#{tmdb_id}?api_key=#{ENV['tmdb_api_key']}&append_to_response=trailers,credits,similar,releases"
api_result = HTTParty.get(movie_url).deep_symbolize_keys rescue nil
Raven.capture_message("API request failed for tmdb_id: #{tmdb_id}") && return unless api_result && api_result[:id] == tmdb_id

updated_data = MovieMore.tmdb_info(api_result)

if movie.title != updated_data.title
Expand All @@ -114,6 +113,8 @@ def self.tmdb_handler_update_movie(tmdb_id)
runtime: updated_data.runtime,
mpaa_rating: updated_data.mpaa_rating
)
rescue ActiveRecord::RecordInvalid => e
Raven.capture_message("Movie failed to save: tmdb_id: #{tmdb_id}. Message: #{e.message}") && return
end

def tmdb_handler_actor_more(actor_id)
Expand Down
16 changes: 15 additions & 1 deletion spec/factories/movies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@
factory :invalid_movie do
tmdb_id { nil }
end
end

factory :movie_in_tmdb do
title { 'Fargo' }
tmdb_id { 275 }
imdb_id { 'tt0116282' }
backdrop_path { '/747dgDfL5d8esobk7h4odaOFhUq.jpg' }
poster_path { '/kKpORM0G7xDvJGQiXpQ0wUp9Dwo.jpg' }
release_date { 'Fri, 08 Mar 1996' }
overview { 'Jerry, a small-town Minnesota car salesman' }
trailer { 'h2tY82z3xXU' }
vote_average { 7.9 }
popularity { 17.27 }
mpaa_rating { 'R' }
director { 'Joel Coen' }
end
end
end
80 changes: 80 additions & 0 deletions spec/modules/tmdb_handler_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require 'rails_helper'

RSpec.describe TmdbHandler, type: :module do
let(:movie) { create(:movie_in_tmdb) }
let(:tmdb_id) { movie.tmdb_id }
subject { TmdbHandler.tmdb_handler_update_movie(tmdb_id) }

context 'with a valid movie' do
it 'returns true' do
VCR.use_cassette('tmdb_handler_update_movie_with_a_valid_movie', record: :new_episodes) do
expect(subject).to eq(true)
end
end

it 'updates the movie' do
VCR.use_cassette('tmdb_handler_update_movie_with_a_valid_movie', record: :new_episodes) do
expect{ subject }.to change{ movie.reload.updated_at }
end
end
end

context 'when movie is not found in db' do
let(:tmdb_id) { 'wrong' }
it { is_expected.to be_nil }
it { expect{subject}.not_to change{ movie.reload.updated_at } }
end

context 'when no movie found from api response' do
before { movie.update(tmdb_id: 'wrong')}
it 'returns nil' do
VCR.use_cassette('tmdb_handler_update_movie_with_an_invalid_movie', record: :new_episodes) do
expect(subject).to be_nil
end
end

it 'does not update the movie' do
VCR.use_cassette('tmdb_handler_update_movie_with_an_invalid_movie', record: :new_episodes) do
expect{ subject }.not_to change{ movie.reload.updated_at }
end
end
end

context 'when the title does not match' do
before { movie.update(title: 'Fargowrong') }
it 'returns nil' do
VCR.use_cassette('tmdb_handler_update_movie_with_wrong_title', record: :new_episodes) do
expect(subject).to be_nil
end
end

it 'does not update the movie' do
VCR.use_cassette('tmdb_handler_update_movie_with_wrong_title', record: :new_episodes) do
expect{ subject }.not_to change{ movie.reload.updated_at }
end
end
end

context 'with outdated info' do
let(:wrong_mpaa_rating) { 'G' }
let(:correct_mpaa_rating) { 'R' }
before { movie.update(mpaa_rating: wrong_mpaa_rating) }
it 'updates movie with latest tmdb info' do
VCR.use_cassette('tmdb_handler_update_movie_with_outdated_info', record: :new_episodes) do
subject
expect(movie.reload.mpaa_rating).to eq(correct_mpaa_rating)
end
end
end

context 'when the movie fails to save' do
let(:dupe_movie) { build(:movie, tmdb_id: movie.tmdb_id) }
before { dupe_movie.save(validate: false) }

it 'returns nil' do
VCR.use_cassette('tmdb_handler_update_movie_with_a_valid_movie', record: :new_episodes) do
expect(subject).to be_nil
end
end
end
end
4 changes: 2 additions & 2 deletions spec/support/vcr.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
VCR.configure do |config|
config.cassette_library_dir = "spec/vcr_cassettes"
config.hook_into :webmock
config.allow_http_connections_when_no_cassette = true
config.allow_http_connections_when_no_cassette = false
config.ignore_localhost = true
end
end

0 comments on commit 863ee6a

Please sign in to comment.