Skip to content

Commit

Permalink
cleanups.
Browse files Browse the repository at this point in the history
test :operation in Suite.
  • Loading branch information
apotonick committed Jan 22, 2025
1 parent 641f0b8 commit 2aa43e5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 123 deletions.
49 changes: 0 additions & 49 deletions lib/trailblazer/test/testing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,54 +58,5 @@ def parse_tag_list(ctx, **)
end # Create
end
end

# TODO: 2BRM.
Song = Struct.new(:band, :title, :duration) do
def save()
@save = true
end

def persisted?
!! @save
end
end

module Song::Contract
class Create < Reform::Form
property :band
property :title
property :duration

require "reform/form/dry"
include Reform::Form::Dry
validation do
params do
required(:title).filled
optional(:duration).maybe(type?: String)
required(:band).filled
end
end
# validates :band, presence: true
end
end

module Song::Operation
class Create < Trailblazer::Operation
step Model(Song, :new)
step Contract::Build(constant: Song::Contract::Create)
step Contract::Validate(key: :song)
step :parse_duration
step Contract::Persist()

def parse_duration(ctx, **)
duration = ctx["contract.default"].duration or return true

m = duration.match(/(\d)\.(\d\d)$/)
duration_seconds = m[1].to_i*60 + m[2].to_i

ctx["contract.default"].duration = duration_seconds
end
end
end
end
end
3 changes: 2 additions & 1 deletion test/activity_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "test_helper"


# Test all assertions with a FastTrack class, not an Operation.
# TODO: This is currently not documented.
class AssertionActivityTest < Minitest::Spec
Trailblazer::Test::Assertion.module!(self, activity: true)
Memo = Trailblazer::Test::Testing::Memo
Expand Down
72 changes: 3 additions & 69 deletions test/docs/suite_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ class OperationSpec < Minitest::Spec
# {Memo}, {Memo::Operation::Create} etc is from lib/trailblazer/test/testing.rb.
class DocsSuiteAssertionsTest < Minitest::Spec
Memo = Trailblazer::Test::Testing::Memo
Song = Trailblazer::Test::Testing::Song
Trailblazer::Test::Assertion.module!(self, suite: true)

Memo::Operation::Update = Class.new(Memo::Operation::Create)

#:test
#:install
class MemoOperationTest < Minitest::Spec # test/memo/operation_test.rb
Expand Down Expand Up @@ -182,7 +183,7 @@ class MemoOperationTest < Minitest::Spec # test/memo/operation_test.rb
it "{Ctx} provides {merge: false} to allow direct ctx building without any automatic merging" do
ctx = Ctx({current_user: yogi}, merge: false)

assert_equal %{{:current_user=>\"Yogi\"}}, ctx.inspect
assert_equal %({:current_user=>\"Yogi\"}), ctx.inspect
end

#~meths end
Expand Down Expand Up @@ -246,73 +247,6 @@ class Create < Trailblazer::Operation
end
end # SongOperation_OMIT_KEY_Test


#:assert_pass
describe "Create with sane data" do
# What are we passing into the operation?
let(:default_ctx) do
{
params: {
song: { # Note the {song} key here!
band: "Rancid",
title: "Timebomb",
}
}
}
end

# What will the model look like after running the operation?
let(:expected_attributes) do
{
band: "Rancid",
title: "Timebomb",
}
end

let(:operation) { Song::Operation::Create }
let(:key_in_params) { :song }

# valid default input, works
it { assert_pass( {}, {} ) }

# Check if {:title} can be overridden
it { assert_pass( {title: "Ruby Soho"}, {title: "Ruby Soho"} ) }
# Assert if automatic trimming works...
# Check if coercing works..

# Check if validations work
it { assert_fail?( {band: ""}, [:band] ) }

it do
assert_pass( {title: "Ruby Soho"}, {title: "Ruby Soho"} ) do |result|
assert_equal "Rancid", result[:model].band
end
end

it do
assert_fail( {band: ""}, [:band] ) do |result|
assert_nil result[:model].band
# puts result[:"contract.default"].errors.messages # Yes, this is good for debugging!
end
end
end

class PassFailTestWithoutSettings < OperationSpec
it "allows to pass the entire context without any automatic merging" do
assert_pass Ctx({params: {song: {title: "Ruby Soho", band: "Rancid"}}}, merge: false),
{title: "Ruby Soho", band: "Rancid"},
operation: Song::Operation::Create, default_ctx: {}
end

it "what" do
assert_pass( {title: "Ruby Soho", band: "Rancid"}, {}, operation: Song::Operation::Create, key_in_params: :song )
end

it "what" do
assert_pass( {song: {title: "Ruby Soho", band: "Rancid"}}, {}, operation: Song::Operation::Create )
end
end

class Test < Minitest::Spec
def call
run
Expand Down
44 changes: 40 additions & 4 deletions test/suite_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

# Brutal unit tests for Suite.
class SuiteTest < Minitest::Spec
Record = Struct.new(:id, :persisted?, :title, :genre, keyword_init: true)

# UNCOMMENT for quick debugging.
# Trailblazer::Test::Assertion.module!(self, suite: true)
# let(:operation) { Trailblazer::Test::Testing::Memo::Operation::Create }
# let(:default_ctx) { {params: {memo: {title: "Note to self", content: "Remember me!"}}} }
# let(:expected_attributes) { {content: "Remember me!", persisted?: true} }
# let(:key_in_params) { :memo }
# it "what" do
# assert_fail({title: nil}, {title: ["is missing"]})
# # assert_fail({title: nil}, {title: ["is missing"]})
# end

it "#assert_pass" do
Expand Down Expand Up @@ -175,6 +173,26 @@ def model(ctx, params:, **)
)
end

Test_for_OperationOption = describe ":operation" do
DIFFERENT_OP = Class.new(Trailblazer::Operation) do
include Memo::Operation::Create::Capture
step :capture
step :model

def model(ctx, params:, **)
ctx[:model] = Memo.new(**params[:memo])
end
end

let(:operation) { raise }

# 01
# We accept {:operation} as a first level kw arg.
it do
@result = assert_pass({title: "Done"}, {title: "Done"}, operation: DIFFERENT_OP)
end
end

Test_assert_fail = describe "{#assert_fail}" do
# 01
# validation error
Expand Down Expand Up @@ -316,11 +334,27 @@ def model(ctx, params:, **)
end

# 01
# {#assert_fail} can be used wit OP withhout contract errors.
# {#assert_fail} can be used with OP withhout contract errors.
it do
@result = assert_fail({})
end
end

Nothing_configured_test = describe "you can use Suite without setting anything, by passing everything manually" do
# 01
# allows to pass the entire context without any automatic merging
it do
@result = assert_pass Ctx(Memo::VALID_INPUT, merge: false),
{title: "TODO", content: "Stock up beer"},
operation: Trailblazer::Test::Testing::Memo::Operation::Create, default_ctx: {}
end

# 02
# # we can pass {:operation} and {:key_in_params}, and expected_attributes.
# it do
# assert_pass( {title: "TODO", content: "Stock up beer"}, {}, operation: Memo::Operation::Create, key_in_params: :memo )
# end
end
end
end

Expand All @@ -344,6 +378,7 @@ def model(ctx, params:, **)
assert_test_case_passes(test, "09", input)
assert_test_case_passes(test, "10", input)
assert_test_case_passes(Test_for_ModelAt, "01", %({:params=>{:memo=>{:title=>\"Done\", :content=>\"Remember me!\"}}}))
assert_test_case_passes(Test_for_OperationOption, "01", %({:params=>{:memo=>{:title=>\"Done\", :content=>\"Remember me!\"}}}))
assert_test_case_passes(test, "11", %({:params=>{:memo=>{:title=>\"Done\", :content=>\"Remember me!\"}}}))

# assert_fail
Expand Down Expand Up @@ -375,6 +410,7 @@ def model(ctx, params:, **)
assert_test_case_passes(Test_assert_fail, "12", %({:params=>{:memo=>{:title=>nil, :content=>\"Remember me!\"}}}))
assert_test_case_passes(Test_assert_fail, "13", %({:params=>{:memo=>{:title=>nil, :content=>\"Remember me!\"}}}))
assert_test_case_passes(No_contract_test, "01", input)
assert_test_case_passes(Nothing_configured_test, "01", %({:params=>{:memo=>{:title=>\"TODO\", :content=>\"Stock up beer\"}}}))
end
end

Expand Down

0 comments on commit 2aa43e5

Please sign in to comment.