<commit_before>require 'spec_helper'
require 'witty_fittings/fittings'

describe WittyFittings::Fittings do
  def setup_and_rollback(fittings)
    begin
      ActiveRecord::Base.transaction do
        fittings.setup
        raise ActiveRecord::Rollback
      end
    rescue ActiveRecord::Rollback
      return nil
    end
  end

  context 'wittings, can yielded setup only once' do
    let :fittings do
      n = 0
      WittyFittings::Fittings.new do
        raise if n > 0
        n += 1
        Lesson.create! name: '1st lesson'
      end
    end

    specify do
      expect { fittings.setup }.should change(Lesson, :count).by(1)
    end

    context 'called setup once and rolled-back' do
      before do
        setup_and_rollback fittings
        flunk 'data lefts' unless Lesson.count.zero?
      end

      specify do
        expect { fittings.setup }.should change(Lesson, :count).by(1)
      end
    end
  end

  context 'setup complex data as fixture' do
    let :fittings do
      WittyFittings::Fittings.new do
        l = Lesson.create! name: '1st Lesson'
        p = Person.create! name: 'Alice'
        p.attend(l)
      end
    end
    let(:lesson) { Lesson.first }

    before do
      setup_and_rollback fittings
      fittings.setup<commit_msg>remove constructor setup w/ WittyFittings::Fittings, too<commit_after>require 'spec_helper'
require 'witty_fittings/fittings'

describe WittyFittings::Fittings do
  def setup_and_rollback(fittings)
    begin
      ActiveRecord::Base.transaction do
        fittings.setup
        raise ActiveRecord::Rollback
      end
    rescue ActiveRecord::Rollback
      return nil
    end
  end

  context 'wittings, can yielded setup only once' do
    let :fittings do
      n = 0
      f = WittyFittings::Fittings.new
      f.fittings do
        raise if n > 0
        n += 1
        Lesson.create! name: '1st lesson'
      end
    end

    specify do
      expect { fittings.setup }.should change(Lesson, :count).by(1)
    end

    context 'called setup once and rolled-back' do
      before do
        setup_and_rollback fittings
        flunk 'data lefts' unless Lesson.count.zero?
      end

      specify do
        expect { fittings.setup }.should change(Lesson, :count).by(1)
      end
    end
  end

  context 'setup complex data as fixture' do
    let :fittings do
      f = WittyFittings::Fittings.new
      f.fittings do
        l = Lesson.create! name: '1st Lesson'
        p = Person.create! name: 'Alice'
        p.attend(l)
      end
    end
    let(:lesson) { Lesson.first }

    before do
      setup_and_rollback fittings
      fittings.setup
{'fraction_non_alphanumeric': 0.07495219885277246, 'fraction_numerical': 0.005353728489483747, 'mean_word_length': 2.241635687732342, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4995841', 'n_tokens_mistral': 870, 'n_tokens_neox': 813, 'n_words': 238}