PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/test/helper.rb

https://github.com/givmo/paperclip
Ruby | 146 lines | 118 code | 28 blank | 0 comment | 0 complexity | 77d27d13cb08b8b09eec22870e52c37a MD5 | raw file
  1. require 'rubygems'
  2. require 'tempfile'
  3. require 'test/unit'
  4. require 'shoulda'
  5. require 'mocha'
  6. require 'active_record'
  7. require 'active_record/version'
  8. require 'active_support'
  9. puts "Testing against version #{ActiveRecord::VERSION::STRING}"
  10. `ruby -e 'exit 0'` # Prime $? with a value.
  11. begin
  12. require 'ruby-debug'
  13. rescue LoadError => e
  14. puts "debugger disabled"
  15. end
  16. ROOT = File.join(File.dirname(__FILE__), '..')
  17. def silence_warnings
  18. old_verbose, $VERBOSE = $VERBOSE, nil
  19. yield
  20. ensure
  21. $VERBOSE = old_verbose
  22. end
  23. class Test::Unit::TestCase
  24. def setup
  25. silence_warnings do
  26. Object.const_set(:Rails, stub('Rails', :root => ROOT, :env => 'test'))
  27. end
  28. end
  29. end
  30. $LOAD_PATH << File.join(ROOT, 'lib')
  31. $LOAD_PATH << File.join(ROOT, 'lib', 'paperclip')
  32. require File.join(ROOT, 'lib', 'paperclip.rb')
  33. require './shoulda_macros/paperclip'
  34. FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
  35. config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
  36. ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new(File.dirname(__FILE__) + "/debug.log")
  37. ActiveRecord::Base.establish_connection(config['test'])
  38. def reset_class class_name
  39. ActiveRecord::Base.send(:include, Paperclip::Glue)
  40. Object.send(:remove_const, class_name) rescue nil
  41. klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
  42. klass.class_eval{ include Paperclip::Glue }
  43. klass
  44. end
  45. def reset_table table_name, &block
  46. block ||= lambda { |table| true }
  47. ActiveRecord::Base.connection.create_table :dummies, {:force => true}, &block
  48. end
  49. def modify_table table_name, &block
  50. ActiveRecord::Base.connection.change_table :dummies, &block
  51. end
  52. def rebuild_model options = {}
  53. ActiveRecord::Base.connection.create_table :dummies, :force => true do |table|
  54. table.column :other, :string
  55. table.column :avatar_file_name, :string
  56. table.column :avatar_content_type, :string
  57. table.column :avatar_file_size, :integer
  58. table.column :avatar_updated_at, :datetime
  59. table.column :avatar_fingerprint, :string
  60. end
  61. rebuild_class options
  62. end
  63. def rebuild_class options = {}
  64. ActiveRecord::Base.send(:include, Paperclip::Glue)
  65. Object.send(:remove_const, "Dummy") rescue nil
  66. Object.const_set("Dummy", Class.new(ActiveRecord::Base))
  67. Dummy.class_eval do
  68. include Paperclip::Glue
  69. has_attached_file :avatar, options
  70. end
  71. end
  72. class FakeModel
  73. attr_accessor :avatar_file_name,
  74. :avatar_file_size,
  75. :avatar_last_updated,
  76. :avatar_content_type,
  77. :avatar_fingerprint,
  78. :id
  79. def errors
  80. @errors ||= []
  81. end
  82. def run_paperclip_callbacks name, *args
  83. end
  84. end
  85. def attachment options
  86. Paperclip::Attachment.new(:avatar, FakeModel.new, options)
  87. end
  88. def silence_warnings
  89. old_verbose, $VERBOSE = $VERBOSE, nil
  90. yield
  91. ensure
  92. $VERBOSE = old_verbose
  93. end
  94. def should_accept_dummy_class
  95. should "accept the class" do
  96. assert_accepts @matcher, @dummy_class
  97. end
  98. should "accept an instance of that class" do
  99. assert_accepts @matcher, @dummy_class.new
  100. end
  101. end
  102. def should_reject_dummy_class
  103. should "reject the class" do
  104. assert_rejects @matcher, @dummy_class
  105. end
  106. should "reject an instance of that class" do
  107. assert_rejects @matcher, @dummy_class.new
  108. end
  109. end
  110. def with_exitstatus_returning(code)
  111. saved_exitstatus = $?.nil? ? 0 : $?.exitstatus
  112. begin
  113. `ruby -e 'exit #{code.to_i}'`
  114. yield
  115. ensure
  116. `ruby -e 'exit #{saved_exitstatus.to_i}'`
  117. end
  118. end