<commit_before>require 'spec_helper'

describe Erlen::Schema::Base do
  subject { described_class }

  describe "#initialize" do
    it "sets all the values given a hash" do
      payload = TestBaseSchema.new({ foo: 'bar' })

      expect(payload.class.schema_attributes).to include(:foo)
      expect(payload.foo).to eq('bar')
    end

    it "sets all the values given an object" do
      payload = TestBaseSchema.import(TestObj.new)

      expect(payload.class.schema_attributes).to include(:foo)
      expect(payload.foo).to eq('bar')
    end

    it 'handles type coercions' do
      data = {
        int: '1',
        flt: '1.1',
        bool: 'true',
        bool2: 't',
        bool3: 'f',
        bool4: 0,
        dt: '1/1/2017/',
        d: '2018-02-03'
      }
      payload = TestTypeSchema.new(data)

      expect(payload.int).to eq(1)
      expect(payload.flt).to eq(1.1)
      expect(payload.bool).to eq(true)
      expect(payload.bool2).to eq(true)
      expect(payload.bool3).to eq(false)
      expect(payload.bool4).to eq(false)
      expect(payload.dt).to eq(DateTime.parse('1/1/2017'))
      expect(payload.d).to eq(Date.parse('2018-02-03'))
    end

    it 'returns correct values if they do not need to be coerced' do
      data = {
        int: 1,
        flt: 1.1,
        bool: true,
        bool2: false,
        dt: DateTime.parse('1/1/2017'),
        d: Date.parse('2018-02-03')
      }
      payload = TestTypeSchema.new(data)

      expect(payload.int).to eq(1)
      expect(payload.flt).to eq(1.1)
      expect(payload.bool).to eq(true)
      expect(payload.bool2).to eq(false)
      expect(payload.dt).to eq(DateTime.parse('1/1/2017'))
      expect(payload.d).to eq(Date.parse('2018-02-03'))
    end
  end

  describe "#valid?" do
    it "returns valid only if payload is perfect" do
      valid = TestBaseSchema.new({ foo: 'bar' })
      expect(valid.valid?).to be_truthy

      valid = TestBaseSchema.new({ foo: 1 })<commit_msg>Uses rails parameters and coerces attr type value<commit_after>require 'spec_helper'

describe Erlen::Schema::Base do
  subject { described_class }

  describe "#initialize" do
    it "sets all the values given a hash" do
      payload = TestBaseSchema.new({ foo: 'bar' })

      expect(payload.class.schema_attributes).to include(:foo)
      expect(payload.foo).to eq('bar')
    end

    it "sets all the values given an object" do
      payload = TestBaseSchema.import(TestObj.new)

      expect(payload.class.schema_attributes).to include(:foo)
      expect(payload.foo).to eq('bar')
    end

    it 'handles type coercions' do
      data = {
        int: '1',
        flt: '1.1',
        bool: 'true',
        bool2: 't',
        bool3: 'f',
        bool4: 0,
        dt: '1/1/2017/',
        d: '2018-02-03'
      }
      payload = TestTypeSchema.new(data)

      expect(payload.int).to eq(1)
      expect(payload.flt).to eq(1.1)
      expect(payload.bool).to eq(true)
      expect(payload.bool2).to eq(true)
      expect(payload.bool3).to eq(false)
      expect(payload.bool4).to eq(false)
      expect(payload.dt).to eq(DateTime.parse('1/1/2017'))
      expect(payload.d).to eq(Date.parse('2018-02-03'))
    end

    it 'returns correct values if they do not need to be coerced' do
      data = {
        int: 1,
        flt: 1.1,
        bool: true,
        bool2: false,
        dt: DateTime.parse('1/1/2017'),
        d: Date.parse('2018-02-03')
      }
      payload = TestTypeSchema.new(data)

      expect(payload.int).to eq(1)
      expect(payload.flt).to eq(1.1)
      expect(payload.bool).to eq(true)
      expect(payload.bool2).to eq(false)
      expect(payload.dt).to eq(DateTime.parse('1/1/2017'))
      expect(payload.d).to eq(Date.parse('2018-02-03'))
=======
>>>>>>> 3b5369b... Uses rails parameters and coerces attr type value
    end
  end

  describe "#valid?" do
    it "returns valid only if payload is perfect" do
      valid = TestBaseSchema.new({ foo: 'bar' })
      expect(valid.valid?).to be_truthy

      valid = TestBaseSchema.new({ foo: 1 })
{'fraction_non_alphanumeric': 0.14483783114632334, 'fraction_numerical': 0.03986135181975736, 'mean_word_length': 2.4179357021996615, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '6414034', 'n_tokens_mistral': 1448, 'n_tokens_neox': 1342, 'n_words': 314}