=begin
require 'spec_helper'

describe GeoIpBlocks do

  include UsesTempFiles

  GEOIPCITY_BLOCKS = 'geo_ip_blocks.csv'

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

  let(:first)  { GeoIpBlocks.lookup(0x01020304) } # 17192
  let(:second) { GeoIpBlocks.lookup(0x12030405) } # 667
  let(:third)  { GeoIpBlocks.lookup(0xffff0001) } # bogus

  it "first" do first.should_not be_nil end
  it "first.beginip" do first.beginip.should == 0x00000000 end
  it "first.endip" do first.endip.should == 0x0fffffff end
  it "first.locid" do first.locid.should == 17192 end

  it "second" do second.should_not be_nil end
  it "second.beginip" do second.beginip.should == 0x10000000 end
  it "second.endip" do second.endip.should == 0x1fffffff end
  it "second.locid" do second.locid.should == 667 end

  it "third" do third.should be_nil end

  describe "import_from_max_mind" do

    in_directory_with_file(GEOIPCITY_BLOCKS)

    let(:geo_ip_city_blocks_data) {tiny_maxmind_dataset[:geo_ip_city_134_blocks]}

    before(:each) do
      content_for_file("Copyright (c) 2011 MaxMind Inc.  All Rights Reserved.\n" +
                       "startIpNum,endIpNum,locId\n" +
                       to_csv(geo_ip_city_blocks_data))
    end

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


    it "succeeds" do
      GeoIpBlocks.import_from_max_mind(file: GEOIPCITY_BLOCKS)

      result = GeoIpBlocks.connection.execute("SELECT * FROM geoipblocks_copied")
      result.ntuples.should == 3
      row1 = geo_ip_city_blocks_data[0]
      result[0]['beginip'].to_i.should == row1[GEOIPBLOCKS_BEGINIP_INDEX]
      result[0]['endip'].to_i.should == row1[GEOIPBLOCKS_ENDIP_INDEX]
      result[0]['locid'].to_i.should == row1[GEOIPBLOCKS_LOCID_INDEX]
      result[0]['geom'].should_not be_nil

      list_indexes('geoipblocks_copied').should =~ [GeoIpBlocks::COPIED_GEOIPBLOCKS_INDEX_NAME]

      # verify we can swap out tables
      GeoIpBlocks.after_maxmind_import

      table_exists?('geoipblocks_copied').should be_false
      result = GeoIpBlocks.connection.execute("SELECT * FROM geoipblocks")
      result.ntuples.should == 3
      list_indexes('geoipblocks').should =~ [GeoIpBlocks::GEOIPBLOCKS_INDEX_NAME]
    end
  end
end
=end