aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/tests_database.rb
blob: 79668dd941866b1e3e8db5cc5a6bf062bce492ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require 'logger'

$:.unshift(File.dirname(__FILE__) + '/../../../activerecord/lib')
require 'active_record'
require 'active_record/test_case'
require 'active_record/fixtures'

module ActiveModel
  module TestsDatabase
    mattr_accessor :connected

    def self.included(base)
      unless self.connected
        setup_connection
        setup_schema
      end

      base.send :include, ActiveRecord::TestFixtures
    end

    def self.setup_schema
      original, $stdout = $stdout, StringIO.new
      load(SCHEMA_FILE)
    ensure
      $stdout = original
      self.connected = true
    end

    def self.setup_connection
      defaults = { :database => ':memory:' }

      adapter = defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3'
      options = defaults.merge :adapter => adapter, :timeout => 500
      ActiveRecord::Base.establish_connection(options)
    end
  end
end