Skip to content

Commit 890f1bf

Browse files
committed
Organize test file better for adapter specific BDD.
1 parent 50a49e5 commit 890f1bf

9 files changed

+221
-247
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,5 @@
11
require 'active_record/connection_adapters/abstract_adapter'
2-
32
require 'base64'
4-
require 'bigdecimal'
5-
require 'bigdecimal/util'
6-
7-
# sqlserver_adapter.rb -- ActiveRecord adapter for Microsoft SQL Server
8-
#
9-
# Author: Joey Gibson <joey@joeygibson.com>
10-
# Date: 10/14/2004
11-
#
12-
# Modifications: DeLynn Berry <delynnb@megastarfinancial.com>
13-
# Date: 3/22/2005
14-
#
15-
# Modifications (ODBC): Mark Imbriaco <mark.imbriaco@pobox.com>
16-
# Date: 6/26/2005
17-
18-
# Modifications (Migrations): Tom Ward <tom@popdog.net>
19-
# Date: 27/10/2005
20-
#
21-
# Modifications (Numerous fixes as maintainer): Ryan Tomayko <rtomayko@gmail.com>
22-
# Date: Up to July 2006
23-
24-
# Previous maintainer: Tom Ward <tom@popdog.net>
25-
#
26-
27-
28-
29-
30-
# Current (interim/unofficial) maintainer: Shawn Balestracci <shawn@vegantech.com>
313

324
module ActiveRecord
335
class Base
@@ -290,7 +262,24 @@ def new_time(year, mon, mday, hour, min, sec, microsec = 0)
290262
# unixODBC 2.2.11, Ruby ODBC 0.996, Ruby DBI 0.0.23 and Ruby 1.8.2.
291263
# [Linux strongmad 2.6.11-1.1369_FC4 #1 Thu Jun 2 22:55:56 EDT 2005 i686 i686 i386 GNU/Linux]
292264
class SQLServerAdapter < AbstractAdapter
293-
265+
266+
ADAPTER_NAME = 'SQLServer'.freeze
267+
268+
# NATIVE_DATABASE_TYPES = {
269+
# :primary_key => "int NOT NULL IDENTITY(1, 1) PRIMARY KEY",
270+
# :string => { :name => "varchar", :limit => 255 },
271+
# :text => { :name => txt },
272+
# :integer => { :name => "int" },
273+
# :float => { :name => "float", :limit => 8 },
274+
# :decimal => { :name => "decimal" },
275+
# :datetime => { :name => "datetime" },
276+
# :timestamp => { :name => "datetime" },
277+
# :time => { :name => "datetime" },
278+
# :date => { :name => "datetime" },
279+
# :binary => { :name => bin },
280+
# :boolean => { :name => "bit"}
281+
# }
282+
294283
def initialize(connection, logger, connection_options=nil)
295284
super(connection, logger)
296285
@connection_options = connection_options
@@ -300,7 +289,10 @@ def initialize(connection, logger, connection_options=nil)
300289
else
301290
raise "Currently, only 2000 and 2005 are supported versions"
302291
end
303-
292+
end
293+
294+
def adapter_name #:nodoc:
295+
ADAPTER_NAME
304296
end
305297

306298
def native_database_types
@@ -324,16 +316,12 @@ def native_database_types
324316
:boolean => { :name => "bit"}
325317
}
326318
end
327-
328-
def adapter_name
329-
'SQLServer'
330-
end
331-
319+
332320
def database_version
333321
# returns string such as:
334322
# "Microsoft SQL Server 2000 - 8.00.2039 (Intel X86) \n\tMay 3 2005 23:18:38 \n\tCopyright (c) 1988-2003 Microsoft Corporation\n\tEnterprise Edition on Windows NT 5.2 (Build 3790: )\n"
335323
# "Microsoft SQL Server 2005 - 9.00.3215.00 (Intel X86) \n\tDec 8 2007 18:51:32 \n\tCopyright (c) 1988-2005 Microsoft Corporation\n\tStandard Edition on Windows NT 5.2 (Build 3790: Service Pack 2)\n"
336-
return select_value("SELECT @@version")
324+
select_value("SELECT @@version")
337325
end
338326

339327
def supports_migrations? #:nodoc:
Lines changed: 20 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -1,180 +1,48 @@
11
require 'cases/sqlserver_helper'
2-
require 'models/default'
3-
require 'models/post'
42
require 'models/task'
3+
require 'models/topic'
54

6-
class SqlServerAdapterTest < ActiveRecord::TestCase
7-
class TableWithRealColumn < ActiveRecord::Base; end
8-
9-
fixtures :posts, :tasks
10-
5+
class AdapterTestSqlserver < ActiveRecord::TestCase
6+
117
def setup
128
@connection = ActiveRecord::Base.connection
139
end
14-
15-
def teardown
16-
@connection.execute("SET LANGUAGE us_english") rescue nil
10+
11+
12+
def test_update_sql_statement_invalid
13+
assert_raise(ActiveRecord::StatementInvalid) { Topic.connection.update_sql("UPDATE XXX") }
1714
end
18-
15+
1916
def test_real_column_has_float_type
2017
assert_equal :float, TableWithRealColumn.columns_hash["real_number"].type
2118
end
22-
23-
# SQL Server 2000 has a bug where some unambiguous date formats are not
24-
# correctly identified if the session language is set to german
19+
2520
def test_date_insertion_when_language_is_german
2621
@connection.execute("SET LANGUAGE deutsch")
27-
2822
assert_nothing_raised do
2923
Task.create(:starting => Time.utc(2000, 1, 31, 5, 42, 0), :ending => Date.new(2006, 12, 31))
3024
end
25+
ensure
26+
@connection.execute("SET LANGUAGE us_english") rescue nil
3127
end
32-
28+
3329
def test_indexes_with_descending_order
34-
# Make sure we have an index with descending order
3530
@connection.execute "CREATE INDEX idx_credit_limit ON accounts (credit_limit DESC)" rescue nil
3631
assert_equal ["credit_limit"], @connection.indexes('accounts').first.columns
3732
ensure
3833
@connection.execute "DROP INDEX accounts.idx_credit_limit"
3934
end
40-
41-
def test_execute_without_block_closes_statement
42-
assert_all_statements_used_are_closed do
43-
@connection.execute("SELECT 1")
44-
end
45-
end
46-
47-
def test_execute_with_block_closes_statement
48-
assert_all_statements_used_are_closed do
49-
@connection.execute("SELECT 1") do |sth|
50-
assert !sth.finished?, "Statement should still be alive within block"
51-
end
52-
end
53-
end
54-
55-
def test_insert_with_identity_closes_statement
56-
assert_all_statements_used_are_closed do
57-
@connection.insert("INSERT INTO accounts ([id], [firm_id],[credit_limit]) values (999, 1, 50)")
58-
end
59-
end
60-
61-
def test_insert_without_identity_closes_statement
62-
assert_all_statements_used_are_closed do
63-
@connection.insert("INSERT INTO accounts ([firm_id],[credit_limit]) values (1, 50)")
64-
end
65-
end
66-
67-
def test_active_closes_statement
68-
assert_all_statements_used_are_closed do
69-
@connection.active?
70-
end
71-
end
72-
73-
def assert_all_statements_used_are_closed(&block)
74-
existing_handles = []
75-
ObjectSpace.each_object(DBI::StatementHandle) {|handle| existing_handles << handle}
76-
GC.disable
77-
78-
yield
79-
80-
used_handles = []
81-
ObjectSpace.each_object(DBI::StatementHandle) {|handle| used_handles << handle unless existing_handles.include? handle}
82-
83-
assert_block "No statements were used within given block" do
84-
used_handles.size > 0
85-
end
86-
87-
ObjectSpace.each_object(DBI::StatementHandle) do |handle|
88-
assert_block "Statement should have been closed within given block" do
89-
handle.finished?
90-
end
91-
end
35+
36+
def test_escaped_table_name
37+
old_table_name, new_table_name = Topic.table_name, '[topics]'
38+
Topic.table_name = new_table_name
39+
assert_nothing_raised { ActiveRecord::Base.connection.select_all "SELECT * FROM #{new_table_name}" }
40+
assert_equal new_table_name, Topic.table_name
41+
assert_equal 12, Topic.columns.length
9242
ensure
93-
GC.enable
94-
end
95-
end
96-
97-
class TypeToSqlForIntegersTest < ActiveRecord::TestCase
98-
99-
def setup
100-
@connection = ActiveRecord::Base.connection
101-
end
102-
103-
# TODO - ugh these tests are pretty literal...
104-
def test_should_create_integers_when_no_limit_supplied
105-
assert_equal 'integer', @connection.type_to_sql(:integer)
106-
end
107-
108-
def test_should_create_integers_when_limit_is_4
109-
assert_equal 'integer', @connection.type_to_sql(:integer, 4)
110-
end
111-
112-
def test_should_create_integers_when_limit_is_3
113-
assert_equal 'integer', @connection.type_to_sql(:integer, 3)
114-
end
115-
116-
def test_should_create_smallints_when_limit_is_less_than_3
117-
assert_equal 'smallint', @connection.type_to_sql(:integer, 2)
118-
assert_equal 'smallint', @connection.type_to_sql(:integer, 1)
119-
end
120-
121-
def test_should_create_bigints_when_limit_is_greateer_than_4
122-
assert_equal 'bigint', @connection.type_to_sql(:integer, 5)
123-
assert_equal 'bigint', @connection.type_to_sql(:integer, 6)
124-
assert_equal 'bigint', @connection.type_to_sql(:integer, 7)
125-
assert_equal 'bigint', @connection.type_to_sql(:integer, 8)
126-
end
127-
128-
end
129-
130-
# NOTE: The existing schema_dumper_test doesn't test the limits of <4 limit things
131-
# for adapaters that aren't mysql, sqlite or postgres. We should.
132-
# It also only tests non-standard id dumping for mysql. We'll do that too.
133-
class SchemaDumperForSqlServerTest < ActiveRecord::TestCase
134-
def standard_dump(ignore_tables = [])
135-
stream = StringIO.new
136-
ActiveRecord::SchemaDumper.ignore_tables = [*ignore_tables]
137-
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
138-
stream.string
43+
Topic.table_name = old_table_name
13944
end
14045

141-
def test_schema_dump_includes_limit_constraint_for_integer_columns
142-
output = standard_dump(/^(?!integer_limits)/)
143-
assert_match %r{c_int_1.*:limit => 2}, output
144-
assert_match %r{c_int_2.*:limit => 2}, output
145-
assert_match %r{c_int_3.*}, output
146-
assert_match %r{c_int_4.*}, output
147-
assert_no_match %r{c_int_3.*:limit}, output
148-
assert_no_match %r{c_int_4.*:limit}, output
149-
end
15046

151-
def test_sqlserver_schema_dump_should_honor_nonstandard_primary_keys
152-
output = standard_dump
153-
match = output.match(%r{create_table "movies"(.*)do})
154-
assert_not_nil(match, "nonstandardpk table not found")
155-
assert_match %r(:primary_key => "movieid"), match[1], "non-standard primary key not preserved"
156-
end
15747
end
15848

159-
class StringDefaultsTest < ActiveRecord::TestCase
160-
class StringDefaults < ActiveRecord::Base; end;
161-
162-
def test_sqlserver_default_strings_before_save
163-
default = StringDefaults.new
164-
assert_equal nil, default.string_with_null_default
165-
assert_equal 'null', default.string_with_pretend_null_one
166-
assert_equal '(null)', default.string_with_pretend_null_two
167-
assert_equal 'NULL', default.string_with_pretend_null_three
168-
assert_equal '(NULL)', default.string_with_pretend_null_four
169-
end
170-
171-
def test_sqlserver_default_strings_after_save
172-
default = StringDefaults.create
173-
assert_equal nil, default.string_with_null_default
174-
assert_equal 'null', default.string_with_pretend_null_one
175-
assert_equal '(null)', default.string_with_pretend_null_two
176-
assert_equal 'NULL', default.string_with_pretend_null_three
177-
assert_equal '(NULL)', default.string_with_pretend_null_four
178-
end
179-
180-
end

test/cases/affected_rows_test_sqlserver.rb

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)