Last active
November 9, 2016 21:40
-
-
Save akostadinov/264120b89fd066ef85b1c04d9c1b6d8f to your computer and use it in GitHub Desktop.
Rails Active Record simple test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# run by `ruby rails_test.rb` | |
# credits to https://github.com/rails/rails/issues/13744#issuecomment-32670636 | |
gem 'activerecord', '5.0.0.1' | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
# Ensure backward compatibility with Minitest 4 | |
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) | |
# This connection will do for database-independent bug reports. | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') # Create the test before, or use `create_database 'test'` in code | |
# For MariaDB you can try this: | |
# ActiveRecord::Base.establish_connection(adapter: 'mysql', user: 'root', database: 'test') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :posts, force: true do |t| | |
t.string "name" | |
end | |
add_index :posts, :name, :unique => true | |
end | |
class Post < ActiveRecord::Base | |
end | |
class BugTest < Minitest::Test | |
def test_persist_state_after_db_error | |
post = Post.new | |
post.name = "garga" | |
post.save! | |
post = Post.new | |
post.name = "garga1" | |
post.save! | |
assert_equal true, post.changed.empty? | |
post.name = "garga" | |
puts "post #{post.id} changed: #{!post.changed.empty?}" | |
assert_equal false, post.changed.empty? | |
raised = false | |
begin | |
post.save | |
rescue ActiveRecord::RecordNotUnique => e | |
raised = true | |
end | |
puts post.id | |
puts post.persisted? | |
assert_equal true, raised | |
assert_equal false, post.changed.empty? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment