test/validations.rb in arrest-0.0.11 vs test/validations.rb in arrest-0.0.12

- old
+ new

@@ -1,21 +1,21 @@ require 'test/unit' require 'arrest' +require 'active_model' -class PresenceOfClass - include Arrest::Validatable +class PresenceOfClass < Arrest::RootResource attr_accessor :foo validates_presence_of :foo def initialize foo @foo = foo end end class PresenceOfTwo - include Arrest::Validatable + include ActiveModel::Validations attr_accessor :foo, :bar validates_presence_of :foo validates_presence_of :bar @@ -35,44 +35,64 @@ @baz = baz end end class CustomVal - include Arrest::Validatable + include ActiveModel::Validations attr_accessor :foo - validates :is_foo + validate :is_foo def initialize foo=nil @foo = foo end def is_foo if self.foo != "Foo" - [Arrest::Validations::ValidationError.new(:foo, "is no foo")] - else - [] + errors.add(:foo, "is not foo") end end end +class InclusionOfClass + include ActiveModel::Validations + + attr_accessor :foo + + validates :foo, :inclusion => { :in => ["Foo", "Bar"] } + + def initialize foo + @foo = foo + end +end + class ValidationsTest < Test::Unit::TestCase def setup Arrest::Source.source = nil #Arrest::Source.debug = true + Arrest::Source.skip_validations = false end def test_prsnc o0 = PresenceOfClass.new nil assert o0.valid? == false, "Foo is '#{o0.foo}' -> not present and thus not valid!" o1 = PresenceOfClass.new "Foo" assert o1.valid?, "Foo is present and valid!" end + def test_skip_validations + Arrest::Source.skip_validations = true + o0 = PresenceOfClass.new nil + assert o0.valid? == false, "Foo is '#{o0.foo}' -> not present and thus not valid!" + assert o0.save, "When skipping validations, in-mem-storage should work" + + Arrest::Source.skip_validations = false + end + def test_presence_of_two o = PresenceOfTwo.new assert o.valid? == false, "Both missing, must not be valid" o = PresenceOfTwo.new "foo" @@ -120,30 +140,18 @@ assert o.valid?, "Foo should be valid" end # ================ inclusion_of ====== - class InclusionOfClass - include Arrest::Validatable - attr_accessor :foo - - validates_inclusion_of :foo, :in => ["Foo", "Bar"] - - def initialize foo - @foo = foo - end - end - def test_inclusion_of invalids = [nil, '', "Baz", "foo", 3, true] valids = ["Foo", "Bar"] invalids.each do |iv| o = InclusionOfClass.new(iv) assert o.valid? == false, "#{iv} is not valid" - assert o.validate.map(&:attribute).include?(:foo), ":foo should be in list of invalid attributes when created with #{iv}" end valids.each do |v| o = InclusionOfClass.new(v) assert o.valid?