-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from trusted/rspec-helpers
Allow enabling/disabling IronTrail in rspec
- Loading branch information
Showing
6 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
if ENV['RAILS_ENV'] == 'production' | ||
raise 'This file should not be required in production. ' \ | ||
'Change the RAILS_ENV env var temporarily to override this.' | ||
end | ||
|
||
require 'iron_trail' | ||
|
||
module IronTrail | ||
module Testing | ||
class << self | ||
attr_accessor :enabled | ||
|
||
def enable! | ||
DbFunctions.new(ActiveRecord::Base.connection).install_functions | ||
@enabled = true | ||
end | ||
|
||
def disable! | ||
# We "disable" it by replacing the trigger function by a no-op one. | ||
# This should be faster than adding/removing triggers from several | ||
# tables every time. | ||
sql = <<~SQL | ||
CREATE OR REPLACE FUNCTION irontrail_log_row() | ||
RETURNS TRIGGER AS $$ | ||
BEGIN | ||
RETURN NULL; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
SQL | ||
|
||
ActiveRecord::Base.connection.execute(sql) | ||
@enabled = false | ||
end | ||
|
||
def with_iron_trail(want_enabled:, &block) | ||
was_enabled = IronTrail::Testing.enabled | ||
|
||
if want_enabled | ||
::IronTrail::Testing.enable! unless was_enabled | ||
else | ||
::IronTrail::Testing.disable! if was_enabled | ||
end | ||
|
||
block.call | ||
ensure | ||
if want_enabled && !was_enabled | ||
::IronTrail::Testing.disable! | ||
elsif !want_enabled && was_enabled | ||
::IronTrail::Testing.enable! | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
RSpec.configure do |config| | ||
config.around(:each, iron_trail: true) do |example| | ||
IronTrail::Testing.with_iron_trail(want_enabled: true) { example.run } | ||
end | ||
config.around(:each, iron_trail: false) do |example| | ||
raise "Using iron_trail: false does not do what you might think it does. To disable iron_trail, " \ | ||
"use IronTrail::Testing.with_iron_trail(want_enabled: false) { ... } instead." | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'iron_trail/testing/rspec' | ||
|
||
IronTrail::Testing.disable! | ||
|
||
RSpec.describe 'lib/iron_trail/testing/rspec.rb' do | ||
let(:person) { Person.create!(first_name: 'Arthur', last_name: 'Schopenhauer') } | ||
|
||
subject(:do_some_changes!) do | ||
person.update!(first_name: 'Jim') | ||
person.update!(first_name: 'Jane') | ||
end | ||
|
||
describe 'IronTrail::Testing#with_iron_trail' do | ||
context 'when IronTrail is disabled but we enable it for a while' do | ||
it 'tracks only while enabled' do | ||
person.update!(first_name: 'Jim') | ||
|
||
expect(person.reload.iron_trails.length).to be(0) | ||
|
||
IronTrail::Testing.with_iron_trail(want_enabled: true) do | ||
person.update!(first_name: 'Jane') | ||
end | ||
|
||
expect(person.reload.iron_trails.length).to be(1) | ||
|
||
person.update!(first_name: 'Joe') | ||
|
||
expect(person.reload.iron_trails.length).to be(1) | ||
end | ||
end | ||
end | ||
|
||
describe 'rspec helpers' do | ||
context 'with IronTrail disabled' do | ||
it 'does not track anything' do | ||
do_some_changes! | ||
|
||
expect(person.reload.iron_trails.length).to be(0) | ||
end | ||
end | ||
|
||
context 'with IronTrail enabled through the helper', iron_trail: true do | ||
it 'does not track anything' do | ||
do_some_changes! | ||
|
||
expect(person.reload.iron_trails.length).to be(3) | ||
end | ||
end | ||
end | ||
end |