<commit_before>require File.dirname(__FILE__) + "/../helpers"

class MigrationsTest < Test::Unit::TestCase
  def table_exists?(table_name)
    database_adapter.storage_exists?(table_name)
  end

  def database_adapter
    DataMapper.repository(:default).adapter
  end

  before(:all) do
    require "integrity/migrations"
  end

  before(:each) do
    [Project, Build, Commit, Notifier].each(&:auto_migrate_down!)
  end

  test "migrating up for the first time creates the database up to last migration" do
    assert !table_exists?("migration_info")
    Integrity.migrate("up")

    database_adapter.query("SELECT * from migration_info").
      should == ["initial", "add_commits"]
    assert table_exists?("integrity_projects")
    assert table_exists?("integrity_builds")
    assert table_exists?("integrity_notifiers")
    assert table_exists?("integrity_commits")
  end

  context "Migrating a pre migration database" do
    it "creates the migration_info table" do
      [Project, Build, Notifier].each(&:auto_migrate!)
      assert !table_exists?("migration_info")
      assert !table_exists?("integrity_commits")

      Integrity.migrate("up")

      assert table_exists?("migration_info")
      database_adapter.query("SELECT * from migration_info").should == ["initial"]
    end

    it "migrates the data as well" do
      pending "42"
    end<commit_msg>Also test for when migrating down<commit_after>require File.dirname(__FILE__) + "/../helpers"

class MigrationsTest < Test::Unit::TestCase
  def database_adapter
    DataMapper.repository(:default).adapter
  end

  def table_exists?(table_name)
    database_adapter.storage_exists?(table_name)
  end

  def current_migrations
    database_adapter.query("SELECT * from migration_info")
  end

  before(:all) do
    require "integrity/migrations"
  end

  before(:each) do
    [Project, Build, Commit, Notifier].each(&:auto_migrate_down!)
    assert !table_exists?("migration_info") # just to be sure
  end

  test "migrating up a pre migration database" do
    Integrity.migrate("up")

    current_migrations.should == ["initial", "add_commits"]
    assert table_exists?("integrity_projects")
    assert table_exists?("integrity_builds")
    assert table_exists?("integrity_notifiers")
    assert table_exists?("integrity_commits")
  end

  test "migrating down a pre migration database" do
    [Project, Build, Notifier].each(&:auto_migrate!)

    Integrity.migrate("down")

    # Initial migration is created but it gets removed just after
    # because we migrate down
    assert table_exists?("migration_info")
    current_migrations.should == []

    assert ! table_exists?("integrity_projects")
    assert ! table_exists?("integrity_builds")
    assert ! table_exists?("integrity_notifiers")
  end

  test "migrating data from initial up to add_commits migration" do
    pending
  end

  test "migrating data from migration down to initial migration" do
    pending
{'fraction_non_alphanumeric': 0.1032016348773842, 'fraction_numerical': 0.0006811989100817438, 'mean_word_length': 4.3497267759562845, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '6932836', 'n_tokens_mistral': 881, 'n_tokens_neox': 880, 'n_words': 219}