extract test models into their own files

This commit is contained in:
Thorsten Böttger 2015-05-16 16:46:45 +12:00
parent 78fe1a4113
commit e29232581d
21 changed files with 217 additions and 231 deletions

View File

@ -0,0 +1,3 @@
require_relative 'simple_new_dsl'
class DerivateNewDsl < SimpleNewDsl
end

View File

@ -0,0 +1,17 @@
class FalseState < ActiveRecord::Base
include AASM
def initialize(*args)
super
self.aasm_state = false
end
aasm do
state :opened
state :closed
event :view do
transitions :to => :read, :from => [:needs_attention]
end
end
end

View File

@ -0,0 +1,19 @@
class Gate < ActiveRecord::Base
include AASM
# Fake this column for testing purposes
# attr_accessor :aasm_state
def value
'value'
end
aasm do
state :opened
state :closed
event :view do
transitions :to => :read, :from => [:needs_attention]
end
end
end

View File

@ -0,0 +1,36 @@
class LocalizerTestModel < ActiveRecord::Base
include AASM
attr_accessor :aasm_state
aasm do
state :opened, :initial => true
state :closed
event :close
event :open
end
end
describe 'localized state names' do
before(:all) do
I18n.load_path << 'spec/en.yml'
I18n.default_locale = :en
I18n.reload!
end
after(:all) do
I18n.load_path.clear
end
it 'should localize' do
state = LocalizerTestModel.aasm.states.detect {|s| s == :opened}
expect(state.localized_name).to eq("It's open now!")
expect(state.human_name).to eq("It's open now!")
end
it 'should use fallback' do
state = LocalizerTestModel.aasm.states.detect {|s| s == :closed}
expect(state.localized_name).to eq('Closed')
expect(state.human_name).to eq('Closed')
end
end

View File

@ -0,0 +1,10 @@
class NoDirectAssignment < ActiveRecord::Base
include AASM
aasm :no_direct_assignment => true do
state :pending, :initial => true
state :running
event :run do
transitions :from => :pending, :to => :running
end
end
end

View File

@ -0,0 +1,10 @@
class NoScope < ActiveRecord::Base
include AASM
aasm :create_scopes => false do
state :pending, :initial => true
state :running
event :run do
transitions :from => :pending, :to => :running
end
end
end

View File

@ -0,0 +1,7 @@
class Reader < ActiveRecord::Base
include AASM
def aasm_read_state
"fi"
end
end

View File

@ -0,0 +1,8 @@
class SimpleNewDsl < ActiveRecord::Base
include AASM
aasm :column => :status
aasm do
state :unknown_scope
state :new
end
end

View File

@ -0,0 +1,14 @@
class Thief < ActiveRecord::Base
if ActiveRecord::VERSION::MAJOR >= 3
self.table_name = 'thieves'
else
set_table_name "thieves"
end
include AASM
aasm do
state :rich
state :jailed
initial_state Proc.new {|thief| thief.skilled ? :rich : :jailed }
end
attr_accessor :skilled, :aasm_state
end

View File

@ -0,0 +1,6 @@
class Transient < ActiveRecord::Base
def aasm_write_state_without_persistence(state)
"fum"
end
include AASM
end

View File

@ -0,0 +1,19 @@
class WithEnum < ActiveRecord::Base
include AASM
# Fake this column for testing purposes
attr_accessor :aasm_state
def self.test
{}
end
aasm :enum => :test do
state :opened
state :closed
event :view do
transitions :to => :read, :from => [:needs_attention]
end
end
end

View File

@ -0,0 +1,15 @@
class WithFalseEnum < ActiveRecord::Base
include AASM
# Fake this column for testing purposes
attr_accessor :aasm_state
aasm :enum => false do
state :opened
state :closed
event :view do
transitions :to => :read, :from => [:needs_attention]
end
end
end

View File

@ -0,0 +1,19 @@
class WithTrueEnum < ActiveRecord::Base
include AASM
# Fake this column for testing purposes
attr_accessor :aasm_state
def value
'value'
end
aasm :enum => true do
state :opened
state :closed
event :view do
transitions :to => :read, :from => [:needs_attention]
end
end
end

View File

@ -0,0 +1,6 @@
class Writer < ActiveRecord::Base
def aasm_write_state(state)
"fo"
end
include AASM
end

15
spec/models/payment.rb Normal file
View File

@ -0,0 +1,15 @@
class Payment
include AASM
aasm do
state :initialised, :initial => true
state :filled_out
state :authorised
event :fill_out do
transitions :from => :initialised, :to => :filled_out
end
event :authorise do
transitions :from => :filled_out, :to => :authorised
end
end
end

View File

@ -1,164 +0,0 @@
class Gate < ActiveRecord::Base
include AASM
# Fake this column for testing purposes
# attr_accessor :aasm_state
def value
'value'
end
aasm do
state :opened
state :closed
event :view do
transitions :to => :read, :from => [:needs_attention]
end
end
end
class FalseState < ActiveRecord::Base
include AASM
def initialize(*args)
super
self.aasm_state = false
end
aasm do
state :opened
state :closed
event :view do
transitions :to => :read, :from => [:needs_attention]
end
end
end
class WithEnum < ActiveRecord::Base
include AASM
# Fake this column for testing purposes
attr_accessor :aasm_state
def self.test
{}
end
aasm :enum => :test do
state :opened
state :closed
event :view do
transitions :to => :read, :from => [:needs_attention]
end
end
end
class WithTrueEnum < ActiveRecord::Base
include AASM
# Fake this column for testing purposes
attr_accessor :aasm_state
def value
'value'
end
aasm :enum => true do
state :opened
state :closed
event :view do
transitions :to => :read, :from => [:needs_attention]
end
end
end
class WithFalseEnum < ActiveRecord::Base
include AASM
# Fake this column for testing purposes
attr_accessor :aasm_state
aasm :enum => false do
state :opened
state :closed
event :view do
transitions :to => :read, :from => [:needs_attention]
end
end
end
class Reader < ActiveRecord::Base
include AASM
def aasm_read_state
"fi"
end
end
class Writer < ActiveRecord::Base
def aasm_write_state(state)
"fo"
end
include AASM
end
class Transient < ActiveRecord::Base
def aasm_write_state_without_persistence(state)
"fum"
end
include AASM
end
class SimpleNewDsl < ActiveRecord::Base
include AASM
aasm :column => :status
aasm do
state :unknown_scope
state :new
end
end
class NoScope < ActiveRecord::Base
include AASM
aasm :create_scopes => false do
state :pending, :initial => true
state :running
event :run do
transitions :from => :pending, :to => :running
end
end
end
class NoDirectAssignment < ActiveRecord::Base
include AASM
aasm :no_direct_assignment => true do
state :pending, :initial => true
state :running
event :run do
transitions :from => :pending, :to => :running
end
end
end
class DerivateNewDsl < SimpleNewDsl
end
class Thief < ActiveRecord::Base
if ActiveRecord::VERSION::MAJOR >= 3
self.table_name = 'thieves'
else
set_table_name "thieves"
end
include AASM
aasm do
state :rich
state :jailed
initial_state Proc.new {|thief| thief.skilled ? :rich : :jailed }
end
attr_accessor :skilled, :aasm_state
end

View File

@ -0,0 +1,12 @@
class SimpleStateMachine
include AASM
aasm do
state :init, :initial => true
state :failed
event :failed do
transitions :from => :init, :to => :failed
end
end
end

View File

@ -1,18 +1,5 @@
require 'spec_helper'
class SimpleStateMachine
include AASM
aasm do
state :init, :initial => true
state :failed
event :failed do
transitions :from => :init, :to => :failed
end
end
end
describe "event naming" do
let(:state_machine) { SimpleStateMachine.new }

View File

@ -5,43 +5,6 @@ require 'i18n'
I18n.enforce_available_locales = false
load_schema
class LocalizerTestModel < ActiveRecord::Base
include AASM
attr_accessor :aasm_state
aasm do
state :opened, :initial => true
state :closed
event :close
event :open
end
end
describe 'localized state names' do
before(:all) do
I18n.load_path << 'spec/en.yml'
I18n.default_locale = :en
I18n.reload!
end
after(:all) do
I18n.load_path.clear
end
it 'should localize' do
state = LocalizerTestModel.aasm.states.detect {|s| s == :opened}
expect(state.localized_name).to eq("It's open now!")
expect(state.human_name).to eq("It's open now!")
end
it 'should use fallback' do
state = LocalizerTestModel.aasm.states.detect {|s| s == :closed}
expect(state.localized_name).to eq('Closed')
expect(state.human_name).to eq('Closed')
end
end
describe AASM::Localizer, "new style" do
before(:all) do
I18n.load_path << 'spec/en.yml'

View File

@ -1,6 +1,6 @@
require 'active_record'
require 'spec_helper'
Dir[File.dirname(__FILE__) + "/../../models/active_record/*.rb"].sort.each { |f| require File.expand_path(f) }
load_schema
# if you want to see the statements while running the spec enable the following line

View File

@ -1,21 +1,5 @@
require 'spec_helper'
class Payment
include AASM
aasm do
state :initialised, :initial => true
state :filled_out
state :authorised
event :fill_out do
transitions :from => :initialised, :to => :filled_out
end
event :authorise do
transitions :from => :filled_out, :to => :authorised
end
end
end
describe 'state machine' do
let(:payment) {Payment.new}