<commit_before>require "test_helper"

class PersistTest < Minitest::Spec
  Song = Struct.new(:title, :saved) do
    def save; self.saved = true; end
  end

  class Create < Trailblazer::Operation
    extend Contract::DSL
    contract do
      property :title
    end

    self.| Model[Song, :create]
    self.| Contract[self["contract.default.class"]]
    self.| Contract::Validate[]
    self.< ->(input, options) { options["1. fail"] = "Validate" }, before: "operation.result"
    self.| Persist[]
  end

  it { Create.(title: "In Recital")["model"].title.must_equal "In Recital" }
  it { Create.(title: "In Recital")["model"].saved.must_equal true }
end<commit_msg>more tests for persist. this is where we can add transactions, rescue and whatnot.<commit_after>require "test_helper"

class PersistTest < Minitest::Spec
  Song = Struct.new(:title, :saved) do
    def save; title=="Fail!" ? false : self.saved = true; end
  end

  class Create < Trailblazer::Operation
    extend Contract::DSL
    contract do
      property :title
    end

    self.| Model[Song, :create]
    self.| Contract[self["contract.default.class"]]
    self.| Contract::Validate[]
    self.< ->(input, options) { options["1. fail"] = "Validate" }, before: "operation.result"
    self.| Persist[]
    self.< ->(input, options) { options["2. fail"] = "Persist" }, before: "operation.result"
  end

  it { Create.(title: "In Recital")["model"].title.must_equal "In Recital" }
  it { Create.(title: "In Recital")["model"].saved.must_equal true }
  # failure
  it do
    result = Create.(title: "Fail!")
    result["model"].saved.must_equal nil
    result["model"].title.must_equal "Fail!"
    result["2. fail"].must_equal "Persist"
    result.success?.must_equal false
  end
end
{'fraction_non_alphanumeric': 0.156, 'fraction_numerical': 0.002285714285714286, 'mean_word_length': 3.6818181818181817, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 6, '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': '5371674', 'n_tokens_mistral': 566, 'n_tokens_neox': 550, 'n_words': 148}