require 'spec_helper'

describe MaxMindGeo do

  include UsesTempFiles

  GEO_CSV='small.csv'

  in_directory_with_file(GEO_CSV)

  before do

    content_for_file('startIpNum,endIpNum,country,region,city,postalCode,latitude,longitude,dmaCode,areaCode
0.116.0.0,0.119.255.255,"AT","","","",47.3333,13.3333,,
1.0.0.0,1.0.0.255,"AU","","","",-27.0000,133.0000,,
1.0.1.0,1.0.1.255,"CN","07","Fuzhou","",26.0614,119.3061,,'.encode(Encoding::ISO_8859_1))

    MaxMindGeo.import_from_max_mind(GEO_CSV)
  end

  let(:first) { MaxMindGeo.find_by_ip_bottom(MaxMindGeo.ip_address_to_int('0.116.0.0')) }
  let(:second) { MaxMindGeo.find_by_ip_bottom(MaxMindGeo.ip_address_to_int('1.0.0.0')) }
  let(:third) { MaxMindGeo.find_by_ip_bottom(MaxMindGeo.ip_address_to_int('1.0.1.0')) }

  it { MaxMindGeo.count.should == 3 }

  it { first.country.should == 'AT' }
  it { first.ip_bottom.should == MaxMindGeo.ip_address_to_int('0.116.0.0') }
  it { first.ip_top.should == MaxMindGeo.ip_address_to_int('0.119.255.255') }

  it { second.country.should == 'AU' }
  it { second.ip_bottom.should == MaxMindGeo.ip_address_to_int('1.0.0.0') }
  it { second.ip_top.should == MaxMindGeo.ip_address_to_int('1.0.0.255') }

  it { third.country.should == 'CN' }
  it { third.ip_bottom.should == MaxMindGeo.ip_address_to_int('1.0.1.0') }
  it { third.ip_top.should == MaxMindGeo.ip_address_to_int('1.0.1.255') }
end

