Sha256: 36234e0f40b13691c8fc19debc55ec05a44a136b9b251f47c533759ac4bf6f63

Contents?: true

Size: 1.35 KB

Versions: 27

Compression:

Stored size: 1.35 KB

Contents

require "helper"

class TestOhmNumberValidations < Test::Unit::TestCase
  class Product < Ohm::Model
    include Ohm::NumberValidations

    attribute :price
    attribute :optional_price

    def validate
      assert_decimal :price
      assert_decimal :optional_price unless optional_price.to_s.empty?
    end
  end

  context "given no price" do
    should "still validate as :not_decimal" do
      product = Product.new(:price => nil)
      product.valid?

      assert_equal [[:price, :not_decimal]], product.errors
    end
  end

  context "given a 0.10 value" do
    should "validate as a decimal" do
      product = Product.new(:price => 0.10)

      assert product.valid?
    end
  end

  context "given 1 as a value" do
    should "validate as a decimal" do
      product = Product.new(:price => 1)

      assert product.valid?
    end
  end

  context "given 1 dot as a value" do
    should "not validate as a decimal" do
      product = Product.new(:price => "1.")

      assert ! product.valid?
      assert_equal [[:price, :not_decimal]], product.errors
    end
  end

  context "given no value for optional price" do
    should "have no validation errors" do
      product = Product.new(:price => 10.1, :optional_price => nil)
      assert product.valid?

      product = Product.new(:price => 10.1, :optional_price => '')
      assert product.valid?
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
ohm-contrib-0.0.31 test/test_ohm_number_validations.rb
ohm-contrib-0.0.30 test/test_ohm_number_validations.rb
ohm-contrib-0.0.29 test/test_ohm_number_validations.rb
ohm-contrib-0.0.28 test/test_ohm_number_validations.rb
ohm-contrib-0.0.27 test/test_ohm_number_validations.rb
ohm-contrib-0.0.26 test/test_ohm_number_validations.rb
ohm-contrib-0.0.25 test/test_ohm_number_validations.rb
ohm-contrib-0.0.24 test/test_ohm_number_validations.rb
ohm-contrib-0.0.23 test/test_ohm_number_validations.rb
ohm-contrib-0.0.22 test/test_ohm_number_validations.rb
ohm-contrib-0.0.21 test/test_ohm_number_validations.rb
ohm-contrib-0.0.20 test/test_ohm_number_validations.rb
ohm-contrib-0.0.19 test/test_ohm_number_validations.rb
ohm-contrib-0.0.18 test/test_ohm_number_validations.rb
ohm-contrib-0.0.17 test/test_ohm_number_validations.rb
ohm-contrib-0.0.16 test/test_ohm_number_validations.rb
ohm-contrib-0.0.15 test/test_ohm_number_validations.rb
ohm-contrib-0.0.14 test/test_ohm_number_validations.rb
ohm-contrib-0.0.13 test/test_ohm_number_validations.rb
ohm-contrib-0.0.12 test/test_ohm_number_validations.rb