=begin
require 'spec_helper'

describe GeoIpLocations do

  include UsesTempFiles

  GEOIPCITY_LOCATIONS = 'geo_ip_locations.csv'

  it "count" do GeoIpLocations.count.should == 16 end


  before(:all) do
    create_phony_database
  end

  let(:first)  { GeoIpLocations.find_by_locid(17192) }
  let(:second) { GeoIpLocations.find_by_locid(1539) }
  let(:third)  { GeoIpLocations.find_by_locid(999999) } # bogus

  describe "first" do
    it "first" do first.should_not be_nil end
    it "first.locid" do first.locid.should == 17192 end
    it "first.countrycode" do first.countrycode.should eql('US') end
    it "first.region" do first.region.should eql('TX') end
    it "first.city" do first.city.should eql('Austin') end
    it "first.latitude" do first.latitude.should == 30.2076 end
    it "first.longitude" do first.longitude.should == -97.8587 end
  end

  describe "second" do
    it "second" do first.should_not be_nil end
    it "second.locid" do second.locid.should == 1539 end
    it "second.countrycode" do second.countrycode.should eql('US') end
    it "second.region" do second.region.should eql('WA') end
    it "second.city" do second.city.should eql('Seattle') end
    it "second.latitude" do second.latitude.should == 47.6103 end
    it "second.longitude" do second.longitude.should == -122.3341 end
  end

  it "third" do third.should be_nil end

  describe "import_from_max_mind" do

    in_directory_with_file(GEOIPCITY_LOCATIONS)

    let(:geo_ip_city_locations_data) {tiny_maxmind_dataset[:geo_ip_city_134_locations]}

    before(:each) do
      content_for_file("Copyright (c) 2012 MaxMind LLC.  All Rights Reserved.\n" +
                           "locId,country,region,city,postalCode,latitude,longitude,metroCode,areaCode\n" +
                           to_csv(geo_ip_city_locations_data))

      create_phony_database

    end

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

    it "succeeds" do
      GeoIpLocations.import_from_max_mind(file: GEOIPCITY_LOCATIONS)

      result = GeoIpLocations.connection.execute("SELECT * FROM geoiplocations_copied")
      result.ntuples.should == 3
      row1 = geo_ip_city_locations_data[0]
      result[0]['locid'].to_i.should == row1[GEOIPLOCATIONS_LOCID_INDEX]
      result[0]['countrycode'].should == row1[GEOIPLOCATIONS_COUNTRY_INDEX]
      result[0]['region'].should == row1[GEOIPLOCATIONS_REGION_INDEX]
      result[0]['city'].should == row1[GEOIPLOCATIONS_CITY_INDEX]
      result[0]['postalcode'].should == row1[GEOIPLOCATIONS_POSTALCODE_INDEX]
      result[0]['latitude'].to_f.should == row1[GEOIPLOCATIONS_LATITUDE_INDEX]
      result[0]['longitude'].to_f.should == row1[GEOIPLOCATIONS_LONGITUDE_INDEX]
      result[0]['metrocode'].to_i.should == row1[GEOIPLOCATIONS_METROCODE_INDEX]
      result[0]['areacode'].to_i.should == row1[GEOIPLOCATIONS_AREACODE_INDEX]
      result[0]['geog'].should_not be_nil

      list_indexes('geoiplocations_copied').should =~ [GeoIpLocations::COPIED_PRIMARY_KEY_NAME, GeoIpLocations::COPIED_GEOIPLOCATIONS_INDEX_NAME]

      # confirm that the cities table also got copied

      result = GeoIpLocations.connection.execute('SELECT * FROM cities_copied')
      result.ntuples.should == 1
      result[0]['city'].should == row1[GEOIPLOCATIONS_CITY_INDEX]
      result[0]['region'].should == row1[GEOIPLOCATIONS_REGION_INDEX]
      result[0]['countrycode'].should == row1[GEOIPLOCATIONS_COUNTRY_INDEX]

      list_indexes('cities_copied').should =~ []

      # verify we can swap out tables
      GeoIpLocations.after_maxmind_import

      table_exists?('geoiplocations_copied').should be_false
      result = GeoIpLocations.connection.execute("SELECT * FROM geoiplocations")
      result.ntuples.should == 3
      list_indexes('geoiplocations').should =~ [GeoIpLocations::PRIMARY_KEY_NAME, GeoIpLocations::GEOIPLOCATIONS_INDEX_NAME]

      table_exists?('cities_copied').should be_false
      result = GeoIpLocations.connection.execute("SELECT * FROM cities")
      result.ntuples.should == 1
      list_indexes('cities').should =~ []
    end

  end
end
=end
