File: tc_database.rb

package info (click to toggle)
sqlite3-ruby 1.1.0-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 904 kB
  • ctags: 753
  • sloc: ruby: 4,099; makefile: 59
file content (216 lines) | stat: -rw-r--r-- 6,566 bytes parent folder | download
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#--
# =============================================================================
# Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
# 
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
# 
#     * The names of its contributors may not be used to endorse or promote
#       products derived from this software without specific prior written
#       permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# =============================================================================
#++

$:.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