Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require File.join(File.dirname(__FILE__), '..', 'CONFIG.rb')
- require 'rubygems'
- require 'test/unit'
- require 'pp'
- require 'og'
- class Logger
- class ObsoleteField < RuntimeError; end
- class MissingField < RuntimeError; end
- @@boom = false
- def self.warn(str, boom = @@boom)
- @@global_logger.warn(str)
- @@global_logger.warn('I WILL BOOM!') if boom
- if boom && str =~ /(Obsolete|Missing) field/
- case $1
- when 'Obsolete'
- raise ObsoleteField.new(str)
- when 'Missing'
- raise MissingField.new(str)
- end
- end
- end # end self.warn()
- def self.boom=(bool)
- bool
- end
- end # end Logger
- class TC_Evolution < Test::Unit::TestCase
- # Sets up a new store with given options
- def self.setup_og(options = {})
- Og.create_schema = options[:create_schema] || Og.create_schema
- Logger.boom = options[:boom] || false
- opt = $og1.instance_variable_get('@options')
- opt = opt.update(options)
- opt[:classes] = []
- Og.setup(opt)
- end
- # Initialize a set of stores with differing options
- STORES = {
- :full => setup_og(:destroy => false, :evolve_schema => :full),
- :warn => setup_og(:destroy => false, :evolve_schema => :warn),
- false => setup_og(:destroy => false, :evolve_schema => false),
- true => setup_og(:destroy => false, :evolve_schema => true),
- :add => setup_og(:destroy => false, :evolve_schema => :add)
- }.freeze
- def setup
- end
- def teardown
- store = $og1.get_store
- destroy_class('User')
- destroy_class('Admin')
- if store.table_exists?("ogtc_evolution_user")
- store.exec("DROP TABLE ogtc_evolution_user")
- end
- ensure
- $og1.put_store
- end
- User = nil
- Admin = nil
- # workaround, can't change constants in method scope. Usage: CHUser.call()
- MKClass = proc do |klass, x|
- case klass
- when 'User'
- User = x
- when 'Admin'
- Admin = x
- else
- raise "No such class #{klass}"
- end
- end
- def test_testcase
- assert_table_no_exists 'ogtc_evolution_user'
- create_class('User', :name => String)
- assert_not_nil User
- check_fields(User, ['oid', 'name'])
- destroy_class
- assert_nil User
- assert !Og::Manager.managed?(User)
- std_setup
- assert Og::Manager.managed?(User)
- end
- def test_evolve_full_simple
- assert_nil User
- o = STORES[:full]
- create_class('User', :name => String, :store => :full)
- check_fields(User, ['oid', 'name'])
- check_db_fields(User, ['oid', 'name'])
- User.property :pass, String
- check_fields(User, ['oid', 'name', 'pass'])
- check_db_fields(User, ['oid', 'name'])
- store = o.get_store
- store.evolve_schema(User)
- o.put_store
- check_fields(User, ['oid', 'name', 'pass'])
- check_db_fields(User, ['oid', 'name', 'pass'])
- end
- def test_evolve_warn_simple
- o = STORES[:warn]
- create_class('User', :name => String, :store => :warn)
- check_fields(User, ['oid', 'name'])
- check_db_fields(User, ['oid', 'name'])
- User.property :pass, String
- check_fields(User, ['oid', 'name', 'pass'])
- check_db_fields(User, ['oid', 'name'])
- store = o.get_store
- store.evolve_schema(User)
- o.put_store
- check_fields(User, ['oid', 'name', 'pass'])
- check_db_fields(User, ['oid', 'name'])
- end
- def test_evolve_full
- std_setup(:store => :full)
- check_db_fields(User, ['oid', 'nick'])
- end
- def test_evolve_add
- std_setup(:store => :add)
- check_db_fields(User, ['oid', 'nick', 'name', 'password'])
- end
- def test_evolve_warn
- std_setup(:store => :warn)
- check_db_fields(User, ['oid', 'name', 'password'])
- end
- def test_evolve_false
- std_setup(:store => false)
- check_db_fields(User, ['oid', 'name', 'password'])
- end
- def test_evolve_true
- std_setup(:store => true)
- check_db_fields(User, ['oid', 'nick', 'name', 'password'])
- end
- ###################################################################
- # Only support stuff after here
- ###################################################################
- private
- # Create standard setup.
- # First creates:
- # class User
- # property :name, String
- # property :password, String
- # end
- # Then it deletes that class after enchanting it and recreates it as
- # following:
- # class User
- # property :nick, String
- # end
- def std_setup(options = {})
- options = {:store => :full}.update(options)
- o = STORES[options[:store]]
- raise "No store for #{options[:store].inspect}?}" unless o
- create_class('User', :name => String, :password => String, :store => options[:store])
- check_fields(User, ['oid', 'name', 'password'])
- destroy_class
- create_class('User', :nick => String, :store => options[:store])
- end
- # Creates:
- # class User
- # is Og::SchemaInheritanceBase
- # property :name, String
- # property :password, String
- # end
- # class Admin < User
- # property :protected, TrueClass
- # end
- def prepare_sti_classes(options = {})
- options = {:store => :full}.update(options)
- o = STORES[options[:store]]
- raise "No store for #{options[:store].inspect}?}" unless o
- create_class('User', :name => String, :password => String, :store => options[:store])
- User.include Og::SchemaInheritanceBase
- create_class('Admin', :superclass => User, :protected => TrueClass, :store => options[:store])
- end
- # Checks if attributes of a class are available as expected
- def check_fields(klass, expected)
- attrs = klass.serializable_attributes.map {|x| x.to_s }.sort
- assert_equal expected.sort, attrs, "Does not have required fields."
- end
- # Checks if database fields of a given class are as expected
- def check_db_fields(klass, expected)
- fields = klass.ogmanager.store.create_field_map(klass).keys.map {|x| x.to_s }.sort
- klass.ogmanager.put_store
- assert_equal expected.sort, fields, "Does not have required db fields."
- end
- # Creates a new class using the MKClass facility
- # Usage:
- # create_class([klass, [options]])
- # create_class('User', :superclass => Object, :store => :full, props...)
- # props will map to properties withing that class:
- # :name => String will map to Klass.property :name, String
- def create_class(klass = 'User', options = {})
- options = {:store => :full}.update(options)
- st = STORES[options.delete(:store)]
- raise "No such store for #{options[:store].inspect}?" unless st
- sc = options.delete(:superclass)
- MKClass.call(klass, Class.new(sc || Object))
- options.each do |prop, type|
- User.property prop, type
- end
- st.manage_classes User
- end
- # Destroys the given class using the given classname
- def destroy_class(klass = 'User')
- kl = constant(klass)
- return unless kl
- kl.ogmanager.unmanage_class kl
- kl.ogmanager.put_store
- MKClass.call(klass, nil)
- end
- ###################################################################
- # Only assertions stuff after here
- ###################################################################
- def assert_table_no_exists(table, message = '')
- store = $og1.get_store
- message = build_message message, "<?> exists.", table
- assert_block message do
- if store.table_exists?("ogtc_evolution_user")
- false
- else
- true
- end
- end
- ensure
- $og1.put_store
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement