<commit_before>require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe GroupedValidations do
  before do
    reset_class Person
  end

  it "should add validation_group class method" do
    Person.should respond_to(:validation_group)
  end

  it "should store defined validation group names" do
    Person.validation_group(:dummy) { }
    Person.validation_groups.should == [:dummy]
  end

  it "it should add group_valid? method which takes a group name param" do
    Person.validation_group(:dummy) { }
    p = Person.new
    p.group_valid?(:dummy)
  end

  it "should raise exception if valiation group not defined on group_valid check" do
    p = Person.new
    lambda { p.group_valid?(:dummy) }.should raise_exception
  end

  it "should run the validations defined inside the validation group" do
    Person.validation_group :name do
      validates_presence_of :first_name
      validates_presence_of :last_name
    end

    p = Person.new
    p.group_valid?(:name)
    p.should have(2).errors

    p.first_name = 'Dave'
    p.last_name = 'Smith'
    p.group_valid?(:name)
    p.should have(0).errors
  end

  it "should run all validation groups passed to groups_valid?" do
    Person.class_eval do
      validation_group :first_name_group do
        validates_presence_of :first_name
      end
      validation_group :last_name_group do
        validates_presence_of :last_name
      end
    end

    p = Person.new
    p.groups_valid?(:first_name_group, :last_name_group)
    p.should have(2).errors
  end

  it "should run all validation including groups when valid? method called" do
    Person.class_eval do
      validation_group :first_name_group do
        validates_presence_of :first_name
      end
      validation_group :last_name_group do
        validates_presence_of :last_name
      end

      validates_presence_of :sex
    end

    p = Person.new
    p.valid?
    p.should have(3).errors
  end

  it "should respect :on validation option" do
    Person.validation_group :name do
      validates_presence_of :first_name, :on => :create
      validates_presence_of :last_name, :on => :update
    end

    p = Person.new
    p.group_valid?(:name)
    p.should have(1).errors
    p.first_name = 'Dave'
    p.group_valid?(:name)
    p.should have(0).errors

    p.save.should be_true
    p.group_valid?(:name)
    p.should have(1).errors
    p.last_name = 'Smith'
    p.group_valid?(:name)
    p.should have(0).errors
  end
end<commit_msg>split out :on spec in to create and update<commit_after>require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe GroupedValidations do
  before do
    reset_class Person
  end

  it "should add validation_group class method" do
    Person.should respond_to(:validation_group)
  end

  it "should store defined validation group names" do
    Person.validation_group(:dummy) { }
    Person.validation_groups.should == [:dummy]
  end

  it "it should add group_valid? method which takes a group name param" do
    Person.validation_group(:dummy) { }
    p = Person.new
    p.group_valid?(:dummy)
  end

  it "should raise exception if valiation group not defined on group_valid check" do
    p = Person.new
    lambda { p.group_valid?(:dummy) }.should raise_exception
  end

  it "should run the validations defined inside the validation group" do
    Person.validation_group :name do
      validates_presence_of :first_name
      validates_presence_of :last_name
    end

    p = Person.new
    p.group_valid?(:name)
    p.should have(2).errors

    p.first_name = 'Dave'
    p.last_name = 'Smith'
    p.group_valid?(:name)
    p.should have(0).errors
  end

  it "should run all validation groups passed to groups_valid?" do
    Person.class_eval do
      validation_group :first_name_group do
        validates_presence_of :first_name
      end
      validation_group :last_name_group do
        validates_presence_of :last_name
      end
    end

    p = Person.new
    p.groups_valid?(:first_name_group, :last_name_group)
    p.should have(2).errors
  end

  it "should run all validation including groups when valid? method called" do
    Person.class_eval do
      validation_group :first_name_group do
        validates_presence_of :first_name
      end
      validation_group :last_name_group do
        validates_presence_of :last_name
      end

      validates_presence_of :sex
    end

    p = Person.new
    p.valid?
    p.should have(3).errors
  end

  it "should respect :on => :create validation option" do
    Person.validation_group :name do
      validates_presence_of :first_name, :on => :create
    end

    p = Person.new
    p.group_valid?(:name)
    p.should have(1).errors
    p.first_name = 'Dave'
    p.group_valid?(:name)
    p.should have(0).errors

    p.save.should be_true
    p.first_name = nil
    p.group_valid?(:name)
    p.should have(0).errors
  end

  it "should respect :on => :update validation option" do
    Person.validation_group :name do
      validates_presence_of :last_name, :on => :update
    end

    p = Person.new
    p.last_name = nil
    p.group_valid?(:name)
    p.should have(0).errors

    p.save.should be_true
    p.group_valid?(:name)
    p.should have(1).errors
    p.last_name = 'Smith'
    p.group_valid?(:name)
    p.should have(0).errors
  end

end
{'fraction_non_alphanumeric': 0.08209376765204293, 'fraction_numerical': 0.0033891922425155338, 'mean_word_length': 3.018154311649017, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '7465079', 'n_tokens_mistral': 1737, 'n_tokens_neox': 1693, 'n_words': 457}