|
1 | 1 | require 'cases/sqlserver_helper' |
2 | 2 | require 'models/book' |
3 | 3 |
|
4 | | -class WhenSelectingWithLimitOffsetAndLimitTest < ActiveRecord::TestCase |
| 4 | +class OffsetAndLimitTestSqlserver < ActiveRecord::TestCase |
| 5 | + |
| 6 | + class Account < ActiveRecord::Base; end |
| 7 | + |
5 | 8 | def setup |
6 | 9 | @connection = ActiveRecord::Base.connection |
7 | | - @select_sql = 'SELECT * FROM schema' |
8 | 10 | end |
| 11 | + |
| 12 | + context 'When selecting with limit' do |
9 | 13 |
|
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 |
14 | 17 |
|
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 |
19 | 22 |
|
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 |
24 | 27 |
|
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 |
30 | 37 |
|
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} |
42 | 38 | end |
43 | 39 |
|
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 |
48 | 41 |
|
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 |
53 | 50 |
|
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 |
58 | 55 |
|
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 |
64 | 77 |
|
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) } |
69 | 78 | end |
| 79 | + |
| 80 | + |
70 | 81 | end |
| 82 | + |
0 commit comments