Skip to content

Instantly share code, notes, and snippets.

@InAbsentia
Last active August 29, 2015 14:09
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 InAbsentia/ad8288795f52dd23b96e to your computer and use it in GitHub Desktop.
Save InAbsentia/ad8288795f52dd23b96e to your computer and use it in GitHub Desktop.
Test UUID default value in Rails
unless File.exist?("Gemfile")
File.write("Gemfile", <<-GEMFILE)
source "https://rubygems.org"
gem "rails", github: "rails/rails"
gem "pg"
GEMFILE
system "bundle"
end
require "bundler"
Bundler.setup(:default)
require "active_record"
require "minitest/autorun"
require "logger"
ActiveRecord::Base.establish_connection(
adapter: "postgresql",
host: "localhost",
username: "test-default",
database: "test-default-values"
)
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
enable_extension "uuid-ossp"
drop_table :posts if table_exists?(:posts)
create_table :posts do |t|
t.string :test_string, default: "A string."
t.uuid :test_key, default: "uuid_generate_v4()"
end
end
class Post < ActiveRecord::Base
end
class BugTest < Minitest::Test
def test_default_string_value
post = Post.create!
assert_equal "A string.", post.test_string
end
def test_default_uuid_value
post = Post.create!
refute_nil post.test_key
end
def test_default_uuid_value_after_reload
post = Post.create!
refute_nil post.reload.test_key
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment