Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for matchers NonExhaustiveMatchError error and new failure matcher #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
PATH
remote: ../trailblazer
specs:
trailblazer (2.0.0.beta1)
trailblazer (2.0.6)
declarative
reform (>= 2.2.0, < 3.0.0)
trailblazer-operation
uber (>= 0.1.0, < 0.2.0)
trailblazer-operation (>= 0.0.12, < 0.1.0)

PATH
remote: .
Expand All @@ -17,12 +16,16 @@ GEM
remote: https://rubygems.org/
specs:
concurrent-ruby (1.0.2)
declarative (0.0.8)
uber (>= 0.0.15)
disposable (0.3.2)
declarative (>= 0.0.8, < 1.0.0)
declarative (0.0.9)
declarative-builder (0.1.0)
declarative-option (< 0.2.0)
declarative-option (0.1.0)
disposable (0.4.3)
declarative (>= 0.0.9, < 1.0.0)
declarative-builder (< 0.2.0)
declarative-option (< 0.2.0)
representable (>= 2.4.0, <= 3.1.0)
uber
uber (< 0.2.0)
dry-configurable (0.3.0)
concurrent-ruby (~> 1.0)
dry-container (0.5.0)
Expand Down Expand Up @@ -57,19 +60,19 @@ GEM
minitest-line (0.6.3)
minitest (~> 5.0)
multi_json (1.12.1)
pipetree (0.0.4)
pipetree (0.1.1)
rake (11.3.0)
reform (2.2.3)
disposable (>= 0.3.0, < 0.4.0)
reform (2.2.4)
disposable (>= 0.4.1)
representable (>= 2.4.0, < 3.1.0)
uber (>= 0.0.15, < 0.2.0)
representable (3.0.2)
declarative (~> 0.0.5)
uber (>= 0.0.15, < 0.2.0)
trailblazer-operation (0.0.6)
representable (3.0.4)
declarative (< 0.1.0)
declarative-option (< 0.2.0)
uber (< 0.2.0)
trailblazer-operation (0.0.13)
declarative
pipetree (>= 0.0.4, < 0.1.0)
uber (>= 0.1.0, < 0.2.0)
pipetree (>= 0.1.1, < 0.2.0)
uber
uber (0.1.0)

PLATFORMS
Expand All @@ -86,4 +89,4 @@ DEPENDENCIES
trailblazer-endpoint!

BUNDLED WITH
1.12.5
1.14.6
6 changes: 5 additions & 1 deletion lib/trailblazer/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ class Endpoint
resolve: ->(result) { result }),
# TODO: we could add unauthorized here.
unauthenticated: Dry::Matcher::Case.new(
match: ->(result) { result.failure? && result["result.policy.default"].failure? }, # FIXME: we might need a &. here ;)
match: ->(result) { result.failure? && result["result.policy.default"] && result["result.policy.default"].failure? },
resolve: ->(result) { result }),
invalid: Dry::Matcher::Case.new(
match: ->(result) { result.failure? && result["result.contract.default"] && result["result.contract.default"].failure? },
resolve: ->(result) { result }),
failure: Dry::Matcher::Case.new(
match: ->(result) { result.failure? },
resolve: ->(result) { result })
)

Expand All @@ -35,6 +38,7 @@ def self.call(operation_class, handlers, *args, &block)
def call(result, handlers=nil, &block)
matcher.(result, &block) and return if block_given? # evaluate user blocks first.
matcher.(result, &handlers) # then, generic Rails handlers in controller context.
rescue Dry::Matcher::NonExhaustiveMatchError # tmp solution, need to figure our how to handle NonExhaustiveMatchError errors
end

def matcher
Expand Down
1 change: 1 addition & 0 deletions lib/trailblazer/endpoint/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def call
m.created { |result| controller.head 201, location: "#{@path}/#{result["model"].id}" }#, result["representer.serializer.class"].new(result["model"]).to_json
m.success { |result| controller.head 200, location: "#{@path}/#{result["model"].id}" }
m.invalid { |result| controller.render json: result["representer.errors.class"].new(result['result.contract.default'].errors).to_json, status: 422 }
m.failure { |result| controller.render json: { :error=>"unknown error" }, status: 422 }
end
end
end
Expand Down
31 changes: 23 additions & 8 deletions test/endpoint_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "test_helper"
require "./test_helper"

require "reform"
require "trailblazer"
Expand Down Expand Up @@ -85,7 +85,6 @@ class Create < Trailblazer::Operation
step Model( Song, :new )
step Contract::Build()
step Contract::Validate( representer: self["representer.deserializer.class"] )
step Persist( method: :sync )
step ->(options) { options["model"].id = 9 }
end

Expand All @@ -100,7 +99,6 @@ def render(options)
# not authenticated, 401
it do
result = Create.( { id: 1 }, "user.current" => false )
# puts "@@@@@ #{result.inspect}"

Trailblazer::Endpoint.new.(result, handlers)
_data.inspect.must_equal %{[[:head, 401]]}
Expand All @@ -110,14 +108,13 @@ def render(options)
# length is ignored as it's not defined in the deserializer.
it do
result = Create.( {}, "user.current" => ::Module, "document" => '{"id": 9, "title": "Encores", "length": 999 }' )
# puts "@@@@@ #{result.inspect}"

Trailblazer::Endpoint.new.(result, handlers)
_data.inspect.must_equal '[[:head, 201, {:location=>"/songs/9"}]]'
end

class Update < Create
self.~ Model( :find_by )
class Update < Trailblazer::Operation
step Model( Song, :find_by )
end

# 404
Expand All @@ -128,6 +125,22 @@ class Update < Create
_data.inspect.must_equal '[[:head, 404]]'
end

class Verified < Trailblazer::Operation
step :verified

def verified(options, params:, **)
false
end
end

# failure case when you don't have any contract or policies, but operation failed
it do
result = Verified.({}, "user.current" => ::Module )

Trailblazer::Endpoint.new.(result, handlers)
_data.inspect.must_equal "[{:json=>{:error=>\"unknown error\"}, :status=>422}]"
end

#---
# validation failure 422
# success
Expand All @@ -154,10 +167,12 @@ class Update < Create
end

# generic handler because user handler doesn't match.
# TODO: fix this, it's fail because we expect user handlers to be run if they were passed
# need to figure out how to run generic Rails handlers if there was no correct user handler
it do
invoked = nil

endpoint( Update, { id: nil }, args: {"user.current" => ::Module} ) do |res|
endpoint(Update, args: [{ id: nil }, {"user.current" => ::Module}]) do |res|
res.invalid { invoked = "my invalid!" }
end

Expand All @@ -167,7 +182,7 @@ class Update < Create

# only generic handler
it do
endpoint(Update, { id: nil })
endpoint(Update, args: [{ id: nil }, {"user.current" => ::Module}] )
_data.must_equal [[:head, 404]]
end
end
6 changes: 2 additions & 4 deletions test/rails-app/Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
# Use sqlite3 as the database for Active Record
Expand All @@ -10,8 +9,7 @@ gem 'sqlite3'
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem "trailblazer-endpoint", path: "../../."
gem "trailblazer", path: "../../../trailblazer"
gem "trailblazer-operation", path: "../../../operation"
# gem "trailblazer-rails"
gem "trailblazer"
gem "trailblazer-operation"

gem "multi_json"
62 changes: 29 additions & 33 deletions test/rails-app/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
PATH
remote: ../../../operation
specs:
trailblazer-operation (0.0.6)
declarative
pipetree (>= 0.0.4, < 0.1.0)
uber (>= 0.1.0, < 0.2.0)

PATH
remote: ../../../trailblazer
specs:
trailblazer (2.0.0.beta1)
declarative
reform (>= 2.2.0, < 3.0.0)
trailblazer-operation
uber (>= 0.1.0, < 0.2.0)

PATH
remote: ../../.
remote: ../..
specs:
trailblazer-endpoint (0.0.1)
dry-matcher
Expand Down Expand Up @@ -64,12 +47,16 @@ GEM
arel (7.1.4)
builder (3.2.2)
concurrent-ruby (1.0.2)
declarative (0.0.8)
uber (>= 0.0.15)
disposable (0.3.2)
declarative (>= 0.0.8, < 1.0.0)
declarative (0.0.9)
declarative-builder (0.1.0)
declarative-option (< 0.2.0)
declarative-option (0.1.0)
disposable (0.4.3)
declarative (>= 0.0.9, < 1.0.0)
declarative-builder (< 0.2.0)
declarative-option (< 0.2.0)
representable (>= 2.4.0, <= 3.1.0)
uber
uber (< 0.2.0)
dry-matcher (0.5.0)
erubis (2.7.0)
globalid (0.3.7)
Expand All @@ -89,7 +76,7 @@ GEM
nio4r (1.2.1)
nokogiri (1.6.8.1)
mini_portile2 (~> 2.1.0)
pipetree (0.0.4)
pipetree (0.1.1)
rack (2.0.1)
rack-test (0.6.3)
rack (>= 1.0)
Expand Down Expand Up @@ -117,13 +104,13 @@ GEM
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (11.3.0)
reform (2.2.3)
disposable (>= 0.3.0, < 0.4.0)
reform (2.2.4)
disposable (>= 0.4.1)
representable (>= 2.4.0, < 3.1.0)
uber (>= 0.0.15, < 0.2.0)
representable (3.0.2)
declarative (~> 0.0.5)
uber (>= 0.0.15, < 0.2.0)
representable (3.0.4)
declarative (< 0.1.0)
declarative-option (< 0.2.0)
uber (< 0.2.0)
sprockets (3.7.0)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
Expand All @@ -134,6 +121,15 @@ GEM
sqlite3 (1.3.12)
thor (0.19.1)
thread_safe (0.3.5)
trailblazer (2.0.0)
declarative
reform (>= 2.2.0, < 3.0.0)
trailblazer-operation (>= 0.0.9)
uber (>= 0.1.0, < 0.2.0)
trailblazer-operation (0.0.13)
declarative
pipetree (>= 0.1.1, < 0.2.0)
uber
tzinfo (1.2.2)
thread_safe (~> 0.1)
uber (0.1.0)
Expand All @@ -148,10 +144,10 @@ DEPENDENCIES
multi_json
rails (~> 5.0.0, >= 5.0.0.1)
sqlite3
trailblazer!
trailblazer
trailblazer-endpoint!
trailblazer-operation!
trailblazer-operation
tzinfo-data

BUNDLED WITH
1.12.5
1.14.6
3 changes: 2 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)

require "minitest/autorun"
require "trailblazer"
require "trailblazer/endpoint"