<commit_before>require 'test_helper'

describe 'Seedbank rake.task' do
  it 'does not pollute the global namespace' do
    Object.new.wont_respond_to :seeds_root
  end

  describe "seeds with dependency" do
    subject { Rake.application.tasks_in_scope(defined?(Rake::Scope) ? Rake::Scope.new('db:seed') : %w[db seed]) }

    it "creates all the seed tasks" do
      seeds = %w(db:seed:circular1 db:seed:circular2 db:seed:common db:seed:dependency db:seed:dependency2
        db:seed:dependent db:seed:dependent_on_nested db:seed:dependent_on_several db:seed:development
        db:seed:development:users db:seed:no_block db:seed:original db:seed:reference_memos db:seed:with_block_memo db:seed:with_inline_memo)

      subject.map(&:to_s).must_equal seeds
    end
  end

  describe "common seeds in the root directory" do
    Dir[File.expand_path('../../../dummy/db/seeds/*.seeds.rb', __FILE__)].each do |seed_file|
      seed = File.basename(seed_file, '.seeds.rb')

      describe seed do

        subject { Rake.application.lookup(['db', 'seed', seed].join(':')) }

        it "is dependent on db:abort_if_pending_migrations" do
          subject.prerequisites.must_equal %w[db:abort_if_pending_migrations]
        end
      end
    end
  end

  describe "db:seed:common" do
    subject { Rake::Task['db:seed:common'] }

    it "is dependent on the common seeds and db:seed:original" do
      prerequisite_seeds = Dir[File.expand_path('../../../dummy/db/seeds/*.seeds.rb', __FILE__)].sort.map do |seed_file|
        ['db', 'seed', File.basename(seed_file, '.seeds.rb')].join(':')
      end.unshift('db:seed:original')

      subject.prerequisites.must_equal prerequisite_seeds
    end
  end

  describe "db:seed:original" do
    subject { Rake::Task['db:seed:original'] }

    it "has no dependencies" do
      subject.prerequisites.must_equal %w[db:abort_if_pending_migrations]
    end

    describe "when seeds are reloaded" do

      before do
        silence_warnings { Dummy::Application.load_tasks }
      end

      it "still has no dependencies" do
        subject.prerequisites.must_equal %w[db:abort_if_pending_migrations]
      end
    end
  end

  describe "environment seeds" do
    Dir[File.expand_path('../../../dummy/db/seeds', __FILE__) + '/*/'].each do |environment_directory|
      environment = File.basename(environment_directory)

      describe "seeds in the #{environment} environment" do
        Dir[File.expand_path("../../../dummy/db/seeds/#{environment}/*.seeds.rb", __FILE__)].each do |seed_file|
          seed = File.basename(seed_file, '.seeds.rb')

          describe seed do

            subject { Rake.application.lookup(['db', 'seed', environment, seed].join(':')) }

            it "is dependent on db:abort_if_pending_migrations" do
              subject.prerequisites.must_equal %w[db:abort_if_pending_migrations]
            end
          end
        end
      end

      describe "db:seed:#{environment}" do
        subject { Rake.application.lookup(['db', 'seed', environment].join(':')) }<commit_msg>Clean up a seed_rake_test a little.<commit_after>require 'test_helper'

describe 'Seedbank rake.task' do
  it 'does not pollute the global namespace' do
    Object.new.wont_respond_to :seeds_root
  end

  describe "seeds with dependency" do
    subject { Rake.application.tasks_in_scope(defined?(Rake::Scope) ? Rake::Scope.new('db:seed') : %w[db seed]) }

    it "creates all the seed tasks" do
      seeds = %w(db:seed:circular1 db:seed:circular2 db:seed:common db:seed:dependency db:seed:dependency2
        db:seed:dependent db:seed:dependent_on_nested db:seed:dependent_on_several db:seed:development
        db:seed:development:users db:seed:no_block db:seed:original db:seed:reference_memos db:seed:with_block_memo db:seed:with_inline_memo)

      subject.map(&:to_s).must_equal seeds
    end
  end

  describe "common seeds in the root directory" do
    Dir[File.expand_path('../../../dummy/db/seeds/*.seeds.rb', __FILE__)].each do |seed_file|
      seed = File.basename(seed_file, '.seeds.rb')

      describe seed do

        subject { Rake.application.lookup(['db', 'seed', seed].join(':')) }

        it "is dependent on db:abort_if_pending_migrations" do
          subject.prerequisites.must_equal %w[db:abort_if_pending_migrations]
        end
      end
    end
  end

  describe "db:seed:common" do
    subject { Rake::Task['db:seed:common'] }

    it "is dependent on the common seeds and db:seed:original" do
      prerequisite_seeds = Dir[File.expand_path('../../../dummy/db/seeds/*.seeds.rb', __FILE__)].sort.map do |seed_file|
        ['db', 'seed', File.basename(seed_file, '.seeds.rb')].join(':')
      end.unshift('db:seed:original')

      subject.prerequisites.must_equal prerequisite_seeds
    end
  end

  describe "db:seed:original" do
    subject { Rake::Task['db:seed:original'] }

    it "is only dependent on db:abort_if_pending_migrations" do
      subject.prerequisites.must_equal %w[db:abort_if_pending_migrations]
    end

    it 'runs within Seedbank::Runner' do
      FakeModel.expect :seed, true, ['db/seeds.rb']

      subject.invoke

      FakeModel.verify
    end

    describe "when seeds are reloaded" do
      before do
        silence_warnings { Dummy::Application.load_tasks }
      end

      it "is still only dependent on db:abort_if_pending_migrations" do
        subject.prerequisites.must_equal %w[db:abort_if_pending_migrations]
      end

      it 'still runs within Seedbank::Runner' do
        skip 'TODO: Appears that it gets invoked twice after reloading.'
        FakeModel.expect :seed, true, ['db/seeds.rb']

        subject.invoke

        FakeModel.verify
      end
    end
  end

  describe "environment seeds" do
    Dir[File.expand_path('../../../dummy/db/seeds', __FILE__) + '/*/'].each do |environment_directory|
      environment = File.basename(environment_directory)

      describe "seeds in the #{environment} environment" do
        Dir[File.expand_path("../../../dummy/db/seeds/#{environment}/*.seeds.rb", __FILE__)].each do |seed_file|
          seed = File.basename(seed_file, '.seeds.rb')

          describe seed do

            subject { Rake.application.lookup(['db', 'seed', environment, seed].join(':')) }

            it "is dependent on db:abort_if_pending_migrations" do
              subject.prerequisites.must_equal %w[db:abort_if_pending_migrations]
            end
          end
        end
      end

      describe "db:seed:#{environment}" do
        subject { Rake.application.lookup(['db', 'seed', environment].join(':')) }
{'fraction_non_alphanumeric': 0.1269043266301036, 'fraction_numerical': 0.0009140767824497258, 'mean_word_length': 3.2083333333333335, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7298721', 'n_tokens_mistral': 2139, 'n_tokens_neox': 2087, 'n_words': 452}