<commit_before>require 'cases/helper'

require 'models/topic'
require 'models/person'

require 'bigdecimal'
require 'active_support/core_ext/big_decimal'

class NumericalityValidationTest < ActiveModel::TestCase

  def teardown
    Topic.clear_validators!
  end

  NIL = [nil]
  BLANK = ["", " ", " \t \r \n"]
  BIGDECIMAL_STRINGS = %w(12345678901234567890.1234567890) # 30 significant digits
  FLOAT_STRINGS = %w(0.0 +0.0 -0.0 10.0 10.5 -10.5 -0.0001 -090.1 90.1e1 -90.1e5 -90.1e-5 90e-5)
  INTEGER_STRINGS = %w(0 +0 -0 10 +10 -10 0090 -090)
  FLOATS = [0.0, 10.0, 10.5, -10.5, -0.0001] + FLOAT_STRINGS
  INTEGERS = [0, 10, -10] + INTEGER_STRINGS
  BIGDECIMAL = BIGDECIMAL_STRINGS.collect! { |bd| BigDecimal.new(bd) }
  JUNK = ["not a number", "42 not a number", "0xdeadbeef", "0xinvalidhex", "0Xdeadbeef", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"]
  INFINITY = [1.0/0.0]

  def test_default_validates_numericality_of
    Topic.validates_numericality_of :approved
    invalid!(NIL + BLANK + JUNK)
    valid!(FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
  end

  def test_validates_numericality_of_with_nil_allowed
    Topic.validates_numericality_of :approved, allow_nil: true

    invalid!(JUNK + BLANK)
    valid!(NIL + FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
  end

  def test_validates_numericality_of_with_integer_only
    Topic.validates_numericality_of :approved, only_integer: true

    invalid!(NIL + BLANK + JUNK + FLOATS + BIGDECIMAL + INFINITY)
    valid!(INTEGERS)
  end

  def test_validates_numericality_of_with_integer_only_and_nil_allowed
    Topic.validates_numericality_of :approved, only_integer: true, allow_nil: true

    invalid!(JUNK + BLANK + FLOATS + BIGDECIMAL + INFINITY)
    valid!(NIL + INTEGERS)
  end

  def test_validates_numericality_of_with_integer_only_and_symbol_as_value
    Topic.validates_numericality_of :approved, only_integer: :condition_is_true_but_its_not

    invalid!(NIL + BLANK + JUNK)
    valid!(FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
  end

  def test_validates_numericality_of_with_integer_only_and_proc_as_value
    Topic.send(:define_method, :allow_only_integers?, lambda { false })
    Topic.validates_numericality_of :approved, only_integer: Proc.new(&:allow_only_integers?)

    invalid!(NIL + BLANK + JUNK)
    valid!(FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
  end

  def test_validates_numericality_with_greater_than
    Topic.validates_numericality_of :approved, greater_than: 10

    invalid!([-10, 10], 'must be greater than 10')
    valid!([11])
  end

  def test_validates_numericality_with_greater_than_using_differing_numeric_types
    Topic.validates_numericality_of :approved, greater_than: BigDecimal.new('97.18')

    invalid!([-97.18, BigDecimal.new('97.18'), BigDecimal('-97.18')], 'must be greater than 97.18')
    valid!([97.18, 98, BigDecimal.new('98')]) # Notice the 97.18 as a float is greater than 97.18 as a BigDecimal due to floating point precision
  end

  def test_validates_numericality_with_greater_than_or_equal
    Topic.validates_numericality_of :approved, greater_than_or_equal_to: 10

    invalid!([-9, 9], 'must be greater than or equal to 10')
    valid!([10])
  end

  def test_validates_numericality_with_greater_than_or_equal_using_differing_numeric_types
    Topic.validates_numericality_of :approved, greater_than_or_equal_to: BigDecimal.new('97.18') 

    invalid!([-97.18, 97.17, 97, BigDecimal.new('97.17'), BigDecimal.new('-97.18')], 'must be greater than or equal to 97.18')
    valid!([97.18, 98, BigDecimal.new('97.19')])
  end

  def test_validates_numericality_with_equal_to
    Topic.validates_numericality_of :approved, equal_to: 10

    invalid!([-10, 11] + INFINITY, 'must be equal to 10')
    valid!([10])
  end

  def test_validates_numericality_with_equal_to_using_differing_numeric_types
    Topic.validates_numericality_of :approved, equal_to: BigDecimal.new('97.18')

    invalid!([-97.18, 97.18], 'must be equal to 97.18')
    valid!([BigDecimal.new('97.18')])
  end

  def test_validates_numericality_with_less_than
    Topic.validates_numericality_of :approved, less_than: 10

    invalid!([10], 'must be less than 10')
    valid!([-9, 9])
  end

  def test_validates_numericality_with_less_than_using_differing_numeric_types
    Topic.validates_numericality_of :approved, less_than: BigDecimal.new('97.18')

    invalid!([97.18, BigDecimal.new('97.18')], 'must be less than 97.18')
    valid!([-97.0, 97.0, -97, 97, BigDecimal.new('-97'), BigDecimal.new('97')])
  end

  def test_validates_numericality_with_less_than_or_equal_to
    Topic.validates_numericality_of :approved, less_than_or_equal_to: 10

    invalid!([11], 'must be less than or equal to 10')
    valid!([-10, 10])
  end

  def test_validates_numericality_with_less_than_or_equal_to_using_differing_numeric_types
    Topic.validates_numericality_of :approved, less_than_or_equal_to: BigDecimal.new('97.18')

    invalid!([97.18, 98], 'must be less than or equal to 97.18')
    valid!([-97.18, BigDecimal.new('-97.18'), BigDecimal.new('97.18')])
  end

  def test_validates_numericality_with_odd
    Topic.validates_numericality_of :approved, odd: true

    invalid!([-2, 2], 'must be odd')
    valid!([-1, 1])
  end

  def test_validates_numericality_with_even
    Topic.validates_numericality_of :approved, even: true

    invalid!([-1, 1], 'must be even')
    valid!([-2, 2])
  end

  def test_validates_numericality_with_greater_than_less_than_and_even
    Topic.validates_numericality_of :approved, greater_than: 1, less_than: 4, even: true

    invalid!([1, 3, 4])
    valid!([2])
  end

  def test_validates_numericality_with_other_than
    Topic.validates_numericality_of :approved, other_than: 0

    invalid!([0, 0.0])
    valid!([-1, 42])
  end
<commit_msg>Failing Tests for Validating String Numbericality<commit_after>require 'cases/helper'

require 'models/topic'
require 'models/person'

require 'bigdecimal'
require 'active_support/core_ext/big_decimal'

class NumericalityValidationTest < ActiveModel::TestCase

  def teardown
    Topic.clear_validators!
  end

  NIL = [nil]
  BLANK = ["", " ", " \t \r \n"]
  BIGDECIMAL_STRINGS = %w(12345678901234567890.1234567890) # 30 significant digits
  FLOAT_STRINGS = %w(0.0 +0.0 -0.0 10.0 10.5 -10.5 -0.0001 -090.1 90.1e1 -90.1e5 -90.1e-5 90e-5)
  INTEGER_STRINGS = %w(0 +0 -0 10 +10 -10 0090 -090)
  FLOATS = [0.0, 10.0, 10.5, -10.5, -0.0001] + FLOAT_STRINGS
  INTEGERS = [0, 10, -10] + INTEGER_STRINGS
  BIGDECIMAL = BIGDECIMAL_STRINGS.collect! { |bd| BigDecimal.new(bd) }
  JUNK = ["not a number", "42 not a number", "0xdeadbeef", "0xinvalidhex", "0Xdeadbeef", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"]
  INFINITY = [1.0/0.0]

  def test_default_validates_numericality_of
    Topic.validates_numericality_of :approved
    invalid!(NIL + BLANK + JUNK)
    valid!(FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
  end

  def test_validates_numericality_of_with_nil_allowed
    Topic.validates_numericality_of :approved, allow_nil: true

    invalid!(JUNK + BLANK)
    valid!(NIL + FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
  end

  def test_validates_numericality_of_with_integer_only
    Topic.validates_numericality_of :approved, only_integer: true

    invalid!(NIL + BLANK + JUNK + FLOATS + BIGDECIMAL + INFINITY)
    valid!(INTEGERS)
  end

  def test_validates_numericality_of_with_integer_only_and_nil_allowed
    Topic.validates_numericality_of :approved, only_integer: true, allow_nil: true

    invalid!(JUNK + BLANK + FLOATS + BIGDECIMAL + INFINITY)
    valid!(NIL + INTEGERS)
  end

  def test_validates_numericality_of_with_integer_only_and_symbol_as_value
    Topic.validates_numericality_of :approved, only_integer: :condition_is_true_but_its_not

    invalid!(NIL + BLANK + JUNK)
    valid!(FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
  end

  def test_validates_numericality_of_with_integer_only_and_proc_as_value
    Topic.send(:define_method, :allow_only_integers?, lambda { false })
    Topic.validates_numericality_of :approved, only_integer: Proc.new(&:allow_only_integers?)

    invalid!(NIL + BLANK + JUNK)
    valid!(FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
  end

  def test_validates_numericality_with_greater_than
    Topic.validates_numericality_of :approved, greater_than: 10

    invalid!([-10, 10], 'must be greater than 10')
    valid!([11])
  end

  def test_validates_numericality_with_greater_than_using_differing_numeric_types
    Topic.validates_numericality_of :approved, greater_than: BigDecimal.new('97.18')

    invalid!([-97.18, BigDecimal.new('97.18'), BigDecimal('-97.18')], 'must be greater than 97.18')
    valid!([97.18, 98, BigDecimal.new('98')]) # Notice the 97.18 as a float is greater than 97.18 as a BigDecimal due to floating point precision
  end

  def test_validates_numericality_with_greater_than_using_string_value
    Topic.validates_numericality_of :approved, greater_than: 10

    invalid!(['-10', '9', '9.9', '10'], 'must be greater than 10')
    valid!(['10.1', '11'])
  end

  def test_validates_numericality_with_greater_than_or_equal
    Topic.validates_numericality_of :approved, greater_than_or_equal_to: 10

    invalid!([-9, 9], 'must be greater than or equal to 10')
    valid!([10])
  end

  def test_validates_numericality_with_greater_than_or_equal_using_differing_numeric_types
    Topic.validates_numericality_of :approved, greater_than_or_equal_to: BigDecimal.new('97.18') 

    invalid!([-97.18, 97.17, 97, BigDecimal.new('97.17'), BigDecimal.new('-97.18')], 'must be greater than or equal to 97.18')
    valid!([97.18, 98, BigDecimal.new('97.19')])
  end

  def test_validates_numericality_with_greater_than_or_equal_using_string_value
    Topic.validates_numericality_of :approved, greater_than_or_equal_to: 10

    invalid!(['-10', '9', '9.9'], 'must be greater than or equal to 10')
    valid!(['10', '10.1', '11'])
  end

  def test_validates_numericality_with_equal_to
    Topic.validates_numericality_of :approved, equal_to: 10

    invalid!([-10, 11] + INFINITY, 'must be equal to 10')
    valid!([10])
  end

  def test_validates_numericality_with_equal_to_using_differing_numeric_types
    Topic.validates_numericality_of :approved, equal_to: BigDecimal.new('97.18')

    invalid!([-97.18, 97.18], 'must be equal to 97.18')
    valid!([BigDecimal.new('97.18')])
  end

  def test_validates_numericality_with_equal_to_using_string_value
    Topic.validates_numericality_of :approved, equal_to: 10

    invalid!(['-10', '9', '9.9', '10.1', '11'], 'must be equal to 10')
    valid!(['10'])
  end

  def test_validates_numericality_with_less_than
    Topic.validates_numericality_of :approved, less_than: 10

    invalid!([10], 'must be less than 10')
    valid!([-9, 9])
  end

  def test_validates_numericality_with_less_than_using_differing_numeric_types
    Topic.validates_numericality_of :approved, less_than: BigDecimal.new('97.18')

    invalid!([97.18, BigDecimal.new('97.18')], 'must be less than 97.18')
    valid!([-97.0, 97.0, -97, 97, BigDecimal.new('-97'), BigDecimal.new('97')])
  end

  def test_validates_numericality_with_less_than_using_string_value
    Topic.validates_numericality_of :approved, less_than: 10

    invalid!(['10', '10.1', '11'], 'must be less than 10')
    valid!(['-10', '9', '9.9'])
  end

  def test_validates_numericality_with_less_than_or_equal_to
    Topic.validates_numericality_of :approved, less_than_or_equal_to: 10

    invalid!([11], 'must be less than or equal to 10')
    valid!([-10, 10])
  end

  def test_validates_numericality_with_less_than_or_equal_to_using_differing_numeric_types
    Topic.validates_numericality_of :approved, less_than_or_equal_to: BigDecimal.new('97.18')

    invalid!([97.18, 98], 'must be less than or equal to 97.18')
    valid!([-97.18, BigDecimal.new('-97.18'), BigDecimal.new('97.18')])
  end

  def test_validates_numericality_with_less_than_or_equal_using_string_value
    Topic.validates_numericality_of :approved, less_than_or_equal_to: 10

    invalid!(['10.1', '11'], 'must be less than or equal to 10')
    valid!(['-10', '9', '9.9', '10'])
  end

  def test_validates_numericality_with_odd
    Topic.validates_numericality_of :approved, odd: true

    invalid!([-2, 2], 'must be odd')
    valid!([-1, 1])
  end

  def test_validates_numericality_with_even
    Topic.validates_numericality_of :approved, even: true

    invalid!([-1, 1], 'must be even')
    valid!([-2, 2])
  end

  def test_validates_numericality_with_greater_than_less_than_and_even
    Topic.validates_numericality_of :approved, greater_than: 1, less_than: 4, even: true

    invalid!([1, 3, 4])
    valid!([2])
  end

  def test_validates_numericality_with_other_than
    Topic.validates_numericality_of :approved, other_than: 0

    invalid!([0, 0.0])
    valid!([-1, 42])
  end

  def test_validates_numericality_with_other_than_using_string_value
    Topic.validates_numericality_of :approved, other_than: 0

    invalid!(['0', '0.0'])
    valid!(['-1', '1.1', '42'])
  end

{'fraction_non_alphanumeric': 0.12110991874904185, 'fraction_numerical': 0.06109152230568757, 'mean_word_length': 4.682491289198606, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5107322', 'n_tokens_mistral': 5540, 'n_tokens_neox': 4845, 'n_words': 965}