Sha256: 77f5a204c846fce1fc9e0764ba941ae97741aab97238f8a73d2ba6b7258c8b6e

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 KB

Contents

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, :new )
    self.| Contract::Build()
    self.| Contract::Validate()
    self.< ->(options) { options["1. fail"] = "Validate" }
    self.| Contract::Persist()
    self.< ->(options) { options["2. fail"] = "Persist" }
  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

  #---
  #- inheritance
  class Update < Create
  end

  it { Update["pipetree"].inspect.must_equal %{[>>operation.new,&model.build,>contract.build,&contract.default.params,&contract.default.validate,<PersistTest::Update:17,&persist.save,<PersistTest::Update:19]} }

  #---
  it do
    skip "show how save! could be applied and how we could rescue and deviate to left track"
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
trailblazer-2.0.0 test/operation/persist_test.rb
trailblazer-2.0.0.rc1 test/operation/persist_test.rb
trailblazer-2.0.0.beta3 test/operation/persist_test.rb
trailblazer-2.0.0.beta2 test/operation/persist_test.rb