Sha256: e1daa8d29df41429ff63402c9e91fdeb6ded85756da5cd253288f4c65d5b2c74

Contents?: true

Size: 1.37 KB

Versions: 5

Compression:

Stored size: 1.37 KB

Contents

require "test_helper"

class ModelTest < Minitest::Spec
  Song = Struct.new(:id) do
    def self.find(id); new(id) end
    def self.find_by(id:nil); id.nil? ? nil : new(id) end
  end

  #---
  # use Model semantics, no customizations.
  class Create < Trailblazer::Operation
    step Model( Song, :new )
  end

  # :new new.
  it { Create.(params: {})[:model].inspect.must_equal %{#<struct ModelTest::Song id=nil>} }

  class Update < Create
    step Model( Song, :find ), override: true
  end

  #---
  #- inheritance

  # :find it
  it { Update.(params: { id: 1 })[:model].inspect.must_equal %{#<struct ModelTest::Song id=1>} }

  # inherited inspect is ok
  it { Trailblazer::Operation::Inspect.(Update).must_equal %{[>model.build]} }

  #---
  # :find_by, exceptionless.
  class Find < Trailblazer::Operation
    step Model Song, :find_by
    step :process

    def process(options, **); options["x"] = true end
  end

  # can't find model.
  #- result object, model
  it do
    Find.(params: {id: nil})["result.model"].failure?.must_equal true
    Find.(params: {id: nil})["x"].must_be_nil
    Find.(params: {id: nil}).failure?.must_equal true
  end

  #- result object, model
  it do
    Find.(params: {id: 9})["result.model"].success?.must_equal true
    Find.(params: {id: 9})["x"].must_equal true
    Find.(params: {id: 9})[:model].inspect.must_equal %{#<struct ModelTest::Song id=9>}
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
trailblazer-macro-2.1.0.beta1 test/operation/model_test.rb
trailblazer-2.1.0.beta4 test/operation/model_test.rb
trailblazer-2.1.0.beta3 test/operation/model_test.rb
trailblazer-2.1.0.beta2 test/operation/model_test.rb
trailblazer-2.1.0.beta1 test/operation/model_test.rb