Skip to content

Instantly share code, notes, and snippets.

@dnagir
Created April 19, 2016 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnagir/62b11e5956e5757a321031f966847c4f to your computer and use it in GitHub Desktop.
Save dnagir/62b11e5956e5757a321031f966847c4f to your computer and use it in GitHub Desktop.
ActiveRecord time truncation
gem 'activerecord', '4.2.5'
require 'active_record'
require 'minitest/autorun'
require 'logger'
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection(
:adapter => 'postgresql',
:database => 'x'
)
ActiveRecord::Schema.define do
create_table :users, :force => true do |t|
t.timestamp :updated_at, null: false
end
end
class User < ActiveRecord::Base
end
class TestMe < MiniTest::Unit::TestCase
def test_preserves_precise_data_time
time = Time.utc(2016, 4, 19, 7, 9, 45, 852407) # '2016-04-19 07:09:45.852407'
deserialised = Time.at(time.to_f)
User.create!(updated_at: time)
assert_equal 0, User.where('updated_at > ?', deserialised).count
end
end
-- create_table(:users, {:force=>true})
D, [2016-04-19T18:53:17.307846 #34798] DEBUG -- : (1.6ms) DROP TABLE "users"
D, [2016-04-19T18:53:17.311649 #34798] DEBUG -- : (3.6ms) CREATE TABLE "users" ("id" serial primary key, "updated_at" timestamp NOT NULL)
-> 0.0220s
MiniTest::Unit::TestCase is now Minitest::Test. From tmp/a.rb:21:in `<main>'
Run options: --seed 64840
# Running:
D, [2016-04-19T18:53:17.407264 #34798] DEBUG -- : (0.1ms) BEGIN
D, [2016-04-19T18:53:17.411897 #34798] DEBUG -- : SQL (0.5ms) INSERT INTO "users" ("updated_at") VALUES ($1) RETURNING "id" [["updated_at", "2016-04-19 07:09:45.852407"]]
D, [2016-04-19T18:53:17.412517 #34798] DEBUG -- : (0.2ms) COMMIT
D, [2016-04-19T18:53:17.413931 #34798] DEBUG -- : (0.4ms) SELECT COUNT(*) FROM "users" WHERE (updated_at > '2016-04-19 07:09:45.852406')
F
Finished in 0.014022s, 71.3158 runs/s, 71.3158 assertions/s.
1) Failure:
TestMe#test_preserves_precise_data_time [tmp/a.rb:28]:
Expected: 0
Actual: 1
1 runs, 1 assertions, 1 failures, 0 errors, 0 skips
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment