|
1 | 1 | require 'cases/sqlserver_helper' |
2 | | -require 'models/default' |
3 | | -require 'models/post' |
4 | 2 | require 'models/task' |
| 3 | +require 'models/topic' |
5 | 4 |
|
6 | | -class SqlServerAdapterTest < ActiveRecord::TestCase |
7 | | - class TableWithRealColumn < ActiveRecord::Base; end |
8 | | - |
9 | | - fixtures :posts, :tasks |
10 | | - |
| 5 | +class AdapterTestSqlserver < ActiveRecord::TestCase |
| 6 | + |
11 | 7 | def setup |
12 | 8 | @connection = ActiveRecord::Base.connection |
13 | 9 | 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") } |
17 | 14 | end |
18 | | - |
| 15 | + |
19 | 16 | def test_real_column_has_float_type |
20 | 17 | assert_equal :float, TableWithRealColumn.columns_hash["real_number"].type |
21 | 18 | 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 | + |
25 | 20 | def test_date_insertion_when_language_is_german |
26 | 21 | @connection.execute("SET LANGUAGE deutsch") |
27 | | - |
28 | 22 | assert_nothing_raised do |
29 | 23 | Task.create(:starting => Time.utc(2000, 1, 31, 5, 42, 0), :ending => Date.new(2006, 12, 31)) |
30 | 24 | end |
| 25 | + ensure |
| 26 | + @connection.execute("SET LANGUAGE us_english") rescue nil |
31 | 27 | end |
32 | | - |
| 28 | + |
33 | 29 | def test_indexes_with_descending_order |
34 | | - # Make sure we have an index with descending order |
35 | 30 | @connection.execute "CREATE INDEX idx_credit_limit ON accounts (credit_limit DESC)" rescue nil |
36 | 31 | assert_equal ["credit_limit"], @connection.indexes('accounts').first.columns |
37 | 32 | ensure |
38 | 33 | @connection.execute "DROP INDEX accounts.idx_credit_limit" |
39 | 34 | 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 |
92 | 42 | 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 |
139 | 44 | end |
140 | 45 |
|
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 |
150 | 46 |
|
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 |
157 | 47 | end |
158 | 48 |
|
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 |
0 commit comments