=begin
require 'spec_helper'

describe ScoreHistory do

  describe "migrate scores" do
    it "works against empty scores" do
      original = GenericState.singleton.score_history_last_imported_at
      Score.count.should == 0
      num_affected, scanned_until = ScoreHistory.migrate_scores
      original.should_not == scanned_until
      num_affected.should == 0
      ScoreHistory.count.should == 0
      GenericState.singleton.score_history_last_imported_at.round.should == scanned_until.round
    end

    it "migrates a single score" do
      austin = austin_geoip
      dallas = dallas_geoip

      Region.count.should == 11
      Country.count.should == 1
      JamCompany.count.should == 4

      user1 = FactoryGirl.create(:user)
      user2 = FactoryGirl.create(:user)

      score1, score2 = create_score(austin, dallas, { auserid: user1.id, buserid: user2.id } )
      Score.count.should == 2
      Score.connection.execute("UPDATE scores SET created_at = TIMESTAMP '#{1.hour.ago}'").check

      num_affected, scanned_until = ScoreHistory.migrate_scores
      ScoreHistory.count.should == 1
      num_affected.should == 1
      GenericState.singleton.score_history_last_imported_at.round.should == scanned_until.round

      score_history = ScoreHistory.first
      score_history.from_client_id.should == score1.anodeid
      score_history.from_user_id.should == score1.auserid
      score_history.from_latency_tester_id.should == score1.alatencytestid
      score_history.from_addr.should == score1.aaddr
      score_history.from_locidispid.should == score1.alocidispid
      score_history.from_isp.should == austin[:jamisp].jam_company.company
      score_history.from_country.should == austin[:geoiplocation].countrycode
      score_history.from_region.should == austin[:geoiplocation].region
      score_history.from_city.should == austin[:geoiplocation].city
      score_history.from_postal == austin[:geoiplocation].postalcode
      score_history.from_latitude == austin[:geoiplocation].latitude
      score_history.from_longitude == austin[:geoiplocation].longitude
      score_history.to_client_id.should == score1.bnodeid
      score_history.to_user_id.should == score1.buserid
      score_history.to_latency_tester_id.should == score1.blatencytestid
      score_history.to_addr.should == score1.baddr
      score_history.to_locidispid.should == score1.blocidispid
      score_history.to_isp.should == dallas[:jamisp].jam_company.company
      score_history.to_country.should == dallas[:geoiplocation].countrycode
      score_history.to_region.should == dallas[:geoiplocation].region
      score_history.to_city.should == dallas[:geoiplocation].city
      score_history.to_postal == dallas[:geoiplocation].postalcode
      score_history.to_latitude == dallas[:geoiplocation].latitude
      score_history.to_longitude == dallas[:geoiplocation].longitude
    end

    it "ignores recent scores" do
      austin = austin_geoip
      dallas = dallas_geoip

      Region.count.should == 11
      Country.count.should == 1
      JamCompany.count.should == 4

      score1, score2 = create_score(austin, dallas) # creates scores with very recent created_at, so it should be skipped
      Score.count.should == 2

      num_affected, scanned_until = ScoreHistory.migrate_scores
      ScoreHistory.count.should == 0
      num_affected.should == 0
    end
  end
end
=end