require "test_helper" class DocsModelTest < Minitest::Spec Song = Struct.new(:id, :title) do def self.find_by(args) key, value = args.flatten return nil if value.nil? return new(value) if key == :id new(2, value) if key == :title end def self.[](id) id.nil? ? nil : new(id+99) end end #:op class Create < Trailblazer::Operation step Model(Song, :new) # .. end #:op end it "defaults {:params} to empty hash when not passed" do result = Create.({}) assert_equal true, result.success? assert_equal %{#}, result[:model].inspect result = Update.({}) assert_equal false, result.success? assert_equal "nil", result[:model].inspect end it do #:create result = Create.(params: {}) result[:model] #=> # #:create end result[:model].inspect.must_equal %{#} end #:update class Update < Trailblazer::Operation step Model( Song, :find_by ) # .. end #:update end #:update-with-find-by-key class UpdateWithFindByKey < Trailblazer::Operation step Model( Song, :find_by, :title ) # .. end #:update-with-find-by-key end #:update-with-not-found-end class UpdateFailureWithModelNotFound < Trailblazer::Operation step Model( Song, :find_by, not_found_terminus: true ) # .. end #:update-with-not-found-end end it do #:update-ok result = Update.(params: { id: 1 }) result[:model] #=> # #:update-ok end result[:model].inspect.must_equal %{#} end it do #:update-with-find-by-key-ok result = UpdateWithFindByKey.(params: { title: "Test" } ) result[:model] #=> # #:update-with-find-by-key-ok end result[:model].inspect.must_equal %{#} end it do #:update-fail result = Update.(params: {}) result[:model] #=> nil result.success? #=> false #:update-fail end result[:model].must_be_nil result.success?.must_equal false end it do #:update-with-find-by-key-fail result = UpdateWithFindByKey.(params: {title: nil}) result[:model] #=> nil result.success? #=> false #:update-with-find-by-key-fail end result[:model].must_be_nil result.success?.must_equal false end it do #:update-with-not-found-end-use result = UpdateFailureWithModelNotFound.(params: {title: nil}) result[:model] #=> nil result.success? #=> false result.event #=> # #:update-with-not-found-end-use end result[:model].must_be_nil result.success?.must_equal false result.event.inspect.must_equal %{#} end #:show class Show < Trailblazer::Operation step Model( Song, :[] ) # .. end #:show end it do result = Show.(params: { id: 1 }) #:show-ok result = Show.(params: { id: 1 }) result[:model] #=> # #:show-ok end result.success?.must_equal true result[:model].inspect.must_equal %{#} end it "allows injecting {:model.class} and friends" do class Hit < Song end #:di-model-class result = Create.(params: {}, :"model.class" => Hit) #:di-model-class end result[:model].inspect.must_equal %{#} # inject all variables #:di-all result = Create.( params: {title: "Olympia"}, # some random variable. "model.class": Hit, "model.action": :find_by, "model.find_by_key": :title ) #:di-all end result[:model].inspect.must_equal %{#} # use empty Model() and inject {model.class} and {model.action} module A #:op-model-empty class Create < Trailblazer::Operation step Model() # .. end #:op-model-empty end end # A result = A::Create.(params: {}, :"model.class" => Hit) result[:model].inspect.must_equal %{#} end end