2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
require "cases/helper"
|
|
|
|
require "support/schema_dumping_helper"
|
2015-02-09 19:04:47 -05:00
|
|
|
|
2020-04-24 13:56:53 -04:00
|
|
|
if supports_datetime_with_precision?
|
2016-08-06 13:55:02 -04:00
|
|
|
class TimePrecisionTest < ActiveRecord::TestCase
|
|
|
|
include SchemaDumpingHelper
|
|
|
|
self.use_transactional_tests = false
|
2015-02-12 13:04:47 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
class Foo < ActiveRecord::Base; end
|
2015-02-12 13:04:47 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
setup do
|
|
|
|
@connection = ActiveRecord::Base.connection
|
|
|
|
Foo.reset_column_information
|
|
|
|
end
|
2015-02-09 19:04:47 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
teardown do
|
|
|
|
@connection.drop_table :foos, if_exists: true
|
|
|
|
end
|
2015-02-09 19:04:47 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_time_data_type_with_precision
|
|
|
|
@connection.create_table(:foos, force: true)
|
|
|
|
@connection.add_column :foos, :start, :time, precision: 3
|
|
|
|
@connection.add_column :foos, :finish, :time, precision: 6
|
|
|
|
assert_equal 3, Foo.columns_hash["start"].precision
|
|
|
|
assert_equal 6, Foo.columns_hash["finish"].precision
|
2015-02-09 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
2018-03-11 14:19:20 -04:00
|
|
|
def test_time_precision_is_truncated_on_assignment
|
|
|
|
@connection.create_table(:foos, force: true)
|
|
|
|
@connection.add_column :foos, :start, :time, precision: 0
|
|
|
|
@connection.add_column :foos, :finish, :time, precision: 6
|
|
|
|
|
|
|
|
time = ::Time.now.change(nsec: 123456789)
|
|
|
|
foo = Foo.new(start: time, finish: time)
|
|
|
|
|
|
|
|
assert_equal 0, foo.start.nsec
|
|
|
|
assert_equal 123456000, foo.finish.nsec
|
|
|
|
|
|
|
|
foo.save!
|
|
|
|
foo.reload
|
|
|
|
|
|
|
|
assert_equal 0, foo.start.nsec
|
|
|
|
assert_equal 123456000, foo.finish.nsec
|
|
|
|
end
|
|
|
|
|
2019-02-26 13:16:28 -05:00
|
|
|
unless current_adapter?(:Mysql2Adapter)
|
|
|
|
def test_no_time_precision_isnt_truncated_on_assignment
|
|
|
|
@connection.create_table(:foos, force: true)
|
|
|
|
@connection.add_column :foos, :start, :time
|
|
|
|
@connection.add_column :foos, :finish, :time, precision: 6
|
|
|
|
|
|
|
|
time = ::Time.now.change(nsec: 123)
|
|
|
|
foo = Foo.new(start: time, finish: time)
|
|
|
|
|
|
|
|
assert_equal 123, foo.start.nsec
|
|
|
|
assert_equal 0, foo.finish.nsec
|
|
|
|
|
|
|
|
foo.save!
|
|
|
|
foo.reload
|
|
|
|
|
|
|
|
assert_equal 0, foo.start.nsec
|
|
|
|
assert_equal 0, foo.finish.nsec
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_passing_precision_to_time_does_not_set_limit
|
2015-02-09 19:04:47 -05:00
|
|
|
@connection.create_table(:foos, force: true) do |t|
|
2016-08-06 13:55:02 -04:00
|
|
|
t.time :start, precision: 3
|
|
|
|
t.time :finish, precision: 6
|
2015-02-09 19:04:47 -05:00
|
|
|
end
|
2016-08-06 13:55:02 -04:00
|
|
|
assert_nil Foo.columns_hash["start"].limit
|
|
|
|
assert_nil Foo.columns_hash["finish"].limit
|
2015-02-09 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_invalid_time_precision_raises_error
|
Raise `ArgumentError` for invalid `:limit` and `:precision` like as other options
When I've added new `:size` option in #35071, I've found that invalid
`:limit` and `:precision` raises `ActiveRecordError` unlike other
invalid options.
I think that is hard to distinguish argument errors and statement
invalid errors since the `StatementInvalid` is a subclass of the
`ActiveRecordError`.
https://github.com/rails/rails/blob/c9e4c848eeeb8999b778fa1ae52185ca5537fffe/activerecord/lib/active_record/errors.rb#L103
```ruby
begin
# execute any migration
rescue ActiveRecord::StatementInvalid
# statement invalid
rescue ActiveRecord::ActiveRecordError, ArgumentError
# `ActiveRecordError` except `StatementInvalid` is maybe an argument error
end
```
I'd say this is the inconsistency worth fixing.
Before:
```ruby
add_column :items, :attr1, :binary, size: 10 # => ArgumentError
add_column :items, :attr2, :decimal, scale: 10 # => ArgumentError
add_column :items, :attr3, :integer, limit: 10 # => ActiveRecordError
add_column :items, :attr4, :datetime, precision: 10 # => ActiveRecordError
```
After:
```ruby
add_column :items, :attr1, :binary, size: 10 # => ArgumentError
add_column :items, :attr2, :decimal, scale: 10 # => ArgumentError
add_column :items, :attr3, :integer, limit: 10 # => ArgumentError
add_column :items, :attr4, :datetime, precision: 10 # => ArgumentError
```
2019-02-12 17:13:23 -05:00
|
|
|
assert_raises ArgumentError do
|
2016-08-06 13:55:02 -04:00
|
|
|
@connection.create_table(:foos, force: true) do |t|
|
|
|
|
t.time :start, precision: 7
|
|
|
|
t.time :finish, precision: 7
|
|
|
|
end
|
|
|
|
end
|
2015-02-12 13:04:47 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_formatting_time_according_to_precision
|
|
|
|
@connection.create_table(:foos, force: true) do |t|
|
|
|
|
t.time :start, precision: 0
|
|
|
|
t.time :finish, precision: 4
|
|
|
|
end
|
|
|
|
time = ::Time.utc(2000, 1, 1, 12, 30, 0, 999999)
|
|
|
|
Foo.create!(start: time, finish: time)
|
|
|
|
assert foo = Foo.find_by(start: time)
|
|
|
|
assert_equal 1, Foo.where(finish: time).count
|
|
|
|
assert_equal time.to_s, foo.start.to_s
|
|
|
|
assert_equal time.to_s, foo.finish.to_s
|
|
|
|
assert_equal 000000, foo.start.usec
|
|
|
|
assert_equal 999900, foo.finish.usec
|
2015-02-19 20:19:33 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_schema_dump_includes_time_precision
|
2015-02-19 20:19:33 -05:00
|
|
|
@connection.create_table(:foos, force: true) do |t|
|
2016-08-06 13:55:02 -04:00
|
|
|
t.time :start, precision: 4
|
|
|
|
t.time :finish, precision: 6
|
2015-02-19 20:19:33 -05:00
|
|
|
end
|
|
|
|
output = dump_table_schema("foos")
|
2016-08-06 13:55:02 -04:00
|
|
|
assert_match %r{t\.time\s+"start",\s+precision: 4$}, output
|
|
|
|
assert_match %r{t\.time\s+"finish",\s+precision: 6$}, output
|
2015-02-19 20:19:33 -05:00
|
|
|
end
|
|
|
|
|
2017-01-15 21:33:47 -05:00
|
|
|
if current_adapter?(:PostgreSQLAdapter, :SQLServerAdapter)
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_time_precision_with_zero_should_be_dumped
|
|
|
|
@connection.create_table(:foos, force: true) do |t|
|
|
|
|
t.time :start, precision: 0
|
|
|
|
t.time :finish, precision: 0
|
|
|
|
end
|
|
|
|
output = dump_table_schema("foos")
|
|
|
|
assert_match %r{t\.time\s+"start",\s+precision: 0$}, output
|
|
|
|
assert_match %r{t\.time\s+"finish",\s+precision: 0$}, output
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-02-09 19:04:47 -05:00
|
|
|
end
|