Rspec Unit tests

This commit is contained in:
Broosk Hamarash 2021-03-02 22:51:58 +03:00
parent 1cd56e6a89
commit b4778982fd
10 changed files with 98 additions and 14 deletions

14
.byebug_history Normal file
View File

@ -0,0 +1,14 @@
c
attrs
c
attrs
attrs["workflow_state"]
attrs[:workflow_state]
attrs
c
@current_sequence
item
c
n
item
@transitions

1
.ruby-version Normal file
View File

@ -0,0 +1 @@
3.0.0

View File

@ -13,6 +13,7 @@ GEM
minitest (~> 5.1) minitest (~> 5.1)
tzinfo (~> 1.1) tzinfo (~> 1.1)
ast (2.4.2) ast (2.4.2)
byebug (11.1.3)
concurrent-ruby (1.1.8) concurrent-ruby (1.1.8)
diff-lcs (1.4.4) diff-lcs (1.4.4)
i18n (1.8.9) i18n (1.8.9)
@ -56,9 +57,11 @@ GEM
unicode-display_width (1.7.0) unicode-display_width (1.7.0)
PLATFORMS PLATFORMS
ruby
x86_64-linux x86_64-linux
DEPENDENCIES DEPENDENCIES
byebug
rake (~> 13.0) rake (~> 13.0)
rspec (~> 3.0) rspec (~> 3.0)
rubocop (~> 0.80) rubocop (~> 0.80)

View File

@ -1,13 +1,19 @@
# frozen_string_literal: true
require "active_support" require "active_support"
# Workflower Module Defintion
module Workflower module Workflower
mattr_accessor :workflower_state_column_name mattr_accessor :workflower_state_column_name
mattr_accessor :workflow_model mattr_accessor :workflow_model
mattr_accessor :transition_model mattr_accessor :transition_model
mattr_accessor :conditions mattr_accessor :conditions
# ActsAsWorkflower Module Definition
module ActsAsWorkflower module ActsAsWorkflower
extend ActiveSupport::Concern extend ActiveSupport::Concern
# InstanceMethods
module InstanceMethods module InstanceMethods
# mattr_accessor :workflower_base # mattr_accessor :workflower_base
attr_accessor :possible_events, :allowed_events, :allowed_transitions attr_accessor :possible_events, :allowed_events, :allowed_transitions
@ -54,7 +60,9 @@ module Workflower
end end
end end
# Class Methods
module ClassMethods module ClassMethods
# rubocop:disable Metrics/AbcSize
def workflower(options = { workflower_state_column_name: "workflow_state" }) def workflower(options = { workflower_state_column_name: "workflow_state" })
if options[:source].blank? || options[:conditions].blank? if options[:source].blank? || options[:conditions].blank?
raise Workflower::WorkflowerError, "Options can't be blank" raise Workflower::WorkflowerError, "Options can't be blank"
@ -75,21 +83,20 @@ module Workflower
def workflower_abilities def workflower_abilities
load = source.get_workflows.values.flatten.uniq load = source.get_workflows.values.flatten.uniq
unless load.blank? return [] if load.blank?
# transitions = load.transitions.where("(metadata->>'roles') IS NOT NULL")
transitions = load.select { |item| item.try(:[], :metadata).try(:key?, :roles) }
roles = transitions.map { |item| item[:metadata][:roles] }.flatten.uniq # transitions = load.transitions.where("(metadata->>'roles') IS NOT NULL")
transitions = load.select { |item| item.try(:[], :metadata).try(:key?, :roles) }
roles_hash = {} roles = transitions.map { |item| item[:metadata][:roles] }.flatten.uniq
roles.each do |role| roles_hash = {}
roles_hash[role] = transitions.select { |trans| trans[:metadata][:roles].to_a.include?(role) }.map { |item| item[:event] }.uniq
end
roles_hash
roles.each do |role|
roles_hash[role] = transitions.select { |trans| trans[:metadata][:roles].to_a.include?(role) }.map { |item| item[:event] }.uniq
end end
roles_hash
end end
end end

View File

@ -2,6 +2,7 @@ module Workflower
class Flow class Flow
attr_accessor :state, :transition_into, :trigger_action_name, :boolean_action_name, :sequence, :downgrade_sequence, :event, :condition, :condition_type, :before_transit, :after_transit, :metadata, :workflow_id, :deviation_id attr_accessor :state, :transition_into, :trigger_action_name, :boolean_action_name, :sequence, :downgrade_sequence, :event, :condition, :condition_type, :before_transit, :after_transit, :metadata, :workflow_id, :deviation_id
# rubocop:disable Metrics/AbcSize
def initialize(options) def initialize(options)
@state = options[:state] @state = options[:state]
@transition_into = options[:transition_into] @transition_into = options[:transition_into]

View File

@ -1,6 +1,6 @@
require "workflower/errors" require "workflower/errors"
require "workflower/flow" require "workflower/flow"
require "byebug"
module Workflower module Workflower
class Manager class Manager
attr_reader :events, :flows_container, :allowed_events attr_reader :events, :flows_container, :allowed_events

29
spec/dummy_feature.rb Normal file
View File

@ -0,0 +1,29 @@
# A dummy class to showcase the use of the Gem
require "workflower"
require "workflow_source"
require "byebug"
class DummyFeature
attr_accessor :workflow_id, :workflow_state, :sequence
include Workflower::ActsAsWorkflower
def initialize
@workflow_id = 1
@workflow_state = "saved"
@sequence = 1
end
def self.before_create(method_name)
end
def assign_attributes(attrs)
@workflow_state = attrs["workflow_state"]
@sequence = attrs["sequence"]
end
workflower source: WorkflowSource.new,
workflower_state_column_name: "workflow_state",
conditions: { name: "Test Workflow", workflow_model_name: "DummyEntity" }
end

22
spec/workflow_source.rb Normal file
View File

@ -0,0 +1,22 @@
class WorkflowSource
def initialize
@workflows = {
"1": [
{
state: "saved",
transition_into: "submitted",
event: "submit",
sequence: 1
}
].flatten
}
end
def get_workflows
@workflows
end
def get_workflows_for_workflow_id(workflow_id)
get_workflows[workflow_id.to_s.to_sym]
end
end

View File

@ -1,11 +1,17 @@
# frozen_string_literal: true # frozen_string_literal: true
require "dummy_feature"
RSpec.describe Workflower do RSpec.describe Workflower do
it "has a version number" do it "has a version number" do
expect(Workflower::VERSION).not_to be nil expect(Workflower::VERSION).not_to be nil
end end
it "transitions from saved to submitted" do it "transitions from saved to submitted" do
expect(true).to eq(true) @test = DummyFeature.new
@test.workflower_initializer
@test.submit!
expect(@test.workflow_state).to eq("submitted")
end end
end end

View File

@ -30,7 +30,8 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"] spec.require_paths = ["lib"]
# Uncomment to register a new dependency of your gem # Uncomment to register a new dependency of your gem
spec.add_dependency 'activesupport', '~> 5.0', '>= 5.0.0.1' spec.add_dependency "activesupport", "~> 5.0", ">= 5.0.0.1"
spec.add_development_dependency "byebug"
# For more information and examples about making a new gem, checkout our # For more information and examples about making a new gem, checkout our
# guide at: https://bundler.io/guides/creating_gem.html # guide at: https://bundler.io/guides/creating_gem.html