class PostgresqlHstoreTest

Public Instance Methods

change() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 94
def change
  change_table("hstores") do |t|
    t.hstore :keys
  end
end
setup() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 21
def setup
  @connection = ActiveRecord::Base.connection

  enable_extension!("hstore", @connection)

  @connection.transaction do
    @connection.create_table("hstores") do |t|
      t.hstore "tags", default: ""
      t.hstore "payload", array: true
      t.hstore "settings"
    end
  end
  Hstore.reset_column_information
  @column = Hstore.columns_hash["tags"]
  @type = Hstore.type_for_attribute("tags")
end
test_array_cycle() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 248
def test_array_cycle
  assert_array_cycle([{ "AA" => "BB", "CC" => "DD" }, { "AA" => nil }])
end
test_array_strings_with_array_delimiters() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 260
def test_array_strings_with_array_delimiters
  assert_array_cycle(["{" => "}"])
end
test_array_strings_with_commas() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 256
def test_array_strings_with_commas
  assert_array_cycle([{ "this,has" => "many,values" }])
end
test_array_strings_with_null_strings() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 264
def test_array_strings_with_null_strings
  assert_array_cycle([{ "NULL" => "NULL" }])
end
test_array_strings_with_quotes() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 252
def test_array_strings_with_quotes
  assert_array_cycle([{ "this has" => 'some "s that need to be escaped"' }])
end
test_arrow() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 302
def test_arrow
  assert_cycle("a=>b" => "bar", '1"foo' => "2")
end
test_backslash() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 294
def test_backslash
  assert_cycle('a\\b' => 'b\\ar', '1"foo' => "2")
end
test_cast_value_on_write() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 109
def test_cast_value_on_write
  x = Hstore.new tags: { "bool" => true, "number" => 5 }
  assert_equal({ "bool" => true, "number" => 5 }, x.tags_before_type_cast)
  assert_equal({ "bool" => "true", "number" => "5" }, x.tags)
  x.save
  assert_equal({ "bool" => "true", "number" => "5" }, x.reload.tags)
end
test_change_table_supports_hstore() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 77
def test_change_table_supports_hstore
  @connection.transaction do
    @connection.change_table("hstores") do |t|
      t.hstore "users", default: ""
    end
    Hstore.reset_column_information
    column = Hstore.columns_hash["users"]
    assert_equal :hstore, column.type

    raise ActiveRecord::Rollback # reset the schema change
  end
ensure
  Hstore.reset_column_information
end
test_changes_in_place() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 162
def test_changes_in_place
  hstore = Hstore.create!(settings: { "one" => "two" })
  hstore.settings["three"] = "four"
  hstore.save!
  hstore.reload

  assert_equal "four", hstore.settings["three"]
  assert_not hstore.changed?
end
test_clone_hstore_with_serialized_attributes() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 335
def test_clone_hstore_with_serialized_attributes
  HstoreWithSerialize.create! tags: TagCollection.new("one" => "two")
  record = HstoreWithSerialize.first
  dupe = record.dup
  assert_equal({ "one" => "two" }, dupe.tags.to_hash)
end
test_column() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 59
def test_column
  assert_equal :hstore, @column.type
  assert_equal "hstore", @column.sql_type
  assert_not @column.array?

  assert_not @type.binary?
end
test_comma() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 298
def test_comma
  assert_cycle("a, b" => "bar", '1"foo' => "2")
end
test_contains_nils() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 268
def test_contains_nils
  assert_array_cycle([{ "NULL" => nil }])
end
test_create() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 278
def test_create
  assert_cycle("a" => "b", "1" => "2")
end
test_default() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 67
def test_default
  @connection.add_column "hstores", "permissions", :hstore, default: '"users"=>"read", "articles"=>"write"'
  Hstore.reset_column_information

  assert_equal({ "users" => "read", "articles" => "write" }, Hstore.column_defaults["permissions"])
  assert_equal({ "users" => "read", "articles" => "write" }, Hstore.new.permissions)
ensure
  Hstore.reset_column_information
end
test_dirty_from_user_equal() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 172
def test_dirty_from_user_equal
  settings = { "alongkey" => "anything", "key" => "value" }
  hstore = Hstore.create!(settings: settings)

  hstore.settings = { "key" => "value", "alongkey" => "anything" }
  assert_equal settings, hstore.settings
  refute hstore.changed?
end
test_disable_enable_hstore() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 48
def test_disable_enable_hstore
  assert @connection.extension_enabled?("hstore")
  @connection.disable_extension "hstore"
  assert_not @connection.extension_enabled?("hstore")
  @connection.enable_extension "hstore"
  assert @connection.extension_enabled?("hstore")
ensure
  # Restore column(s) dropped by `drop extension hstore cascade;`
  load_schema
end
test_duplication_with_store_accessors() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 142
def test_duplication_with_store_accessors
  x = Hstore.new(language: "fr", timezone: "GMT")
  assert_equal "fr", x.language
  assert_equal "GMT", x.timezone

  y = x.dup
  assert_equal "fr", y.language
  assert_equal "GMT", y.timezone
end
test_gen1() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 191
def test_gen1
  assert_equal('" "=>""', @type.serialize(" " => ""))
end
test_gen2() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 195
def test_gen2
  assert_equal('","=>""', @type.serialize("," => ""))
end
test_gen3() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 199
def test_gen3
  assert_equal('"="=>""', @type.serialize("=" => ""))
end
test_gen4() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 203
def test_gen4
  assert_equal('">"=>""', @type.serialize(">" => ""))
end
test_hstore_dirty_from_database_equal() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 181
def test_hstore_dirty_from_database_equal
  settings = { "alongkey" => "anything", "key" => "value" }
  hstore = Hstore.create!(settings: settings)
  hstore.reload

  assert_equal settings, hstore.settings
  hstore.settings = settings
  refute hstore.changed?
end
test_hstore_included_in_extensions() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 43
def test_hstore_included_in_extensions
  assert @connection.respond_to?(:extensions), "connection should have a list of extensions"
  assert_includes @connection.extensions, "hstore", "extension list should include hstore"
end
test_hstore_migration() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 92
def test_hstore_migration
  hstore_migration = Class.new(ActiveRecord::Migration::Current) do
    def change
      change_table("hstores") do |t|
        t.hstore :keys
      end
    end
  end

  hstore_migration.new.suppress_messages do
    hstore_migration.migrate(:up)
    assert_includes @connection.columns(:hstores).map(&:name), "keys"
    hstore_migration.migrate(:down)
    assert_not_includes @connection.columns(:hstores).map(&:name), "keys"
  end
end
test_hstore_with_serialized_attributes() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 325
def test_hstore_with_serialized_attributes
  HstoreWithSerialize.create! tags: TagCollection.new("one" => "two")
  record = HstoreWithSerialize.first
  assert_instance_of TagCollection, record.tags
  assert_equal({ "one" => "two" }, record.tags.to_hash)
  record.tags = TagCollection.new("three" => "four")
  record.save!
  assert_equal({ "three" => "four" }, HstoreWithSerialize.first.tags.to_hash)
end
test_multiline() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 310
def test_multiline
  assert_cycle("a\nb" => "c\nd")
end
test_nil() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 282
def test_nil
  assert_cycle("a" => nil)
end
test_parse1() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 207
def test_parse1
  assert_equal({ "a" => nil, "b" => nil, "c" => "NuLl", "null" => "c" }, @type.deserialize('a=>null,b=>NuLl,c=>"NuLl",null=>c'))
end
test_parse2() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 211
def test_parse2
  assert_equal({ " " => " " },  @type.deserialize("\\ =>\\ "))
end
test_parse3() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 215
def test_parse3
  assert_equal({ "=" => ">" },  @type.deserialize("==>>"))
end
test_parse4() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 219
def test_parse4
  assert_equal({ "=a" => "q=w" },   @type.deserialize('\=a=>q=w'))
end
test_parse5() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 223
def test_parse5
  assert_equal({ "=a" => "q=w" },   @type.deserialize('"=a"=>q\=w'))
end
test_parse6() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 227
def test_parse6
  assert_equal({ "\"a" => "q>w" },  @type.deserialize('"\"a"=>q>w'))
end
test_parse7() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 231
def test_parse7
  assert_equal({ "\"a" => "q\"w" }, @type.deserialize('\"a=>q"w'))
end
test_quotes() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 286
def test_quotes
  assert_cycle("a" => 'b"ar', '1"foo' => "2")
end
test_quoting_special_characters() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 306
def test_quoting_special_characters
  assert_cycle("ca" => "cà", "ac" => "àc")
end
test_rewrite() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 235
def test_rewrite
  @connection.execute "insert into hstores (tags) VALUES ('1=>2')"
  x = Hstore.first
  x.tags = { '"a\'' => "b" }
  assert x.save!
end
test_schema_dump_with_shorthand() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 342
def test_schema_dump_with_shorthand
  output = dump_table_schema("hstores")
  assert_match %r[t\.hstore "tags",\s+default: {}], output
end
test_select() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 242
def test_select
  @connection.execute "insert into hstores (tags) VALUES ('1=>2')"
  x = Hstore.first
  assert_equal({ "1" => "2" }, x.tags)
end
test_select_multikey() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 272
def test_select_multikey
  @connection.execute "insert into hstores (tags) VALUES ('1=>2,2=>3')"
  x = Hstore.first
  assert_equal({ "1" => "2", "2" => "3" }, x.tags)
end
test_supports_to_unsafe_h_values() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 347
def test_supports_to_unsafe_h_values
  assert_equal("\"hi\"=>\"hi\"", @type.serialize(FakeParameters.new))
end
test_type_cast_hstore() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 117
def test_type_cast_hstore
  assert_equal({ "1" => "2" }, @type.deserialize("\"1\"=>\"2\""))
  assert_equal({}, @type.deserialize(""))
  assert_equal({ "key" => nil }, @type.deserialize("key => NULL"))
  assert_equal({ "c" => "}", '"a"' => 'b "a b' }, @type.deserialize(%q(c=>"}", "\"a\""=>"b \"a b")))
end
test_whitespace() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 290
def test_whitespace
  assert_cycle("a b" => "b ar", '1"foo' => "2")
end
test_with_store_accessors() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 124
def test_with_store_accessors
  x = Hstore.new(language: "fr", timezone: "GMT")
  assert_equal "fr", x.language
  assert_equal "GMT", x.timezone

  x.save!
  x = Hstore.first
  assert_equal "fr", x.language
  assert_equal "GMT", x.timezone

  x.language = "de"
  x.save!

  x = Hstore.first
  assert_equal "de", x.language
  assert_equal "GMT", x.timezone
end
test_yaml_round_trip_with_store_accessors() click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 152
def test_yaml_round_trip_with_store_accessors
  x = Hstore.new(language: "fr", timezone: "GMT")
  assert_equal "fr", x.language
  assert_equal "GMT", x.timezone

  y = YAML.load(YAML.dump(x))
  assert_equal "fr", y.language
  assert_equal "GMT", y.timezone
end

Private Instance Methods

assert_array_cycle(array) click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 352
def assert_array_cycle(array)
  # test creation
  x = Hstore.create!(payload: array)
  x.reload
  assert_equal(array, x.payload)

  # test updating
  x = Hstore.create!(payload: [])
  x.payload = array
  x.save!
  x.reload
  assert_equal(array, x.payload)
end
assert_cycle(hash) click to toggle source
# File activerecord/test/cases/adapters/postgresql/hstore_test.rb, line 366
def assert_cycle(hash)
  # test creation
  x = Hstore.create!(tags: hash)
  x.reload
  assert_equal(hash, x.tags)

  # test updating
  x = Hstore.create!(tags: {})
  x.tags = hash
  x.save!
  x.reload
  assert_equal(hash, x.tags)
end