require File.join(File.dirname(__FILE__), 'CONFIG.rb') require 'test/unit' require 'og' module UnitTestClasses class NoValidation property :prop, String def initialize(prop) @prop = prop end end class UniqueValidation < NoValidation property :slop, String validate_unique :prop, :slop end class ValueValidation < NoValidation validate_value :prop end class ConfirmValidation < NoValidation validate_confirmation :prop end class FormatValidation < NoValidation validate_format :prop, :format => /^\w\d\w-\d\w\d$/ end class MinValidation < NoValidation validate_length :prop, :min => 4 end class MaxValidation < NoValidation validate_length :prop, :max => 8 end class RangeValidation < NoValidation validate_length :prop, :range => 10..20 end class LengthValidation < NoValidation validate_length :prop, :length => 8 end class InclusionValidation < NoValidation validate_inclusion :prop, :in => %w{ Male Female } end class NumericValidation < NoValidation property :prop, Fixnum validate_numeric :prop, :integer => true attr_accessor :prop2, Float validate_numeric :prop2 end class RelatedClass attr_accessor :test, String belongs_to :relation_validation, RelationValidation end class RelationValidation < NoValidation has_many :relations, RelatedClass validate_related :relations end $og1.manage_classes(NoValidation, UniqueValidation, ValueValidation, ConfirmValidation, FormatValidation, MinValidation, MaxValidation, RangeValidation, LengthValidation, InclusionValidation, NumericValidation, RelatedClass, RelationValidation) end module Test::Unit::Assertions # Assert than an Og object is successfully saved and is error free. def assert_saved_without_errors(obj) assert_block(obj.class.to_s + ' was expected to save, but failed.') do obj.save obj.saved? && obj.errors.size == 0 end end # Assert that an Og object did not save and has errors. def assert_save_failed(obj) assert_block(obj.class.to_s + ' saving was expected to fail, but it succeeded.') do obj.save !obj.saved? && obj.errors.size > 0 end end end # Unit tests for the Og validation system class TestFixes < Test::Unit::TestCase include UnitTestClasses def test_no_validation t = NoValidation.new('nochecking') assert_saved_without_errors t end def test_validation_unique assert_saved_without_errors UniqueValidation.new('test') assert_save_failed UniqueValidation.new('test') end def test_value_validation assert_saved_without_errors ValueValidation.new('value') assert_save_failed ValueValidation.new(nil) end def test_confirmation prop = 'confirm' c = ConfirmValidation.new(prop) c.prop_confirmation = prop assert_saved_without_errors c assert_save_failed ConfirmValidation.new(prop) end def test_format assert_saved_without_errors FormatValidation.new('a1b-2c3') assert_save_failed FormatValidation.new('banana_pudding') end def test_length_min assert_saved_without_errors MinValidation.new('12345') assert_save_failed MinValidation.new('1') end def test_length_max assert_saved_without_errors MaxValidation.new('12345') assert_save_failed MaxValidation.new('123456789012345') end def test_length_range l = RangeValidation.new('12345678901234') assert_saved_without_errors l l = RangeValidation.new('12') assert_save_failed l l = RangeValidation.new('1234567890123456789012345') assert_save_failed l end def test_length_length assert_saved_without_errors LengthValidation.new('12345678') assert_save_failed LengthValidation.new('1') assert_save_failed LengthValidation.new('123456789012345') end def test_inclusion assert_saved_without_errors InclusionValidation.new('Male') assert_save_failed InclusionValidation.new('Shemale') end def test_numeric # Both valid n = NumericValidation.new(100) n.prop2 = 1.52 assert_saved_without_errors n # float for required integer n = NumericValidation.new(1.52) n.prop2 = 1.52 assert_save_failed n assert_equal 1, n.errors.count # string for required integer n = NumericValidation.new('sausage') n.prop2 = 1.52 assert_save_failed n assert_equal 1, n.errors.count # string for required integer and float n = NumericValidation.new('sausage') n.prop2 = 'cheese' assert_save_failed n assert_equal 2, n.errors.count # string for float n = NumericValidation.new(15) n.prop2 = 'cheese' assert_save_failed n assert_equal 1, n.errors.count end def test_related r = RelatedClass.new test_prop = 'example string' r.test = test_prop rv = RelationValidation.new('relation') rv.relations << r assert_saved_without_errors rv assert_equal test_prop, rv.relations.members[0].test # Test bad assert_save_failed RelationValidation.new('relation') end end