Class: UseDbTest

Inherits:
Object
  • Object
show all
Extended by:
UseDbPlugin
Defined in:
lib/use_db/use_db_test.rb

Class Method Summary collapse

Methods included from UseDbPlugin

all_use_dbs, debug_print, debug_print=, get_use_db_conn_spec, use_db

Class Method Details

.other_databasesObject



5
6
7
8
# File 'lib/use_db/use_db_test.rb', line 5

def self.other_databases
	use_db_config = (defined?(USE_DB_CONFIG)) ? USE_DB_CONFIG : "#{RAILS_ROOT}/config/use_db.yml"
	YAML.load(File.read(use_db_config)).values.collect(&:symbolize_keys!)
end

.prepare_test_db(options = {}) ⇒ Object



10
11
12
13
# File 'lib/use_db/use_db_test.rb', line 10

def self.prepare_test_db(options={})
	schema_dump(options)
	schema_load(options)
end

.schema_dump(options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/use_db/use_db_test.rb', line 15

def self.schema_dump(options)
	#	puts "In schema_dump"
	options_dup = options.dup
	options_dup[:rails_env] = "development"		
	conn_spec = get_use_db_conn_spec(options_dup)
	test_class = setup_test_model(options[:prefix], options[:suffix], "ForSchemaDump")
	test_class.establish_connection(conn_spec)
	require 'active_record/schema_dumper'
	File.open(schema_file(options), "w") do |file|
		ActiveRecord::SchemaDumper.dump(test_class.connection, file)
	end
end

.schema_file(options) ⇒ Object



57
58
59
# File 'lib/use_db/use_db_test.rb', line 57

def self.schema_file(options)
	"#{RAILS_ROOT}/db/#{RAILS_ENV}_#{options[:prefix]}_#{options[:suffix]}_schema.rb"
end

.schema_load(options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/use_db/use_db_test.rb', line 28

def self.schema_load(options)
	#	puts "In schema_load"
	options_dup = options.dup
	conn_spec = get_use_db_conn_spec(options_dup)
	ActiveRecord::Base.establish_connection(conn_spec)
	file = schema_file(options)
#	database.rake suggests that I should purge first, but why?
#	the schema is :force => true and will wipe everything out anyway.
#	using schema seems a better option than to the DSL that it came from.
	if File.exists?(file)
		#	puts "loading #{file}"
		ActiveRecord::Schema.verbose = UseDbPlugin.debug_print
		load(file)
		ActiveRecord::Base.connection.disconnect!
		ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations["test"])
	else
		abort %{#{file} doesn't exist yet. Run "rake db:migrate" to create it then try again. If you do not intend to use a database, you should instead alter #{RAILS_ROOT}/config/environment.rb to prevent active_record from loading: config.frameworks -= [ :active_record ]}
	end
end

.setup_test_model(prefix = "", suffix = "", model_suffix = "", rails_env = RAILS_ENV) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/use_db/use_db_test.rb', line 48

def self.setup_test_model(prefix="", suffix="", model_suffix="", rails_env=RAILS_ENV)
	prefix ||= ""
	suffix ||= ""
	model_name = "TestModel#{prefix.camelize}#{suffix.camelize}#{model_suffix}".gsub("_","").gsub("-","")
	return eval(model_name) if eval("defined?(#{model_name})")
	create_test_model(model_name, prefix, suffix, rails_env)
	return eval(model_name)
end