<commit_before>require 'test_helper'

class SampleAttributeTypeTest < ActiveSupport::TestCase
  test 'valid?' do
    type = SampleAttributeType.new(title: 'x-type', base_type: 'Integer')
    assert type.valid?
    assert_equal '.*', type.regexp

    type = SampleAttributeType.new(base_type: 'Integer')
    refute type.valid?

    type = SampleAttributeType.new(title: 'x-type', base_type: 'ActionPack')
    refute type.valid?

    type = SampleAttributeType.new(title: 'x-type', base_type: 'Fish')
    refute type.valid?

    type = SampleAttributeType.new(title: 'x-type', base_type: 'Integer', regexp: '[')
    refute type.valid?

    type = SampleAttributeType.new(title: 'x-type', base_type: 'String', regexp: 'xxx')
    assert type.valid?

    type = SampleAttributeType.new(title: 'x-type', base_type: 'CV')
    assert type.valid?

  end

  test 'default regexp' do
    type = SampleAttributeType.new(title: 'x-type', base_type: 'Integer')
    type.save!
    type = SampleAttributeType.find(type.id)
    assert_equal '.*',type[:regexp]
  end

  test 'validate_value' do
    type = SampleAttributeType.new(title: 'x-type', base_type: 'String', regexp: 'xxx')
    assert type.validate_value?('xxx')
    refute type.validate_value?('fish')
    refute type.validate_value?(nil)

    attribute = SampleAttributeType.new(title: 'fish', base_type: 'Integer')
    assert attribute.validate_value?(1)
    assert attribute.validate_value?('1')
    refute attribute.validate_value?('frog')
    refute attribute.validate_value?('1.1')
    refute attribute.validate_value?(1.1)
    refute attribute.validate_value?(nil)
    refute attribute.validate_value?('')


    refute attribute.validate_value?(1.0)
    refute attribute.validate_value?('1.0')

    attribute = SampleAttributeType.new(title: 'fish', base_type: 'String', regexp: '.*yyy')
    assert attribute.validate_value?('yyy')
    assert attribute.validate_value?('happpp - yyy')
    refute attribute.validate_value?('')
    refute attribute.validate_value?(nil)
    refute attribute.validate_value?(1)
    refute attribute.validate_value?('xxx')

    attribute = SampleAttributeType.new(title: 'fish', base_type: 'Text', regexp: '.*yyy')
    assert attribute.validate_value?('yyy')
    assert attribute.validate_value?('happpp - yyy')
    refute attribute.validate_value?('')
    refute attribute.validate_value?(nil)
    refute attribute.validate_value?(1)
    refute attribute.validate_value?('xxx')

    attribute = SampleAttributeType.new(title: 'fish', base_type: 'Float')
    assert attribute.validate_value?(1.0)
    assert attribute.validate_value?(1.2)
    assert attribute.validate_value?(0.78)
    assert attribute.validate_value?('0.78')
    refute attribute.validate_value?('fish')
    refute attribute.validate_value?('2 Feb 2015')
    refute attribute.validate_value?(nil)

    assert attribute.validate_value?(1.0)
    assert attribute.validate_value?(1)
    assert attribute.validate_value?('1.0')
    assert attribute.validate_value?('1')

    attribute = SampleAttributeType.new(title: 'fish', base_type: 'DateTime')
    assert attribute.validate_value?('2 Feb 2015')
    assert attribute.validate_value?('Thu, 11 Feb 2016 15:39:55 +0000')
    assert attribute.validate_value?('2016-02-11T15:40:14+00:00')
    assert attribute.validate_value?(DateTime.parse('2 Feb 2015'))
    assert attribute.validate_value?(DateTime.now)
    refute attribute.validate_value?(1)
    refute attribute.validate_value?(1.2)
    refute attribute.validate_value?(nil)
    refute attribute.validate_value?('30 Feb 2015')

    attribute = SampleAttributeType.new(title: 'fish', base_type: 'Date')
    assert attribute.validate_value?('2 Feb 2015')
    assert attribute.validate_value?('Thu, 11 Feb 2016 15:39:55 +0000')
    assert attribute.validate_value?('2016-02-11T15:40:14+00:00')
    assert attribute.validate_value?(Date.parse('2 Feb 2015'))
    assert attribute.validate_value?(Date.today)
    refute attribute.validate_value?(1)
    refute attribute.validate_value?(1.2)
    refute attribute.validate_value?(nil)
    refute attribute.validate_value?('30 Feb 2015')
  end

  test 'regular expression match' do
    #whole string must match<commit_msg>Changed to accept leading and trailing zeros<commit_after>require 'test_helper'

class SampleAttributeTypeTest < ActiveSupport::TestCase
  test 'valid?' do
    type = SampleAttributeType.new(title: 'x-type', base_type: 'Integer')
    assert type.valid?
    assert_equal '.*', type.regexp

    type = SampleAttributeType.new(base_type: 'Integer')
    refute type.valid?

    type = SampleAttributeType.new(title: 'x-type', base_type: 'ActionPack')
    refute type.valid?

    type = SampleAttributeType.new(title: 'x-type', base_type: 'Fish')
    refute type.valid?

    type = SampleAttributeType.new(title: 'x-type', base_type: 'Integer', regexp: '[')
    refute type.valid?

    type = SampleAttributeType.new(title: 'x-type', base_type: 'String', regexp: 'xxx')
    assert type.valid?

    type = SampleAttributeType.new(title: 'x-type', base_type: 'CV')
    assert type.valid?

  end

  test 'default regexp' do
    type = SampleAttributeType.new(title: 'x-type', base_type: 'Integer')
    type.save!
    type = SampleAttributeType.find(type.id)
    assert_equal '.*',type[:regexp]
  end

  test 'validate_value' do
    type = SampleAttributeType.new(title: 'x-type', base_type: 'String', regexp: 'xxx')
    assert type.validate_value?('xxx')
    refute type.validate_value?('fish')
    refute type.validate_value?(nil)

    attribute = SampleAttributeType.new(title: 'fish', base_type: 'Integer')
    assert attribute.validate_value?(1)
    assert attribute.validate_value?('1')
    assert attribute.validate_value?('01')
    refute attribute.validate_value?('frog')
    refute attribute.validate_value?('1.1')
    refute attribute.validate_value?(1.1)
    refute attribute.validate_value?(nil)
    refute attribute.validate_value?('')

    # refute attribute.validate_value?(1.0)
  #  refute attribute.validate_value?('1.0')

    attribute = SampleAttributeType.new(title: 'fish', base_type: 'String', regexp: '.*yyy')
    assert attribute.validate_value?('yyy')
    assert attribute.validate_value?('happpp - yyy')
    refute attribute.validate_value?('')
    refute attribute.validate_value?(nil)
    refute attribute.validate_value?(1)
    refute attribute.validate_value?('xxx')

    attribute = SampleAttributeType.new(title: 'fish', base_type: 'Text', regexp: '.*yyy')
    assert attribute.validate_value?('yyy')
    assert attribute.validate_value?('happpp - yyy')
    refute attribute.validate_value?('')
    refute attribute.validate_value?(nil)
    refute attribute.validate_value?(1)
    refute attribute.validate_value?('xxx')

    attribute = SampleAttributeType.new(title: 'fish', base_type: 'Float')
    assert attribute.validate_value?(1.0)
    assert attribute.validate_value?(1.2)
    assert attribute.validate_value?(0.78)
    assert attribute.validate_value?('0.78')
    refute attribute.validate_value?('fish')
    refute attribute.validate_value?('2 Feb 2015')
    refute attribute.validate_value?(nil)

    assert attribute.validate_value?(1.0)
    assert attribute.validate_value?(1)
    assert attribute.validate_value?('1.0')
    assert attribute.validate_value?('012')
    assert attribute.validate_value?('012.3')
    assert attribute.validate_value?('12.30')
    assert attribute.validate_value?('1')

    attribute = SampleAttributeType.new(title: 'fish', base_type: 'DateTime')
    assert attribute.validate_value?('2 Feb 2015')
    assert attribute.validate_value?('Thu, 11 Feb 2016 15:39:55 +0000')
    assert attribute.validate_value?('2016-02-11T15:40:14+00:00')
    assert attribute.validate_value?(DateTime.parse('2 Feb 2015'))
    assert attribute.validate_value?(DateTime.now)
    refute attribute.validate_value?(1)
    refute attribute.validate_value?(1.2)
    refute attribute.validate_value?(nil)
    refute attribute.validate_value?('30 Feb 2015')

    attribute = SampleAttributeType.new(title: 'fish', base_type: 'Date')
    assert attribute.validate_value?('2 Feb 2015')
    assert attribute.validate_value?('Thu, 11 Feb 2016 15:39:55 +0000')
    assert attribute.validate_value?('2016-02-11T15:40:14+00:00')
    assert attribute.validate_value?(Date.parse('2 Feb 2015'))
    assert attribute.validate_value?(Date.today)
    refute attribute.validate_value?(1)
    refute attribute.validate_value?(1.2)
    refute attribute.validate_value?(nil)
    refute attribute.validate_value?('30 Feb 2015')
  end

  test 'regular expression match' do
    #whole string must match
{'fraction_non_alphanumeric': 0.13322520852641334, 'fraction_numerical': 0.03371177015755329, 'mean_word_length': 4.624104234527687, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '2635945', 'n_tokens_mistral': 2875, 'n_tokens_neox': 2703, 'n_words': 537}