From 3fd620b3729828e9ba9ccef371057a58ccfaa674 Mon Sep 17 00:00:00 2001 From: Austin Miller Date: Sun, 6 Feb 2022 20:16:57 -0600 Subject: [PATCH] Fix: Adding Tests (#2) * Adding some tests * fix: readme * Serialize using yaml so we can support lower versions of rails * Fixing rails 6 tests --- Gemfile.lock | 2 +- README.md | 5 +- .../deferred_request/test_controller.rb | 19 +++++++ app/helpers/deferred_request/test_helper.rb | 4 ++ .../deferred_request/deferred_request.rb | 9 ++-- config/routes.rb | 2 + .../deferred_request/test_controller_test.rb | 52 +++++++++++++++++++ .../deferred_request/deferred_requests.yml | 14 ++--- 8 files changed, 89 insertions(+), 18 deletions(-) create mode 100644 app/controllers/deferred_request/test_controller.rb create mode 100644 app/helpers/deferred_request/test_helper.rb create mode 100644 test/controllers/deferred_request/test_controller_test.rb diff --git a/Gemfile.lock b/Gemfile.lock index aaac3fd..3e54541 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - deferred_request (0.0.1) + deferred_request (1.0.0) rails (>= 6.0.0) GEM diff --git a/README.md b/README.md index 39ac527..c51da7f 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/app/controllers/deferred_request/test_controller.rb b/app/controllers/deferred_request/test_controller.rb new file mode 100644 index 0000000..5f3ef9a --- /dev/null +++ b/app/controllers/deferred_request/test_controller.rb @@ -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 diff --git a/app/helpers/deferred_request/test_helper.rb b/app/helpers/deferred_request/test_helper.rb new file mode 100644 index 0000000..c935e28 --- /dev/null +++ b/app/helpers/deferred_request/test_helper.rb @@ -0,0 +1,4 @@ +module DeferredRequest + module TestHelper + end +end diff --git a/app/models/deferred_request/deferred_request.rb b/app/models/deferred_request/deferred_request.rb index 9163389..db2403a 100644 --- a/app/models/deferred_request/deferred_request.rb +++ b/app/models/deferred_request/deferred_request.rb @@ -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" @@ -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 @@ -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}") diff --git a/config/routes.rb b/config/routes.rb index 518e52c..1b08dc8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,2 +1,4 @@ DeferredRequest::Engine.routes.draw do + post "status_callback" => "test#status_callback" + post "status_error" => "test#status_error" end diff --git a/test/controllers/deferred_request/test_controller_test.rb b/test/controllers/deferred_request/test_controller_test.rb new file mode 100644 index 0000000..a88b906 --- /dev/null +++ b/test/controllers/deferred_request/test_controller_test.rb @@ -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 diff --git a/test/fixtures/deferred_request/deferred_requests.yml b/test/fixtures/deferred_request/deferred_requests.yml index c9e3e0c..071c748 100644 --- a/test/fixtures/deferred_request/deferred_requests.yml +++ b/test/fixtures/deferred_request/deferred_requests.yml @@ -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"