class PostgresqlUUIDTest

Public Class Methods

name() click to toggle source
# File activerecord/test/cases/adapters/postgresql/uuid_test.rb, line 159
def self.name
  "UUIDType"
end

Public Instance Methods

test_acceptable_uuid_regex() click to toggle source
# File activerecord/test/cases/adapters/postgresql/uuid_test.rb, line 104
def test_acceptable_uuid_regex
  # Valid uuids
  ["A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11",
   "{a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11}",
   "a0eebc999c0b4ef8bb6d6bb9bd380a11",
   "a0ee-bc99-9c0b-4ef8-bb6d-6bb9-bd38-0a11",
   "{a0eebc99-9c0b4ef8-bb6d6bb9-bd380a11}",
   # The following is not a valid RFC 4122 UUID, but PG doesn't seem to care,
   # so we shouldn't block it either. (Pay attention to "fb6d" – the "f" here
   # is invalid – it must be one of 8, 9, A, B, a, b according to the spec.)
   "{a0eebc99-9c0b-4ef8-fb6d-6bb9bd380a11}",
  ].each do |valid_uuid|
    uuid = UUIDType.new guid: valid_uuid
    assert_not_nil uuid.guid
  end

  # Invalid uuids
  [["A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11"],
   Hash.new,
   0,
   0.0,
   true,
   "Z0000C99-9C0B-4EF8-BB6D-6BB9BD380A11",
   "a0eebc999r0b4ef8ab6d6bb9bd380a11",
   "a0ee-bc99------4ef8-bb6d-6bb9-bd38-0a11",
   "{a0eebc99-bb6d6bb9-bd380a11}",
   "{a0eebc99-9c0b4ef8-bb6d6bb9-bd380a11",
   "a0eebc99-9c0b4ef8-bb6d6bb9-bd380a11}"].each do |invalid_uuid|
    uuid = UUIDType.new guid: invalid_uuid
    assert_nil uuid.guid
  end
end
test_add_column_with_null_true_and_default_nil() click to toggle source
# File activerecord/test/cases/adapters/postgresql/uuid_test.rb, line 69
def test_add_column_with_null_true_and_default_nil
  connection.add_column :uuid_data_type, :thingy, :uuid, null: true, default: nil

  UUIDType.reset_column_information
  column = UUIDType.columns_hash["thingy"]

  assert column.null
  assert_nil column.default
end
test_change_column_default() click to toggle source
# File activerecord/test/cases/adapters/postgresql/uuid_test.rb, line 55
def test_change_column_default
  connection.add_column :uuid_data_type, :thingy, :uuid, null: false, default: "uuid_generate_v1()"
  UUIDType.reset_column_information
  column = UUIDType.columns_hash["thingy"]
  assert_equal "uuid_generate_v1()", column.default_function

  connection.change_column :uuid_data_type, :thingy, :uuid, null: false, default: "uuid_generate_v4()"
  UUIDType.reset_column_information
  column = UUIDType.columns_hash["thingy"]
  assert_equal "uuid_generate_v4()", column.default_function
ensure
  UUIDType.reset_column_information
end
test_data_type_of_uuid_types() click to toggle source
# File activerecord/test/cases/adapters/postgresql/uuid_test.rb, line 79
def test_data_type_of_uuid_types
  column = UUIDType.columns_hash["guid"]
  assert_equal :uuid, column.type
  assert_equal "uuid", column.sql_type
  assert_not column.array?

  type = UUIDType.type_for_attribute("guid")
  assert_not type.binary?
end
test_invalid_uuid_dont_modify_before_type_cast() click to toggle source
# File activerecord/test/cases/adapters/postgresql/uuid_test.rb, line 99
def test_invalid_uuid_dont_modify_before_type_cast
  uuid = UUIDType.new guid: "foobar"
  assert_equal "foobar", uuid.guid_before_type_cast
end
test_schema_dump_with_shorthand() click to toggle source
# File activerecord/test/cases/adapters/postgresql/uuid_test.rb, line 149
def test_schema_dump_with_shorthand
  output = dump_table_schema "uuid_data_type"
  assert_match %r{t\.uuid "guid"}, output
end
test_treat_blank_uuid_as_nil() click to toggle source
# File activerecord/test/cases/adapters/postgresql/uuid_test.rb, line 89
def test_treat_blank_uuid_as_nil
  UUIDType.create! guid: ""
  assert_nil(UUIDType.last.guid)
end
test_treat_invalid_uuid_as_nil() click to toggle source
# File activerecord/test/cases/adapters/postgresql/uuid_test.rb, line 94
def test_treat_invalid_uuid_as_nil
  uuid = UUIDType.create! guid: "foobar"
  assert_nil(uuid.guid)
end
test_uniqueness_validation_ignores_uuid() click to toggle source
# File activerecord/test/cases/adapters/postgresql/uuid_test.rb, line 154
def test_uniqueness_validation_ignores_uuid
  klass = Class.new(ActiveRecord::Base) do
    self.table_name = "uuid_data_type"
    validates :guid, uniqueness: { case_sensitive: false }

    def self.name
      "UUIDType"
    end
  end

  record = klass.create!(guid: "a0ee-bc99-9c0b-4ef8-bb6d-6bb9-bd38-0a11")
  duplicate = klass.new(guid: record.guid)

  assert record.guid.present? # Ensure we actually are testing a UUID
  assert_not duplicate.valid?
end
test_uuid_column_default() click to toggle source
# File activerecord/test/cases/adapters/postgresql/uuid_test.rb, line 47
def test_uuid_column_default
  connection.add_column :uuid_data_type, :thingy, :uuid, null: false, default: "gen_random_uuid()"
  UUIDType.reset_column_information
  column = UUIDType.columns_hash["thingy"]
  assert_equal "gen_random_uuid()", column.default_function
end
test_uuid_formats() click to toggle source
# File activerecord/test/cases/adapters/postgresql/uuid_test.rb, line 137
def test_uuid_formats
  ["A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11",
   "{a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11}",
   "a0eebc999c0b4ef8bb6d6bb9bd380a11",
   "a0ee-bc99-9c0b-4ef8-bb6d-6bb9-bd38-0a11",
   "{a0eebc99-9c0b4ef8-bb6d6bb9-bd380a11}"].each do |valid_uuid|
    UUIDType.create(guid: valid_uuid)
    uuid = UUIDType.last
    assert_equal "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", uuid.guid
  end
end