<commit_before>require "spec_helper"
require Rails.root.join("db/migrate/20120620215904_convert_old_states_to_new_format.rb")

describe ConvertOldStatesToNewFormat do
  migration_context :up do
    let(:state_stub) { migration_stub(:State) }

    shared_examples_for("states converted") do |filename|
      it filename do
        load_data_file(filename).each { |s| state_stub.create!(s) }

        disable_paralleism { migrate }

        actual   = state_stub.order(:id).all.collect(&:data)
        expected = load_data_file("#{filename}_expected").collect(&:to_yaml)

        actual.zip(expected).each_with_index do |(a, e), i|
          a.should eq(e), "on index #{i}"
        end
      end
    end

    context "converts old states to new format" do
      include_examples "states converted", "vm_linux"
      include_examples "states converted", "vm_windows"
      include_examples "states converted", "host"
    end

    it "cleans up bad states before migrating" do
      states = [
        state_stub.create!(:timestamp => 1.days.ago, :scantype => "diff"),
        state_stub.create!(:timestamp => 2.days.ago, :scantype => "full"),
        state_stub.create!(:timestamp => 3.days.ago, :scantype => "diff"),
        state_stub.create!(:timestamp => 4.days.ago, :scantype => "full"),
        state_stub.create!(:timestamp => 5.days.ago, :scantype => "diff"),
      ]
      expected_states = states[1, 2]

      described_class.any_instance.should_receive(:migrate_states_to_new_format).with(expected_states)

      disable_paralleism { migrate }

      state_stub.order(:id).all.should == expected_states
    end
  end

  private

  def load_data_file(filename = "")
    YAML.load_file(File.expand_path("data/#{File.basename(__FILE__, ".rb")}/#{filename}.yaml", File.dirname(__FILE__)))
  end

  def disable_paralleism
    old_val, ENV["MIGRATION_PROCESSES"] = ENV["MIGRATION_PROCESSES"], "0"
    yield
  ensure
    if old_val.nil?
      ENV.delete("MIGRATION_PROCESSES")
    else
      ENV["MIGRATION_PROCESSES"] = old_val
    end
  end
end
<commit_msg>Rails 4 always deserializes #data, so compare the native hashes<commit_after>require "spec_helper"
require Rails.root.join("db/migrate/20120620215904_convert_old_states_to_new_format.rb")

describe ConvertOldStatesToNewFormat do
  migration_context :up do
    let(:state_stub) { migration_stub(:State) }

    shared_examples_for("states converted") do |filename|
      it filename do
        load_data_file(filename).each { |s| state_stub.create!(s) }

        disable_paralleism { migrate }

        actual   = state_stub.order(:id).all.collect(&:data)
        expected = load_data_file("#{filename}_expected")

        actual.zip(expected).each_with_index do |(a, e), i|
          a.should eq(e), "on index #{i}"
        end
      end
    end

    context "converts old states to new format" do
      include_examples "states converted", "vm_linux"
      include_examples "states converted", "vm_windows"
      include_examples "states converted", "host"
    end

    it "cleans up bad states before migrating" do
      states = [
        state_stub.create!(:timestamp => 1.days.ago, :scantype => "diff"),
        state_stub.create!(:timestamp => 2.days.ago, :scantype => "full"),
        state_stub.create!(:timestamp => 3.days.ago, :scantype => "diff"),
        state_stub.create!(:timestamp => 4.days.ago, :scantype => "full"),
        state_stub.create!(:timestamp => 5.days.ago, :scantype => "diff"),
      ]
      expected_states = states[1, 2]

      described_class.any_instance.should_receive(:migrate_states_to_new_format).with(expected_states)

      disable_paralleism { migrate }

      state_stub.order(:id).all.should == expected_states
    end
  end

  private

  def load_data_file(filename = "")
    YAML.load_file(File.expand_path("data/#{File.basename(__FILE__, ".rb")}/#{filename}.yaml", File.dirname(__FILE__)))
  end

  def disable_paralleism
    old_val, ENV["MIGRATION_PROCESSES"] = ENV["MIGRATION_PROCESSES"], "0"
    yield
  ensure
    if old_val.nil?
      ENV.delete("MIGRATION_PROCESSES")
    else
      ENV["MIGRATION_PROCESSES"] = old_val
    end
  end
end

{'fraction_non_alphanumeric': 0.1279209828956878, 'fraction_numerical': 0.010840761262346423, 'mean_word_length': 3.4886486486486485, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5834329', 'n_tokens_mistral': 1420, 'n_tokens_neox': 1358, 'n_words': 259}