PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/plugins/attachment_fu/test/test_helper.rb

https://github.com/grantneufeld/wayground-old
Ruby | 142 lines | 116 code | 25 blank | 1 comment | 8 complexity | 9d272003c294faa402ab4dd124ceb2ca MD5 | raw file
  1. $:.unshift(File.dirname(__FILE__) + '/../lib')
  2. ENV['RAILS_ENV'] = 'test'
  3. ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
  4. require 'test/unit'
  5. require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
  6. require 'active_record/fixtures'
  7. require 'action_controller/test_process'
  8. config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
  9. ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
  10. db_adapter = ENV['DB']
  11. # no db passed, try one of these fine config-free DBs before bombing.
  12. db_adapter ||=
  13. begin
  14. require 'rubygems'
  15. require 'sqlite'
  16. 'sqlite'
  17. rescue MissingSourceFile
  18. begin
  19. require 'sqlite3'
  20. 'sqlite3'
  21. rescue MissingSourceFile
  22. end
  23. end
  24. if db_adapter.nil?
  25. raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
  26. end
  27. ActiveRecord::Base.establish_connection(config[db_adapter])
  28. load(File.dirname(__FILE__) + "/schema.rb")
  29. Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures"
  30. $LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
  31. class Test::Unit::TestCase #:nodoc:
  32. include ActionController::TestProcess
  33. def create_fixtures(*table_names)
  34. if block_given?
  35. Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
  36. else
  37. Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
  38. end
  39. end
  40. def setup
  41. Attachment.saves = 0
  42. DbFile.transaction { [Attachment, FileAttachment, OrphanAttachment, MinimalAttachment, DbFile].each { |klass| klass.delete_all } }
  43. attachment_model self.class.attachment_model
  44. end
  45. def teardown
  46. FileUtils.rm_rf File.join(File.dirname(__FILE__), 'files')
  47. end
  48. self.use_transactional_fixtures = true
  49. self.use_instantiated_fixtures = false
  50. def self.attachment_model(klass = nil)
  51. @attachment_model = klass if klass
  52. @attachment_model
  53. end
  54. def self.test_against_class(test_method, klass, subclass = false)
  55. define_method("#{test_method}_on_#{:sub if subclass}class") do
  56. klass = Class.new(klass) if subclass
  57. attachment_model klass
  58. send test_method, klass
  59. end
  60. end
  61. def self.test_against_subclass(test_method, klass)
  62. test_against_class test_method, klass, true
  63. end
  64. protected
  65. def upload_file(options = {})
  66. use_temp_file options[:filename] do |file|
  67. att = attachment_model.create :uploaded_data => fixture_file_upload(file, options[:content_type] || 'image/png')
  68. att.reload unless att.new_record?
  69. return att
  70. end
  71. end
  72. def use_temp_file(fixture_filename)
  73. temp_path = File.join('/tmp', File.basename(fixture_filename))
  74. FileUtils.mkdir_p File.join(fixture_path, 'tmp')
  75. FileUtils.cp File.join(fixture_path, fixture_filename), File.join(fixture_path, temp_path)
  76. yield temp_path
  77. ensure
  78. FileUtils.rm_rf File.join(fixture_path, 'tmp')
  79. end
  80. def assert_created(num = 1)
  81. assert_difference attachment_model.base_class, :count, num do
  82. if attachment_model.included_modules.include? DbFile
  83. assert_difference DbFile, :count, num do
  84. yield
  85. end
  86. else
  87. yield
  88. end
  89. end
  90. end
  91. def assert_not_created
  92. assert_created(0) { yield }
  93. end
  94. def should_reject_by_size_with(klass)
  95. attachment_model klass
  96. assert_not_created do
  97. attachment = upload_file :filename => '/files/rails.png'
  98. assert attachment.new_record?
  99. assert attachment.errors.on(:size)
  100. assert_nil attachment.db_file if attachment.respond_to?(:db_file)
  101. end
  102. end
  103. def assert_difference(object, method = nil, difference = 1)
  104. initial_value = object.send(method)
  105. yield
  106. assert_equal initial_value + difference, object.send(method)
  107. end
  108. def assert_no_difference(object, method, &block)
  109. assert_difference object, method, 0, &block
  110. end
  111. def attachment_model(klass = nil)
  112. @attachment_model = klass if klass
  113. @attachment_model
  114. end
  115. end
  116. require File.join(File.dirname(__FILE__), 'fixtures/attachment')
  117. require File.join(File.dirname(__FILE__), 'base_attachment_tests')