Skip to content

Commit 70d7c9d

Browse files
committed
Move to Shoulda syntax for adapter specific tests.
1 parent 16a2831 commit 70d7c9d

9 files changed

+149
-105
lines changed

test/cases/aaaa_create_tables_test_sqlserver.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
require 'cases/sqlserver_helper'
33

44
class AAAACreateTablesTestSqlserver < ActiveRecord::TestCase
5-
6-
def test_load_sqlserver_specific_schema
5+
6+
should 'load sqlserver specific schema' do
77
sqlserver_specific_schema_file = File.expand_path(File.join(File.dirname(__FILE__), '..', 'schema', 'sqlserver_specific_schema.rb'))
88
eval(File.read(sqlserver_specific_schema_file))
99
assert true

test/cases/adapter_test_sqlserver.rb

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,62 @@ def setup
99
end
1010

1111

12-
def test_update_sql_statement_invalid
12+
should 'raise invalid statement error' do
1313
assert_raise(ActiveRecord::StatementInvalid) { Topic.connection.update_sql("UPDATE XXX") }
1414
end
1515

16-
def test_real_column_has_float_type
16+
should 'return real_number as float' do
1717
assert_equal :float, TableWithRealColumn.columns_hash["real_number"].type
1818
end
1919

20-
def test_date_insertion_when_language_is_german
21-
@connection.execute("SET LANGUAGE deutsch")
22-
assert_nothing_raised do
23-
Task.create(:starting => Time.utc(2000, 1, 31, 5, 42, 0), :ending => Date.new(2006, 12, 31))
20+
context 'With different language' do
21+
22+
teardown do
23+
@connection.execute("SET LANGUAGE us_english") rescue nil
24+
end
25+
26+
should 'do a date insertion when language is german' do
27+
@connection.execute("SET LANGUAGE deutsch")
28+
assert_nothing_raised do
29+
Task.create(:starting => Time.utc(2000, 1, 31, 5, 42, 0), :ending => Date.new(2006, 12, 31))
30+
end
2431
end
25-
ensure
26-
@connection.execute("SET LANGUAGE us_english") rescue nil
32+
2733
end
2834

29-
def test_indexes_with_descending_order
30-
@connection.execute "CREATE INDEX idx_credit_limit ON accounts (credit_limit DESC)" rescue nil
31-
assert_equal ["credit_limit"], @connection.indexes('accounts').first.columns
32-
ensure
33-
@connection.execute "DROP INDEX accounts.idx_credit_limit"
35+
context 'For indexes' do
36+
37+
setup do
38+
@connection.execute "CREATE INDEX idx_credit_limit ON accounts (credit_limit DESC)" rescue nil
39+
end
40+
41+
teardown do
42+
@connection.execute "DROP INDEX accounts.idx_credit_limit"
43+
end
44+
45+
should 'have indexes with descending order' do
46+
assert_equal ["credit_limit"], @connection.indexes('accounts').first.columns
47+
end
48+
3449
end
3550

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
42-
ensure
43-
Topic.table_name = old_table_name
51+
context 'For .table_name' do
52+
53+
setup do
54+
@old_table_name, @new_table_name = Topic.table_name, '[topics]'
55+
Topic.table_name = @new_table_name
56+
end
57+
58+
teardown do
59+
Topic.table_name = @old_table_name
60+
end
61+
62+
should 'escape table name' do
63+
assert_nothing_raised { @connection.select_all "SELECT * FROM #{@new_table_name}" }
64+
assert_equal @new_table_name, Topic.table_name
65+
assert_equal 12, Topic.columns.length
66+
end
67+
4468
end
4569

4670

test/cases/column_test_sqlserver.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def setup
66
@column_klass = ActiveRecord::ConnectionAdapters::SQLServerColumn
77
end
88

9-
def test_placeholder
9+
should 'be a placeholder' do
1010
assert true
1111
end
1212

test/cases/connection_test_sqlserver.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def setup
1313
end
1414

1515

16-
def test_affected_rows
16+
should 'affecte rows' do
1717
assert Topic.connection.instance_variable_get("@connection")["AutoCommit"]
1818
topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } }
1919
updated = Topic.update(topic_data.keys, topic_data.values)
@@ -23,33 +23,33 @@ def test_affected_rows
2323
assert_equal 2, Topic.delete([1, 2])
2424
end
2525

26-
def test_execute_without_block_closes_statement
26+
should 'execute without block closes statement' do
2727
assert_all_statements_used_are_closed do
2828
@connection.execute("SELECT 1")
2929
end
3030
end
3131

32-
def test_execute_with_block_closes_statement
32+
should 'execute with block closes statement' do
3333
assert_all_statements_used_are_closed do
3434
@connection.execute("SELECT 1") do |sth|
3535
assert !sth.finished?, "Statement should still be alive within block"
3636
end
3737
end
3838
end
3939

40-
def test_insert_with_identity_closes_statement
40+
should 'insert with identity closes statement' do
4141
assert_all_statements_used_are_closed do
4242
@connection.insert("INSERT INTO accounts ([id], [firm_id],[credit_limit]) values (999, 1, 50)")
4343
end
4444
end
4545

46-
def test_insert_without_identity_closes_statement
46+
should 'insert without identity closes statement' do
4747
assert_all_statements_used_are_closed do
4848
@connection.insert("INSERT INTO accounts ([firm_id],[credit_limit]) values (1, 50)")
4949
end
5050
end
5151

52-
def test_active_closes_statement
52+
should 'active closes statement' do
5353
assert_all_statements_used_are_closed do
5454
@connection.active?
5555
end
Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,82 @@
11
require 'cases/sqlserver_helper'
22
require 'models/book'
33

4-
class WhenSelectingWithLimitOffsetAndLimitTest < ActiveRecord::TestCase
4+
class OffsetAndLimitTestSqlserver < ActiveRecord::TestCase
5+
6+
class Account < ActiveRecord::Base; end
7+
58
def setup
69
@connection = ActiveRecord::Base.connection
7-
@select_sql = 'SELECT * FROM schema'
810
end
11+
12+
context 'When selecting with limit' do
913

10-
def test_should_alter_SQL_to_limit_number_of_records_returned
11-
options = { :limit => 10 }
12-
assert_equal('SELECT TOP 10 * FROM schema', @connection.add_limit_offset!(@select_sql, options))
13-
end
14+
setup do
15+
@select_sql = 'SELECT * FROM schema'
16+
end
1417

15-
def test_should_only_allow_integers_for_limit
16-
options = { :limit => 'ten' }
17-
assert_raise(ArgumentError) {@connection.add_limit_offset!(@select_sql, options) }
18-
end
18+
should 'alter SQL to limit number of records returned' do
19+
options = { :limit => 10 }
20+
assert_equal('SELECT TOP 10 * FROM schema', @connection.add_limit_offset!(@select_sql, options))
21+
end
1922

20-
def test_should_convert_strings_which_look_like_integers_to_integers
21-
options = { :limit => '42' }
22-
assert_nothing_raised(ArgumentError) {@connection.add_limit_offset!(@select_sql, options)}
23-
end
23+
should 'only allow integers for limit' do
24+
options = { :limit => 'ten' }
25+
assert_raise(ArgumentError) {@connection.add_limit_offset!(@select_sql, options) }
26+
end
2427

25-
def test_should_not_allow_sql_injection_via_limit
26-
options = { :limit => '1 * FROM schema; DELETE * FROM table; SELECT TOP 10 *'}
27-
assert_raise(ArgumentError) { @connection.add_limit_offset!(@select_sql, options) }
28-
end
29-
end
28+
should 'convert strings which look like integers to integers' do
29+
options = { :limit => '42' }
30+
assert_nothing_raised(ArgumentError) {@connection.add_limit_offset!(@select_sql, options)}
31+
end
32+
33+
should 'not allow sql injection via limit' do
34+
options = { :limit => '1 * FROM schema; DELETE * FROM table; SELECT TOP 10 *'}
35+
assert_raise(ArgumentError) { @connection.add_limit_offset!(@select_sql, options) }
36+
end
3037

31-
class WhenSelectingWithLimitAndOffsetOffsetAndLimitTest < ActiveRecord::TestCase
32-
class Account < ActiveRecord::Base; end
33-
def setup
34-
@connection = ActiveRecord::Base.connection
35-
# we have to use a real table as we need the counts
36-
@select_sql = 'SELECT * FROM books'
37-
@books = (1..10).map {|i| Book.create!}
38-
end
39-
40-
def teardown
41-
@books.each {|b| b.destroy}
4238
end
4339

44-
def test_should_have_limit_if_offset_is_passed
45-
options = { :offset => 1 }
46-
assert_raise(ArgumentError) { @connection.add_limit_offset!(@select_sql, options) }
47-
end
40+
context 'When selecting with limit and offset' do
4841

49-
def test_should_only_allow_integers_for_offset
50-
options = { :limit => 10, :offset => 'five' }
51-
assert_raise(ArgumentError) { @connection.add_limit_offset!(@select_sql, options)}
52-
end
42+
setup do
43+
@select_sql = 'SELECT * FROM books'
44+
@books = (1..10).map {|i| Book.create!}
45+
end
46+
47+
teardown do
48+
@books.each {|b| b.destroy}
49+
end
5350

54-
def test_should_convert_strings_which_look_like_integers_to_integers
55-
options = { :limit => 10, :offset => '5' }
56-
assert_nothing_raised(ArgumentError) {@connection.add_limit_offset!(@select_sql, options)}
57-
end
51+
should 'have limit if offset is passed' do
52+
options = { :offset => 1 }
53+
assert_raise(ArgumentError) { @connection.add_limit_offset!(@select_sql, options) }
54+
end
5855

59-
def test_should_alter_SQL_to_limit_number_of_records_returned_offset_by_specified_amount
60-
options = { :limit => 3, :offset => 5 }
61-
expected_sql = %&SELECT * FROM (SELECT TOP 3 * FROM (SELECT TOP 8 * FROM books) AS tmp1) AS tmp2&
62-
assert_equal(expected_sql, @connection.add_limit_offset!(@select_sql, options))
63-
end
56+
should 'only allow integers for offset' do
57+
options = { :limit => 10, :offset => 'five' }
58+
assert_raise(ArgumentError) { @connection.add_limit_offset!(@select_sql, options)}
59+
end
60+
61+
should 'convert strings which look like integers to integers' do
62+
options = { :limit => 10, :offset => '5' }
63+
assert_nothing_raised(ArgumentError) {@connection.add_limit_offset!(@select_sql, options)}
64+
end
65+
66+
should 'alter SQL to limit number of records returned offset by specified amount' do
67+
options = { :limit => 3, :offset => 5 }
68+
expected_sql = %&SELECT * FROM (SELECT TOP 3 * FROM (SELECT TOP 8 * FROM books) AS tmp1) AS tmp2&
69+
assert_equal(expected_sql, @connection.add_limit_offset!(@select_sql, options))
70+
end
71+
72+
# Not really sure what an offset sql injection might look like
73+
should 'not allow sql injection via offset' do
74+
options = { :limit => 10, :offset => '1 * FROM schema; DELETE * FROM table; SELECT TOP 10 *'}
75+
assert_raise(ArgumentError) { @connection.add_limit_offset!(@select_sql, options) }
76+
end
6477

65-
# Not really sure what an offset sql injection might look like
66-
def test_should_not_allow_sql_injection_via_offset
67-
options = { :limit => 10, :offset => '1 * FROM schema; DELETE * FROM table; SELECT TOP 10 *'}
68-
assert_raise(ArgumentError) { @connection.add_limit_offset!(@select_sql, options) }
6978
end
79+
80+
7081
end
82+

test/cases/schema_dumper_test_sqlserver.rb

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,27 @@
55
# non-standard id dumping for mysql. We'll do that too.
66
class SchemaDumperTestSqlserver < ActiveRecord::TestCase
77

8-
def test_schema_dump_includes_limit_constraint_for_integer_columns
9-
output = standard_dump(/^(?!integer_limits)/)
10-
assert_match %r{c_int_1.*:limit => 2}, output
11-
assert_match %r{c_int_2.*:limit => 2}, output
12-
assert_match %r{c_int_3.*}, output
13-
assert_match %r{c_int_4.*}, output
14-
assert_no_match %r{c_int_3.*:limit}, output
15-
assert_no_match %r{c_int_4.*:limit}, output
8+
context 'In schema dump' do
9+
10+
should 'include limit constraint for integer columns' do
11+
output = standard_dump(/^(?!integer_limits)/)
12+
assert_match %r{c_int_1.*:limit => 2}, output
13+
assert_match %r{c_int_2.*:limit => 2}, output
14+
assert_match %r{c_int_3.*}, output
15+
assert_match %r{c_int_4.*}, output
16+
assert_no_match %r{c_int_3.*:limit}, output
17+
assert_no_match %r{c_int_4.*:limit}, output
18+
end
19+
20+
should 'honor nonstandard primary keys' do
21+
output = standard_dump
22+
match = output.match(%r{create_table "movies"(.*)do})
23+
assert_not_nil(match, "nonstandardpk table not found")
24+
assert_match %r(:primary_key => "movieid"), match[1], "non-standard primary key not preserved"
25+
end
26+
1627
end
1728

18-
def test_sqlserver_schema_dump_should_honor_nonstandard_primary_keys
19-
output = standard_dump
20-
match = output.match(%r{create_table "movies"(.*)do})
21-
assert_not_nil(match, "nonstandardpk table not found")
22-
assert_match %r(:primary_key => "movieid"), match[1], "non-standard primary key not preserved"
23-
end
2429

2530
private
2631

test/cases/schema_statement_test_sqlserver.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,29 @@ def setup
66
@connection = ActiveRecord::Base.connection
77
end
88

9-
def test_should_create_integers_when_no_limit_supplied
9+
should 'create integers when no limit supplied' do
1010
assert_equal 'integer', @connection.type_to_sql(:integer)
1111
end
1212

13-
def test_should_create_integers_when_limit_is_4
13+
should 'create integers when limit is 4' do
1414
assert_equal 'integer', @connection.type_to_sql(:integer, 4)
1515
end
1616

17-
def test_should_create_integers_when_limit_is_3
17+
should 'create integers when limit is 3' do
1818
assert_equal 'integer', @connection.type_to_sql(:integer, 3)
1919
end
2020

21-
def test_should_create_smallints_when_limit_is_less_than_3
21+
should 'create smallints when limit is less than 3' do
2222
assert_equal 'smallint', @connection.type_to_sql(:integer, 2)
2323
assert_equal 'smallint', @connection.type_to_sql(:integer, 1)
2424
end
2525

26-
def test_should_create_bigints_when_limit_is_greateer_than_4
26+
should 'create bigints when limit is greateer than 4' do
2727
assert_equal 'bigint', @connection.type_to_sql(:integer, 5)
2828
assert_equal 'bigint', @connection.type_to_sql(:integer, 6)
2929
assert_equal 'bigint', @connection.type_to_sql(:integer, 7)
3030
assert_equal 'bigint', @connection.type_to_sql(:integer, 8)
3131
end
32-
32+
33+
3334
end

test/cases/specific_schema_test_sqlserver.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
class StringDefaults < ActiveRecord::Base; end;
44

55
class SpecificSchemaTestSqlserver < ActiveRecord::TestCase
6-
7-
def test_sqlserver_default_strings_before_save
6+
7+
should 'default strings before save' do
88
default = StringDefaults.new
99
assert_equal nil, default.string_with_null_default
1010
assert_equal 'null', default.string_with_pretend_null_one
@@ -13,13 +13,13 @@ def test_sqlserver_default_strings_before_save
1313
assert_equal '(NULL)', default.string_with_pretend_null_four
1414
end
1515

16-
def test_sqlserver_default_strings_after_save
16+
should 'default strings after save' do
1717
default = StringDefaults.create
1818
assert_equal nil, default.string_with_null_default
1919
assert_equal 'null', default.string_with_pretend_null_one
2020
assert_equal '(null)', default.string_with_pretend_null_two
2121
assert_equal 'NULL', default.string_with_pretend_null_three
2222
assert_equal '(NULL)', default.string_with_pretend_null_four
2323
end
24-
24+
2525
end

test/cases/sqlserver_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'rubygems'
2+
require 'shoulda'
13
require 'cases/helper'
24

35
ActiveRecord::Migration.verbose = false

0 commit comments

Comments
 (0)