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

class SimpleForm < Bureaucrat::Forms::Form
  include Bureaucrat::Fields

  field :name, CharField.new
end

SimpleFormFormSet = Bureaucrat::Formsets.make_formset_class(SimpleForm,
                                                            :extra => 2)

class TestFormset < BureaucratTestCase
  describe 'formset with empty data' do
    setup do
      management_form_data = {
        :'form-TOTAL_FORMS' => '2',
        :'form-INITIAL_FORMS' => '2'
      }
      valid_data = {:'form-0-name' => 'Lynch', :'form-1-name' => 'Tio'}
      invalid_data = {:'form-0-name' => 'Lynch', :'form-1-name' => ''}
      @set = SimpleFormFormSet.new
      @valid_bound_set = SimpleFormFormSet.new(management_form_data.merge(valid_data))
      @invalid_bound_set = SimpleFormFormSet.new(management_form_data.merge(invalid_data))
    end

    should 'correctly render when calling as_table' do
      expected = normalize_html("<input name='form-TOTAL_FORMS' id='id_form-TOTAL_FORMS' type='hidden' value='2'/><input name='form-INITIAL_FORMS' id='id_form-INITIAL_FORMS' type='hidden' value='0'/>\n<tr><th><label for='id_form-0-name'>Name:</label></th><td><input name='form-0-name' id='id_form-0-name' type='text'/></td></tr> <tr><th><label for='id_form-1-name'>Name:</label></th><td><input name='form-1-name' id='id_form-1-name' type='text'/></td></tr>")
      rendered = normalize_html(@set.as_table)
      assert_equal(expected, rendered)
    end

    should '#valid? returns true if all forms are valid' do
      assert(@valid_bound_set.valid?)
    end

    should '#valid? returns false if there is an invalid form' do
      assert(!@invalid_bound_set.valid?)
    end

    should 'correctly return the list of errors' do
      assert_equal([{}, {:name => ["This field is required"]}],
                   @invalid_bound_set.errors)
    end

    should 'correctly return the list of cleaned data' do
      expected = [{:name => 'Lynch'}, {:name => 'Tio'}]
      result = @valid_bound_set.cleaned_data
      assert_equal(expected, result)
    end
  end<commit_msg>Add formset clean method test.<commit_after>require File.dirname(__FILE__) + "/test_helper"

class SimpleForm < Bureaucrat::Forms::Form
  include Bureaucrat::Fields

  field :name, CharField.new
end

SimpleFormFormSet =
  Bureaucrat::Formsets.make_formset_class(SimpleForm, :extra => 2)

class CustomFormSet < Bureaucrat::Formsets::BaseFormSet
  def clean
    raise FieldValidationError.new('This is wrong!')
  end
end

SimpleFormCustomFormSet =
  Bureaucrat::Formsets.make_formset_class(SimpleForm,
                                          :extra => 2,
                                          :formset => CustomFormSet)

class TestFormset < BureaucratTestCase
  describe 'formset with empty data' do
    setup do
      management_form_data = {
        :'form-TOTAL_FORMS' => '2',
        :'form-INITIAL_FORMS' => '2'
      }
      valid_data = {:'form-0-name' => 'Lynch', :'form-1-name' => 'Tio'}
      invalid_data = {:'form-0-name' => 'Lynch', :'form-1-name' => ''}
      @set = SimpleFormFormSet.new
      @valid_bound_set = SimpleFormFormSet.new(management_form_data.merge(valid_data))
      @invalid_bound_set = SimpleFormFormSet.new(management_form_data.merge(invalid_data))
    end

    should 'correctly render when calling as_table' do
      expected = normalize_html("<input name='form-TOTAL_FORMS' id='id_form-TOTAL_FORMS' type='hidden' value='2'/><input name='form-INITIAL_FORMS' id='id_form-INITIAL_FORMS' type='hidden' value='0'/>\n<tr><th><label for='id_form-0-name'>Name:</label></th><td><input name='form-0-name' id='id_form-0-name' type='text'/></td></tr> <tr><th><label for='id_form-1-name'>Name:</label></th><td><input name='form-1-name' id='id_form-1-name' type='text'/></td></tr>")
      rendered = normalize_html(@set.as_table)
      assert_equal(expected, rendered)
    end

    should '#valid? returns true if all forms are valid' do
      assert(@valid_bound_set.valid?)
    end

    should '#valid? returns false if there is an invalid form' do
      assert(!@invalid_bound_set.valid?)
    end

    should 'correctly return the list of errors' do
      assert_equal([{}, {:name => ["This field is required"]}],
                   @invalid_bound_set.errors)
    end

    should 'correctly return the list of cleaned data' do
      expected = [{:name => 'Lynch'}, {:name => 'Tio'}]
      result = @valid_bound_set.cleaned_data
      assert_equal(expected, result)
    end
  end

  describe "Formset with clean method raising a FieldValidationError exception" do
    setup do
      management_form_data = {
        :'form-TOTAL_FORMS' => '2',
        :'form-INITIAL_FORMS' => '2'
      }
      valid_data = {:'form-0-name' => 'Lynch', :'form-1-name' => 'Tio'}
      @bound_set =
        SimpleFormCustomFormSet.new(management_form_data.merge(valid_data))
    end

    should 'not be valid' do
      assert_equal(false, @bound_set.valid?)
    end

    should 'add clean errors to nonfield errors' do
      @bound_set.valid?
      assert_equal(["This is wrong!"],  @bound_set.non_form_errors)
    end
  end
{'fraction_non_alphanumeric': 0.1425515363671723, 'fraction_numerical': 0.006806690003889537, 'mean_word_length': 3.3474218089602705, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '474822', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1656, 'n_words': 348}