File: ts_dbd.rb

package info (click to toggle)
ruby-dbd-mysql 0.4.4%2Bgem2deb-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 292 kB
  • sloc: ruby: 1,403; sql: 47; makefile: 2
file content (131 lines) | stat: -rw-r--r-- 3,572 bytes parent folder | download | duplicates (10)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require 'rubygems'
gem 'test-unit'
# figure out what tests to run
require 'yaml'
require 'test/unit/testsuite'
require 'test/unit/ui/console/testrunner'

if File.basename(Dir.pwd) == "test"
    $:.unshift('../lib')
else
    $:.unshift('lib')
end

module Test::Unit::Assertions
    def build_message(head, template=nil, *arguments)
        template += "\n" + "DATABASE: " + dbtype
        template &&= template.chomp
        return AssertionMessage.new(head, template, arguments)
    end
end

class Class
    def name=(klass_name)
        @name = klass_name
    end

    def name
        return @name || super
    end
end

module DBDConfig
    @testbase = { }
    @current_dbtype = nil

    def self.get_config
        config = nil

        begin
            config = YAML.load_file(File.join(ENV["HOME"], ".ruby-dbi.test-config.yaml"))
        rescue Exception => e
            config = { }
            config["dbtypes"] = [ ]
        end

        return config
    end

    def self.inject_sql(dbh, dbtype, file)
        # splits by --- in the file, strips newlines and the semicolons.
        # this way we can still manually import the file, but use it with our
        # drivers for client-independent injection.
        File.open(file).read.split(/\n*---\n*/, -1).collect { |x| x.gsub!(/\n/, ''); x.sub(/;\z/, '') }.each do |stmt|
            tmp = STDERR.dup
            STDERR.reopen('sql.log', 'a')
            begin
                dbh.commit rescue nil
                dbh["AutoCommit"] = true rescue nil
                dbh.do(stmt)
                dbh.commit unless dbtype == 'sqlite3'
            rescue Exception => e
                tmp.puts "Error injecting '#{stmt}' for db #{dbtype}"
                tmp.puts "Error: #{e.message}"
            end
            STDERR.reopen(tmp)
        end
    end

    def self.current_dbtype
        @current_dbtype
    end

    def self.current_dbtype=(setting)
        @current_dbtype = setting
    end

    def self.testbase(klass_name)
        return @testbase[klass_name]
    end

    def self.set_testbase(klass_name, klass)
        @testbase[klass_name] = klass
        klass.name = klass_name.to_s
    end

    def self.suite
        @suite ||= []
    end
end

if __FILE__ == $0
    Dir.chdir("..") if File.basename(Dir.pwd) == "test"
    $LOAD_PATH.unshift(File.join(Dir.pwd, "lib"))
    Dir.chdir("test") rescue nil

    begin
        require 'dbi'
    rescue LoadError => e
        begin
            require 'rubygems'
            gem 'dbi'
            require 'dbi'
        rescue LoadError => e
            abort "DBI must already be installed or must come with this package for tests to work."
        end
    end

    Deprecate.set_action(proc { })

    config = DBDConfig.get_config

    config["dbtypes"] = ENV["DBTYPES"].split(/\s+/) if ENV["DBTYPES"]

    if config and config["dbtypes"]
        config["dbtypes"].each do |dbtype|
            unless config[dbtype]
                warn "#{dbtype} is selected for testing but not configured; see test/DBD_TESTS"
                next
            end

            # base.rb is special, see DBD_TESTS
            require "dbd/#{dbtype}/base.rb"
            Dir["dbd/#{dbtype}/test*.rb"].each { |file| require file }
            # run the general tests
            DBDConfig.current_dbtype = dbtype.to_sym
            Dir["dbd/general/test*.rb"].each { |file| load file; @class.name = file; DBDConfig.suite << @class }
        end
    elsif !config["dbtypes"]
        warn "Please see test/DBD_TESTS for information on configuring DBD tests."
    end
end