<commit_before>require "test_helper"
require "reform/form/coercion"

class CoercionTest < BaseTest
  class Irreversible
    def self.call(value)
      value * 2
    end
  end

  class Form < TestForm
    feature Coercion

    property :released_at, type: DRY_TYPES_CONSTANT::DateTime

    property :hit do
      property :length, type: DRY_TYPES_INT_CONSTANT
      property :good,   type: DRY_TYPES_CONSTANT::Bool
    end

    property :band do
      property :label do
        property :value, type: Irreversible
      end
    end
  end

  subject do
    Form.new(album)
  end

  let(:album) do
    OpenStruct.new(
      released_at: "31/03/1981",
      hit: OpenStruct.new(length: "312"),
      band: Band.new(OpenStruct.new(value: "9999.99"))
    )
  end

  # it { subject.released_at.must_be_kind_of DateTime }
  it { subject.released_at.must_equal "31/03/1981" } # NO coercion in setup.
  it { subject.hit.length.must_equal "312" }
  it { subject.band.label.value.must_equal "9999.99" }

  let(:params) do
    {
      released_at: "30/03/1981",
      hit: {length: "312"},
      band: {label: {value: "9999.99"}}
    }
  end

  # validate
  describe "#validate" do
    before { subject.validate(params) }

    it { subject.released_at.must_equal DateTime.parse("30/03/1981") }
    it { subject.hit.length.must_equal 312 }
    it { assert_nil subject.hit.good }
    it { subject.band.label.value.must_equal "9999.999999.99" } # coercion happened once.
  end

  # save
end<commit_msg>Just tests introduced with PR #469 (#489)<commit_after>require "test_helper"
require "reform/form/coercion"
require "disposable/twin/property/hash"

class CoercionTest < BaseTest
  class Irreversible
    def self.call(value)
      value * 2
    end
  end

  class Form < TestForm
    feature Coercion
    include Disposable::Twin::Property::Hash

    property :released_at, type: DRY_TYPES_CONSTANT::DateTime

    property :hit do
      property :length, type: DRY_TYPES_INT_CONSTANT
      property :good,   type: DRY_TYPES_CONSTANT::Bool
    end

    property :band do
      property :label do
        property :value, type: Irreversible
      end
    end

    property :metadata, field: :hash do
      property :publication_settings do
        property :featured, type: DRY_TYPES_CONSTANT::Bool
      end
    end
  end

  subject do
    Form.new(album)
  end

  let(:album) do
    OpenStruct.new(
      released_at: "31/03/1981",
      hit: OpenStruct.new(length: "312"),
      band: Band.new(OpenStruct.new(value: "9999.99")),
      metadata: {}
    )
  end

  # it { subject.released_at.must_be_kind_of DateTime }
  it { subject.released_at.must_equal "31/03/1981" } # NO coercion in setup.
  it { subject.hit.length.must_equal "312" }
  it { subject.band.label.value.must_equal "9999.99" }

  let(:params) do
    {
      released_at: "30/03/1981",
      hit: {
        length: "312",
        good: "0",
      },
      band: {
        label: {
          value: "9999.99"
        }
      },
      metadata: {
        publication_settings: {
          featured: "0"
        }
      }
    }
  end

  # validate
  describe "#validate" do
    before { subject.validate(params) }

    it { subject.released_at.must_equal DateTime.parse("30/03/1981") }
    it { subject.hit.length.must_equal 312 }
    it { subject.hit.good.must_equal false }
    it { subject.band.label.value.must_equal "9999.999999.99" } # coercion happened once.
    it { subject.metadata.publication_settings.featured.must_equal false }
  end

  # sync
  describe "#sync" do
    before do
      subject.validate(params).must_equal true
      subject.sync
    end

    it { album.released_at.must_equal DateTime.parse("30/03/1981") }
    it { album.hit.length.must_equal 312 }
    it { album.hit.good.must_equal false }
    it { assert_nil album.metadata[:publication_settings] }
    it { album.metadata["publication_settings"]["featured"].must_equal false }
  end
end
{'fraction_non_alphanumeric': 0.11116777154513004, 'fraction_numerical': 0.04309026007139215, 'mean_word_length': 2.68703007518797, 'pattern_counts': {'":': 0, '<': 7, '<?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': '5410336', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1304, 'n_words': 292}