datatype_test.rb 9.7 KB
Newer Older
1
require "cases/helper"
2

3
class PostgresqlArray < ActiveRecord::Base
4 5
end

6 7 8
class PostgresqlTsvector < ActiveRecord::Base
end

9 10 11 12 13 14 15 16 17 18 19 20 21 22
class PostgresqlMoney < ActiveRecord::Base
end

class PostgresqlNumber < ActiveRecord::Base
end

class PostgresqlTime < ActiveRecord::Base
end

class PostgresqlNetworkAddress < ActiveRecord::Base
end

class PostgresqlBitString < ActiveRecord::Base
end
23

24 25 26
class PostgresqlOid < ActiveRecord::Base
end

27 28 29
class PostgresqlTimestampWithZone < ActiveRecord::Base
end

30 31 32
class PostgresqlUUID < ActiveRecord::Base
end

33
class PostgresqlDataTypeTest < ActiveRecord::TestCase
34
  self.use_transactional_fixtures = false
35 36 37

  def setup
    @connection = ActiveRecord::Base.connection
38
    @connection.execute("set lc_monetary = 'C'")
39

40 41 42
    @connection.execute("INSERT INTO postgresql_arrays (commission_by_quarter, nicknames) VALUES ( '{35000,21000,18000,17000}', '{foo,bar,baz}' )")
    @first_array = PostgresqlArray.find(1)

43 44 45
    @connection.execute("INSERT INTO postgresql_tsvectors (text_vector) VALUES (' ''text'' ''vector'' ')")
    @first_tsvector = PostgresqlTsvector.find(1)

T
Tarmo Tänav 已提交
46 47
    @connection.execute("INSERT INTO postgresql_moneys (wealth) VALUES ('567.89'::money)")
    @connection.execute("INSERT INTO postgresql_moneys (wealth) VALUES ('-567.89'::money)")
48 49 50 51 52 53 54 55 56 57 58
    @first_money = PostgresqlMoney.find(1)
    @second_money = PostgresqlMoney.find(2)

    @connection.execute("INSERT INTO postgresql_numbers (single, double) VALUES (123.456, 123456.789)")
    @first_number = PostgresqlNumber.find(1)

    @connection.execute("INSERT INTO postgresql_times (time_interval) VALUES ('1 year 2 days ago')")
    @first_time = PostgresqlTime.find(1)

    @connection.execute("INSERT INTO postgresql_network_addresses (cidr_address, inet_address, mac_address) VALUES('192.168.0/24', '172.16.1.254/32', '01:23:45:67:89:0a')")
    @first_network_address = PostgresqlNetworkAddress.find(1)
J
Jeremy Kemper 已提交
59

60 61
    @connection.execute("INSERT INTO postgresql_bit_strings (bit_string, bit_string_varying) VALUES (B'00010101', X'15')")
    @first_bit_string = PostgresqlBitString.find(1)
J
Jeremy Kemper 已提交
62

63 64
    @connection.execute("INSERT INTO postgresql_oids (obj_id) VALUES (1234)")
    @first_oid = PostgresqlOid.find(1)
65

66
    @connection.execute("INSERT INTO postgresql_timestamp_with_zones (time) VALUES ('2010-01-01 10:00:00-1')")
67 68 69

    @connection.execute("INSERT INTO postgresql_uuids (guid, compact_guid) VALUES('d96c3da0-96c1-012f-1316-64ce8f32c6d8', 'f06c715096c1012f131764ce8f32c6d8')")
    @first_uuid = PostgresqlUUID.find(1)
70 71 72
  end

  def test_data_type_of_array_types
73 74 75 76
    assert_equal :string, @first_array.column_for_attribute(:commission_by_quarter).type
    assert_equal :string, @first_array.column_for_attribute(:nicknames).type
  end

77 78 79 80
  def test_data_type_of_tsvector_types
    assert_equal :tsvector, @first_tsvector.column_for_attribute(:text_vector).type
  end

81 82 83 84 85 86 87 88 89 90 91 92 93 94
  def test_data_type_of_money_types
    assert_equal :decimal, @first_money.column_for_attribute(:wealth).type
  end

  def test_data_type_of_number_types
    assert_equal :float, @first_number.column_for_attribute(:single).type
    assert_equal :float, @first_number.column_for_attribute(:double).type
  end

  def test_data_type_of_time_types
    assert_equal :string, @first_time.column_for_attribute(:time_interval).type
  end

  def test_data_type_of_network_address_types
95 96 97
    assert_equal :cidr, @first_network_address.column_for_attribute(:cidr_address).type
    assert_equal :inet, @first_network_address.column_for_attribute(:inet_address).type
    assert_equal :macaddr, @first_network_address.column_for_attribute(:mac_address).type
98 99 100 101 102 103 104 105 106
  end

  def test_data_type_of_bit_string_types
    assert_equal :string, @first_bit_string.column_for_attribute(:bit_string).type
    assert_equal :string, @first_bit_string.column_for_attribute(:bit_string_varying).type
  end

  def test_data_type_of_oid_types
    assert_equal :integer, @first_oid.column_for_attribute(:obj_id).type
107 108
  end

109 110 111 112
  def test_data_type_of_uuid_types
    assert_equal :uuid, @first_uuid.column_for_attribute(:guid).type
  end

113
  def test_array_values
114 115 116 117
   assert_equal '{35000,21000,18000,17000}', @first_array.commission_by_quarter
   assert_equal '{foo,bar,baz}', @first_array.nicknames
  end

118 119 120 121
  def test_tsvector_values
    assert_equal "'text' 'vector'", @first_tsvector.text_vector
  end

122 123
  def test_money_values
    assert_equal 567.89, @first_money.wealth
124
    assert_equal(-567.89, @second_money.wealth)
125 126
  end

127 128 129 130 131 132 133 134 135 136 137
  def test_update_tsvector
    new_text_vector = "'new' 'text' 'vector'"
    assert @first_tsvector.text_vector = new_text_vector
    assert @first_tsvector.save
    assert @first_tsvector.reload
    assert @first_tsvector.text_vector = new_text_vector
    assert @first_tsvector.save
    assert @first_tsvector.reload
    assert_equal @first_tsvector.text_vector, new_text_vector
  end

138 139 140 141 142 143 144 145 146
  def test_number_values
    assert_equal 123.456, @first_number.single
    assert_equal 123456.789, @first_number.double
  end

  def test_time_values
    assert_equal '-1 years -2 days', @first_time.time_interval
  end

D
Dan McClain 已提交
147 148 149 150
  def test_network_address_values_ipaddr
    cidr_address = IPAddr.new '192.168.0.0/24'
    inet_address = IPAddr.new '172.16.1.254'

151 152
    assert_equal cidr_address, @first_network_address.cidr_address
    assert_equal inet_address, @first_network_address.inet_address
153 154 155
    assert_equal '01:23:45:67:89:0a', @first_network_address.mac_address
  end

156 157 158 159 160
  def test_uuid_values
    assert_equal 'd96c3da0-96c1-012f-1316-64ce8f32c6d8', @first_uuid.guid
    assert_equal 'f06c7150-96c1-012f-1317-64ce8f32c6d8', @first_uuid.compact_guid
  end

161 162 163 164 165 166 167
  def test_bit_string_values
    assert_equal '00010101', @first_bit_string.bit_string
    assert_equal '00010101', @first_bit_string.bit_string_varying
  end

  def test_oid_values
    assert_equal 1234, @first_oid.obj_id
168 169 170 171
  end

  def test_update_integer_array
    new_value = '{32800,95000,29350,17000}'
172 173 174 175 176 177 178 179
    assert @first_array.commission_by_quarter = new_value
    assert @first_array.save
    assert @first_array.reload
    assert_equal @first_array.commission_by_quarter, new_value
    assert @first_array.commission_by_quarter = new_value
    assert @first_array.save
    assert @first_array.reload
    assert_equal @first_array.commission_by_quarter, new_value
180 181 182 183
  end

  def test_update_text_array
    new_value = '{robby,robert,rob,robbie}'
184 185 186 187 188 189 190 191 192 193 194
    assert @first_array.nicknames = new_value
    assert @first_array.save
    assert @first_array.reload
    assert_equal @first_array.nicknames, new_value
    assert @first_array.nicknames = new_value
    assert @first_array.save
    assert @first_array.reload
    assert_equal @first_array.nicknames, new_value
  end

  def test_update_money
T
Tarmo Tänav 已提交
195
    new_value = BigDecimal.new('123.45')
196 197 198
    assert @first_money.wealth = new_value
    assert @first_money.save
    assert @first_money.reload
T
Tarmo Tänav 已提交
199
    assert_equal new_value, @first_money.wealth
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
  end

  def test_update_number
    new_single = 789.012
    new_double = 789012.345
    assert @first_number.single = new_single
    assert @first_number.double = new_double
    assert @first_number.save
    assert @first_number.reload
    assert_equal @first_number.single, new_single
    assert_equal @first_number.double, new_double
  end

  def test_update_time
    assert @first_time.time_interval = '2 years 3 minutes'
    assert @first_time.save
    assert @first_time.reload
    assert_equal @first_time.time_interval, '2 years 00:03:00'
  end

  def test_update_network_address
221 222
    new_inet_address = '10.1.2.3/32'
    new_cidr_address = '10.0.0.0/8'
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    new_mac_address = 'bc:de:f0:12:34:56'
    assert @first_network_address.cidr_address = new_cidr_address
    assert @first_network_address.inet_address = new_inet_address
    assert @first_network_address.mac_address = new_mac_address
    assert @first_network_address.save
    assert @first_network_address.reload
    assert_equal @first_network_address.cidr_address, new_cidr_address
    assert_equal @first_network_address.inet_address, new_inet_address
    assert_equal @first_network_address.mac_address, new_mac_address
  end

  def test_update_bit_string
    new_bit_string = '11111111'
    new_bit_string_varying = 'FF'
    assert @first_bit_string.bit_string = new_bit_string
    assert @first_bit_string.bit_string_varying = new_bit_string_varying
    assert @first_bit_string.save
    assert @first_bit_string.reload
    assert_equal @first_bit_string.bit_string, new_bit_string
    assert_equal @first_bit_string.bit_string, @first_bit_string.bit_string_varying
  end

  def test_update_oid
    new_value = 567890
    assert @first_oid.obj_id = new_value
    assert @first_oid.save
    assert @first_oid.reload
    assert_equal @first_oid.obj_id, new_value
251
  end
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

  def test_timestamp_with_zone_values_with_rails_time_zone_support
    old_tz         = ActiveRecord::Base.time_zone_aware_attributes
    old_default_tz = ActiveRecord::Base.default_timezone

    ActiveRecord::Base.time_zone_aware_attributes = true
    ActiveRecord::Base.default_timezone = :utc

    @connection.reconnect!

    @first_timestamp_with_zone = PostgresqlTimestampWithZone.find(1)
    assert_equal Time.utc(2010,1,1, 11,0,0), @first_timestamp_with_zone.time
  ensure
    ActiveRecord::Base.default_timezone = old_default_tz
    ActiveRecord::Base.time_zone_aware_attributes = old_tz
    @connection.reconnect!
  end

  def test_timestamp_with_zone_values_without_rails_time_zone_support
    old_tz         = ActiveRecord::Base.time_zone_aware_attributes
    old_default_tz = ActiveRecord::Base.default_timezone

    ActiveRecord::Base.time_zone_aware_attributes = false
    ActiveRecord::Base.default_timezone = :local

    @connection.reconnect!

    @first_timestamp_with_zone = PostgresqlTimestampWithZone.find(1)
    assert_equal Time.utc(2010,1,1, 11,0,0), @first_timestamp_with_zone.time
  ensure
    ActiveRecord::Base.default_timezone = old_default_tz
    ActiveRecord::Base.time_zone_aware_attributes = old_tz
    @connection.reconnect!
  end
286
end