require 'spec_helper'

describe Country do

  include UsesTempFiles

  ISO3166_CSV = 'iso3166.csv'

  in_directory_with_file(ISO3166_CSV)

  let(:iso3166_data) {tiny_maxmind_dataset[:iso3166]}

  before(:each) do
    content_for_file(to_csv(iso3166_data))
  end

  describe "import_from_iso3166" do

    after(:all) do
      # anything that calls after_maxmind_import seems to break transactions (DatabaseCleaner)
      create_phony_database
    end

    it "succeeds" do
      Country.import_from_iso3166(file: ISO3166_CSV)

      result = Country.connection.execute("SELECT * FROM countries_copied")
      result.ntuples.should == 1
      row1 = iso3166_data[0]
      result[0]['countrycode'].should == row1[ISO3166_COUNTRYCODE_INDEX]
      result[0]['countryname'].should == row1[ISO3166_COUNTRYNAME_INDEX]

      list_indexes('countries_copied').should == []

      # verify we can swap out tables
      Country.after_maxmind_import

      table_exists?('countries_copied').should be_false
      result = Country.connection.execute("SELECT * FROM countries")
      result.ntuples.should == 1
      list_indexes('countries').should =~ []

    end
  end

end
