<commit_before>require 'test_helper'
require_relative '../util/countries_test_helper'

class IosRatingsFetcherForItunesClientTest < Minitest::Test
  include CountriesTestHelper
  
  def setup
    @header = AppReputation::IosRatingsFetcherForItunesClient.class_variable_get(:@@header_user_agent)
    @resource = MiniTest::Mock.new
    countries = get_countries_from_stub_file(%w(US CN GB DE ES))
    AppReputation::IosRatingsFetcherForItunesClient.class_variable_set(:@@supported_countries, countries)
    @ios_ratings_fetcher = AppReputation::IosRatingsFetcherForItunesClient.new
  end
  
  def test_set_timeout
    timeout = 4
    AppReputation::IosRatingsFetcherForItunesClient.set_timeout(timeout)
    assert_equal(timeout, AppReputation::IosRatingsFetcherForItunesClient.class_variable_get(:@@timeout))
  end
  
  def test_set_max_retries
    max_retries = 2
    AppReputation::IosRatingsFetcherForItunesClient.set_max_retries(max_retries)
    assert_equal(max_retries, AppReputation::IosRatingsFetcherForItunesClient.class_variable_get(:@@max_retries))
  end
  
  def test_get_single_ratings_valid
    response = '{"ratingCountList":[1,2,3,5,8], "ratingCount": 19}'
    @resource.expect(:get, response, [@header])
    RestClient::Resource.stub(:new, @resource) do
      ratings = @ios_ratings_fetcher.send(:get_single_ratings, 123456789, 'us')
      assert_equal(AppReputation::Ratings.new(1, 2, 3, 5, 8), ratings)
    end
  end
  
  def test_get_single_ratings_invalid
    response = '{"iHaveNoRatingCountList": 0}'
    @resource.expect(:get, response, [@header])
    RestClient::Resource.stub(:new, @resource) do
      ratings = @ios_ratings_fetcher.send(:get_single_ratings, 123456789, 'us')
      assert_nil(ratings)
    end
  end
  
  def test_get_ratings_succeed_after_retry
    response = '{"ratingCountList":[1,2,3,5,8], "ratingCount": 19}'
    resource = mock()
    resource.stubs(:get).with(@header).raises(RestClient::RequestTimeout).then.returns(response)
    RestClient::Resource.stub(:new, resource) do
      ratings = @ios_ratings_fetcher.send(:get_single_ratings, 123456789, 'us')
      assert_equal(AppReputation::Ratings.new(1, 2, 3, 5, 8), ratings)
    end
  end
  
  def test_get_ratings_fail_all_retries
    resource = mock()
    resource.stubs(:get).with(@header).raises(RestClient::RequestTimeout)
    RestClient::Resource.stub(:new, resource) do
      assert_raises RestClient::RequestTimeout do
        @ios_ratings_fetcher.send(:get_single_ratings, 123456789, 'us')
      end
    end
  end
  
  def test_get_multiple_ratings<commit_msg>Rename test methods<commit_after>require 'test_helper'
require_relative '../util/countries_test_helper'

class IosRatingsFetcherForItunesClientTest < Minitest::Test
  include CountriesTestHelper
  
  def setup
    @header = AppReputation::IosRatingsFetcherForItunesClient.class_variable_get(:@@header_user_agent)
    @resource = MiniTest::Mock.new
    countries = get_countries_from_stub_file(%w(US CN GB DE ES))
    AppReputation::IosRatingsFetcherForItunesClient.class_variable_set(:@@supported_countries, countries)
    @ios_ratings_fetcher = AppReputation::IosRatingsFetcherForItunesClient.new
  end
  
  def test_set_timeout
    timeout = 4
    AppReputation::IosRatingsFetcherForItunesClient.set_timeout(timeout)
    assert_equal(timeout, AppReputation::IosRatingsFetcherForItunesClient.class_variable_get(:@@timeout))
  end
  
  def test_set_max_retries
    max_retries = 2
    AppReputation::IosRatingsFetcherForItunesClient.set_max_retries(max_retries)
    assert_equal(max_retries, AppReputation::IosRatingsFetcherForItunesClient.class_variable_get(:@@max_retries))
  end
  
  def test_get_single_ratings_valid
    response = '{"ratingCountList":[1,2,3,5,8], "ratingCount": 19}'
    @resource.expect(:get, response, [@header])
    RestClient::Resource.stub(:new, @resource) do
      ratings = @ios_ratings_fetcher.send(:get_single_ratings, 123456789, 'us')
      assert_equal(AppReputation::Ratings.new(1, 2, 3, 5, 8), ratings)
    end
  end
  
  def test_get_single_ratings_invalid
    response = '{"iHaveNoRatingCountList": 0}'
    @resource.expect(:get, response, [@header])
    RestClient::Resource.stub(:new, @resource) do
      ratings = @ios_ratings_fetcher.send(:get_single_ratings, 123456789, 'us')
      assert_nil(ratings)
    end
  end
  
  def test_get_single_ratings_succeed_after_retry
    response = '{"ratingCountList":[1,2,3,5,8], "ratingCount": 19}'
    resource = mock()
    resource.stubs(:get).with(@header).raises(RestClient::RequestTimeout).then.returns(response)
    RestClient::Resource.stub(:new, resource) do
      ratings = @ios_ratings_fetcher.send(:get_single_ratings, 123456789, 'us')
      assert_equal(AppReputation::Ratings.new(1, 2, 3, 5, 8), ratings)
    end
  end
  
  def test_get_single_ratings_fail_all_retries
    resource = mock()
    resource.stubs(:get).with(@header).raises(RestClient::RequestTimeout)
    RestClient::Resource.stub(:new, resource) do
      assert_raises RestClient::RequestTimeout do
        @ios_ratings_fetcher.send(:get_single_ratings, 123456789, 'us')
      end
    end
  end
  
  def test_get_multiple_ratings
{'fraction_non_alphanumeric': 0.1086534730306558, 'fraction_numerical': 0.024447031431897557, 'mean_word_length': 5.451814768460576, 'pattern_counts': {'":': 10, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1213280', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1769, 'n_words': 255}