Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8049: unit_tests.diff

File unit_tests.diff, 15.9 kB (added by FooBarWidget, 1 year ago)

New PostgreSQL unit tests

  • activerecord/test/migration_test.rb

    old new  
    194194      Person.connection.create_table :testings do |t| 
    195195        t.column :foo, :string 
    196196      end 
    197        
    198       con = Person.connection      
     197 
     198      con = Person.connection 
    199199      Person.connection.enable_identity_insert("testings", true) if current_adapter?(:SybaseAdapter) 
    200200      Person.connection.execute "insert into testings (#{con.quote_column_name('id')}, #{con.quote_column_name('foo')}) values (1, 'hello')" 
    201201      Person.connection.enable_identity_insert("testings", false) if current_adapter?(:SybaseAdapter) 
     
    278278 
    279279      # Test for 30 significent digits (beyond the 16 of float), 10 of them 
    280280      # after the decimal place. 
    281       
     281 
    282282      assert_equal BigDecimal.new("0012345678901234567890.0123456789"), bob.wealth 
    283283 
    284284      assert_equal true, bob.male? 
     
    405405          t.column :url, :string 
    406406        end 
    407407        ActiveRecord::Base.connection.add_index :octopuses, :url 
    408          
     408 
    409409        ActiveRecord::Base.connection.rename_table :octopuses, :octopi 
    410410 
    411411        # Using explicit id in insert for compatibility across all databases 
     
    436436      old_columns = Topic.connection.columns(Topic.table_name, "#{name} Columns") 
    437437      assert old_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true } 
    438438      assert_nothing_raised { Topic.connection.change_column :topics, :approved, :boolean, :default => false } 
    439       new_columns = Topic.connection.columns(Topic.table_name, "#{name} Columns")      
     439      new_columns = Topic.connection.columns(Topic.table_name, "#{name} Columns") 
    440440      assert_nil new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true } 
    441441      assert new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == false } 
    442442      assert_nothing_raised { Topic.connection.change_column :topics, :approved, :boolean, :default => true } 
    443443    end 
    444      
     444 
    445445    def test_change_column_with_nil_default 
    446446      Person.connection.add_column "people", "contributor", :boolean, :default => true 
    447447      Person.reset_column_information 
    448448      assert Person.new.contributor? 
    449        
     449 
    450450      assert_nothing_raised { Person.connection.change_column "people", "contributor", :boolean, :default => nil } 
    451451      Person.reset_column_information 
    452452      assert !Person.new.contributor? 
     
    466466    ensure 
    467467      Person.connection.remove_column("people", "administrator") rescue nil 
    468468    end 
    469      
     469 
    470470    def test_change_column_default 
    471471      Person.connection.change_column_default "people", "first_name", "Tester" 
    472472      Person.reset_column_information 
     
    731731    def test_migrator_with_missing_version_numbers 
    732732      ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations_with_missing_versions/', 500) 
    733733      assert !Person.column_methods_hash.include?(:middle_name) 
    734        assert_equal 4, ActiveRecord::Migrator.current_version 
    735                          
    736                        ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations_with_missing_versions/', 2) 
    737                        assert !Reminder.table_exists? 
    738       assert Person.column_methods_hash.include?(:last_name)                    
    739                        assert_equal 2, ActiveRecord::Migrator.current_version 
     734      assert_equal 4, ActiveRecord::Migrator.current_version 
     735 
     736      ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations_with_missing_versions/', 2) 
     737      assert !Reminder.table_exists? 
     738      assert Person.column_methods_hash.include?(:last_name) 
     739      assert_equal 2, ActiveRecord::Migrator.current_version 
    740740    end 
    741      
     741 
    742742    def test_create_table_with_custom_sequence_name 
    743743      return unless current_adapter? :OracleAdapter 
    744744 
     
    778778 
    779779  end 
    780780end 
    781  
  • activerecord/test/datatype_test_postgresql.rb

    old new  
    11require 'abstract_unit' 
    22 
    3 class PostgresqlDatatype < ActiveRecord::Base 
     3class PostgresqlArray < ActiveRecord::Base 
    44end 
    55 
    6 class PGDataTypeTest < Test::Unit::TestCase 
     6class PostgresqlMoney < ActiveRecord::Base 
     7end 
     8 
     9class PostgresqlNumber < ActiveRecord::Base 
     10end 
     11 
     12class PostgresqlTime < ActiveRecord::Base 
     13end 
     14 
     15class PostgresqlNetworkAddress < ActiveRecord::Base 
     16end 
     17 
     18class PostgresqlBitString < ActiveRecord::Base 
     19end 
     20 
     21class PostgresqlOid < ActiveRecord::Base 
     22end 
     23 
     24class PostgresqlDataTypeTest < Test::Unit::TestCase 
    725  self.use_transactional_fixtures = false 
    826 
    9   TABLE_NAME = 'postgresql_datatypes' 
    10   COLUMNS = [ 
     27  ARRAY_TABLE_NAME = 'postgresql_arrays' 
     28  ARRAY_COLUMNS = [ 
    1129    'id SERIAL PRIMARY KEY', 
    1230    'commission_by_quarter INTEGER[]', 
    1331    'nicknames TEXT[]' 
    1432  ] 
    1533 
     34  MONEY_TABLE_NAME = 'postgresql_moneys' 
     35  MONEY_COLUMNS = [ 
     36    'id SERIAL PRIMARY KEY', 
     37    'wealth MONEY' 
     38  ] 
     39 
     40  NUMBER_TABLE_NAME = 'postgresql_numbers' 
     41  NUMBER_COLUMNS = [ 
     42    'id SERIAL PRIMARY KEY',   
     43    'single REAL', 
     44    'double DOUBLE PRECISION' 
     45  ] 
     46 
     47  TIME_TABLE_NAME = 'postgresql_times' 
     48  TIME_COLUMNS = [ 
     49    'id SERIAL PRIMARY KEY', 
     50    'time_interval INTERVAL' 
     51  ] 
     52 
     53  NETWORK_ADDRESS_TABLE_NAME = 'postgresql_network_addresses' 
     54  NETWORK_ADDRESS_COLUMNS = [ 
     55    'id SERIAL PRIMARY KEY', 
     56    'cidr_address CIDR', 
     57    'inet_address INET', 
     58    'mac_address MACADDR' 
     59  ] 
     60 
     61  BIT_STRING_TABLE_NAME = 'postgresql_bit_strings' 
     62  BIT_STRING_COLUMNS = [ 
     63    'id SERIAL PRIMARY KEY', 
     64    'bit_string BIT(8)', 
     65    'bit_string_varying BIT VARYING(8)' 
     66  ] 
     67 
     68  OID_TABLE_NAME = 'postgresql_oids' 
     69  OID_COLUMNS = [ 
     70    'id SERIAL PRIMARY KEY', 
     71    'obj_id OID' 
     72  ] 
     73 
    1674  def setup 
    1775    @connection = ActiveRecord::Base.connection 
    18     @connection.execute "CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})" 
    19     @connection.execute "INSERT INTO #{TABLE_NAME} (commission_by_quarter, nicknames) VALUES ( '{35000,21000,18000,17000}', '{foo,bar,baz}' )" 
    20     @first = PostgresqlDatatype.find( 1 ) 
     76 
     77    @connection.execute("CREATE TABLE #{ARRAY_TABLE_NAME} (#{ARRAY_COLUMNS.join(',')})") 
     78    @connection.execute("INSERT INTO #{ARRAY_TABLE_NAME} (commission_by_quarter, nicknames) VALUES ( '{35000,21000,18000,17000}', '{foo,bar,baz}' )") 
     79    @first_array = PostgresqlArray.find(1) 
     80 
     81    @connection.execute("CREATE TABLE #{MONEY_TABLE_NAME} (#{MONEY_COLUMNS.join(',')})") 
     82    @connection.execute("INSERT INTO #{MONEY_TABLE_NAME} (wealth) VALUES ('$567.89')") 
     83    @connection.execute("INSERT INTO #{MONEY_TABLE_NAME} (wealth) VALUES ('-$567.89')") 
     84    @first_money = PostgresqlMoney.find(1) 
     85    @second_money = PostgresqlMoney.find(2) 
     86 
     87    @connection.execute("CREATE TABLE #{NUMBER_TABLE_NAME} (#{NUMBER_COLUMNS.join(',')})") 
     88    @connection.execute("INSERT INTO #{NUMBER_TABLE_NAME} (single, double) VALUES (123.456, 123456.789)") 
     89    @first_number = PostgresqlNumber.find(1) 
     90 
     91    @connection.execute("CREATE TABLE #{TIME_TABLE_NAME} (#{TIME_COLUMNS.join(',')})") 
     92    @connection.execute("INSERT INTO #{TIME_TABLE_NAME} (time_interval) VALUES ('1 year 2 days ago')") 
     93    @first_time = PostgresqlTime.find(1) 
     94 
     95    @connection.execute("CREATE TABLE #{NETWORK_ADDRESS_TABLE_NAME} (#{NETWORK_ADDRESS_COLUMNS.join(',')})") 
     96    @connection.execute("INSERT INTO #{NETWORK_ADDRESS_TABLE_NAME} (cidr_address, inet_address, mac_address) VALUES('192.168.0/24', '172.16.1.254/32', '01:23:45:67:89:0a')") 
     97    @first_network_address = PostgresqlNetworkAddress.find(1) 
     98     
     99    @connection.execute("CREATE TABLE #{BIT_STRING_TABLE_NAME} (#{BIT_STRING_COLUMNS.join(',')})") 
     100    @connection.execute("INSERT INTO #{BIT_STRING_TABLE_NAME} (bit_string, bit_string_varying) VALUES (B'00010101', X'15')") 
     101    @first_bit_string = PostgresqlBitString.find(1) 
     102     
     103    @connection.execute("CREATE TABLE #{OID_TABLE_NAME} (#{OID_COLUMNS.join(',')})") 
     104    @connection.execute("INSERT INTO #{OID_TABLE_NAME} (obj_id) VALUES (1234)") 
     105    @first_oid = PostgresqlOid.find(1) 
    21106  end 
    22107 
    23108  def teardown 
    24     @connection.execute "DROP TABLE #{TABLE_NAME}" 
     109    @connection.execute("DROP TABLE #{ARRAY_TABLE_NAME}") 
     110    @connection.execute("DROP TABLE #{MONEY_TABLE_NAME}") 
     111    @connection.execute("DROP TABLE #{NUMBER_TABLE_NAME}") 
     112    @connection.execute("DROP TABLE #{TIME_TABLE_NAME}") 
     113    @connection.execute("DROP TABLE #{NETWORK_ADDRESS_TABLE_NAME}") 
     114    @connection.execute("DROP TABLE #{BIT_STRING_TABLE_NAME}") 
     115    @connection.execute("DROP TABLE #{OID_TABLE_NAME}") 
    25116  end 
    26117 
    27118  def test_data_type_of_array_types 
    28     assert_equal :string, @first.column_for_attribute("commission_by_quarter").type 
    29     assert_equal :string, @first.column_for_attribute("nicknames").type 
     119    assert_equal :string, @first_array.column_for_attribute("commission_by_quarter").type 
     120    assert_equal :string, @first_array.column_for_attribute("nicknames").type 
    30121  end 
    31122 
     123  def test_data_type_of_money_types 
     124    assert_equal :decimal, @first_money.column_for_attribute("wealth").type 
     125  end 
     126 
     127  def test_data_type_of_number_types 
     128    assert_equal :float, @first_number.column_for_attribute("single").type 
     129    assert_equal :float, @first_number.column_for_attribute("double").type 
     130  end 
     131 
     132  def test_data_type_of_time_types 
     133    assert_equal :string, @first_time.column_for_attribute("time_interval").type 
     134  end 
     135 
     136  def test_data_type_of_network_address_types 
     137    assert_equal :string, @first_network_address.column_for_attribute("cidr_address").type 
     138    assert_equal :string, @first_network_address.column_for_attribute("inet_address").type 
     139    assert_equal :string, @first_network_address.column_for_attribute("mac_address").type 
     140  end 
     141 
     142  def test_data_type_of_bit_string_types 
     143    assert_equal :string, @first_bit_string.column_for_attribute("bit_string").type 
     144    assert_equal :string, @first_bit_string.column_for_attribute("bit_string_varying").type 
     145  end 
     146 
     147  def test_data_type_of_oid_types 
     148    assert_equal :integer, @first_oid.column_for_attribute("obj_id").type 
     149  end 
     150 
    32151  def test_array_values 
    33     assert_equal '{35000,21000,18000,17000}', @first.commission_by_quarter 
    34     assert_equal '{foo,bar,baz}', @first.nicknames 
     152    assert_equal '{35000,21000,18000,17000}', @first_array.commission_by_quarter 
     153    assert_equal '{foo,bar,baz}', @first_array.nicknames 
    35154  end 
    36155 
     156  def test_money_values 
     157    assert_equal 567.89, @first_money.wealth 
     158    assert_equal -567.89, @second_money.wealth 
     159  end 
     160 
     161  def test_number_values 
     162    assert_equal 123.456, @first_number.single 
     163    assert_equal 123456.789, @first_number.double 
     164  end 
     165 
     166  def test_time_values 
     167    assert_equal '-1 years -2 days', @first_time.time_interval 
     168  end 
     169 
     170  def test_network_address_values 
     171    assert_equal '192.168.0.0/24', @first_network_address.cidr_address 
     172    assert_equal '172.16.1.254', @first_network_address.inet_address 
     173    assert_equal '01:23:45:67:89:0a', @first_network_address.mac_address 
     174  end 
     175 
     176  def test_bit_string_values 
     177    assert_equal '00010101', @first_bit_string.bit_string 
     178    assert_equal '00010101', @first_bit_string.bit_string_varying 
     179  end 
     180 
     181  def test_oid_values 
     182    assert_equal 1234, @first_oid.obj_id 
     183  end 
     184 
    37185  def test_update_integer_array 
    38186    new_value = '{32800,95000,29350,17000}' 
    39     assert @first.commission_by_quarter = new_value 
    40     assert @first.save 
    41     assert @first.reload 
    42     assert_equal @first.commission_by_quarter, new_value 
     187    assert @first_array.commission_by_quarter = new_value 
     188    assert @first_array.save 
     189    assert @first_array.reload 
     190    assert_equal @first_array.commission_by_quarter, new_value 
    43191  end 
    44192 
    45193  def test_update_text_array 
    46194    new_value = '{robby,robert,rob,robbie}' 
    47     assert @first.nicknames = new_value 
    48     assert @first.save 
    49     assert @first.reload 
    50     assert_equal @first.nicknames, new_value 
     195    assert @first_array.nicknames = new_value 
     196    assert @first_array.save 
     197    assert @first_array.reload 
     198    assert_equal @first_array.nicknames, new_value 
    51199  end 
     200 
     201  def test_update_money 
     202    new_value = 123.45 
     203    assert @first_money.wealth = new_value 
     204    assert @first_money.save 
     205    assert @first_money.reload 
     206    assert_equal @first_money.wealth, new_value 
     207  end 
     208 
     209  def test_update_number 
     210    new_single = 789.012 
     211    new_double = 789012.345 
     212    assert @first_number.single = new_single 
     213    assert @first_number.double = new_double 
     214    assert @first_number.save 
     215    assert @first_number.reload 
     216    assert_equal @first_number.single, new_single 
     217    assert_equal @first_number.double, new_double 
     218  end 
     219 
     220  def test_update_time 
     221    assert @first_time.time_interval = '2 years 3 minutes' 
     222    assert @first_time.save 
     223    assert @first_time.reload 
     224    assert_equal @first_time.time_interval, '2 years 00:03:00' 
     225  end 
     226 
     227  def test_update_network_address 
     228    new_cidr_address = '10.1.2.3/32' 
     229    new_inet_address = '10.0.0.0/8' 
     230    new_mac_address = 'bc:de:f0:12:34:56' 
     231    assert @first_network_address.cidr_address = new_cidr_address 
     232    assert @first_network_address.inet_address = new_inet_address 
     233    assert @first_network_address.mac_address = new_mac_address 
     234    assert @first_network_address.save 
     235    assert @first_network_address.reload 
     236    assert_equal @first_network_address.cidr_address, new_cidr_address 
     237    assert_equal @first_network_address.inet_address, new_inet_address 
     238    assert_equal @first_network_address.mac_address, new_mac_address 
     239  end 
     240 
     241  def test_update_bit_string 
     242    new_bit_string = '11111111' 
     243    new_bit_string_varying = 'FF' 
     244    assert @first_bit_string.bit_string = new_bit_string 
     245    assert @first_bit_string.bit_string_varying = new_bit_string_varying 
     246    assert @first_bit_string.save 
     247    assert @first_bit_string.reload 
     248    assert_equal @first_bit_string.bit_string, new_bit_string 
     249    assert_equal @first_bit_string.bit_string, @first_bit_string.bit_string_varying 
     250  end 
     251 
     252  def test_update_oid 
     253    new_value = 567890 
     254    assert @first_oid.obj_id = new_value 
     255    assert @first_oid.save 
     256    assert @first_oid.reload 
     257    assert_equal @first_oid.obj_id, new_value 
     258  end 
    52259end 
  • activerecord/test/finder_test.rb

    old new  
    232232  end 
    233233 
    234234  def test_bind_enumerable 
     235    quoted_abc = %(#{ActiveRecord::Base.connection.quote('a')},#{ActiveRecord::Base.connection.quote('b')},#{ActiveRecord::Base.connection.quote('c')}) 
     236 
    235237    assert_equal '1,2,3', bind('?', [1, 2, 3]) 
    236     assert_equal %('a','b','c'), bind('?', %w(a b c)) 
     238    assert_equal quoted_abc, bind('?', %w(a b c)) 
    237239 
    238240    assert_equal '1,2,3', bind(':a', :a => [1, 2, 3]) 
    239     assert_equal %('a','b','c'), bind(':a', :a => %w(a b c)) # ' 
     241    assert_equal quoted_abc, bind(':a', :a => %w(a b c)) # ' 
    240242 
    241243    require 'set' 
    242244    assert_equal '1,2,3', bind('?', Set.new([1, 2, 3])) 
    243     assert_equal %('a','b','c'), bind('?', Set.new(%w(a b c))) 
     245    assert_equal quoted_abc, bind('?', Set.new(%w(a b c))) 
    244246 
    245247    assert_equal '1,2,3', bind(':a', :a => Set.new([1, 2, 3])) 
    246     assert_equal %('a','b','c'), bind(':a', :a => Set.new(%w(a b c))) # ' 
     248    assert_equal quoted_abc, bind(':a', :a => Set.new(%w(a b c))) # ' 
    247249  end 
    248250 
    249251  def test_bind_empty_enumerable 
     
    254256  end 
    255257 
    256258  def test_bind_string 
    257     assert_equal "''", bind('?', '') 
     259    assert_equal ActiveRecord::Base.connection.quote(''), bind('?', '') 
    258260  end 
    259261 
    260262  def test_bind_record 
     
    267269 
    268270  def test_string_sanitation 
    269271    assert_not_equal "'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1") 
    270     assert_equal "'something; select table'", ActiveRecord::Base.sanitize("something; select table") 
     272    assert_not_equal "#{ActiveRecord::Base.connection.quoted_string_prefix}'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1") 
     273    assert_equal "#{ActiveRecord::Base.connection.quoted_string_prefix}'something; select table'", ActiveRecord::Base.sanitize("something; select table") 
    271274  end 
    272275 
    273276  def test_count