<commit_before>require 'test_helper'

class EverypoliticianTest < Minitest::Test
  def test_that_it_has_a_version_number
    refute_nil ::Everypolitician::VERSION
  end

  # Clear the countries.json cache before each run
  def setup
    Everypolitician.countries = nil
  end

  def test_country_find
    VCR.use_cassette('countries_json') do
      country = Everypolitician::Country.find('Australia')
      assert_equal 'Australia', country.name
      assert_equal 'AU', country.code
      assert_equal 2, country.legislatures.size
    end
  end

  def test_legislature_find
    VCR.use_cassette('countries_json') do
      legislature = Everypolitician::Legislature.find('Australia', 'Senate')
      assert_equal 'Senate', legislature.name
      assert_equal 'https://raw.githubusercontent.com/everypolitician/everypolitician-data/ea04acd/data/Australia/Senate/ep-popolo-v1.0.json', legislature.popolo_url
      assert legislature.legislative_periods.is_a?(Array)
    end
  end

  def test_country_convenience_method
    VCR.use_cassette('countries_json') do
      country = Everypolitician.country('Australia')
      assert_equal 'Australia', country.name
      assert_equal 'AU', country.code
      assert_equal 2, country.legislatures.size
    end
  end

  def test_legislature_convenience_method
    VCR.use_cassette('countries_json') do
      legislature = Everypolitician.legislature('Australia', 'Senate')
      assert_equal 'Senate', legislature.name
      assert_equal 'https://raw.githubusercontent.com/everypolitician/everypolitician-data/ea04acd/data/Australia/Senate/ep-popolo-v1.0.json', legislature.popolo_url
      assert legislature.legislative_periods.is_a?(Array)
    end
  end

  def test_country_and_legislature_convenience_method
    VCR.use_cassette('countries_json') do
      country, legislature = Everypolitician.country_legislature('Australia', 'Senate')
      assert_equal 'Australia', country.name
      assert_equal 'AU', country.code
      assert_equal 2, country.legislatures.size
      assert_equal 'Senate', legislature.name
      assert_equal 'https://raw.githubusercontent.com/everypolitician/everypolitician-data/ea04acd/data/Australia/Senate/ep-popolo-v1.0.json', legislature.popolo_url
      assert_equal 'Australia/Senate', legislature.directory
      assert legislature.legislative_periods.is_a?(Array)
    end
  end

  def test_sources_dir_convenience_method
    VCR.use_cassette('countries_json') do
      legislature = Everypolitician::Legislature.from_sources_dir('Australia/Senate')
      assert_equal 'Australia', legislature.country.name
      assert_equal 'AU', legislature.country.code
      assert_equal 'Senate', legislature.name
    end
  end

  def test_raises_an_error_for_unknown_slugs
    VCR.use_cassette('countries_json') do
      assert_raises Everypolitician::Error do
        Everypolitician.country('Foo')
      end
    end
    VCR.use_cassette('countries_json') do
      assert_raises Everypolitician::Error do
        Everypolitician.legislature('Foo', 'Bar')
      end
    end
  end

  def test_retrieving_countries_json
    VCR.use_cassette('countries_json') do
      assert_equal Everypolitician.countries_json, 'https://raw.githubusercontent.com/' \
        'everypolitician/everypolitician-data/master/countries.json'
    end
  end

  def test_setting_countries_json_url
    Everypolitician.countries_json = 'path/to/local/countries.json'
    assert_equal 'path/to/local/countries.json', Everypolitician.countries_json
    Everypolitician.countries_json = nil
  end

  def test_alternative_constant_name
    assert_equal Everypolitician, EveryPolitician
  end

  def test_retrieving_popolo
    VCR.use_cassette('popolo') do
      australia_senate = Everypolitician.legislature('Australia', 'Senate')
      assert_instance_of Everypolitician::Popolo::JSON, australia_senate.popolo
    end
  end

  def test_getting_all_countries
    VCR.use_cassette('countries_json') do
      all_countries = Everypolitician.countries
      assert_equal 233, all_countries.size
      country = all_countries.first
      assert_instance_of Everypolitician::Country, country
    end
  end

  def test_finding_country_by_attributes
    VCR.use_cassette('countries_json') do
      country = Everypolitician.country(code: 'AU')
      assert_equal 'Australia', country.name
    end
  end

  def test_finding_legislature_by_attributes
    VCR.use_cassette('countries_json') do
      country = Everypolitician.country(code: 'AU')
      senate = country.legislature(slug: 'Senate')
      assert_equal 'Senate', senate.name
    end
  end

  def test_accessing_properties_with_square_brackets
    VCR.use_cassette('countries_json') do
      country = Everypolitician.country(code: 'AU')
      assert_equal 'Australia', country[:name]
    end
  end

  def test_legislative_periods
    VCR.use_cassette('countries_json') do
      au_senate = Everypolitician.country(slug: 'Australia').legislature(slug: 'Senate')
      lp = au_senate.legislative_periods.first
      assert_equal 'term/44', lp.id
      assert_equal '44th Parliament', lp.name
      assert_equal Date.new(2013, 9, 7), lp.start_date
      assert_equal '44', lp.slug
      assert_equal 'https://raw.githubusercontent.com/everypolitician/everypolitician-data/ea04acd/data/Australia/Senate/term-44.csv', lp.csv_url
      assert_equal 'Senate', lp.legislature.name
      assert_equal 'Australia', lp.country.name
    end
  end

  def test_legislative_period_csv
    VCR.use_cassette('term-44-csv') do
      au_senate = Everypolitician.country(slug: 'Australia').legislature(slug: 'Senate')
      lp = au_senate.legislative_periods.first
      csv = lp.csv
      assert_instance_of CSV::Table, csv
    end
  end

  def test_legislative_period_square_brackets_expose_raw_data
    VCR.use_cassette('countries_json') do
      au_senate = Everypolitician.country(slug: 'Australia').legislature(slug: 'Senate')
      lp = au_senate.legislative_periods.first
      assert_equal 'term/44', lp[:id]
      assert_equal '44th Parliament', lp[:name]
      assert_equal '2013-09-07', lp[:start_date]
      assert_equal 'data/Australia/Senate/term-44.csv', lp[:csv]
    end<commit_msg>Ease updating tests when VCR files change by extracting sha<commit_after>require 'test_helper'

class EverypoliticianTest < Minitest::Test

  # Clear the countries.json cache before each run
  def setup
    Everypolitician.countries = nil
    @current_sha = 'ea04acd'
  end

  def test_that_it_has_a_version_number
    refute_nil ::Everypolitician::VERSION
  end

  def test_country_find
    VCR.use_cassette('countries_json') do
      country = Everypolitician::Country.find('Australia')
      assert_equal 'Australia', country.name
      assert_equal 'AU', country.code
      assert_equal 2, country.legislatures.size
    end
  end

  def test_legislature_find
    VCR.use_cassette('countries_json') do
      legislature = Everypolitician::Legislature.find('Australia', 'Senate')
      assert_equal 'Senate', legislature.name
      assert_equal "https://raw.githubusercontent.com/everypolitician/everypolitician-data/#{@current_sha}/data/Australia/Senate/ep-popolo-v1.0.json", legislature.popolo_url
      assert legislature.legislative_periods.is_a?(Array)
    end
  end

  def test_country_convenience_method
    VCR.use_cassette('countries_json') do
      country = Everypolitician.country('Australia')
      assert_equal 'Australia', country.name
      assert_equal 'AU', country.code
      assert_equal 2, country.legislatures.size
    end
  end

  def test_legislature_convenience_method
    VCR.use_cassette('countries_json') do
      legislature = Everypolitician.legislature('Australia', 'Senate')
      assert_equal 'Senate', legislature.name
      assert_equal "https://raw.githubusercontent.com/everypolitician/everypolitician-data/#{@current_sha}/data/Australia/Senate/ep-popolo-v1.0.json", legislature.popolo_url
      assert legislature.legislative_periods.is_a?(Array)
    end
  end

  def test_country_and_legislature_convenience_method
    VCR.use_cassette('countries_json') do
      country, legislature = Everypolitician.country_legislature('Australia', 'Senate')
      assert_equal 'Australia', country.name
      assert_equal 'AU', country.code
      assert_equal 2, country.legislatures.size
      assert_equal 'Senate', legislature.name
      assert_equal "https://raw.githubusercontent.com/everypolitician/everypolitician-data/#{@current_sha}/data/Australia/Senate/ep-popolo-v1.0.json", legislature.popolo_url
      assert_equal 'Australia/Senate', legislature.directory
      assert legislature.legislative_periods.is_a?(Array)
    end
  end

  def test_sources_dir_convenience_method
    VCR.use_cassette('countries_json') do
      legislature = Everypolitician::Legislature.from_sources_dir('Australia/Senate')
      assert_equal 'Australia', legislature.country.name
      assert_equal 'AU', legislature.country.code
      assert_equal 'Senate', legislature.name
    end
  end

  def test_raises_an_error_for_unknown_slugs
    VCR.use_cassette('countries_json') do
      assert_raises Everypolitician::Error do
        Everypolitician.country('Foo')
      end
    end
    VCR.use_cassette('countries_json') do
      assert_raises Everypolitician::Error do
        Everypolitician.legislature('Foo', 'Bar')
      end
    end
  end

  def test_retrieving_countries_json
    VCR.use_cassette('countries_json') do
      assert_equal Everypolitician.countries_json, 'https://raw.githubusercontent.com/' \
        'everypolitician/everypolitician-data/master/countries.json'
    end
  end

  def test_setting_countries_json_url
    Everypolitician.countries_json = 'path/to/local/countries.json'
    assert_equal 'path/to/local/countries.json', Everypolitician.countries_json
    Everypolitician.countries_json = nil
  end

  def test_alternative_constant_name
    assert_equal Everypolitician, EveryPolitician
  end

  def test_retrieving_popolo
    VCR.use_cassette('popolo') do
      australia_senate = Everypolitician.legislature('Australia', 'Senate')
      assert_instance_of Everypolitician::Popolo::JSON, australia_senate.popolo
    end
  end

  def test_getting_all_countries
    VCR.use_cassette('countries_json') do
      all_countries = Everypolitician.countries
      assert_equal 233, all_countries.size
      country = all_countries.first
      assert_instance_of Everypolitician::Country, country
    end
  end

  def test_finding_country_by_attributes
    VCR.use_cassette('countries_json') do
      country = Everypolitician.country(code: 'AU')
      assert_equal 'Australia', country.name
    end
  end

  def test_finding_legislature_by_attributes
    VCR.use_cassette('countries_json') do
      country = Everypolitician.country(code: 'AU')
      senate = country.legislature(slug: 'Senate')
      assert_equal 'Senate', senate.name
    end
  end

  def test_accessing_properties_with_square_brackets
    VCR.use_cassette('countries_json') do
      country = Everypolitician.country(code: 'AU')
      assert_equal 'Australia', country[:name]
    end
  end

  def test_legislative_periods
    VCR.use_cassette('countries_json') do
      au_senate = Everypolitician.country(slug: 'Australia').legislature(slug: 'Senate')
      lp = au_senate.legislative_periods.first
      assert_equal 'term/44', lp.id
      assert_equal '44th Parliament', lp.name
      assert_equal Date.new(2013, 9, 7), lp.start_date
      assert_equal '44', lp.slug
      assert_equal "https://raw.githubusercontent.com/everypolitician/everypolitician-data/#{@current_sha}/data/Australia/Senate/term-44.csv", lp.csv_url
      assert_equal 'Senate', lp.legislature.name
      assert_equal 'Australia', lp.country.name
    end
  end

  def test_legislative_period_csv
    VCR.use_cassette('term-44-csv') do
      au_senate = Everypolitician.country(slug: 'Australia').legislature(slug: 'Senate')
      lp = au_senate.legislative_periods.first
      csv = lp.csv
      assert_instance_of CSV::Table, csv
    end
  end

  def test_legislative_period_square_brackets_expose_raw_data
    VCR.use_cassette('countries_json') do
      au_senate = Everypolitician.country(slug: 'Australia').legislature(slug: 'Senate')
      lp = au_senate.legislative_periods.first
      assert_equal 'term/44', lp[:id]
      assert_equal '44th Parliament', lp[:name]
      assert_equal '2013-09-07', lp[:start_date]
      assert_equal 'data/Australia/Senate/term-44.csv', lp[:csv]
    end
{'fraction_non_alphanumeric': 0.09028112449799197, 'fraction_numerical': 0.007550200803212851, 'mean_word_length': 4.086192810457517, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 10, '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': '5189766', 'n_tokens_mistral': 4204, 'n_tokens_neox': 4006, 'n_words': 664}