<commit_before>require 'spec_helper'

describe '/api/movies' do
  it 'allows me to get information and ratings of a movie as JSON' do
    stub_rottentomatoes_query('Inception', 'rt_inception')
    stub_imdb_query('Inception', 2010, 'imdb_inception')

    get '/api/movies/2010/Inception'

    parsed_body = JSON.parse(last_response.body)
    parsed_body['name'].should == 'Inception'
    parsed_body['ratings']['Rotten Tomatoes']['score'].should == 8.95
    parsed_body['ratings']['IMDB']['score'].should == 8.8
  end

  it 'does not fail if one clients has a parse error' do
    stub_rottentomatoes_query('Inception', 'rt_inception')
    stub_imdb_query('Inception', 2010, 'json_parse_error')

    get '/api/movies/2010/Inception'

    parsed_body = JSON.parse(last_response.body)
    parsed_body['ratings']['IMDB']['error'].should == 'Could not be parsed'
    parsed_body['combined_rating'].should == 8.95
  end

  it 'does not fail if one client can not find the movie matching the criteria' do
    stub_rottentomatoes_query('Inception', 'rt_inception_wrong_year')

    stub_imdb_query('Inception', 2010, 'imdb_inception')

    get '/api/movies/2010/Inception'

    parsed_body = JSON.parse(last_response.body)

    parsed_body['ratings']['IMDB']['score'].should == 8.8
    parsed_body['combined_rating'].should == 8.8
    parsed_body['ratings']['Rotten Tomatoes']['error'].should == 'Could not be found'
  end

  it 'works with escaped movie names in the url and returns the right moviename' do
    stub_imdb_query("The%20Good,%20The%20Bad%20And%20The%20Ugly", 1966, 'imdb_goodbadandugly')
    stub_rottentomatoes_query('The%20Good,%20The%20Bad%20And%20The%20Ugly', 'rt_goodbadandugly')

    get '/api/movies/1966/The%20Good,%20The%20Bad%20And%20The%20Ugly'

    parsed_body = JSON.parse(last_response.body)
    parsed_body['name'].should == 'The Good, The Bad And The Ugly'
  end

  it 'reconstructs the movie ratings and info from the cache without hitting the network' do
    AnyGood::REDIS.set(
      "movierating:#{URI.encode('Inception')}:#{URI.encode('IMDB')}",
      {score: 9.0, url: 'example.org'}.to_json
    )
    AnyGood::REDIS.set(
      "movierating:#{URI.encode('Inception')}:#{URI.encode('Rotten Tomatoes')}",
      {score: 8.95, url: 'http://www.rottentomatoes.com/m/inception/'}.to_json
    )
    AnyGood::REDIS.set(
      "movieinfo:#{URI.encode('Inception')}:#{URI.encode('Rotten Tomatoes')}",
      {
        poster: 'http://content8.flixster.com/movie/10/93/37/10933762_det.jpg',
        year: 2010
      }.to_json
    )

    get '/api/movies/2010/Inception'

    parsed_body = JSON.parse(last_response.body)
    parsed_body['combined_rating'].should == 8.975
    parsed_body['ratings']['IMDB']['url'].should == 'example.org'

    a_request(:get, /www/).should_not have_been_made
  end
end<commit_msg>Add the long lost missing spec<commit_after>require 'spec_helper'

describe '/api/movies' do
  before(:each) do
    stub_rottentomatoes_query('Inception', 'rt_inception')
    stub_imdb_query('Inception', 2010, 'imdb_inception')
  end

  it 'allows me to get information and ratings of a movie as JSON' do
    get '/api/movies/2010/Inception'

    parsed_body = JSON.parse(last_response.body)
    parsed_body['name'].should == 'Inception'
    parsed_body['ratings']['Rotten Tomatoes']['score'].should == 8.95
    parsed_body['ratings']['IMDB']['score'].should == 8.8
  end

  it 'does not fail if one clients has a parse error' do
    stub_imdb_query('Inception', 2010, 'json_parse_error')

    get '/api/movies/2010/Inception'

    parsed_body = JSON.parse(last_response.body)
    parsed_body['ratings']['IMDB']['error'].should == 'Could not be parsed'
    parsed_body['combined_rating'].should == 8.95
  end

  it 'does not fail if one client can not find the movie matching the criteria' do
    stub_rottentomatoes_query('Inception', 'rt_inception_wrong_year')

    get '/api/movies/2010/Inception'

    parsed_body = JSON.parse(last_response.body)

    parsed_body['ratings']['IMDB']['score'].should == 8.8
    parsed_body['combined_rating'].should == 8.8
    parsed_body['ratings']['Rotten Tomatoes']['error'].should == 'Could not be found'
  end

  it 'works with escaped movie names in the url and returns the right moviename' do
    stub_imdb_query("The%20Good,%20The%20Bad%20And%20The%20Ugly", 1966, 'imdb_goodbadandugly')
    stub_rottentomatoes_query('The%20Good,%20The%20Bad%20And%20The%20Ugly', 'rt_goodbadandugly')

    get '/api/movies/1966/The%20Good,%20The%20Bad%20And%20The%20Ugly'

    parsed_body = JSON.parse(last_response.body)
    parsed_body['name'].should == 'The Good, The Bad And The Ugly'
  end

  it 'reconstructs the movie ratings and info from the cache without hitting the network' do
    AnyGood::REDIS.set(
      "movierating:#{URI.encode('Inception')}:#{URI.encode('IMDB')}",
      {score: 9.0, url: 'example.org'}.to_json
    )
    AnyGood::REDIS.set(
      "movierating:#{URI.encode('Inception')}:#{URI.encode('Rotten Tomatoes')}",
      {score: 8.95, url: 'http://www.rottentomatoes.com/m/inception/'}.to_json
    )
    AnyGood::REDIS.set(
      "movieinfo:#{URI.encode('Inception')}:#{URI.encode('Rotten Tomatoes')}",
      {
        poster: 'http://content8.flixster.com/movie/10/93/37/10933762_det.jpg',
        year: 2010
      }.to_json
    )

    get '/api/movies/2010/Inception'

    parsed_body = JSON.parse(last_response.body)
    parsed_body['combined_rating'].should == 8.975
    parsed_body['ratings']['IMDB']['url'].should == 'example.org'

    a_request(:get, /www/).should_not have_been_made
  end

  it 'increments the score of a movie after it is fetched' do
    movie_matcher = stub(:movie_matcher)
    AnyGood::MovieMatcher.stub(new: movie_matcher)

    movie_matcher.should_receive(:incr_score_for).with({name: 'Inception', year: 2010})

    get '/api/movies/2010/Inception'
  end
end
{'fraction_non_alphanumeric': 0.14959959107173285, 'fraction_numerical': 0.03884818538081445, 'mean_word_length': 4.302619692863595, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1348110', 'n_tokens_mistral': 2198, 'n_tokens_neox': 2058, 'n_words': 391}