1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

run ActiveRecord specs only if ActiveRecord is loaded

This commit is contained in:
Thorsten Böttger 2017-02-10 23:03:19 +11:00
parent 2dfca83995
commit 18b6187d6f
11 changed files with 1270 additions and 1257 deletions

View file

@ -1,44 +1,47 @@
require 'spec_helper'
require 'generator_spec'
require 'generators/active_record/aasm_generator'
describe ActiveRecord::Generators::AASMGenerator, type: :generator do
destination File.expand_path("../../../tmp", __FILE__)
if defined?(ActiveRecord)
require 'generator_spec'
require 'generators/active_record/aasm_generator'
describe ActiveRecord::Generators::AASMGenerator, type: :generator do
destination File.expand_path("../../../tmp", __FILE__)
before(:all) do
prepare_destination
end
it "creates model with aasm block for default column_name" do
run_generator %w(user)
assert_file "app/models/user.rb", /include AASM\n\n aasm do\n end\n/
end
it "creates model with aasm block for custom column_name" do
run_generator %w(user state)
assert_file "app/models/user.rb", /aasm :column => 'state' do\n end\n/
end
it "creates model with aasm block for namespaced model" do
run_generator %w(Admin::User state)
assert_file "app/models/admin/user.rb", /aasm :column => 'state' do\n end\n/
end
it "creates migration for model with aasm_column" do
run_generator %w(post)
assert_migration "db/migrate/aasm_create_posts.rb", /create_table(:posts) do |t|\n t.string :aasm_state\n/
end
it "add aasm_column in existing model" do
run_generator %w(job)
assert_file "app/models/job.rb"
run_generator %w(job)
assert_migration "db/migrate/add_aasm_state_to_jobs.rb"
end
it "add custom aasm_column in existing model" do
run_generator %w(job state)
assert_migration "db/migrate/add_state_to_jobs.rb"
end
before(:all) do
prepare_destination
end
it "creates model with aasm block for default column_name" do
run_generator %w(user)
assert_file "app/models/user.rb", /include AASM\n\n aasm do\n end\n/
end
it "creates model with aasm block for custom column_name" do
run_generator %w(user state)
assert_file "app/models/user.rb", /aasm :column => 'state' do\n end\n/
end
it "creates model with aasm block for namespaced model" do
run_generator %w(Admin::User state)
assert_file "app/models/admin/user.rb", /aasm :column => 'state' do\n end\n/
end
it "creates migration for model with aasm_column" do
run_generator %w(post)
assert_migration "db/migrate/aasm_create_posts.rb", /create_table(:posts) do |t|\n t.string :aasm_state\n/
end
it "add aasm_column in existing model" do
run_generator %w(job)
assert_file "app/models/job.rb"
run_generator %w(job)
assert_migration "db/migrate/add_aasm_state_to_jobs.rb"
end
it "add custom aasm_column in existing model" do
run_generator %w(job state)
assert_migration "db/migrate/add_state_to_jobs.rb"
end
end

View file

@ -1,5 +1,3 @@
require 'active_record'
class InvalidPersistor < ActiveRecord::Base
include AASM
aasm :column => :status, :skip_validation_on_save => true do

View file

@ -1,5 +1,3 @@
require 'active_record'
class SilentPersistor < ActiveRecord::Base
include AASM
aasm :column => :status, :whiny_persistence => false do

View file

@ -1,5 +1,3 @@
require 'active_record'
class Transactor < ActiveRecord::Base
belongs_to :worker

View file

@ -1,5 +1,3 @@
require 'active_record'
class Validator < ActiveRecord::Base
attr_accessor :after_all_transactions_performed,
:after_transaction_performed_on_fail,

View file

@ -0,0 +1,7 @@
# encoding: utf-8
begin
require 'active_record'
puts "active_record gem found, running ActiveRecord specs \e[32m#{'✔'}\e[0m"
rescue LoadError
puts "active_record gem not found, not running ActiveRecord specs \e[31m#{'✖'}\e[0m"
end

View file

@ -1,97 +1,100 @@
require 'spec_helper'
require 'models/default_state.rb'
require 'models/provided_state.rb'
require 'models/active_record/persisted_state.rb'
require 'models/active_record/provided_and_persisted_state.rb'
load_schema
if defined?(ActiveRecord)
require 'models/default_state.rb'
require 'models/provided_state.rb'
require 'models/active_record/persisted_state.rb'
require 'models/active_record/provided_and_persisted_state.rb'
describe "reading the current state" do
it "uses the AASM default" do
expect(DefaultState.new.aasm.current_state).to eql :alpha
end
load_schema
it "uses the provided method" do
expect(ProvidedState.new.aasm.current_state).to eql :beta
end
describe "reading the current state" do
it "uses the AASM default" do
expect(DefaultState.new.aasm.current_state).to eql :alpha
end
it "uses the persistence storage" do
expect(PersistedState.new.aasm.current_state).to eql :alpha
end
it "uses the provided method" do
expect(ProvidedState.new.aasm.current_state).to eql :beta
end
it "uses the provided method even if persisted" do
expect(ProvidedAndPersistedState.new.aasm.current_state).to eql :gamma
end
context "after dup" do
it "uses the persistence storage" do
source = PersistedState.create!
copy = source.dup
copy.save!
expect(PersistedState.new.aasm.current_state).to eql :alpha
end
copy.release!
it "uses the provided method even if persisted" do
expect(ProvidedAndPersistedState.new.aasm.current_state).to eql :gamma
end
expect(source.aasm_state).to eql 'alpha'
expect(source.aasm.current_state).to eql :alpha
context "after dup" do
it "uses the persistence storage" do
source = PersistedState.create!
copy = source.dup
copy.save!
source2 = PersistedState.find(source.id)
expect(source2.reload.aasm_state).to eql 'alpha'
expect(source2.aasm.current_state).to eql :alpha
copy.release!
expect(copy.aasm_state).to eql 'beta'
expect(copy.aasm.current_state).to eql :beta
expect(source.aasm_state).to eql 'alpha'
expect(source.aasm.current_state).to eql :alpha
source2 = PersistedState.find(source.id)
expect(source2.reload.aasm_state).to eql 'alpha'
expect(source2.aasm.current_state).to eql :alpha
expect(copy.aasm_state).to eql 'beta'
expect(copy.aasm.current_state).to eql :beta
end
end
end
describe "writing and persisting the current state" do
it "uses the AASM default" do
o = DefaultState.new
o.release!
expect(o.persisted_store).to be_nil
end
it "uses the provided method" do
o = ProvidedState.new
o.release!
expect(o.persisted_store).to eql :beta
end
it "uses the persistence storage" do
o = PersistedState.new
o.release!
expect(o.persisted_store).to be_nil
end
it "uses the provided method even if persisted" do
o = ProvidedAndPersistedState.new
o.release!
expect(o.persisted_store).to eql :beta
end
end
describe "writing the current state without persisting it" do
it "uses the AASM default" do
o = DefaultState.new
o.release
expect(o.transient_store).to be_nil
end
it "uses the provided method" do
o = ProvidedState.new
o.release
expect(o.transient_store).to eql :beta
end
it "uses the persistence storage" do
o = PersistedState.new
o.release
expect(o.transient_store).to be_nil
end
it "uses the provided method even if persisted" do
o = ProvidedAndPersistedState.new
o.release
expect(o.transient_store).to eql :beta
end
end
end
describe "writing and persisting the current state" do
it "uses the AASM default" do
o = DefaultState.new
o.release!
expect(o.persisted_store).to be_nil
end
it "uses the provided method" do
o = ProvidedState.new
o.release!
expect(o.persisted_store).to eql :beta
end
it "uses the persistence storage" do
o = PersistedState.new
o.release!
expect(o.persisted_store).to be_nil
end
it "uses the provided method even if persisted" do
o = ProvidedAndPersistedState.new
o.release!
expect(o.persisted_store).to eql :beta
end
end
describe "writing the current state without persisting it" do
it "uses the AASM default" do
o = DefaultState.new
o.release
expect(o.transient_store).to be_nil
end
it "uses the provided method" do
o = ProvidedState.new
o.release
expect(o.transient_store).to eql :beta
end
it "uses the persistence storage" do
o = PersistedState.new
o.release
expect(o.transient_store).to be_nil
end
it "uses the provided method even if persisted" do
o = ProvidedAndPersistedState.new
o.release
expect(o.transient_store).to eql :beta
end
end

View file

@ -1,76 +1,78 @@
require 'spec_helper'
require 'active_record'
require 'i18n'
I18n.enforce_available_locales = false
load_schema
if defined?(ActiceRecord)
require 'i18n'
describe AASM::Localizer, "new style" do
before(:all) do
I18n.load_path << 'spec/en.yml'
I18n.default_locale = :en
I18n.reload!
end
I18n.enforce_available_locales = false
load_schema
after(:all) do
I18n.load_path.clear
end
let (:foo_opened) { LocalizerTestModel.new }
let (:foo_closed) { LocalizerTestModel.new.tap { |x| x.aasm_state = :closed } }
context 'aasm.human_state' do
it 'should return translated state value' do
expect(foo_opened.aasm.human_state).to eq("It's open now!")
describe AASM::Localizer, "new style" do
before(:all) do
I18n.load_path << 'spec/en.yml'
I18n.default_locale = :en
I18n.reload!
end
it 'should return humanized value if not localized' do
expect(foo_closed.aasm.human_state).to eq("Closed")
after(:all) do
I18n.load_path.clear
end
let (:foo_opened) { LocalizerTestModel.new }
let (:foo_closed) { LocalizerTestModel.new.tap { |x| x.aasm_state = :closed } }
context 'aasm.human_state' do
it 'should return translated state value' do
expect(foo_opened.aasm.human_state).to eq("It's open now!")
end
it 'should return humanized value if not localized' do
expect(foo_closed.aasm.human_state).to eq("Closed")
end
end
context 'aasm.human_event_name' do
it 'should return translated event name' do
expect(LocalizerTestModel.aasm.human_event_name(:close)).to eq("Let's close it!")
end
it 'should return humanized event name' do
expect(LocalizerTestModel.aasm.human_event_name(:open)).to eq("Open")
end
end
end
context 'aasm.human_event_name' do
it 'should return translated event name' do
expect(LocalizerTestModel.aasm.human_event_name(:close)).to eq("Let's close it!")
describe AASM::Localizer, "deprecated style" do
before(:all) do
I18n.load_path << 'spec/en_deprecated_style.yml'
I18n.default_locale = :en
I18n.reload!
end
it 'should return humanized event name' do
expect(LocalizerTestModel.aasm.human_event_name(:open)).to eq("Open")
end
end
end
describe AASM::Localizer, "deprecated style" do
before(:all) do
I18n.load_path << 'spec/en_deprecated_style.yml'
I18n.default_locale = :en
I18n.reload!
end
after(:all) do
I18n.load_path.clear
end
let (:foo_opened) { LocalizerTestModel.new }
let (:foo_closed) { LocalizerTestModel.new.tap { |x| x.aasm_state = :closed } }
context 'aasm.human_state' do
it 'should return translated state value' do
expect(foo_opened.aasm.human_state).to eq("It's open now!")
end
it 'should return humanized value if not localized' do
expect(foo_closed.aasm.human_state).to eq("Closed")
end
end
context 'aasm.human_event_name' do
it 'should return translated event name' do
expect(LocalizerTestModel.aasm.human_event_name(:close)).to eq("Let's close it!")
end
it 'should return humanized event name' do
expect(LocalizerTestModel.aasm.human_event_name(:open)).to eq("Open")
after(:all) do
I18n.load_path.clear
end
let (:foo_opened) { LocalizerTestModel.new }
let (:foo_closed) { LocalizerTestModel.new.tap { |x| x.aasm_state = :closed } }
context 'aasm.human_state' do
it 'should return translated state value' do
expect(foo_opened.aasm.human_state).to eq("It's open now!")
end
it 'should return humanized value if not localized' do
expect(foo_closed.aasm.human_state).to eq("Closed")
end
end
context 'aasm.human_event_name' do
it 'should return translated event name' do
expect(LocalizerTestModel.aasm.human_event_name(:close)).to eq("Let's close it!")
end
it 'should return humanized event name' do
expect(LocalizerTestModel.aasm.human_event_name(:open)).to eq("Open")
end
end
end
end

File diff suppressed because it is too large Load diff