service_template 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +21 -0
- data/.rubocop.yml +23 -0
- data/.travis.yml +13 -0
- data/CHANGELOG.md +64 -0
- data/Gemfile +4 -0
- data/LICENSE +24 -0
- data/README.md +217 -0
- data/Rakefile +9 -0
- data/bin/service_template +5 -0
- data/lib/service_template.rb +55 -0
- data/lib/service_template/active_record_extensions/notifications_subscriber.rb +17 -0
- data/lib/service_template/active_record_extensions/seeder.rb +14 -0
- data/lib/service_template/active_record_extensions/stats.rb +37 -0
- data/lib/service_template/authentication.rb +8 -0
- data/lib/service_template/cli.rb +111 -0
- data/lib/service_template/deploy.rb +98 -0
- data/lib/service_template/gem_dependency.rb +37 -0
- data/lib/service_template/generators.rb +3 -0
- data/lib/service_template/generators/api_generator.rb +30 -0
- data/lib/service_template/generators/readme_generator.rb +47 -0
- data/lib/service_template/generators/scaffold_generator.rb +29 -0
- data/lib/service_template/generators/templates/api/app/apis/%name_tableize%_api.rb.tt +40 -0
- data/lib/service_template/generators/templates/api/app/models/%name_underscore%.rb.tt +2 -0
- data/lib/service_template/generators/templates/api/app/representers/%name_underscore%_representer.rb.tt +4 -0
- data/lib/service_template/generators/templates/api/spec/apis/%name_tableize%_api_spec.rb.tt +16 -0
- data/lib/service_template/generators/templates/api/spec/models/%name_underscore%_spec.rb.tt +9 -0
- data/lib/service_template/generators/templates/readme/README.md.tt +55 -0
- data/lib/service_template/generators/templates/readme/spec/docs/readme_spec.rb +7 -0
- data/lib/service_template/generators/templates/scaffold/.env.development.tt +9 -0
- data/lib/service_template/generators/templates/scaffold/.env.test.tt +10 -0
- data/lib/service_template/generators/templates/scaffold/.gitignore.tt +13 -0
- data/lib/service_template/generators/templates/scaffold/.rubocop.yml +24 -0
- data/lib/service_template/generators/templates/scaffold/.ruby-version.tt +1 -0
- data/lib/service_template/generators/templates/scaffold/Gemfile.tt +29 -0
- data/lib/service_template/generators/templates/scaffold/README.md +3 -0
- data/lib/service_template/generators/templates/scaffold/Rakefile +21 -0
- data/lib/service_template/generators/templates/scaffold/app.rb +19 -0
- data/lib/service_template/generators/templates/scaffold/app/apis/application_api.rb +9 -0
- data/lib/service_template/generators/templates/scaffold/app/apis/hello_api.rb.tt +10 -0
- data/lib/service_template/generators/templates/scaffold/config.ru.tt +21 -0
- data/lib/service_template/generators/templates/scaffold/config/database.yml.tt +19 -0
- data/lib/service_template/generators/templates/scaffold/config/initializers/active_record.rb +5 -0
- data/lib/service_template/generators/templates/scaffold/db/schema.rb +11 -0
- data/lib/service_template/generators/templates/scaffold/lib/.keep +0 -0
- data/lib/service_template/generators/templates/scaffold/log/.keep +0 -0
- data/lib/service_template/generators/templates/scaffold/spec/apis/hello_api_spec.rb.tt +17 -0
- data/lib/service_template/generators/templates/scaffold/spec/factories/.gitkeep +0 -0
- data/lib/service_template/generators/templates/scaffold/spec/spec_helper.rb +47 -0
- data/lib/service_template/grape_extenders.rb +30 -0
- data/lib/service_template/grape_extensions/error_formatter.rb +18 -0
- data/lib/service_template/grape_extensions/grape_helpers.rb +27 -0
- data/lib/service_template/identity.rb +45 -0
- data/lib/service_template/json_error.rb +24 -0
- data/lib/service_template/logger/log_transaction.rb +17 -0
- data/lib/service_template/logger/logger.rb +42 -0
- data/lib/service_template/logger/parseable.rb +37 -0
- data/lib/service_template/middleware/app_monitor.rb +17 -0
- data/lib/service_template/middleware/authentication.rb +32 -0
- data/lib/service_template/middleware/database_stats.rb +15 -0
- data/lib/service_template/middleware/logger.rb +67 -0
- data/lib/service_template/middleware/request_stats.rb +42 -0
- data/lib/service_template/output_formatters/entity.rb +15 -0
- data/lib/service_template/output_formatters/include_nil.rb +16 -0
- data/lib/service_template/output_formatters/json_api_representer.rb +9 -0
- data/lib/service_template/param_sanitizer.rb +30 -0
- data/lib/service_template/rspec_extensions/response_helpers.rb +46 -0
- data/lib/service_template/setup.rb +36 -0
- data/lib/service_template/sortable_api.rb +17 -0
- data/lib/service_template/stats.rb +43 -0
- data/lib/service_template/stats_d_timer.rb +26 -0
- data/lib/service_template/version.rb +45 -0
- data/lib/tasks/deploy.rake +11 -0
- data/lib/tasks/routes.rake +11 -0
- data/service_template.gemspec +42 -0
- data/spec/active_record_extensions/filter_by_hash_spec.rb +23 -0
- data/spec/active_record_extensions/seeder_spec.rb +13 -0
- data/spec/authentication_spec.rb +17 -0
- data/spec/deprecations/application_api_spec.rb +19 -0
- data/spec/deprecations/entity_spec.rb +9 -0
- data/spec/deprecations/filter_by_hash_spec.rb +9 -0
- data/spec/deprecations/napa_setup_spec.rb +52 -0
- data/spec/generators/api_generator_spec.rb +63 -0
- data/spec/generators/migration_generator_spec.rb +105 -0
- data/spec/generators/readme_generator_spec.rb +35 -0
- data/spec/generators/scaffold_generator_spec.rb +90 -0
- data/spec/grape_extenders_spec.rb +50 -0
- data/spec/grape_extensions/error_formatter_spec.rb +29 -0
- data/spec/grape_extensions/include_nil_spec.rb +23 -0
- data/spec/identity_spec.rb +50 -0
- data/spec/json_error_spec.rb +33 -0
- data/spec/logger/log_transaction_spec.rb +34 -0
- data/spec/logger/logger_spec.rb +14 -0
- data/spec/logger/parseable_spec.rb +16 -0
- data/spec/middleware/authentication_spec.rb +54 -0
- data/spec/middleware/database_stats_spec.rb +64 -0
- data/spec/middleware/request_stats_spec.rb +21 -0
- data/spec/sortable_api_spec.rb +56 -0
- data/spec/spec_helper.rb +45 -0
- data/spec/stats_d_timer_spec.rb +23 -0
- data/spec/stats_spec.rb +66 -0
- data/spec/version_spec.rb +40 -0
- data/tasks/spec.rake +9 -0
- data/tasks/version.rake +51 -0
- metadata +456 -0
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'service_template/generators/migration_generator'
|
3
|
+
require 'service_template/cli'
|
4
|
+
|
5
|
+
describe ServiceTemplate::Generators::MigrationGenerator do
|
6
|
+
|
7
|
+
let(:migration_filename) { 'foo' }
|
8
|
+
let(:test_migrations_directory) { 'spec/tmp' }
|
9
|
+
|
10
|
+
before do
|
11
|
+
allow_any_instance_of(described_class).to receive(:output_directory).and_return(test_migrations_directory)
|
12
|
+
allow_any_instance_of(described_class).to receive(:migration_filename).and_return(migration_filename)
|
13
|
+
end
|
14
|
+
|
15
|
+
after do
|
16
|
+
FileUtils.rm_rf(test_migrations_directory)
|
17
|
+
end
|
18
|
+
|
19
|
+
describe 'AddFooToBar flew:string:index brew:integer' do
|
20
|
+
before do
|
21
|
+
ServiceTemplate::CLI::Base.new.generate("migration", 'AddFooToBar', 'flew:string:index', 'brew:integer')
|
22
|
+
expected_migration_file = File.join(test_migrations_directory, 'foo.rb')
|
23
|
+
@migration_code = File.read(expected_migration_file)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'creates a camelized migration class' do
|
27
|
+
expect(@migration_code).to match(/class AddFooToBar/)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'adds the columns' do
|
31
|
+
expect(@migration_code).to match(/add_column :bars, :flew, :string/)
|
32
|
+
expect(@migration_code).to match(/add_column :bars, :brew, :integer/)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'adds the index on the correct column' do
|
36
|
+
expect(@migration_code).to match(/add_index :bars, :flew/)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe 'RemoveFooFromBar flew:string brew:integer' do
|
41
|
+
before do
|
42
|
+
ServiceTemplate::CLI::Base.new.generate("migration", 'RemoveFooFromBar', 'flew:string', 'brew:integer')
|
43
|
+
expected_migration_file = File.join(test_migrations_directory, 'foo.rb')
|
44
|
+
@migration_code = File.read(expected_migration_file)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'creates a camelized migration class' do
|
48
|
+
expect(@migration_code).to match(/class RemoveFooFromBar/)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'removes the columns' do
|
52
|
+
expect(@migration_code).to match(/remove_column :bars, :flew, :string/)
|
53
|
+
expect(@migration_code).to match(/remove_column :bars, :brew, :integer/)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe 'CreateJoinTableFooBar foo bar' do
|
58
|
+
before do
|
59
|
+
ServiceTemplate::CLI::Base.new.generate("migration", 'CreateJoinTableFooBar', 'foo', 'bar')
|
60
|
+
expected_migration_file = File.join(test_migrations_directory, 'foo.rb')
|
61
|
+
@migration_code = File.read(expected_migration_file)
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'creates a camelized migration class' do
|
65
|
+
expect(@migration_code).to match(/class CreateJoinTableFooBar/)
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'creates the join table' do
|
69
|
+
expect(@migration_code).to match(/create_join_table :foos, :bars/)
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'adds placeholders for the indexing' do
|
73
|
+
expect(@migration_code).to match(/# t.index \[:foo_id, :bar_id\]/)
|
74
|
+
expect(@migration_code).to match(/# t.index \[:bar_id, :foo_id\]/)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe 'CreateSkrillex drops:integer hair:string:index' do
|
79
|
+
before do
|
80
|
+
ServiceTemplate::CLI::Base.new.generate("migration", 'CreateSkrillex', 'drops:integer', 'hair:string:index')
|
81
|
+
expected_migration_file = File.join(test_migrations_directory, 'foo.rb')
|
82
|
+
@migration_code = File.read(expected_migration_file)
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'creates a camelized migration class' do
|
86
|
+
expect(@migration_code).to match(/class CreateSkrillex/)
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'creates the table' do
|
90
|
+
expect(@migration_code).to match(/create_table :skrillexes/)
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'adds the columns' do
|
94
|
+
expect(@migration_code).to match(/t.integer :drops/)
|
95
|
+
expect(@migration_code).to match(/t.string :hair/)
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'adds the index on the correct column' do
|
99
|
+
expect(@migration_code).to match(/add_index :skrillexes, :hair/)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe
|
104
|
+
|
105
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'service_template/generators/readme_generator'
|
3
|
+
require 'service_template/cli'
|
4
|
+
|
5
|
+
describe ServiceTemplate::Generators::ReadmeGenerator do
|
6
|
+
let(:test_readme_directory) { 'spec/tmp' }
|
7
|
+
|
8
|
+
before do
|
9
|
+
allow_any_instance_of(described_class).to receive(:output_directory).and_return(test_readme_directory)
|
10
|
+
ServiceTemplate::CLI::Base.new.generate("readme")
|
11
|
+
end
|
12
|
+
|
13
|
+
after do
|
14
|
+
FileUtils.rm_rf(test_readme_directory)
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'README' do
|
18
|
+
it 'creates a README in the current directory' do
|
19
|
+
expected_readme_file = File.join(test_readme_directory, 'README.md')
|
20
|
+
readme = File.read(expected_readme_file)
|
21
|
+
|
22
|
+
expect(readme).to match /# #{ServiceTemplate::Identity.name}/
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'spec' do
|
27
|
+
it 'creates a README spec' do
|
28
|
+
expected_spec_file = File.join(test_readme_directory, 'spec/docs/readme_spec.rb')
|
29
|
+
spec_code = File.read(expected_spec_file)
|
30
|
+
|
31
|
+
expect(spec_code).to match(/describe \'README\'/)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'service_template/generators/scaffold_generator'
|
3
|
+
require 'service_template/cli'
|
4
|
+
|
5
|
+
describe ServiceTemplate::Generators::ScaffoldGenerator do
|
6
|
+
let(:app_name) { 'my_test_app' }
|
7
|
+
let(:app_path) { 'spec/my_different_directory' }
|
8
|
+
let(:options) { {} }
|
9
|
+
|
10
|
+
before do
|
11
|
+
scaffold = ServiceTemplate::CLI::Base.new(args, options)
|
12
|
+
scaffold.invoke(:new)
|
13
|
+
end
|
14
|
+
|
15
|
+
after do
|
16
|
+
if args[1] # if app_path exists, delete dir at app_path
|
17
|
+
FileUtils.rm_rf(args[1])
|
18
|
+
else # otherwise delete dir at app_name
|
19
|
+
FileUtils.rm_rf(args[0])
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'given only an app name' do
|
24
|
+
let(:args) { [app_name] }
|
25
|
+
|
26
|
+
it 'creates a scaffold app in a directory that mirrors the app name' do
|
27
|
+
args = [app_name]
|
28
|
+
expect(Dir).to exist(app_name)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'given an app name and a directory' do
|
33
|
+
let(:args) { [app_name, app_path] }
|
34
|
+
|
35
|
+
it 'creates a scaffold app in a directory of my choosing' do
|
36
|
+
expect(Dir).to exist(app_path)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'creates a .env file with with a development database name' do
|
40
|
+
expect(File).to exist("#{app_path}/.env")
|
41
|
+
env_file = File.read("#{app_path}/.env")
|
42
|
+
expect(env_file).to match(/#{app_name}_development/)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'creates a .env.test file with a test database name' do
|
46
|
+
expect(File).to exist("#{app_path}/.env.test")
|
47
|
+
env_test_file = File.read("#{app_path}/.env.test")
|
48
|
+
expect(env_test_file).to match(/#{app_name}_test/)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'selects mysql as the default database adapter' do
|
52
|
+
database_config_file = File.read("#{app_path}/config/database.yml")
|
53
|
+
expect(database_config_file).to match(/adapter: mysql2/)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'adds the mysql2 gem in the Gemfile' do
|
57
|
+
gemfile = File.read("#{app_path}/Gemfile")
|
58
|
+
expect(gemfile).to match(/gem 'mysql2'/)
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'generates an application api' do
|
62
|
+
expect(File).to exist("#{app_path}/app/apis/application_api.rb")
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'the application API inherits from Grape::API' do
|
66
|
+
application_api_file = File.read("#{app_path}/app/apis/application_api.rb")
|
67
|
+
expect(application_api_file).to match(/class ApplicationApi < Grape::API/)
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'generates an example API and spec' do
|
71
|
+
expect(File).to exist("#{app_path}/app/apis/hello_api.rb")
|
72
|
+
expect(File).to exist("#{app_path}/spec/apis/hello_api_spec.rb")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'with the -d=pg option' do
|
77
|
+
let(:args) { [app_name, app_path] }
|
78
|
+
let(:options) { { :database => "pg" } }
|
79
|
+
|
80
|
+
it 'selects postres/pg as the database' do
|
81
|
+
database_config_file = File.read("#{app_path}/config/database.yml")
|
82
|
+
expect(database_config_file).to match(/adapter: postgresql/)
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'adds the pg gem in the Gemfile' do
|
86
|
+
gemfile = File.read("#{app_path}/Gemfile")
|
87
|
+
expect(gemfile).to match(/gem 'pg'/)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rack/test'
|
3
|
+
|
4
|
+
describe ServiceTemplate::GrapeExtenders do
|
5
|
+
include Rack::Test::Methods
|
6
|
+
|
7
|
+
before do
|
8
|
+
allow(ServiceTemplate::Logger.logger).to receive(:debug)
|
9
|
+
end
|
10
|
+
|
11
|
+
subject do
|
12
|
+
Class.new(Grape::API) do
|
13
|
+
format :json
|
14
|
+
extend ServiceTemplate::GrapeExtenders
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def app
|
19
|
+
subject
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'ActiveRecord extensions' do
|
23
|
+
it 'rescues ActiveRecord::RecordNotFound and returns a 404' do
|
24
|
+
subject.get do
|
25
|
+
raise ActiveRecord::RecordNotFound
|
26
|
+
end
|
27
|
+
|
28
|
+
get '/'
|
29
|
+
expect(response_code).to be 404
|
30
|
+
expect(parsed_response.error.code).to eq 'record_not_found'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe 'AASM extensions' do
|
35
|
+
it 'rescues AASM::InvalidTransition and returns a 422' do
|
36
|
+
module AASM
|
37
|
+
class InvalidTransition < StandardError
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
subject.get do
|
42
|
+
raise AASM::InvalidTransition
|
43
|
+
end
|
44
|
+
|
45
|
+
get '/'
|
46
|
+
expect(response_code).to be 422
|
47
|
+
expect(parsed_response.error.code).to eq 'unprocessable_entity'
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'service_template/grape_extensions/error_formatter'
|
3
|
+
|
4
|
+
describe Grape::ErrorFormatter::Json do
|
5
|
+
context '#call' do
|
6
|
+
it 'returns an api_error for plain error messages' do
|
7
|
+
error = Grape::ErrorFormatter::Json.call('test message', nil)
|
8
|
+
parsed = JSON.parse(error)
|
9
|
+
expect(parsed['error']['code']).to eq('api_error')
|
10
|
+
expect(parsed['error']['message']).to eq('test message')
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'returns a specified error when given a ServiceTemplate::JsonError object' do
|
14
|
+
json_error = ServiceTemplate::JsonError.new(:foo, 'bar')
|
15
|
+
error = Grape::ErrorFormatter::Json.call(json_error, nil)
|
16
|
+
parsed = JSON.parse(error)
|
17
|
+
expect(parsed['error']['code']).to eq('foo')
|
18
|
+
expect(parsed['error']['message']).to eq('bar')
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'adds the backtrace with rescue_option[:backtrace] specified' do
|
22
|
+
error = Grape::ErrorFormatter::Json.call('',
|
23
|
+
'backtrace',
|
24
|
+
rescue_options: {backtrace: true})
|
25
|
+
parsed = JSON.parse(error)
|
26
|
+
expect(parsed['backtrace']).to eq('backtrace')
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'service_template/output_formatters/include_nil'
|
3
|
+
require 'service_template/grape_extensions/grape_helpers'
|
4
|
+
require 'pry'
|
5
|
+
|
6
|
+
describe ServiceTemplate::Representable::IncludeNil do
|
7
|
+
class FooRepresenter < ServiceTemplate::Representer
|
8
|
+
include ServiceTemplate::Representable::IncludeNil
|
9
|
+
property :foo
|
10
|
+
property :bar
|
11
|
+
end
|
12
|
+
|
13
|
+
class DummyClass
|
14
|
+
include ServiceTemplate::GrapeHelpers
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'includes nil keys in a represented hash' do
|
18
|
+
input = OpenStruct.new(foo: 1, bar: nil)
|
19
|
+
output = DummyClass.new.represent(input, with: FooRepresenter).to_h
|
20
|
+
expect(output[:data]).to have_key('foo')
|
21
|
+
expect(output[:data]).to have_key('bar')
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'service_template/identity'
|
3
|
+
|
4
|
+
describe ServiceTemplate::Identity do
|
5
|
+
context '#name' do
|
6
|
+
it "returns 'api-service' if no ENV['SERVICE_NAME'] is set" do
|
7
|
+
ENV['SERVICE_NAME'] = nil
|
8
|
+
expect(ServiceTemplate::Identity.name).to eq('api-service')
|
9
|
+
end
|
10
|
+
|
11
|
+
it "returns the ENV['SERVICE_NAME'] when specified" do
|
12
|
+
ENV['SERVICE_NAME'] = nil
|
13
|
+
ENV['SERVICE_NAME'] = 'my-service'
|
14
|
+
expect(ServiceTemplate::Identity.name).to eq('my-service')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context '#hostname' do
|
19
|
+
it 'returns the value of the hostname system call and doesn\'t make a second system call' do
|
20
|
+
expect(ServiceTemplate::Identity).to receive(:`).with('hostname').and_return('system-hostname')
|
21
|
+
expect(ServiceTemplate::Identity.hostname).to eq('system-hostname')
|
22
|
+
|
23
|
+
expect(ServiceTemplate::Identity).to_not receive(:`).with('hostname')
|
24
|
+
expect(ServiceTemplate::Identity.hostname).to eq('system-hostname')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context '#revision' do
|
29
|
+
it 'returns the value of the \'git rev-parse HEAD\' system call and doesn\'t make a second system call' do
|
30
|
+
expect(ServiceTemplate::Identity).to receive(:`).with('git rev-parse HEAD').and_return('12345')
|
31
|
+
expect(ServiceTemplate::Identity.revision).to eq('12345')
|
32
|
+
|
33
|
+
expect(ServiceTemplate::Identity).to_not receive(:`).with('git rev-parse HEAD')
|
34
|
+
expect(ServiceTemplate::Identity.revision).to eq('12345')
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context '#pid' do
|
39
|
+
it 'returns the process ID value' do
|
40
|
+
allow(Process).to receive(:pid).and_return(112233)
|
41
|
+
expect(ServiceTemplate::Identity.pid).to eq(112233)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context '#platform_revision' do
|
46
|
+
it 'returns the current version of the platform gem' do
|
47
|
+
expect(ServiceTemplate::Identity.platform_revision).to eq(ServiceTemplate::VERSION)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'service_template/json_error'
|
3
|
+
|
4
|
+
describe ServiceTemplate::JsonError do
|
5
|
+
context '#to_json' do
|
6
|
+
it 'returns a json hash with the error data' do
|
7
|
+
error = ServiceTemplate::JsonError.new(:code, 'message').to_json
|
8
|
+
parsed = JSON.parse(error)
|
9
|
+
|
10
|
+
expect(parsed['error']['code']).to eq('code')
|
11
|
+
expect(parsed['error']['message']).to eq('message')
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'returns a json hash with additional reasons' do
|
15
|
+
error = ServiceTemplate::JsonError.new(:code, 'message', {foo: 'bar'}).to_json
|
16
|
+
parsed = JSON.parse(error)
|
17
|
+
|
18
|
+
expect(parsed['error']['code']).to eq('code')
|
19
|
+
expect(parsed['error']['message']).to eq('message')
|
20
|
+
expect(parsed['error']['reasons']['foo']).to eq('bar')
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'excludes `reasons` from hash if no reasons given' do
|
24
|
+
error = ServiceTemplate::JsonError.new(:code, 'message').to_json
|
25
|
+
parsed = JSON.parse(error)
|
26
|
+
|
27
|
+
expect(parsed['error']['code']).to eq('code')
|
28
|
+
expect(parsed['error']['message']).to eq('message')
|
29
|
+
expect(parsed['error']['reasons']).to be nil
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'service_template/logger/log_transaction'
|
3
|
+
|
4
|
+
describe ServiceTemplate::LogTransaction do
|
5
|
+
before(:each) do
|
6
|
+
ServiceTemplate::LogTransaction.clear
|
7
|
+
end
|
8
|
+
|
9
|
+
context '#id' do
|
10
|
+
it 'returns the current transaction id if it has been set' do
|
11
|
+
id = SecureRandom.hex(10)
|
12
|
+
Thread.current[:service_template_tid] = id
|
13
|
+
expect(ServiceTemplate::LogTransaction.id).to eq(id)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'sets and returns a new id if the transaction id hasn\'t been set' do
|
17
|
+
expect(ServiceTemplate::LogTransaction.id).to_not be_nil
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'allows the id to be overridden by a setter' do
|
21
|
+
expect(ServiceTemplate::LogTransaction.id).to_not be_nil
|
22
|
+
ServiceTemplate::LogTransaction.id = 'foo'
|
23
|
+
expect(ServiceTemplate::LogTransaction.id).to eq('foo')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context '#clear' do
|
28
|
+
it 'sets the id to nil' do
|
29
|
+
expect(ServiceTemplate::LogTransaction.id).to_not be_nil
|
30
|
+
ServiceTemplate::LogTransaction.clear
|
31
|
+
expect(Thread.current[:service_template_tid]).to be_nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|