From ea9caa4ed9e2a02ccd264101627ffdceca40193e Mon Sep 17 00:00:00 2001 From: Alejandro Date: Wed, 29 May 2024 17:01:00 +0200 Subject: [PATCH] Add tests --- .devcontainer/devcontainer.json | 22 +++ .github/dependabot.yml | 12 ++ .rubocop.yml | 1 + .rubocop_todo.yml | 36 +++++ Gemfile.lock | 138 ++++++++++++++++++ Gemfile.runtime | 0 .../services => }/hanikamu-services-async.rb | 0 lib/hanikamu/services/async.rb | 14 +- spec/hanikamu/services/async_spec.rb | 21 ++- spec/spec_helper.rb | 4 +- 10 files changed, 237 insertions(+), 11 deletions(-) create mode 100644 .devcontainer/devcontainer.json create mode 100644 .github/dependabot.yml create mode 100644 .rubocop.yml create mode 100644 .rubocop_todo.yml create mode 100644 Gemfile.runtime rename lib/{hanikamu/services => }/hanikamu-services-async.rb (100%) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..6c6df7e --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,22 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the +// README at: https://github.com/devcontainers/templates/tree/main/src/ruby +{ + "name": "Ruby", + // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile + "image": "mcr.microsoft.com/devcontainers/ruby:1-3.3-bullseye" + + // Features to add to the dev container. More info: https://containers.dev/features. + // "features": {}, + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], + + // Use 'postCreateCommand' to run commands after the container is created. + // "postCreateCommand": "ruby --version", + + // Configure tool-specific properties. + // "customizations": {}, + + // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. + // "remoteUser": "root" +} diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..f33a02c --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,12 @@ +# To get started with Dependabot version updates, you'll need to specify which +# package ecosystems to update and where the package manifests are located. +# Please see the documentation for more information: +# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates +# https://containers.dev/guide/dependabot + +version: 2 +updates: + - package-ecosystem: "devcontainers" + directory: "/" + schedule: + interval: weekly diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..cc32da4 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1 @@ +inherit_from: .rubocop_todo.yml diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..72d47a9 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,36 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2024-05-29 15:00:00 UTC using RuboCop version 1.64.0. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +# Configuration parameters: Severity, Include. +# Include: **/*.gemspec +Gemspec/RequiredRubyVersion: + Exclude: + - 'hanikamu-services-async.gemspec' + +# Offense count: 1 +# Configuration parameters: AllowedMethods. +# AllowedMethods: enums +Lint/ConstantDefinitionInBlock: + Exclude: + - 'spec/hanikamu/services/async_spec.rb' + +# Offense count: 2 +# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. +# AllowedMethods: refine +Metrics/BlockLength: + Max: 34 + +# Offense count: 1 +# Configuration parameters: ExpectMatchingDefinition, CheckDefinitionPathHierarchy, CheckDefinitionPathHierarchyRoots, Regex, IgnoreExecutableScripts, AllowedAcronyms. +# CheckDefinitionPathHierarchyRoots: lib, spec, test, src +# AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS +Naming/FileName: + Exclude: + - 'Rakefile.rb' + - 'lib/hanikamu-services-async.rb' diff --git a/Gemfile.lock b/Gemfile.lock index 7edd15c..bd4bc33 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,11 +1,149 @@ +PATH + remote: . + specs: + hanikamu-active_async (0.1.0) + activejob (< 8) + hanikamu-service (~> 0.1) + GEM + remote: https://rubygems.org/ specs: + activejob (7.1.3.3) + activesupport (= 7.1.3.3) + globalid (>= 0.3.6) + activesupport (7.1.3.3) + base64 + bigdecimal + concurrent-ruby (~> 1.0, >= 1.0.2) + connection_pool (>= 2.2.5) + drb + i18n (>= 1.6, < 2) + minitest (>= 5.1) + mutex_m + tzinfo (~> 2.0) + ast (2.4.2) + base64 (0.2.0) + bigdecimal (3.1.8) + coderay (1.1.3) + concurrent-ruby (1.2.3) + connection_pool (2.4.1) + diff-lcs (1.5.1) + drb (2.2.1) + dry-configurable (1.1.0) + dry-core (~> 1.0, < 2) + zeitwerk (~> 2.6) + dry-core (1.0.1) + concurrent-ruby (~> 1.0) + zeitwerk (~> 2.6) + dry-inflector (1.0.0) + dry-logic (1.5.0) + concurrent-ruby (~> 1.0) + dry-core (~> 1.0, < 2) + zeitwerk (~> 2.6) + dry-monads (1.6.0) + concurrent-ruby (~> 1.0) + dry-core (~> 1.0, < 2) + zeitwerk (~> 2.6) + dry-struct (1.6.0) + dry-core (~> 1.0, < 2) + dry-types (>= 1.7, < 2) + ice_nine (~> 0.11) + zeitwerk (~> 2.6) + dry-types (1.7.2) + bigdecimal (~> 3.0) + concurrent-ruby (~> 1.0) + dry-core (~> 1.0) + dry-inflector (~> 1.0) + dry-logic (~> 1.4) + zeitwerk (~> 2.6) + globalid (1.2.1) + activesupport (>= 6.1) + hanikamu-service (0.1.5) + dry-configurable (~> 1.1.0) + dry-monads (~> 1.6.0) + dry-struct (~> 1.6.0) + i18n (1.14.5) + concurrent-ruby (~> 1.0) + ice_nine (0.11.2) + json (2.7.2) + language_server-protocol (3.17.0.3) + method_source (1.1.0) + minitest (5.23.1) + mutex_m (0.2.0) + parallel (1.24.0) + parser (3.3.1.0) + ast (~> 2.4.1) + racc + pry (0.14.2) + coderay (~> 1.1) + method_source (~> 1.0) + racc (1.8.0) + rainbow (3.1.1) + rake (13.2.1) + regexp_parser (2.9.2) + rexml (3.2.8) + strscan (>= 3.0.9) + rspec (3.13.0) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.0) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-support (3.13.1) + rubocop (1.64.0) + json (~> 2.3) + language_server-protocol (>= 3.17.0) + parallel (~> 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.31.1, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.31.3) + parser (>= 3.3.1.0) + rubocop-capybara (2.20.0) + rubocop (~> 1.41) + rubocop-factory_bot (2.25.1) + rubocop (~> 1.41) + rubocop-performance (1.21.0) + rubocop (>= 1.48.1, < 2.0) + rubocop-ast (>= 1.31.1, < 2.0) + rubocop-rspec (2.29.2) + rubocop (~> 1.40) + rubocop-capybara (~> 2.17) + rubocop-factory_bot (~> 2.22) + rubocop-rspec_rails (~> 2.28) + rubocop-rspec_rails (2.28.3) + rubocop (~> 1.40) + ruby-progressbar (1.13.0) + strscan (3.1.0) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) + unicode-display_width (2.5.0) + zeitwerk (2.6.15) PLATFORMS ruby x86_64-linux DEPENDENCIES + bundler (>= 2) + hanikamu-active_async! + hanikamu-service (~> 0.1.0) + pry (>= 0.14.0) + rake + rspec (~> 3.0) + rubocop + rubocop-performance + rubocop-rspec BUNDLED WITH 2.5.9 diff --git a/Gemfile.runtime b/Gemfile.runtime new file mode 100644 index 0000000..e69de29 diff --git a/lib/hanikamu/services/hanikamu-services-async.rb b/lib/hanikamu-services-async.rb similarity index 100% rename from lib/hanikamu/services/hanikamu-services-async.rb rename to lib/hanikamu-services-async.rb diff --git a/lib/hanikamu/services/async.rb b/lib/hanikamu/services/async.rb index 39648e9..538ed21 100644 --- a/lib/hanikamu/services/async.rb +++ b/lib/hanikamu/services/async.rb @@ -1,5 +1,9 @@ # frozen_string_literal: true +require 'active_support/concern' +require 'active_job' +require 'active_support/core_ext/module/introspection' + module Hanikamu module Services # You can call from the operation the method @@ -30,7 +34,7 @@ def self.included(base) base.class_eval do const_set( :Async, - Class.new(ApplicationJob) do + Class.new(ActiveJob::Base) do class << self def call!(args = {}) perform_later(args: args, bang: true) @@ -42,7 +46,13 @@ def call(args = {}) end def perform(args:, bang:) - self.class.module_parent.send(bang ? :call! : :call, args) + parent_name_space.send(bang ? :call! : :call, args) + end + + private + + def parent_name_space + self.class.name.deconstantize.constantize end end ) diff --git a/spec/hanikamu/services/async_spec.rb b/spec/hanikamu/services/async_spec.rb index 3ab4bce..c1acd8f 100644 --- a/spec/hanikamu/services/async_spec.rb +++ b/spec/hanikamu/services/async_spec.rb @@ -1,17 +1,20 @@ # frozen_string_literal: true require 'spec_helper' +require 'hanikamu-service' -RSpec.describe Hanikamu::Services::Async do +RSpec.describe Hanikamu::Services::Async, type: :job do context 'when passing the wrong argument type' do let(:service) do - class TestFooModule::Bar < Hanikamu::Service - include Services::Async + module TestFooModule + class Bar < Hanikamu::Service + include Hanikamu::Services::Async - attribute :some_string, Dry::Types['string'] + attribute :some_string, Dry::Types['string'] - def call! - 'hola' + def call! + 'hola' + end end end TestFooModule::Bar @@ -24,7 +27,8 @@ def call! subject { service::Async.call(some_string: 'hey') } it 'calls the the service .call method' do - expect(service).to have_received(:call).with_arguments(some_string: 'hey') + subject + expect(service).to have_received(:call).with(some_string: 'hey') end end @@ -35,7 +39,8 @@ def call! subject { service::Async.call!(some_string: 'hey') } it 'returns a Hanikamu::ActiveCommand::Error' do - expect(service).to have_received(:call!).with_arguments(some_string: 'hey') + subject + expect(service).to have_received(:call!).with(some_string: 'hey') end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d9e21ab..1344b2a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -15,7 +15,9 @@ RSpec.configure do |config| config.disable_monkey_patching! config.filter_run_when_matching :focus - + config.before(:each) do + ActiveJob::Base.queue_adapter = :inline + end config.before do stub_const('TestFooModule', Module.new) end