PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/plugins/classic_pagination/test/helper.rb

https://bitbucket.org/sqctest02/redmine
Ruby | 117 lines | 88 code | 18 blank | 11 comment | 7 complexity | 50c7fabeb3d6728ce24eada59d5b2b67 MD5 | raw file
Possible License(s): GPL-2.0
  1. require 'test/unit'
  2. unless defined?(ActiveRecord)
  3. plugin_root = File.join(File.dirname(__FILE__), '..')
  4. # first look for a symlink to a copy of the framework
  5. if framework_root = ["#{plugin_root}/rails", "#{plugin_root}/../../rails"].find { |p| File.directory? p }
  6. puts "found framework root: #{framework_root}"
  7. # this allows for a plugin to be tested outside an app
  8. $:.unshift "#{framework_root}/activesupport/lib", "#{framework_root}/activerecord/lib", "#{framework_root}/actionpack/lib"
  9. else
  10. # is the plugin installed in an application?
  11. app_root = plugin_root + '/../../..'
  12. if File.directory? app_root + '/config'
  13. puts 'using config/boot.rb'
  14. ENV['RAILS_ENV'] = 'test'
  15. require File.expand_path(app_root + '/config/boot')
  16. else
  17. # simply use installed gems if available
  18. puts 'using rubygems'
  19. require 'rubygems'
  20. gem 'actionpack'; gem 'activerecord'
  21. end
  22. end
  23. %w(action_pack active_record action_controller active_record/fixtures action_controller/test_process).each {|f| require f}
  24. Dependencies.load_paths.unshift "#{plugin_root}/lib"
  25. end
  26. # Define the connector
  27. class ActiveRecordTestConnector
  28. cattr_accessor :able_to_connect
  29. cattr_accessor :connected
  30. # Set our defaults
  31. self.connected = false
  32. self.able_to_connect = true
  33. class << self
  34. def setup
  35. unless self.connected || !self.able_to_connect
  36. setup_connection
  37. load_schema
  38. require_fixture_models
  39. self.connected = true
  40. end
  41. rescue Exception => e # errors from ActiveRecord setup
  42. $stderr.puts "\nSkipping ActiveRecord assertion tests: #{e}"
  43. #$stderr.puts " #{e.backtrace.join("\n ")}\n"
  44. self.able_to_connect = false
  45. end
  46. private
  47. def setup_connection
  48. if Object.const_defined?(:ActiveRecord)
  49. defaults = { :database => ':memory:' }
  50. begin
  51. options = defaults.merge :adapter => 'sqlite3', :timeout => 500
  52. ActiveRecord::Base.establish_connection(options)
  53. ActiveRecord::Base.configurations = { 'sqlite3_ar_integration' => options }
  54. ActiveRecord::Base.connection
  55. rescue Exception # errors from establishing a connection
  56. $stderr.puts 'SQLite 3 unavailable; trying SQLite 2.'
  57. options = defaults.merge :adapter => 'sqlite'
  58. ActiveRecord::Base.establish_connection(options)
  59. ActiveRecord::Base.configurations = { 'sqlite2_ar_integration' => options }
  60. ActiveRecord::Base.connection
  61. end
  62. Object.send(:const_set, :QUOTED_TYPE, ActiveRecord::Base.connection.quote_column_name('type')) unless Object.const_defined?(:QUOTED_TYPE)
  63. else
  64. raise "Can't setup connection since ActiveRecord isn't loaded."
  65. end
  66. end
  67. # Load actionpack sqlite tables
  68. def load_schema
  69. File.read(File.dirname(__FILE__) + "/fixtures/schema.sql").split(';').each do |sql|
  70. ActiveRecord::Base.connection.execute(sql) unless sql.blank?
  71. end
  72. end
  73. def require_fixture_models
  74. Dir.glob(File.dirname(__FILE__) + "/fixtures/*.rb").each {|f| require f}
  75. end
  76. end
  77. end
  78. # Test case for inheritance
  79. class ActiveRecordTestCase < Test::Unit::TestCase
  80. # Set our fixture path
  81. if ActiveRecordTestConnector.able_to_connect
  82. self.fixture_path = "#{File.dirname(__FILE__)}/fixtures/"
  83. self.use_transactional_fixtures = false
  84. end
  85. def self.fixtures(*args)
  86. super if ActiveRecordTestConnector.connected
  87. end
  88. def run(*args)
  89. super if ActiveRecordTestConnector.connected
  90. end
  91. # Default so Test::Unit::TestCase doesn't complain
  92. def test_truth
  93. end
  94. end
  95. ActiveRecordTestConnector.setup
  96. ActionController::Routing::Routes.reload rescue nil
  97. ActionController::Routing::Routes.draw do |map|
  98. map.connect ':controller/:action/:id'
  99. end