hstore_test.rb 8.2 KB
Newer Older
1 2
# encoding: utf-8

3
require "cases/helper"
4 5
require 'active_record/base'
require 'active_record/connection_adapters/postgresql_adapter'
6 7 8 9

class PostgresqlHstoreTest < ActiveRecord::TestCase
  class Hstore < ActiveRecord::Base
    self.table_name = 'hstores'
10 11

    store_accessor :settings, :language, :timezone
12 13 14 15
  end

  def setup
    @connection = ActiveRecord::Base.connection
16

17 18
    unless @connection.extension_enabled?('hstore')
      @connection.enable_extension 'hstore'
19
      @connection.commit_db_transaction
20
    end
21

22 23
    @connection.reconnect!

24 25 26
    @connection.transaction do
      @connection.create_table('hstores') do |t|
        t.hstore 'tags', :default => ''
27
        t.hstore 'payload', array: true
28
        t.hstore 'settings'
29 30
      end
    end
31
    @column = Hstore.columns_hash['tags']
32 33
  end

G
Guo Xiang Tan 已提交
34
  teardown do
35 36 37
    @connection.execute 'drop table if exists hstores'
  end

38 39 40 41 42
  if ActiveRecord::Base.connection.supports_extensions?
    def test_hstore_included_in_extensions
      assert @connection.respond_to?(:extensions), "connection should have a list of extensions"
      assert @connection.extensions.include?('hstore'), "extension list should include hstore"
    end
43

44 45 46 47 48 49 50 51 52 53
    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
54

55 56
    def test_column
      assert_equal :hstore, @column.type
57 58 59 60 61
      assert_equal "hstore", @column.sql_type
      assert_not @column.number?
      assert_not @column.text?
      assert_not @column.binary?
      assert_not @column.array
62
    end
63

64 65 66 67 68 69 70 71
    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.new.permissions)
    ensure
      Hstore.reset_column_information
    end

72 73 74 75 76 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
78
        column = Hstore.columns_hash['users']
79 80 81
        assert_equal :hstore, column.type

        raise ActiveRecord::Rollback # reset the schema change
82
      end
83
    ensure
84 85 86
      Hstore.reset_column_information
    end

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    def test_hstore_migration
      hstore_migration = Class.new(ActiveRecord::Migration) 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

104 105 106 107 108 109
    def test_cast_value_on_write
      x = Hstore.new tags: {"bool" => true, "number" => 5}
      assert_equal({"bool" => "true", "number" => "5"}, x.tags)
      x.save
      assert_equal({"bool" => "true", "number" => "5"}, x.reload.tags)
    end
110

111 112
    def test_type_cast_hstore
      assert @column
113

114 115 116 117
      data = "\"1\"=>\"2\""
      hash = @column.class.string_to_hstore data
      assert_equal({'1' => '2'}, hash)
      assert_equal({'1' => '2'}, @column.type_cast(data))
118

119 120 121 122
      assert_equal({}, @column.type_cast(""))
      assert_equal({'key'=>nil}, @column.type_cast('key => NULL'))
      assert_equal({'c'=>'}','"a"'=>'b "a b'}, @column.type_cast(%q(c=>"}", "\"a\""=>"b \"a b")))
    end
123

124 125 126 127
    def test_with_store_accessors
      x = Hstore.new(language: "fr", timezone: "GMT")
      assert_equal "fr", x.language
      assert_equal "GMT", x.timezone
128

129 130 131 132
      x.save!
      x = Hstore.first
      assert_equal "fr", x.language
      assert_equal "GMT", x.timezone
133

134 135
      x.language = "de"
      x.save!
136

137 138 139 140
      x = Hstore.first
      assert_equal "de", x.language
      assert_equal "GMT", x.timezone
    end
141

142 143 144 145 146 147 148 149 150 151
    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

152 153 154
    def test_gen1
      assert_equal(%q(" "=>""), @column.class.hstore_to_string({' '=>''}))
    end
155

156 157 158
    def test_gen2
      assert_equal(%q(","=>""), @column.class.hstore_to_string({','=>''}))
    end
159

160 161 162
    def test_gen3
      assert_equal(%q("="=>""), @column.class.hstore_to_string({'='=>''}))
    end
163

164 165 166
    def test_gen4
      assert_equal(%q(">"=>""), @column.class.hstore_to_string({'>'=>''}))
    end
167

168 169 170
    def test_parse1
      assert_equal({'a'=>nil,'b'=>nil,'c'=>'NuLl','null'=>'c'}, @column.type_cast('a=>null,b=>NuLl,c=>"NuLl",null=>c'))
    end
171

172 173 174
    def test_parse2
      assert_equal({" " => " "},  @column.type_cast("\\ =>\\ "))
    end
175

176 177 178
    def test_parse3
      assert_equal({"=" => ">"},  @column.type_cast("==>>"))
    end
179

180 181 182
    def test_parse4
      assert_equal({"=a"=>"q=w"},   @column.type_cast('\=a=>q=w'))
    end
183

184 185 186
    def test_parse5
      assert_equal({"=a"=>"q=w"},   @column.type_cast('"=a"=>q\=w'))
    end
187

188 189 190
    def test_parse6
      assert_equal({"\"a"=>"q>w"},  @column.type_cast('"\"a"=>q>w'))
    end
191

192 193 194
    def test_parse7
      assert_equal({"\"a"=>"q\"w"}, @column.type_cast('\"a=>q"w'))
    end
195

196 197 198 199 200 201
    def test_rewrite
      @connection.execute "insert into hstores (tags) VALUES ('1=>2')"
      x = Hstore.first
      x.tags = { '"a\'' => 'b' }
      assert x.save!
    end
202

203 204 205 206 207
    def test_select
      @connection.execute "insert into hstores (tags) VALUES ('1=>2')"
      x = Hstore.first
      assert_equal({'1' => '2'}, x.tags)
    end
A
Aaron Patterson 已提交
208

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    def test_array_cycle
      assert_array_cycle([{"AA" => "BB", "CC" => "DD"}, {"AA" => nil}])
    end

    def test_array_strings_with_quotes
      assert_array_cycle([{'this has' => 'some "s that need to be escaped"'}])
    end

    def test_array_strings_with_commas
      assert_array_cycle([{'this,has' => 'many,values'}])
    end

    def test_array_strings_with_array_delimiters
      assert_array_cycle(['{' => '}'])
    end

    def test_array_strings_with_null_strings
      assert_array_cycle([{'NULL' => 'NULL'}])
    end

    def test_contains_nils
      assert_array_cycle([{'NULL' => nil}])
    end

233 234 235 236 237
    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
238

239 240 241
    def test_create
      assert_cycle('a' => 'b', '1' => '2')
    end
A
Aaron Patterson 已提交
242

243 244 245
    def test_nil
      assert_cycle('a' => nil)
    end
246

247 248 249
    def test_quotes
      assert_cycle('a' => 'b"ar', '1"foo' => '2')
    end
A
Aaron Patterson 已提交
250

251 252 253
    def test_whitespace
      assert_cycle('a b' => 'b ar', '1"foo' => '2')
    end
A
Aaron Patterson 已提交
254

255 256 257
    def test_backslash
      assert_cycle('a\\b' => 'b\\ar', '1"foo' => '2')
    end
A
Aaron Patterson 已提交
258

259 260 261
    def test_comma
      assert_cycle('a, b' => 'bar', '1"foo' => '2')
    end
A
Aaron Patterson 已提交
262

263 264 265
    def test_arrow
      assert_cycle('a=>b' => 'bar', '1"foo' => '2')
    end
A
Aaron Patterson 已提交
266

267 268 269
    def test_quoting_special_characters
      assert_cycle('ca' => 'cà', 'ac' => 'àc')
    end
270

271 272 273
    def test_multiline
      assert_cycle("a\nb" => "c\nd")
    end
274 275

    def test_update_all
276 277
      hstore = Hstore.create! tags: { "one" => "two" }

278
      Hstore.update_all tags: { "three" => "four" }
279 280 281 282
      assert_equal({ "three" => "four" }, hstore.reload.tags)

      Hstore.update_all tags: { }
      assert_equal({ }, hstore.reload.tags)
283
    end
284 285
  end

A
Aaron Patterson 已提交
286
  private
287

288 289 290 291 292 293 294 295 296 297 298 299 300 301
    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

302 303 304 305 306 307 308 309 310 311 312 313 314
    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
315
end