napa 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +14 -0
  3. data/README.md +41 -8
  4. data/Rakefile +1 -0
  5. data/docs/grape_entity_to_roar.md +110 -0
  6. data/docs/quickstart.md +29 -3
  7. data/lib/napa/active_record_extensions/filter_by_hash.rb +1 -0
  8. data/lib/napa/active_record_extensions/seeder.rb +14 -0
  9. data/lib/napa/cli.rb +28 -2
  10. data/lib/napa/deploy.rb +98 -0
  11. data/lib/napa/deprecations/active_support_behavior.rb +8 -0
  12. data/lib/napa/deprecations/application_api.rb +9 -0
  13. data/lib/napa/deprecations/grape_entity.rb +5 -0
  14. data/lib/napa/deprecations/napa_setup.rb +38 -0
  15. data/lib/napa/deprecations.rb +13 -0
  16. data/lib/napa/gem_dependency.rb +37 -0
  17. data/lib/napa/generators/migration_generator.rb +199 -2
  18. data/lib/napa/generators/readme_generator.rb +47 -0
  19. data/lib/napa/generators/templates/create_table_migration/%migration_filename%.rb.tt +19 -0
  20. data/lib/napa/generators/templates/migration/%migration_filename%.rb.tt +36 -1
  21. data/lib/napa/generators/templates/readme/README.md.tt +55 -0
  22. data/lib/napa/generators/templates/readme/spec/docs/readme_spec.rb +7 -0
  23. data/lib/napa/generators/templates/scaffold/.env.test.tt +3 -0
  24. data/lib/napa/generators/templates/scaffold/.env.tt +3 -0
  25. data/lib/napa/generators/templates/scaffold/.gitignore.tt +1 -0
  26. data/lib/napa/generators/templates/scaffold/Rakefile +2 -1
  27. data/lib/napa/generators/templates/scaffold/app.rb +1 -1
  28. data/lib/napa/generators/templates/scaffold/config.ru.tt +3 -2
  29. data/lib/napa/generators/templates/scaffold/log/{.gitkeep → .keep} +0 -0
  30. data/lib/napa/generators.rb +1 -0
  31. data/lib/napa/middleware/logger.rb +1 -1
  32. data/lib/napa/middleware/request_stats.rb +0 -2
  33. data/lib/napa/output_formatters/entity.rb +4 -0
  34. data/lib/napa/rspec_extensions/response_helpers.rb +29 -0
  35. data/lib/napa/setup.rb +20 -0
  36. data/lib/napa/stats_d_timer.rb +1 -1
  37. data/lib/napa/version.rb +1 -1
  38. data/lib/napa.rb +19 -1
  39. data/lib/tasks/db.rake +7 -0
  40. data/lib/tasks/deploy.rake +2 -4
  41. data/lib/tasks/routes.rake +4 -3
  42. data/napa.gemspec +2 -1
  43. data/spec/active_record_extensions/filter_by_hash_spec.rb +5 -3
  44. data/spec/active_record_extensions/seeder_spec.rb +13 -0
  45. data/spec/authentication_spec.rb +3 -3
  46. data/spec/deprecations/application_api_spec.rb +19 -0
  47. data/spec/deprecations/entity_spec.rb +9 -0
  48. data/spec/deprecations/filter_by_hash_spec.rb +9 -0
  49. data/spec/deprecations/napa_setup_spec.rb +52 -0
  50. data/spec/generators/api_generator_spec.rb +1 -1
  51. data/spec/generators/migration_generator_spec.rb +86 -8
  52. data/spec/generators/readme_generator_spec.rb +35 -0
  53. data/spec/grape_extensions/error_formatter_spec.rb +6 -6
  54. data/spec/identity_spec.rb +13 -13
  55. data/spec/json_error_spec.rb +3 -3
  56. data/spec/logger/log_transaction_spec.rb +6 -6
  57. data/spec/middleware/authentication_spec.rb +8 -8
  58. data/spec/middleware/database_stats_spec.rb +8 -8
  59. data/spec/middleware/request_stats_spec.rb +2 -11
  60. data/spec/spec_helper.rb +11 -2
  61. data/spec/stats_d_timer_spec.rb +23 -0
  62. data/spec/stats_spec.rb +2 -2
  63. data/spec/version_spec.rb +6 -6
  64. metadata +35 -9
  65. data/lib/tasks/git.rake +0 -53
data/napa.gemspec CHANGED
@@ -14,6 +14,7 @@ Gem::Specification.new do |gem|
14
14
  gem.name = "napa"
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Napa::VERSION
17
+ gem.required_ruby_version = '>= 2.0'
17
18
 
18
19
 
19
20
  gem.add_dependency 'rake'
@@ -27,10 +28,10 @@ Gem::Specification.new do |gem|
27
28
  gem.add_dependency 'roar'
28
29
  gem.add_dependency 'statsd-ruby'
29
30
  gem.add_dependency 'racksh'
31
+ gem.add_dependency 'git'
30
32
 
31
33
  gem.add_development_dependency 'rspec'
32
34
  gem.add_development_dependency 'pry'
33
- gem.add_development_dependency 'git'
34
35
  gem.add_development_dependency 'rubocop'
35
36
  gem.add_development_dependency 'activerecord', '~>3.2.2'
36
37
  gem.add_development_dependency 'sqlite3'
@@ -2,11 +2,13 @@ require 'active_record'
2
2
  require 'spec_helper'
3
3
  require 'napa/active_record_extensions/filter_by_hash'
4
4
 
5
- class Foo < ActiveRecord::Base
6
- include Napa::FilterByHash
7
- end
8
5
 
9
6
  describe Napa::FilterByHash do
7
+ before do
8
+ expect(ActiveSupport::Deprecation).to receive(:warn)
9
+ class Foo < ActiveRecord::Base; include Napa::FilterByHash; end
10
+ end
11
+
10
12
  context 'when a hash is provided' do
11
13
  it 'returns an AR relation' do
12
14
  expect(Foo.filter).to be_a(ActiveRecord::Relation)
@@ -0,0 +1,13 @@
1
+ require 'active_record'
2
+ require 'spec_helper'
3
+ require 'napa/active_record_extensions/seeder'
4
+
5
+ describe Napa::ActiveRecordSeeder do
6
+
7
+ context 'when a valid file is not provided' do
8
+ it 'raises an exception' do
9
+ expect{Napa::ActiveRecordSeeder.load_file 'not-a-file'}.to raise_error
10
+ end
11
+ end
12
+
13
+ end
@@ -5,8 +5,8 @@ describe Napa::Authentication do
5
5
  context '#password_header' do
6
6
  it "returns a password hash for the request header" do
7
7
  ENV['HEADER_PASSWORD'] = 'foo'
8
- Napa::Authentication.password_header.class.should eq(Hash)
9
- Napa::Authentication.password_header.should eq('Password' => 'foo')
8
+ expect(Napa::Authentication.password_header.class).to eq(Hash)
9
+ expect(Napa::Authentication.password_header).to eq('Password' => 'foo')
10
10
  end
11
11
 
12
12
  it 'raises when the HEADER_PASSWORD env var is not defined' do
@@ -14,4 +14,4 @@ describe Napa::Authentication do
14
14
  expect{Napa::Authentication.password_header}.to raise_error
15
15
  end
16
16
  end
17
- end
17
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+ require 'active_support'
3
+ require 'napa/deprecations/application_api'
4
+
5
+ describe Napa::Deprecations do
6
+ describe '.application_api_check' do
7
+ it 'does not raise a deprecation warning if the file exists' do
8
+ allow(File).to receive(:exists?).and_return(true)
9
+ expect(ActiveSupport::Deprecation).to_not receive(:warn)
10
+ Napa::Deprecations.application_api_check
11
+ end
12
+
13
+ it 'raises a deprecation warning if the file is missing' do
14
+ allow(File).to receive(:exists?).and_return(false)
15
+ expect(ActiveSupport::Deprecation).to receive(:warn)
16
+ Napa::Deprecations.application_api_check
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+ require 'active_support'
3
+
4
+ describe "Napa::Entity Deprecation" do
5
+ it 'raises a deprecation warning when a class inherits' do
6
+ expect(ActiveSupport::Deprecation).to receive(:warn)
7
+ class FooEntity < Napa::Entity; end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Napa::FilterByHash Deprecation" do
4
+ it 'raises a deprecation warning when mixed in to a class' do
5
+ expect(ActiveSupport::Deprecation).to receive(:warn)
6
+ class FooModel; include Napa::FilterByHash; end
7
+ ActiveSupport::Deprecation.silenced = false
8
+ end
9
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+ require 'active_support'
3
+ require 'napa/deprecations/napa_setup'
4
+
5
+ describe Napa::Deprecations do
6
+ before do
7
+ allow(File).to receive(:exists?).and_return(true)
8
+
9
+ @apprb_stub = [
10
+ "require 'bundler/setup'",
11
+ "Bundler.setup(:default)",
12
+ "require 'napa/setup'",
13
+ "Bundler.require(:default, Napa.env.to_sym)",
14
+ "require 'napa'",
15
+ "Napa.load_environment",
16
+ "Dir['./config/initializers/**/*.rb'].map { |file| require file }",
17
+ "Dir['./config/middleware/**/*.rb'].map { |file| require file }",
18
+ "relative_load_paths = %w(app/apis app/entities app/models app/workers app/representers lib)",
19
+ "ActiveSupport::Dependencies.autoload_paths += relative_load_paths"
20
+ ]
21
+ end
22
+
23
+ describe '.napa_setup_check' do
24
+ it 'does not raise a deprecation warning if all the required_patterns are matched' do
25
+ allow(File).to receive(:readlines).with('./app.rb').and_return(@apprb_stub)
26
+ expect(ActiveSupport::Deprecation).to_not receive(:warn)
27
+ Napa::Deprecations.napa_setup_check
28
+ end
29
+
30
+ it 'raises a deprecation warning if any of the required_patterns are missing' do
31
+ (0..@apprb_stub.count - 1).each do |line_num|
32
+ apprb_missing_line = @apprb_stub
33
+ apprb_missing_line.delete_at(line_num)
34
+ allow(File).to receive(:readlines).with('./app.rb').and_return(apprb_missing_line)
35
+ expect(ActiveSupport::Deprecation).to receive(:warn)
36
+
37
+ Napa::Deprecations.napa_setup_check
38
+ end
39
+ end
40
+
41
+ it 'raises a deprecation warning if any of the expired_patterns are matched' do
42
+ Napa::Deprecations::EXPIRED_PATTERNS.each do |pattern|
43
+ app_rb_stub = [pattern]
44
+
45
+ allow(File).to receive(:readlines).with('./app.rb').and_return(app_rb_stub)
46
+ expect(ActiveSupport::Deprecation).to receive(:warn)
47
+
48
+ Napa::Deprecations.napa_setup_check
49
+ end
50
+ end
51
+ end
52
+ end
@@ -7,7 +7,7 @@ describe Napa::Generators::ApiGenerator do
7
7
  let(:test_api_directory) { 'spec/tmp' }
8
8
 
9
9
  before do
10
- described_class.any_instance.stub(:output_directory) { test_api_directory }
10
+ allow_any_instance_of(described_class).to receive(:output_directory).and_return(test_api_directory)
11
11
  Napa::CLI::Base.new.generate("api", api_name)
12
12
  end
13
13
 
@@ -4,24 +4,102 @@ require 'napa/cli'
4
4
 
5
5
  describe Napa::Generators::MigrationGenerator do
6
6
 
7
- let(:migration_name) { 'foo_bars' }
7
+ let(:migration_filename) { 'foo' }
8
8
  let(:test_migrations_directory) { 'spec/tmp' }
9
9
 
10
10
  before do
11
- described_class.any_instance.stub(:output_directory) { test_migrations_directory }
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)
12
13
  end
13
14
 
14
15
  after do
15
16
  FileUtils.rm_rf(test_migrations_directory)
16
17
  end
17
18
 
18
- it 'creates a camelized migration class' do
19
- described_class.any_instance.stub(:migration_filename) { 'foo' }
20
- Napa::CLI::Base.new.generate("migration", migration_name)
21
- expected_migration_file = File.join(test_migrations_directory, 'foo.rb')
22
- migration_code = File.read(expected_migration_file)
23
- expect(migration_code).to match(/class FooBars/)
19
+ describe 'AddFooToBar flew:string:index brew:integer' do
20
+ before do
21
+ Napa::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
+ Napa::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
+ Napa::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
+ Napa::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
24
101
  end
25
102
 
103
+ describe
26
104
 
27
105
  end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+ require 'napa/generators/readme_generator'
3
+ require 'napa/cli'
4
+
5
+ describe Napa::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
+ Napa::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 /# #{Napa::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
@@ -6,16 +6,16 @@ describe Grape::ErrorFormatter::Json do
6
6
  it 'returns an api_error for plain error messages' do
7
7
  error = Grape::ErrorFormatter::Json.call('test message', nil)
8
8
  parsed = JSON.parse(error)
9
- parsed['error']['code'].should eq('api_error')
10
- parsed['error']['message'].should eq('test message')
9
+ expect(parsed['error']['code']).to eq('api_error')
10
+ expect(parsed['error']['message']).to eq('test message')
11
11
  end
12
12
 
13
13
  it 'returns a specified error when given a Napa::JsonError object' do
14
14
  json_error = Napa::JsonError.new(:foo, 'bar')
15
15
  error = Grape::ErrorFormatter::Json.call(json_error, nil)
16
16
  parsed = JSON.parse(error)
17
- parsed['error']['code'].should eq('foo')
18
- parsed['error']['message'].should eq('bar')
17
+ expect(parsed['error']['code']).to eq('foo')
18
+ expect(parsed['error']['message']).to eq('bar')
19
19
  end
20
20
 
21
21
  it 'adds the backtrace with rescue_option[:backtrace] specified' do
@@ -23,7 +23,7 @@ describe Grape::ErrorFormatter::Json do
23
23
  'backtrace',
24
24
  rescue_options: {backtrace: true})
25
25
  parsed = JSON.parse(error)
26
- parsed['backtrace'].should eq('backtrace')
26
+ expect(parsed['backtrace']).to eq('backtrace')
27
27
  end
28
28
  end
29
- end
29
+ end
@@ -5,46 +5,46 @@ describe Napa::Identity do
5
5
  context '#name' do
6
6
  it "returns 'api-service' if no ENV['SERVICE_NAME'] is set" do
7
7
  ENV['SERVICE_NAME'] = nil
8
- Napa::Identity.name.should == 'api-service'
8
+ expect(Napa::Identity.name).to eq('api-service')
9
9
  end
10
10
 
11
11
  it "returns the ENV['SERVICE_NAME'] when specified" do
12
12
  ENV['SERVICE_NAME'] = nil
13
13
  ENV['SERVICE_NAME'] = 'my-service'
14
- Napa::Identity.name.should eq('my-service')
14
+ expect(Napa::Identity.name).to eq('my-service')
15
15
  end
16
16
  end
17
17
 
18
18
  context '#hostname' do
19
19
  it 'returns the value of the hostname system call and doesn\'t make a second system call' do
20
- Napa::Identity.should_receive(:`).with('hostname').and_return('system-hostname')
21
- Napa::Identity.hostname.should eq('system-hostname')
20
+ expect(Napa::Identity).to receive(:`).with('hostname').and_return('system-hostname')
21
+ expect(Napa::Identity.hostname).to eq('system-hostname')
22
22
 
23
- Napa::Identity.should_not_receive(:`).with('hostname')
24
- Napa::Identity.hostname.should eq('system-hostname')
23
+ expect(Napa::Identity).to_not receive(:`).with('hostname')
24
+ expect(Napa::Identity.hostname).to eq('system-hostname')
25
25
  end
26
26
  end
27
27
 
28
28
  context '#revision' do
29
29
  it 'returns the value of the \'git rev-parse HEAD\' system call and doesn\'t make a second system call' do
30
- Napa::Identity.should_receive(:`).with('git rev-parse HEAD').and_return('12345')
31
- Napa::Identity.revision.should eq('12345')
30
+ expect(Napa::Identity).to receive(:`).with('git rev-parse HEAD').and_return('12345')
31
+ expect(Napa::Identity.revision).to eq('12345')
32
32
 
33
- Napa::Identity.should_not_receive(:`).with('git rev-parse HEAD')
34
- Napa::Identity.revision.should eq('12345')
33
+ expect(Napa::Identity).to_not receive(:`).with('git rev-parse HEAD')
34
+ expect(Napa::Identity.revision).to eq('12345')
35
35
  end
36
36
  end
37
37
 
38
38
  context '#pid' do
39
39
  it 'returns the process ID value' do
40
- Process.stub(:pid).and_return(112233)
41
- Napa::Identity.pid.should be(112233)
40
+ allow(Process).to receive(:pid).and_return(112233)
41
+ expect(Napa::Identity.pid).to eq(112233)
42
42
  end
43
43
  end
44
44
 
45
45
  context '#platform_revision' do
46
46
  it 'returns the current version of the platform gem' do
47
- Napa::Identity.platform_revision.should == Napa::VERSION
47
+ expect(Napa::Identity.platform_revision).to eq(Napa::VERSION)
48
48
  end
49
49
  end
50
50
  end
@@ -7,8 +7,8 @@ describe Napa::JsonError do
7
7
  error = Napa::JsonError.new(:code, 'message').to_json
8
8
  parsed = JSON.parse(error)
9
9
 
10
- parsed['error']['code'].should eq('code')
11
- parsed['error']['message'].should eq('message')
10
+ expect(parsed['error']['code']).to eq('code')
11
+ expect(parsed['error']['message']).to eq('message')
12
12
  end
13
13
  end
14
- end
14
+ end
@@ -10,25 +10,25 @@ describe Napa::LogTransaction do
10
10
  it 'returns the current transaction id if it has been set' do
11
11
  id = SecureRandom.hex(10)
12
12
  Thread.current[:napa_tid] = id
13
- Napa::LogTransaction.id.should == id
13
+ expect(Napa::LogTransaction.id).to eq(id)
14
14
  end
15
15
 
16
16
  it 'sets and returns a new id if the transaction id hasn\'t been set' do
17
- Napa::LogTransaction.id.should_not be_nil
17
+ expect(Napa::LogTransaction.id).to_not be_nil
18
18
  end
19
19
 
20
20
  it 'allows the id to be overridden by a setter' do
21
- Napa::LogTransaction.id.should_not be_nil
21
+ expect(Napa::LogTransaction.id).to_not be_nil
22
22
  Napa::LogTransaction.id = 'foo'
23
- Napa::LogTransaction.id.should == 'foo'
23
+ expect(Napa::LogTransaction.id).to eq('foo')
24
24
  end
25
25
  end
26
26
 
27
27
  context '#clear' do
28
28
  it 'sets the id to nil' do
29
- Napa::LogTransaction.id.should_not be_nil
29
+ expect(Napa::LogTransaction.id).to_not be_nil
30
30
  Napa::LogTransaction.clear
31
- Thread.current[:napa_tid].should be_nil
31
+ expect(Thread.current[:napa_tid]).to be_nil
32
32
  end
33
33
  end
34
34
  end
@@ -14,7 +14,7 @@ describe Napa::Identity do
14
14
  env = Rack::MockRequest.env_for('/test', {'HTTP_PASSWORD' => 'foo'})
15
15
  status, headers, body = middleware.call(env)
16
16
 
17
- status.should be(200)
17
+ expect(status).to eq(200)
18
18
  end
19
19
  end
20
20
 
@@ -25,8 +25,8 @@ describe Napa::Identity do
25
25
  env = Rack::MockRequest.env_for('/test')
26
26
  status, headers, body = middleware.call(env)
27
27
 
28
- status.should be(401)
29
- body.should eq([Napa::JsonError.new('bad_password', 'bad password').to_json])
28
+ expect(status).to eq(401)
29
+ expect(body).to eq([Napa::JsonError.new('bad_password', 'bad password').to_json])
30
30
  end
31
31
 
32
32
  it 'returns an error message if an incorrect Password header is supplied' do
@@ -35,8 +35,8 @@ describe Napa::Identity do
35
35
  env = Rack::MockRequest.env_for('/test', {'HTTP_PASSWORD' => 'incorrect'})
36
36
  status, headers, body = middleware.call(env)
37
37
 
38
- status.should be(401)
39
- body.should eq([Napa::JsonError.new('bad_password', 'bad password').to_json])
38
+ expect(status).to eq(401)
39
+ expect(body).to eq([Napa::JsonError.new('bad_password', 'bad password').to_json])
40
40
  end
41
41
 
42
42
  it 'returns an error message if HEADER_PASSWORDS is not configured' do
@@ -47,8 +47,8 @@ describe Napa::Identity do
47
47
  env = Rack::MockRequest.env_for('/test', {'HTTP_PASSWORD' => 'incorrect'})
48
48
  status, headers, body = middleware.call(env)
49
49
 
50
- status.should be(401)
51
- body.should eq([Napa::JsonError.new('not_configured', 'password not configured').to_json])
50
+ expect(status).to eq(401)
51
+ expect(body).to eq([Napa::JsonError.new('not_configured', 'password not configured').to_json])
52
52
  end
53
53
  end
54
- end
54
+ end
@@ -35,29 +35,29 @@ describe Napa::Middleware::DatabaseStats do
35
35
  end
36
36
 
37
37
  it 'should send a query_time for an insert' do
38
- Napa::Stats.emitter.should_receive(:timing).with('sql.query_time', an_instance_of(Float))
39
- Napa::Stats.emitter.should_receive(:timing).with('sql.table.foos.insert.query_time', an_instance_of(Float))
38
+ allow(Napa::Stats.emitter).to receive(:timing).with('sql.query_time', an_instance_of(Float))
39
+ allow(Napa::Stats.emitter).to receive(:timing).with('sql.table.foos.insert.query_time', an_instance_of(Float))
40
40
 
41
41
  @app = lambda { |env| [200, { 'Content-Type' => 'application/json' }, Foo.create(word: 'baz')] }
42
42
  end
43
43
 
44
44
  it 'should send a query_time for a select' do
45
- Napa::Stats.emitter.should_receive(:timing).with('sql.query_time', an_instance_of(Float))
46
- Napa::Stats.emitter.should_receive(:timing).with('sql.table.foos.select.query_time', an_instance_of(Float))
45
+ allow(Napa::Stats.emitter).to receive(:timing).with('sql.query_time', an_instance_of(Float))
46
+ allow(Napa::Stats.emitter).to receive(:timing).with('sql.table.foos.select.query_time', an_instance_of(Float))
47
47
 
48
48
  @app = lambda { |env| [200, { 'Content-Type' => 'application/json' }, Foo.first] }
49
49
  end
50
50
 
51
51
  it 'should send a query_time for a delete' do
52
- Napa::Stats.emitter.should_receive(:timing).with('sql.query_time', an_instance_of(Float))
53
- Napa::Stats.emitter.should_receive(:timing).with('sql.table.foos.delete.query_time', an_instance_of(Float))
52
+ allow(Napa::Stats.emitter).to receive(:timing).with('sql.query_time', an_instance_of(Float))
53
+ allow(Napa::Stats.emitter).to receive(:timing).with('sql.table.foos.delete.query_time', an_instance_of(Float))
54
54
 
55
55
  @app = lambda { |env| [200, { 'Content-Type' => 'application/json' }, @foo.delete ] }
56
56
  end
57
57
 
58
58
  it 'should send a query_time for an update' do
59
- Napa::Stats.emitter.should_receive(:timing).with('sql.query_time', an_instance_of(Float))
60
- Napa::Stats.emitter.should_receive(:timing).with('sql.table.foos.update.query_time', an_instance_of(Float))
59
+ allow(Napa::Stats.emitter).to receive(:timing).with('sql.query_time', an_instance_of(Float))
60
+ allow(Napa::Stats.emitter).to receive(:timing).with('sql.table.foos.update.query_time', an_instance_of(Float))
61
61
 
62
62
  @app = lambda { |env| [200, { 'Content-Type' => 'application/json' }, @foo.update_attributes(word: 'baz') ] }
63
63
  end
@@ -9,18 +9,9 @@ describe Napa::Middleware::RequestStats do
9
9
  ENV['STATSD_PORT'] = '8125'
10
10
  end
11
11
 
12
- it 'should increment api_requests counter' do
13
- Napa::Stats.emitter.should_receive(:increment).with('request_count')
14
- Napa::Stats.emitter.should_receive(:increment).with('path.get.test.path.request_count')
15
- app = lambda { |env| [200, { 'Content-Type' => 'application/json' }, Array.new] }
16
- middleware = Napa::Middleware::RequestStats.new(app)
17
- env = Rack::MockRequest.env_for('/test/path')
18
- middleware.call(env)
19
- end
20
-
21
12
  it 'should send the api_response_time' do
22
- Napa::Stats.emitter.should_receive(:timing).with('response_time', an_instance_of(Float))
23
- Napa::Stats.emitter.should_receive(:timing).with('path.get.test.path.response_time', an_instance_of(Float))
13
+ expect(Napa::Stats.emitter).to receive(:timing).with('response_time', an_instance_of(Float))
14
+ expect(Napa::Stats.emitter).to receive(:timing).with('path.get.test.path.response_time', an_instance_of(Float))
24
15
  app = lambda { |env| [200, { 'Content-Type' => 'application/json'}, Array.new] }
25
16
  middleware = Napa::Middleware::RequestStats.new(app)
26
17
  env = Rack::MockRequest.env_for('/test/path')
data/spec/spec_helper.rb CHANGED
@@ -1,11 +1,20 @@
1
- require 'napa'
2
-
3
1
  ENV['RACK_ENV'] = 'test'
4
2
 
3
+ require 'napa/setup'
4
+
5
+ Napa.skip_initialization = true
6
+
7
+ require 'napa'
8
+
5
9
  # from https://gist.github.com/adamstegman/926858
6
10
  RSpec.configure do |config|
7
11
  config.before(:all) { silence_output }
8
12
  config.after(:all) { enable_output }
13
+
14
+ config.before(:each) do
15
+ allow(Napa).to receive(:initialize)
16
+ allow(Napa::Logger).to receive_message_chain('logger.info').with(:napa_deprecation_warning)
17
+ end
9
18
  end
10
19
 
11
20
  # Redirects stderr and stdout to /dev/null.
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+ require 'napa/stats_d_timer'
3
+
4
+ class FooTimer
5
+ include Napa::StatsDTimer
6
+ end
7
+
8
+ describe Napa::StatsDTimer do
9
+ before do
10
+ # Delete any prevous instantiations of the emitter
11
+ Napa::Stats.emitter = nil
12
+ # Stub out logging since there is no log to output to
13
+ allow(Napa::Logger).to receive_message_chain(:logger, :warn)
14
+ end
15
+
16
+ it 'logs a timing event based on how long the block takes' do
17
+ expect(Napa::Stats.emitter).to receive(:timing).with('foo', an_instance_of(Float))
18
+ FooTimer.report_time('foo') do
19
+ sleep(0.1)
20
+ end
21
+ end
22
+
23
+ end
data/spec/stats_spec.rb CHANGED
@@ -6,14 +6,14 @@ describe Napa::Stats do
6
6
  # Delete any prevous instantiations of the emitter
7
7
  Napa::Stats.emitter = nil
8
8
  # Stub out logging since there is no log to output to
9
- Napa::Logger.stub_chain(:logger, :warn)
9
+ allow(Napa::Logger).to receive_message_chain(:logger, :warn)
10
10
  end
11
11
 
12
12
  it 'should log an error if StatsD env variables are not configured' do
13
13
  ENV['STATSD_HOST'] = nil
14
14
  ENV['STATSD_PORT'] = nil
15
15
  message = 'StatsD host and port not configured in environment variables, using default settings'
16
- Napa::Logger.logger.should_receive(:warn).with(message)
16
+ expect(Napa::Logger.logger).to receive(:warn).with(message)
17
17
  Napa::Stats.emitter
18
18
  end
19
19