require 'spec_helper'
=begin

describe JamIsp do

  include UsesTempFiles

  GEOIPISP = 'geoip_isp.csv'

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

  let(:first_addr) { JamIsp.ip_to_num('1.2.3.4') }
  let(:second_addr) { JamIsp.ip_to_num('2.3.4.5') }
  let(:third_addr) { JamIsp.ip_to_num('3.4.5.6') }
  let(:fourth_addr) { JamIsp.ip_to_num('4.5.6.7') }
  let(:fifth_addr) { JamIsp.ip_to_num('192.168.1.107') }
  let(:sixth_addr) { JamIsp.ip_to_num('255.254.253.252') }

  it "first_addr" do first_addr.should == 0x01020304 end
  it "second_addr" do second_addr.should == 0x02030405 end
  it "third_addr" do third_addr.should == 0x03040506 end
  it "fourth_addr" do fourth_addr.should == 0x04050607 end
  it "fifth_addr" do fifth_addr.should == 0xc0A8016b end
  it "sixth_addr" do sixth_addr.should == 0xfffefdfc end

  let(:first) { JamIsp.lookup(0x01020304) }
  let(:second) { JamIsp.lookup(0x12030405) }
  let(:third) { JamIsp.lookup(0x43040506) }
  let(:seventh) { JamIsp.lookup(0xffff0123) } # bogus

  it "first.coid" do first.coid.should == 2 end
  it "second.coid" do second.coid.should == 3 end
  it "third.coid" do third.coid.should == 4 end
  it "seventh" do seventh.should be_nil end

  describe "import_from_max_mind" do
    in_directory_with_file(GEOIPISP)

    let(:geo_ip_isp_data) {tiny_maxmind_dataset[:geo_ip_isp]}

    before(:each) do
      create_phony_database
      content_for_file(to_csv(geo_ip_isp_data))
    end

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

    it "succeeded" do
      JamIsp.import_from_max_mind(file: GEOIPISP)

      # verify geoipisp
      result = JamIsp.connection.execute("SELECT * FROM geoipisp_copied")
      result.ntuples.should == 1
      row1 = geo_ip_isp_data[0]
      result[0]['beginip'].to_i.should == row1[GEOIPISP_BEGINIP_INDEX]
      result[0]['endip'].to_i.should == row1[GEOIPISP_ENDIP_INDEX]
      result[0]['company'].should == row1[GEOIPISP_COMPANY_INDEX]

      list_indexes('geoipisp_copied').should =~ [JamIsp::COPIED_GEOIPISP_INDEX_NAME]


      # verify jamcompany
      result = JamIsp.connection.execute("SELECT * FROM jamcompany_copied")
      result.ntuples.should ==  1
      row1 = geo_ip_isp_data[0]
      result[0]['coid'].to_i.should == 1
      result[0]['company'].should == row1[GEOIPISP_COMPANY_INDEX]

      list_indexes('jamcompany_copied').should =~ [JamIsp::COPIED_JAMCOMPANY_UNIQUE_INDEX, JamIsp::COPIED_JAMCOMPANY_PRIMARY_KEY_NAME]

      # verify jamisp
      result = JamIsp.connection.execute("SELECT * FROM jamisp_copied")
      result.ntuples.should == 1
      row1 = geo_ip_isp_data[0]
      result[0]['beginip'].to_i.should == row1[GEOIPISP_BEGINIP_INDEX]
      result[0]['endip'].to_i.should == row1[GEOIPISP_ENDIP_INDEX]
      result[0]['coid'].to_i.should == 1
      result[0]['geom'].should_not be_nil

      list_indexes('jamisp_copied').should =~ [JamIsp::COPIED_JAMISP_GEOM_INDEX_NAME, JamIsp::COPIED_JAMISP_COID_INDEX_NAME]

      # verify we can swap out tables
      JamIsp.after_maxmind_import

      table_exists?('jamisp_copied').should be_false
      result = JamIsp.connection.execute("SELECT * FROM jamisp")
      result.ntuples.should == 1
      list_indexes('jamisp').should =~ [JamIsp::JAMISP_GEOM_INDEX_NAME, JamIsp::JAMISP_COID_INDEX_NAME]

      table_exists?('jamcompany_copied').should be_false
      result = JamIsp.connection.execute("SELECT * FROM jamcompany")
      result.ntuples.should == 1
      list_indexes('jamcompany').should =~ [JamIsp::JAMCOMPANY_UNIQUE_INDEX, JamIsp::JAMCOMPANY_PRIMARY_KEY_NAME]

      table_exists?('geoipisp_copied').should be_false
      result = JamIsp.connection.execute("SELECT * FROM geoipisp")
      result.ntuples.should == 1
      list_indexes('geoipisp').should =~ [JamIsp::GEOIPISP_INDEX_NAME]
    end
  end
end
=end

