Skip to content
This repository
Browse code

moving column attributes tests to their own class

  • Loading branch information...
commit 28bb02a78fd47527bb7a208d01a4594bb212812c 1 parent 3c2e7a8
Aaron Patterson authored
174  activerecord/test/cases/migration/column_attributes_test.rb
... ...
@@ -0,0 +1,174 @@
  1
+require "cases/helper"
  2
+
  3
+module ActiveRecord
  4
+  class Migration
  5
+    class ColumnAttributesTest < ActiveRecord::TestCase
  6
+      self.use_transactional_fixtures = false
  7
+
  8
+      class TestModel < ActiveRecord::Base
  9
+        self.table_name = 'test_models'
  10
+      end
  11
+
  12
+      attr_reader :connection, :table_name
  13
+
  14
+      def setup
  15
+        super
  16
+        @connection = ActiveRecord::Base.connection
  17
+        connection.create_table :test_models do |t|
  18
+          t.timestamps
  19
+        end
  20
+
  21
+        TestModel.reset_column_information
  22
+      end
  23
+
  24
+      def teardown
  25
+        super
  26
+        connection.drop_table :test_models rescue nil
  27
+      end
  28
+
  29
+      # We specifically do a manual INSERT here, and then test only the SELECT
  30
+      # functionality. This allows us to more easily catch INSERT being broken,
  31
+      # but SELECT actually working fine.
  32
+      def test_native_decimal_insert_manual_vs_automatic
  33
+        correct_value = '0012345678901234567890.0123456789'.to_d
  34
+
  35
+        connection.add_column "test_models", "wealth", :decimal, :precision => '30', :scale => '10'
  36
+
  37
+        # Do a manual insertion
  38
+        if current_adapter?(:OracleAdapter)
  39
+          connection.execute "insert into test_models (id, wealth, created_at, updated_at) values (people_seq.nextval, 12345678901234567890.0123456789, sysdate, sysdate)"
  40
+        elsif current_adapter?(:OpenBaseAdapter) || (current_adapter?(:MysqlAdapter) && Mysql.client_version < 50003) #before mysql 5.0.3 decimals stored as strings
  41
+          connection.execute "insert into test_models (wealth, created_at, updated_at) values ('12345678901234567890.0123456789', 0, 0)"
  42
+        elsif current_adapter?(:PostgreSQLAdapter)
  43
+          connection.execute "insert into test_models (wealth, created_at, updated_at) values (12345678901234567890.0123456789, now(), now())"
  44
+        else
  45
+          connection.execute "insert into test_models (wealth, created_at, updated_at) values (12345678901234567890.0123456789, 0, 0)"
  46
+        end
  47
+
  48
+        # SELECT
  49
+        row = TestModel.find(:first)
  50
+        assert_kind_of BigDecimal, row.wealth
  51
+
  52
+        # If this assert fails, that means the SELECT is broken!
  53
+        unless current_adapter?(:SQLite3Adapter)
  54
+          assert_equal correct_value, row.wealth
  55
+        end
  56
+
  57
+        # Reset to old state
  58
+        TestModel.delete_all
  59
+
  60
+        # Now use the Rails insertion
  61
+        TestModel.create :wealth => BigDecimal.new("12345678901234567890.0123456789")
  62
+
  63
+        # SELECT
  64
+        row = TestModel.find(:first)
  65
+        assert_kind_of BigDecimal, row.wealth
  66
+
  67
+        # If these asserts fail, that means the INSERT (create function, or cast to SQL) is broken!
  68
+        unless current_adapter?(:SQLite3Adapter)
  69
+          assert_equal correct_value, row.wealth
  70
+        end
  71
+      end
  72
+
  73
+      def test_add_column_with_precision_and_scale
  74
+        connection.add_column 'test_models', 'wealth', :decimal, :precision => 9, :scale => 7
  75
+
  76
+        wealth_column = TestModel.columns_hash['wealth']
  77
+        assert_equal 9, wealth_column.precision
  78
+        assert_equal 7, wealth_column.scale
  79
+      end
  80
+
  81
+      def test_change_column_preserve_other_column_precision_and_scale
  82
+        skip "only on sqlite3" unless current_adapter?(:SQLite3Adapter)
  83
+
  84
+        connection.add_column 'test_models', 'last_name', :string
  85
+        connection.add_column 'test_models', 'wealth', :decimal, :precision => 9, :scale => 7
  86
+
  87
+        wealth_column = TestModel.columns_hash['wealth']
  88
+        assert_equal 9, wealth_column.precision
  89
+        assert_equal 7, wealth_column.scale
  90
+
  91
+        connection.change_column 'test_models', 'last_name', :string, :null => false
  92
+        TestModel.reset_column_information
  93
+
  94
+        wealth_column = TestModel.columns_hash['wealth']
  95
+        assert_equal 9, wealth_column.precision
  96
+        assert_equal 7, wealth_column.scale
  97
+      end
  98
+
  99
+      def test_native_types
  100
+        add_column "test_models", "first_name", :string
  101
+        add_column "test_models", "last_name", :string
  102
+        add_column "test_models", "bio", :text
  103
+        add_column "test_models", "age", :integer
  104
+        add_column "test_models", "height", :float
  105
+        add_column "test_models", "wealth", :decimal, :precision => '30', :scale => '10'
  106
+        add_column "test_models", "birthday", :datetime
  107
+        add_column "test_models", "favorite_day", :date
  108
+        add_column "test_models", "moment_of_truth", :datetime
  109
+        add_column "test_models", "male", :boolean
  110
+
  111
+        TestModel.create :first_name => 'bob', :last_name => 'bobsen',
  112
+          :bio => "I was born ....", :age => 18, :height => 1.78,
  113
+          :wealth => BigDecimal.new("12345678901234567890.0123456789"),
  114
+          :birthday => 18.years.ago, :favorite_day => 10.days.ago,
  115
+          :moment_of_truth => "1782-10-10 21:40:18", :male => true
  116
+
  117
+        bob = TestModel.find(:first)
  118
+        assert_equal 'bob', bob.first_name
  119
+        assert_equal 'bobsen', bob.last_name
  120
+        assert_equal "I was born ....", bob.bio
  121
+        assert_equal 18, bob.age
  122
+
  123
+        # Test for 30 significant digits (beyond the 16 of float), 10 of them
  124
+        # after the decimal place.
  125
+
  126
+        unless current_adapter?(:SQLite3Adapter)
  127
+          assert_equal BigDecimal.new("0012345678901234567890.0123456789"), bob.wealth
  128
+        end
  129
+
  130
+        assert_equal true, bob.male?
  131
+
  132
+        assert_equal String, bob.first_name.class
  133
+        assert_equal String, bob.last_name.class
  134
+        assert_equal String, bob.bio.class
  135
+        assert_equal Fixnum, bob.age.class
  136
+        assert_equal Time, bob.birthday.class
  137
+
  138
+        if current_adapter?(:OracleAdapter, :SybaseAdapter)
  139
+          # Sybase, and Oracle don't differentiate between date/time
  140
+          assert_equal Time, bob.favorite_day.class
  141
+        else
  142
+          assert_equal Date, bob.favorite_day.class
  143
+        end
  144
+
  145
+        # Oracle adapter stores Time or DateTime with timezone value already in _before_type_cast column
  146
+        # therefore no timezone change is done afterwards when default timezone is changed
  147
+        unless current_adapter?(:OracleAdapter)
  148
+          # Test DateTime column and defaults, including timezone.
  149
+          # FIXME: moment of truth may be Time on 64-bit platforms.
  150
+          if bob.moment_of_truth.is_a?(DateTime)
  151
+
  152
+            with_env_tz 'US/Eastern' do
  153
+              bob.reload
  154
+              assert_equal DateTime.local_offset, bob.moment_of_truth.offset
  155
+              assert_not_equal 0, bob.moment_of_truth.offset
  156
+              assert_not_equal "Z", bob.moment_of_truth.zone
  157
+              # US/Eastern is -5 hours from GMT
  158
+              assert_equal Rational(-5, 24), bob.moment_of_truth.offset
  159
+              assert_match(/\A-05:?00\Z/, bob.moment_of_truth.zone) #ruby 1.8.6 uses HH:MM, prior versions use HHMM
  160
+              assert_equal DateTime::ITALY, bob.moment_of_truth.start
  161
+            end
  162
+          end
  163
+        end
  164
+
  165
+        assert_instance_of TrueClass, bob.male?
  166
+        assert_kind_of BigDecimal, bob.wealth
  167
+      end
  168
+
  169
+      def add_column(*args)
  170
+        connection.add_column(*args)
  171
+      end
  172
+    end
  173
+  end
  174
+end
169  activerecord/test/cases/migration_test.rb
@@ -80,175 +80,6 @@ def drop_table; raise "no"; end
80 80
     Person.connection.drop_table :testings2 rescue nil
81 81
   end
82 82
 
83  
-  # We specifically do a manual INSERT here, and then test only the SELECT
84  
-  # functionality. This allows us to more easily catch INSERT being broken,
85  
-  # but SELECT actually working fine.
86  
-  def test_native_decimal_insert_manual_vs_automatic
87  
-    correct_value = '0012345678901234567890.0123456789'.to_d
88  
-
89  
-    Person.delete_all
90  
-    Person.connection.add_column "people", "wealth", :decimal, :precision => '30', :scale => '10'
91  
-    Person.reset_column_information
92  
-
93  
-    # Do a manual insertion
94  
-    if current_adapter?(:OracleAdapter)
95  
-      Person.connection.execute "insert into people (id, wealth, created_at, updated_at) values (people_seq.nextval, 12345678901234567890.0123456789, sysdate, sysdate)"
96  
-    elsif current_adapter?(:OpenBaseAdapter) || (current_adapter?(:MysqlAdapter) && Mysql.client_version < 50003) #before mysql 5.0.3 decimals stored as strings
97  
-      Person.connection.execute "insert into people (wealth, created_at, updated_at) values ('12345678901234567890.0123456789', 0, 0)"
98  
-    elsif current_adapter?(:PostgreSQLAdapter)
99  
-      Person.connection.execute "insert into people (wealth, created_at, updated_at) values (12345678901234567890.0123456789, now(), now())"
100  
-    else
101  
-      Person.connection.execute "insert into people (wealth, created_at, updated_at) values (12345678901234567890.0123456789, 0, 0)"
102  
-    end
103  
-
104  
-    # SELECT
105  
-    row = Person.find(:first)
106  
-    assert_kind_of BigDecimal, row.wealth
107  
-
108  
-    # If this assert fails, that means the SELECT is broken!
109  
-    unless current_adapter?(:SQLite3Adapter)
110  
-      assert_equal correct_value, row.wealth
111  
-    end
112  
-
113  
-    # Reset to old state
114  
-    Person.delete_all
115  
-
116  
-    # Now use the Rails insertion
117  
-    assert_nothing_raised { Person.create :wealth => BigDecimal.new("12345678901234567890.0123456789") }
118  
-
119  
-    # SELECT
120  
-    row = Person.find(:first)
121  
-    assert_kind_of BigDecimal, row.wealth
122  
-
123  
-    # If these asserts fail, that means the INSERT (create function, or cast to SQL) is broken!
124  
-    unless current_adapter?(:SQLite3Adapter)
125  
-      assert_equal correct_value, row.wealth
126  
-    end
127  
-
128  
-    # Reset to old state
129  
-    Person.connection.del_column "people", "wealth" rescue nil
130  
-    Person.reset_column_information
131  
-  end
132  
-
133  
-  def test_add_column_with_precision_and_scale
134  
-    Person.connection.add_column 'people', 'wealth', :decimal, :precision => 9, :scale => 7
135  
-    Person.reset_column_information
136  
-
137  
-    wealth_column = Person.columns_hash['wealth']
138  
-    assert_equal 9, wealth_column.precision
139  
-    assert_equal 7, wealth_column.scale
140  
-  end
141  
-
142  
-  # Test SQLite adapter specifically for decimal types with precision and scale
143  
-  # attributes, since these need to be maintained in schema but aren't actually
144  
-  # used in SQLite itself
145  
-  def test_change_column_with_new_precision_and_scale
146  
-    skip "only on sqlite3" unless current_adapter?(:SQLite3Adapter)
147  
-
148  
-    Person.delete_all
149  
-    Person.connection.add_column 'people', 'wealth', :decimal, :precision => 9, :scale => 7
150  
-
151  
-    Person.connection.change_column 'people', 'wealth', :decimal, :precision => 12, :scale => 8
152  
-    Person.reset_column_information
153  
-
154  
-    wealth_column = Person.columns_hash['wealth']
155  
-    assert_equal 12, wealth_column.precision
156  
-    assert_equal 8, wealth_column.scale
157  
-  end
158  
-
159  
-  def test_change_column_preserve_other_column_precision_and_scale
160  
-    skip "only on sqlite3" unless current_adapter?(:SQLite3Adapter)
161  
-
162  
-    Person.delete_all
163  
-    Person.connection.add_column 'people', 'last_name', :string
164  
-    Person.connection.add_column 'people', 'wealth', :decimal, :precision => 9, :scale => 7
165  
-    Person.reset_column_information
166  
-
167  
-    wealth_column = Person.columns_hash['wealth']
168  
-    assert_equal 9, wealth_column.precision
169  
-    assert_equal 7, wealth_column.scale
170  
-
171  
-    Person.connection.change_column 'people', 'last_name', :string, :null => false
172  
-    Person.reset_column_information
173  
-
174  
-    wealth_column = Person.columns_hash['wealth']
175  
-    assert_equal 9, wealth_column.precision
176  
-    assert_equal 7, wealth_column.scale
177  
-  end
178  
-
179  
-  def test_native_types
180  
-    Person.delete_all
181  
-    Person.connection.add_column "people", "last_name", :string
182  
-    Person.connection.add_column "people", "bio", :text
183  
-    Person.connection.add_column "people", "age", :integer
184  
-    Person.connection.add_column "people", "height", :float
185  
-    Person.connection.add_column "people", "wealth", :decimal, :precision => '30', :scale => '10'
186  
-    Person.connection.add_column "people", "birthday", :datetime
187  
-    Person.connection.add_column "people", "favorite_day", :date
188  
-    Person.connection.add_column "people", "moment_of_truth", :datetime
189  
-    Person.connection.add_column "people", "male", :boolean
190  
-    Person.reset_column_information
191  
-
192  
-    assert_nothing_raised do
193  
-      Person.create :first_name => 'bob', :last_name => 'bobsen',
194  
-        :bio => "I was born ....", :age => 18, :height => 1.78,
195  
-        :wealth => BigDecimal.new("12345678901234567890.0123456789"),
196  
-        :birthday => 18.years.ago, :favorite_day => 10.days.ago,
197  
-        :moment_of_truth => "1782-10-10 21:40:18", :male => true
198  
-    end
199  
-
200  
-    bob = Person.find(:first)
201  
-    assert_equal 'bob', bob.first_name
202  
-    assert_equal 'bobsen', bob.last_name
203  
-    assert_equal "I was born ....", bob.bio
204  
-    assert_equal 18, bob.age
205  
-
206  
-    # Test for 30 significant digits (beyond the 16 of float), 10 of them
207  
-    # after the decimal place.
208  
-
209  
-    unless current_adapter?(:SQLite3Adapter)
210  
-      assert_equal BigDecimal.new("0012345678901234567890.0123456789"), bob.wealth
211  
-    end
212  
-
213  
-    assert_equal true, bob.male?
214  
-
215  
-    assert_equal String, bob.first_name.class
216  
-    assert_equal String, bob.last_name.class
217  
-    assert_equal String, bob.bio.class
218  
-    assert_equal Fixnum, bob.age.class
219  
-    assert_equal Time, bob.birthday.class
220  
-
221  
-    if current_adapter?(:OracleAdapter, :SybaseAdapter)
222  
-      # Sybase, and Oracle don't differentiate between date/time
223  
-      assert_equal Time, bob.favorite_day.class
224  
-    else
225  
-      assert_equal Date, bob.favorite_day.class
226  
-    end
227  
-
228  
-    # Oracle adapter stores Time or DateTime with timezone value already in _before_type_cast column
229  
-    # therefore no timezone change is done afterwards when default timezone is changed
230  
-    unless current_adapter?(:OracleAdapter)
231  
-      # Test DateTime column and defaults, including timezone.
232  
-      # FIXME: moment of truth may be Time on 64-bit platforms.
233  
-      if bob.moment_of_truth.is_a?(DateTime)
234  
-
235  
-        with_env_tz 'US/Eastern' do
236  
-          bob.reload
237  
-          assert_equal DateTime.local_offset, bob.moment_of_truth.offset
238  
-          assert_not_equal 0, bob.moment_of_truth.offset
239  
-          assert_not_equal "Z", bob.moment_of_truth.zone
240  
-          # US/Eastern is -5 hours from GMT
241  
-          assert_equal Rational(-5, 24), bob.moment_of_truth.offset
242  
-          assert_match(/\A-05:?00\Z/, bob.moment_of_truth.zone) #ruby 1.8.6 uses HH:MM, prior versions use HHMM
243  
-          assert_equal DateTime::ITALY, bob.moment_of_truth.start
244  
-        end
245  
-      end
246  
-    end
247  
-
248  
-    assert_instance_of TrueClass, bob.male?
249  
-    assert_kind_of BigDecimal, bob.wealth
250  
-  end
251  
-
252 83
   def test_unabstracted_database_dependent_types
253 84
     skip "not supported" unless current_adapter?(:MysqlAdapter, :Mysql2Adapter)
254 85
 

0 notes on commit 28bb02a

Please sign in to comment.
Something went wrong with that request. Please try again.