summaryrefslogtreecommitdiffstats
path: root/test/tc_database.rb
blob: 4bb6891bd9fed4caac2fcb67f7a985efc8517a5e (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
$:.unshift "../lib"

require 'sqlite3/database'
require 'test/unit'

require 'mocks'

class TC_Database_Init < Test::Unit::TestCase
  def test_new
    db = SQLite3::Database.new( "foo.db", :driver => Driver )
    assert_equal 1, Driver.instance.mock_count(:open)
    assert !db.closed?
    assert_equal [["foo.db",false]], Driver.instance.mock_args[:open]
    assert !db.results_as_hash
    assert !db.type_translation
  end

  def test_open
    db = SQLite3::Database.open( "foo.db", :driver => Driver )
    assert_equal 1, Driver.instance.mock_count(:open)
    assert !db.closed?
    assert_equal [["foo.db",false]], Driver.instance.mock_args[:open]
    assert !db.results_as_hash
    assert !db.type_translation
  end
  
  def test_with_type_translation
    db = SQLite3::Database.open( "foo.db", :driver => Driver,
      :type_translation => true )
    assert db.type_translation
  end
  
  def test_with_results_as_hash
    db = SQLite3::Database.open( "foo.db", :driver => Driver,
      :results_as_hash => true )
    assert db.results_as_hash
  end
  
  def test_with_type_translation_and_results_as_hash
    db = SQLite3::Database.open( "foo.db", :driver => Driver,
      :results_as_hash => true,
      :type_translation => true )
    assert db.results_as_hash
    assert db.type_translation
  end
end

class TC_Database < Test::Unit::TestCase
  def setup
    @db = SQLite3::Database.open( "foo.db",
      :driver => Driver, :statement_factory => Statement )
  end

  def test_quote
    assert_equal "''one''two''three''", SQLite3::Database.quote(
      "'one'two'three'" )
  end

  def test_complete
    @db.complete? "foo"
    assert_equal 1, Driver.instance.mock_count( :complete? )
  end

  def test_errmsg
    @db.errmsg
    assert_equal 1, Driver.instance.mock_count( :errmsg )
  end

  def test_errcode
    @db.errcode
    assert_equal 1, Driver.instance.mock_count( :errcode )
  end

  def test_translator
    translator = @db.translator
    assert_instance_of SQLite3::Translator, translator
  end

  def test_close
    @db.close
    assert_equal 1, Driver.instance.mock_count( :close )
    assert @db.closed?
    @db.close
    assert_equal 1, Driver.instance.mock_count( :close )
  end

  def test_trace
    @db.trace( 15 ) { "foo" }
    driver = Driver.instance
    assert_equal 1, driver.mock_count( :trace )
    assert_equal [[ "cookie", 15 ]], driver.mock_args[:trace]
    assert_equal 1, driver.mock_blocks[:trace].length
  end

  def test_authorizer
    @db.authorizer( 15 ) { "foo" }
    driver = Driver.instance
    assert_equal 1, driver.mock_count( :set_authorizer )
    assert_equal [[ "cookie", 15 ]], driver.mock_args[:set_authorizer]
    assert_equal 1, driver.mock_blocks[:set_authorizer].length
  end

  def test_prepare_no_block
    assert_nothing_raised { @db.prepare( "foo" ) }
    assert_equal 0, Statement.instance.mock_count( :close )
  end

  def test_prepare_with_block
    called = false
    @db.prepare( "foo" ) { |stmt| called = true }
    assert called
    assert_equal 1, Statement.instance.mock_count( :close )
  end

  def test_execute_no_block
    result = @db.execute( "foo", "bar", "baz" )
    stmt = Statement.instance
    assert_equal [["foo"]], result
    assert_equal [["bar", "baz"]], stmt.mock_args[:execute]
  end

  def test_execute_with_block
    called = false
    @db.execute( "foo", "bar", "baz" ) do |row|
      called = true
      assert_equal ["foo"], row
    end

    stmt = Statement.instance
    assert called
    assert_equal [["bar", "baz"]], stmt.mock_args[:execute]
  end

  def test_execute2_no_block
    result = @db.execute2( "foo", "bar", "baz" )
    stmt = Statement.instance
    assert_equal [["name"],["foo"]], result
    assert_equal [["bar", "baz"]], stmt.mock_args[:execute]
  end

  def test_execute2_with_block
    called = false
    parts = [ ["name"],["foo"] ]
    @db.execute2( "foo", "bar", "baz" ) do |row|
      called = true
      assert_equal parts.shift, row
    end

    stmt = Statement.instance
    assert called
    assert_equal [["bar", "baz"]], stmt.mock_args[:execute]
  end

  def test_execute_batch
    @db.execute_batch( "foo", "bar", "baz" )
    stmt = Statement.instance
    assert_equal [["bar", "baz"]], stmt.mock_args[:execute]
  end

  def test_get_first_row
    result = @db.get_first_row( "foo", "bar", "baz" )
    assert_equal ["foo"], result
  end

  def test_get_first_value
    result = @db.get_first_value( "foo", "bar", "baz" )
    assert_equal "foo", result
  end

  def test_changes
    assert_equal 14, @db.changes
    assert_equal 1, Driver.instance.mock_count(:changes)
  end

  def test_total_changes
    assert_equal 28, @db.total_changes
    assert_equal 1, Driver.instance.mock_count(:total_changes)
  end

  def test_interrupt
    @db.interrupt
    assert_equal 1, Driver.instance.mock_count(:interrupt)
  end
end