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"
|
2016-01-03 13:13:53 -05:00
|
|
|
|
2016-04-19 15:16:24 -04:00
|
|
|
if ActiveRecord::Base.connection.supports_comments?
|
2016-08-06 13:55:02 -04:00
|
|
|
class CommentTest < ActiveRecord::TestCase
|
|
|
|
include SchemaDumpingHelper
|
2016-01-03 13:13:53 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
class Commented < ActiveRecord::Base
|
|
|
|
self.table_name = "commenteds"
|
2016-04-19 15:16:24 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
class BlankComment < ActiveRecord::Base
|
2016-01-03 13:13:53 -05:00
|
|
|
end
|
2016-04-19 15:16:24 -04:00
|
|
|
|
2019-06-03 07:05:36 -04:00
|
|
|
class PkCommented < ActiveRecord::Base
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
setup do
|
|
|
|
@connection = ActiveRecord::Base.connection
|
|
|
|
|
|
|
|
@connection.create_table("commenteds", comment: "A table with comment", force: true) do |t|
|
|
|
|
t.string "name", comment: "Comment should help clarify the column purpose"
|
|
|
|
t.boolean "obvious", comment: "Question is: should you comment obviously named objects?"
|
|
|
|
t.string "content"
|
|
|
|
t.index "name", comment: %Q["Very important" index that powers all the performance.\nAnd it's fun!]
|
|
|
|
end
|
|
|
|
|
|
|
|
@connection.create_table("blank_comments", comment: " ", force: true) do |t|
|
|
|
|
t.string :space_comment, comment: " "
|
|
|
|
t.string :empty_comment, comment: ""
|
|
|
|
t.string :nil_comment, comment: nil
|
|
|
|
t.string :absent_comment
|
|
|
|
t.index :space_comment, comment: " "
|
|
|
|
t.index :empty_comment, comment: ""
|
|
|
|
t.index :nil_comment, comment: nil
|
|
|
|
t.index :absent_comment
|
|
|
|
end
|
|
|
|
|
2019-06-03 07:05:36 -04:00
|
|
|
@connection.create_table("pk_commenteds", comment: "Table comment", id: false, force: true) do |t|
|
2020-05-23 17:38:21 -04:00
|
|
|
t.primary_key :id, comment: "Primary key comment"
|
2019-06-03 07:05:36 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
Commented.reset_column_information
|
|
|
|
BlankComment.reset_column_information
|
2019-06-03 07:05:36 -04:00
|
|
|
PkCommented.reset_column_information
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2016-04-19 15:16:24 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
teardown do
|
|
|
|
@connection.drop_table "commenteds", if_exists: true
|
|
|
|
@connection.drop_table "blank_comments", if_exists: true
|
|
|
|
end
|
2016-01-03 13:13:53 -05:00
|
|
|
|
2019-06-03 07:05:36 -04:00
|
|
|
def test_default_primary_key_comment
|
2019-05-31 15:59:47 -04:00
|
|
|
column = Commented.columns_hash["id"]
|
|
|
|
assert_nil column.comment
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_column_created_in_block
|
|
|
|
column = Commented.columns_hash["name"]
|
2016-01-03 13:13:53 -05:00
|
|
|
assert_equal :string, column.type
|
2016-08-06 13:55:02 -04:00
|
|
|
assert_equal "Comment should help clarify the column purpose", column.comment
|
2016-01-03 13:13:53 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_blank_columns_created_in_block
|
|
|
|
%w[ space_comment empty_comment nil_comment absent_comment ].each do |field|
|
|
|
|
column = BlankComment.columns_hash[field]
|
|
|
|
assert_equal :string, column.type
|
|
|
|
assert_nil column.comment
|
|
|
|
end
|
2016-04-24 18:54:56 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_blank_indexes_created_in_block
|
|
|
|
@connection.indexes("blank_comments").each do |index|
|
|
|
|
assert_nil index.comment
|
|
|
|
end
|
|
|
|
end
|
2016-01-03 13:13:53 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_add_column_with_comment_later
|
|
|
|
@connection.add_column :commenteds, :rating, :integer, comment: "I am running out of imagination"
|
|
|
|
Commented.reset_column_information
|
|
|
|
column = Commented.columns_hash["rating"]
|
2016-01-03 13:13:53 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
assert_equal :integer, column.type
|
|
|
|
assert_equal "I am running out of imagination", column.comment
|
|
|
|
end
|
2016-01-03 13:13:53 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_add_index_with_comment_later
|
2017-04-05 06:14:56 -04:00
|
|
|
unless current_adapter?(:OracleAdapter)
|
|
|
|
@connection.add_index :commenteds, :obvious, name: "idx_obvious", comment: "We need to see obvious comments"
|
|
|
|
index = @connection.indexes("commenteds").find { |idef| idef.name == "idx_obvious" }
|
|
|
|
assert_equal "We need to see obvious comments", index.comment
|
|
|
|
end
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2016-01-03 13:13:53 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_add_comment_to_column
|
|
|
|
@connection.change_column :commenteds, :content, :string, comment: "Whoa, content describes itself!"
|
2016-01-03 13:13:53 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
Commented.reset_column_information
|
|
|
|
column = Commented.columns_hash["content"]
|
2016-01-03 13:13:53 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
assert_equal :string, column.type
|
|
|
|
assert_equal "Whoa, content describes itself!", column.comment
|
|
|
|
end
|
2016-01-03 13:13:53 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_remove_comment_from_column
|
|
|
|
@connection.change_column :commenteds, :obvious, :string, comment: nil
|
2016-01-03 13:13:53 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
Commented.reset_column_information
|
|
|
|
column = Commented.columns_hash["obvious"]
|
2016-01-03 13:13:53 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
assert_equal :string, column.type
|
|
|
|
assert_nil column.comment
|
|
|
|
end
|
2016-04-19 15:16:24 -04:00
|
|
|
|
2020-04-20 04:06:40 -04:00
|
|
|
def test_rename_column_preserves_comment
|
|
|
|
@connection.add_column :commenteds, :rating, :string, comment: "I am running out of imagination"
|
|
|
|
@connection.rename_column :commenteds, :rating, :new_rating
|
|
|
|
|
|
|
|
Commented.reset_column_information
|
|
|
|
column = Commented.columns_hash["new_rating"]
|
|
|
|
|
|
|
|
assert_equal :string, column.type
|
|
|
|
assert_equal column.comment, "I am running out of imagination"
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_schema_dump_with_comments
|
|
|
|
# Do all the stuff from other tests
|
|
|
|
@connection.add_column :commenteds, :rating, :integer, comment: "I am running out of imagination"
|
|
|
|
@connection.change_column :commenteds, :content, :string, comment: "Whoa, content describes itself!"
|
2016-12-04 17:16:08 -05:00
|
|
|
@connection.change_column :commenteds, :content, :string
|
2016-08-06 13:55:02 -04:00
|
|
|
@connection.change_column :commenteds, :obvious, :string, comment: nil
|
|
|
|
@connection.add_index :commenteds, :obvious, name: "idx_obvious", comment: "We need to see obvious comments"
|
|
|
|
|
|
|
|
# And check that these changes are reflected in dump
|
|
|
|
output = dump_table_schema "commenteds"
|
2017-08-27 09:16:05 -04:00
|
|
|
assert_match %r[create_table "commenteds",.*\s+comment: "A table with comment"], output
|
2016-08-06 13:55:02 -04:00
|
|
|
assert_match %r[t\.string\s+"name",\s+comment: "Comment should help clarify the column purpose"], output
|
|
|
|
assert_match %r[t\.string\s+"obvious"\n], output
|
|
|
|
assert_match %r[t\.string\s+"content",\s+comment: "Whoa, content describes itself!"], output
|
2017-04-07 06:02:11 -04:00
|
|
|
if current_adapter?(:OracleAdapter)
|
|
|
|
assert_match %r[t\.integer\s+"rating",\s+precision: 38,\s+comment: "I am running out of imagination"], output
|
|
|
|
else
|
|
|
|
assert_match %r[t\.integer\s+"rating",\s+comment: "I am running out of imagination"], output
|
2017-04-05 06:14:56 -04:00
|
|
|
assert_match %r[t\.index\s+.+\s+comment: "\\\"Very important\\\" index that powers all the performance.\\nAnd it's fun!"], output
|
|
|
|
assert_match %r[t\.index\s+.+\s+name: "idx_obvious",\s+comment: "We need to see obvious comments"], output
|
|
|
|
end
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2016-04-19 15:16:24 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def test_schema_dump_omits_blank_comments
|
|
|
|
output = dump_table_schema "blank_comments"
|
2016-04-19 15:16:24 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
assert_match %r[create_table "blank_comments"], output
|
|
|
|
assert_no_match %r[create_table "blank_comments",.+comment:], output
|
2016-04-19 15:16:24 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
assert_match %r[t\.string\s+"space_comment"\n], output
|
|
|
|
assert_no_match %r[t\.string\s+"space_comment", comment:\n], output
|
2016-04-19 15:16:24 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
assert_match %r[t\.string\s+"empty_comment"\n], output
|
|
|
|
assert_no_match %r[t\.string\s+"empty_comment", comment:\n], output
|
2016-04-19 15:16:24 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
assert_match %r[t\.string\s+"nil_comment"\n], output
|
|
|
|
assert_no_match %r[t\.string\s+"nil_comment", comment:\n], output
|
|
|
|
|
|
|
|
assert_match %r[t\.string\s+"absent_comment"\n], output
|
|
|
|
assert_no_match %r[t\.string\s+"absent_comment", comment:\n], output
|
|
|
|
end
|
2017-09-21 12:55:16 -04:00
|
|
|
|
|
|
|
def test_change_table_comment
|
|
|
|
@connection.change_table_comment :commenteds, "Edited table comment"
|
|
|
|
assert_equal "Edited table comment", @connection.table_comment("commenteds")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_change_table_comment_to_nil
|
|
|
|
@connection.change_table_comment :commenteds, nil
|
2017-09-22 10:04:54 -04:00
|
|
|
assert_nil @connection.table_comment("commenteds")
|
2017-09-21 12:55:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_change_column_comment
|
|
|
|
@connection.change_column_comment :commenteds, :name, "Edited column comment"
|
|
|
|
column = Commented.columns_hash["name"]
|
|
|
|
assert_equal "Edited column comment", column.comment
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_change_column_comment_to_nil
|
|
|
|
@connection.change_column_comment :commenteds, :name, nil
|
|
|
|
column = Commented.columns_hash["name"]
|
|
|
|
assert_nil column.comment
|
|
|
|
end
|
2019-06-03 07:05:36 -04:00
|
|
|
|
|
|
|
def test_comment_on_primary_key
|
|
|
|
column = PkCommented.columns_hash["id"]
|
|
|
|
assert_equal "Primary key comment", column.comment
|
|
|
|
assert_equal "Table comment", @connection.table_comment("pk_commenteds")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_schema_dump_with_primary_key_comment
|
|
|
|
output = dump_table_schema "pk_commenteds"
|
2020-05-23 17:38:21 -04:00
|
|
|
assert_match %r[create_table "pk_commenteds", id: { comment: "Primary key comment" }.*, comment: "Table comment"], output
|
2019-06-03 07:05:36 -04:00
|
|
|
end
|
2016-01-03 13:13:53 -05:00
|
|
|
end
|
2016-04-19 16:48:40 -04:00
|
|
|
end
|