Skip to content

Commit

Permalink
Fix: Adding Tests (#2)
Browse files Browse the repository at this point in the history
* Adding some tests

* fix: readme

* Serialize using yaml so we can support lower versions of rails

* Fixing rails 6 tests
  • Loading branch information
armiiller authored Feb 7, 2022
1 parent 59c8f6e commit 3fd620b
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
deferred_request (0.0.1)
deferred_request (1.0.0)
rails (>= 6.0.0)

GEM
Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ def status_callback
head :ok

# Then queue the request to run later
deferred_request = DeferredRequest::DeferredRequest.from_request(request)
deferred_request.save!

deferred_request.peform_later
deferred_request = DeferredRequest::DeferredRequest.perform_later_from_request!(request)
end

# Your deferred request method will be called later (via a job)
Expand Down
19 changes: 19 additions & 0 deletions app/controllers/deferred_request/test_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module DeferredRequest
class TestController < ApplicationController
def status_callback
head :ok
DeferredRequest.perform_later_from_request!(request)
end

def status_callback_deferred(deferred_request)
# Do something with the deferred request
deferred_request.params.dig("answer")
end

def status_error
# don't add the "_deferred" method (used for testing)
head :ok
DeferredRequest.perform_later_from_request!(request)
end
end
end
4 changes: 4 additions & 0 deletions app/helpers/deferred_request/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module DeferredRequest
module TestHelper
end
end
9 changes: 5 additions & 4 deletions app/models/deferred_request/deferred_request.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module DeferredRequest
class DeferredRequest < ApplicationRecord
serialize :routing, JSON
serialize :request, JSON
serialize :routing
serialize :request
serialize :result

store_accessor :routing, "controller", "action"
store_accessor :request, "url", "method", "headers", "params", "remote_ip"
Expand All @@ -13,7 +14,7 @@ class DeferredRequest < ApplicationRecord
def self.from_request(request)
deferred_request = DeferredRequest.new

deferred_request.controller = request.controller_class
deferred_request.controller = request.controller_class.name
deferred_request.action = request.params["action"]

deferred_request.url = request.url
Expand Down Expand Up @@ -43,7 +44,7 @@ def perform!

klass = controller.constantize.new

self.result = klass.try("#{action}_deferred".to_sym, self)
self.result = klass.send("#{action}_deferred".to_sym, self)
self.status = :complete
rescue => e
Rails.logger.error("DeferredRequest::DeferredRequestJob: #{e.message}")
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
DeferredRequest::Engine.routes.draw do
post "status_callback" => "test#status_callback"
post "status_error" => "test#status_error"
end
52 changes: 52 additions & 0 deletions test/controllers/deferred_request/test_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require "test_helper"

module DeferredRequest
class TestControllerTest < ActionDispatch::IntegrationTest
include Engine.routes.url_helpers

test "can_defer json request successfully" do
answer = 1

assert_no_performed_jobs

assert_difference("DeferredRequest.count") do
post status_callback_url, params: {answer: answer}, as: :json
end

assert_response :success

assert_enqueued_jobs 1

perform_enqueued_jobs

last_request = DeferredRequest.last

assert_equal answer, last_request.result

assert_performed_jobs 1
end

test "can_defer json request successfully but handle no deferred method" do
answer = 2

assert_no_performed_jobs

assert_difference("DeferredRequest.count") do
post status_error_url, params: {answer: answer}, as: :json
end

assert_response :success

assert_enqueued_jobs 1

perform_enqueued_jobs

last_request = DeferredRequest.last

assert_not_equal answer, last_request.result
assert_equal "error", last_request.status

assert_performed_jobs 1
end
end
end
14 changes: 5 additions & 9 deletions test/fixtures/deferred_request/deferred_requests.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
request: MyText
routing: MyText
status: 1

two:
request: MyText
routing: MyText
status: 1
# one:
# request: '{"url":"http://localhost:3000/status_callback?yo=some_text","method":"POST","headers":{"HTTP_VERSION":"HTTP/1.1","HTTP_USER_AGENT":"PostmanRuntime/7.29.0","HTTP_ACCEPT":"*/*","HTTP_POSTMAN_TOKEN":"ab8d6da5-575e-49a1-85bd-2a1f7be0c06e","HTTP_HOST":"localhost:3000","HTTP_ACCEPT_ENCODING":"gzip,
# deflate, br","HTTP_CONNECTION":"keep-alive"},"params":{"some":"more","yo":"some_text","test":{"some":"more"}},"remote_ip":"::1"}'
# routing: '{"controller":"TestController","action":"status_callback"}'
# status: "queued"

0 comments on commit 3fd620b

Please sign in to comment.