-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chain error handling integration tests (#307)
* + chain error handling integration tests * ! rubocop ~ feedback: private method * ! spec * ! spec * ! spec naming * ! specs; * ! spec * ~ docs * ! Gemfile * ~ wording * ~ doc * ! rubocop
- Loading branch information
Showing
13 changed files
with
139 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,4 @@ Gemfile.*.lock | |
bower.json | ||
|
||
spec/dummy/log/test.log | ||
spec/dummy/tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
spec/dummy/app/controllers/error_handling_with_chains_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class ErrorHandlingWithChainsController < ApplicationController | ||
|
||
# Example where the query chain is resolved | ||
# in the view (during render 'show') | ||
def fetch_in_view | ||
@records = Record | ||
.handle(LHC::Error, ->(error) { handle_error(error) }) | ||
.where(color: 'blue') | ||
render 'show' | ||
render_error if @error | ||
end | ||
|
||
# Example where the query chain is resolved | ||
# before the view is rendered | ||
def fetch_in_controller | ||
@records = Record | ||
.handle(LHC::Error, ->(error) { handle_error(error) }) | ||
.where(color: 'blue').fetch | ||
render 'show' | ||
render_error if @error | ||
end | ||
|
||
private | ||
|
||
def handle_error(error) | ||
@error = error | ||
nil | ||
end | ||
|
||
def render_error | ||
self.response_body = nil | ||
render 'error' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Record < LHS::Record | ||
endpoint 'http://datastore/v2/records' | ||
endpoint 'http://datastore/v2/records/{id}' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Sorry there was an error. |
3 changes: 3 additions & 0 deletions
3
spec/dummy/app/views/error_handling_with_chains/show.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<% @records.each do |record| %> | ||
Name: <%= record.name %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
Rails.application.routes.draw do | ||
root 'application#root' | ||
|
||
# Request Cycle Cache | ||
get 'request_cycle_cache/simple' => 'request_cycle_cache#simple' | ||
get 'request_cycle_cache/no_caching_interceptor' => 'request_cycle_cache#no_caching_interceptor' | ||
get 'request_cycle_cache/parallel' => 'request_cycle_cache#parallel' | ||
get 'request_cycle_cache/headers' => 'request_cycle_cache#headers' | ||
|
||
# Error handling with chains | ||
get 'error_handling_with_chains/fetch_in_controller' => 'error_handling_with_chains#fetch_in_controller' | ||
get 'error_handling_with_chains/fetch_in_view' => 'error_handling_with_chains#fetch_in_view' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'rails_helper' | ||
require 'lhc/test/cache_helper.rb' | ||
|
||
describe 'Error handling with chains', type: :request do | ||
let!(:request) do | ||
stub_request(:get, "http://datastore/v2/records?color=blue") | ||
.to_return(status: 404) | ||
end | ||
|
||
it 'handles errors in rails controllers when query resolved in controller', | ||
dummy_models: true do | ||
get '/error_handling_with_chains/fetch_in_controller' | ||
expect(request).to have_been_made.once | ||
expect(response.body).to include('Sorry there was an error.') | ||
end | ||
|
||
it 'handles errors in rails controllers when query resolved in view', | ||
dummy_models: true do | ||
get '/error_handling_with_chains/fetch_in_view' | ||
expect(request).to have_been_made.once | ||
expect(response.body).to include('Sorry there was an error.') | ||
end | ||
end |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'lhc' | ||
class LHC::Config | ||
|
||
def _cleanup | ||
@endpoints = {} | ||
@placeholders = {} | ||
@interceptors = nil | ||
end | ||
end | ||
|
||
class LHS::Record | ||
|
||
CHILDREN = [] | ||
|
||
def self.inherited(child) | ||
CHILDREN.push(child) | ||
super | ||
end | ||
|
||
end | ||
|
||
def reset_lhc | ||
LHC::Config.instance._cleanup | ||
end | ||
|
||
def reset_lhs | ||
LHS::Record::Endpoints.all = {} | ||
LHS::Record::CHILDREN.each do |child| | ||
child.endpoints = [] if !child.name['LHS'] && defined?(child.endpoints) | ||
child.configuration({}) if !child.name['LHS'] | ||
end | ||
end | ||
|
||
RSpec.configure do |config| | ||
config.before do |spec| | ||
reset_lhc unless spec.metadata.key?(:reset_before) && spec.metadata[:reset_before] == false | ||
reset_lhs unless spec.metadata.key?(:reset_before) && spec.metadata[:reset_before] == false | ||
next unless spec.metadata.key?(:dummy_models) && spec.metadata[:dummy_models] == true | ||
Dir.glob(Rails.root.join('app', 'models', '**', '*.rb')).each do |file| | ||
load file if File.read(file).match('LHS::Record') | ||
end | ||
end | ||
end |