Class: Jackal::Utils::Spec::Generator

Inherits:
Bogo::Cli::Command
  • Object
show all
Defined in:
lib/jackal/utils/spec/generator.rb

Overview

Test files generator

Constant Summary collapse

TEST_DIR =

Test directory path

'test/specs'
TEST_CONFIG_DIR =

Test configuration directory path

'test/specs/config'
GEMFILE_LINES =

Lines required within Gemfile

[
  "gem 'carnivore-actor'",
  "gem 'minitest'",
  "gem 'pry'"
]

Instance Method Summary collapse

Constructor Details

#initialize(*_) ⇒ self

Create new instance



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jackal/utils/spec/generator.rb', line 25

def initialize(*_)
  super

  @orig_service_name = options[:service_name].downcase
  @service_name = Bogo::Utility.snake(@orig_service_name)
  @service_class_name = Bogo::Utility.camel(@service_name)

  @module_name = options[:module_name].downcase
  @module_class_name = Bogo::Utility.camel(@module_name)

  @callback_type   = 'jackal'
  @supervisor_name = "jackal_#{@service_name}_input"
end

Instance Method Details

#config_file_contentString

Configuration file content

Returns:

  • (String)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/jackal/utils/spec/generator.rb', line 78

def config_file_content
  <<-TEXT
Configuration.new do
  jackal do
    require ["carnivore-actor", "jackal-#{@orig_service_name}"]

    mail do
      config do
      end

      sources do
input  { type 'actor' }
output { type 'spec' }
      end

      callbacks ['Jackal::#{@service_class_name}::#{@module_class_name}']
    end
  end
end
TEXT
end

#execute!TrueClass

Generate test files

Returns:

  • (TrueClass)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/jackal/utils/spec/generator.rb', line 42

def execute!
  ui.info 'Generating jackal test files'

  # ensure dependencies are present in Gemfile
  run_action 'Update Gemfile contents' do
    update_gemfile
    nil
  end

  run_action 'Create testing directory structure' do
    # Create test directory structure
    [TEST_DIR, TEST_CONFIG_DIR].each do |dir|
      FileUtils.mkdir_p(dir)
    end
    nil
  end

  run_action 'Write test configuration file' do
    conf_path = File.join(Dir.pwd, TEST_CONFIG_DIR, "#{@module_name}.rb")
    write_file(conf_path, config_file_content)
    nil
  end

  run_action 'Write default test spec file' do
    spec_path = File.join(Dir.pwd, TEST_DIR, "#{@service_name}_spec.rb")
    write_file(spec_path, spec_file_content)
    nil
  end

  ui.info 'Jackal test file generation complete!'
  true
end

#spec_file_contentString

Spec file content

Returns:

  • (String)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/jackal/utils/spec/generator.rb', line 103

def spec_file_content
  <<TEXT
require '#{@callback_type}-#{@orig_service_name}'
require 'pry'

# To stub out an api call for your callback
class #{callback_class}::#{@service_class_name}::#{@module_class_name}
  attr_accessor :test_payload

  #def api_call(args)
  #  test_payload.set(:args, args)
  #end
end

describe #{callback_class}::#{@service_class_name}::#{@module_class_name} do

  before do
    @runner = run_setup(:#{@module_name})
    track_execution(#{callback_class}::#{@service_class_name}::#{@module_class_name})
  end

  after do
    @runner.terminate
  end

  let(:actor) { Carnivore::Supervisor.supervisor[:#{@supervisor_name}] }

  it 'executes with empty payload' do
    result = transmit_and_wait(actor, payload)
    (!!result).must_equal true
    callback_executed?(result).must_equal true
  end

  private

  # payload to send for callback execution
  def payload
    Jackal::Utils.new_payload(:test, Smash.new)
  end

end
TEXT
end