hstore_test.rb 5.7 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 'settings'
28 29
      end
    end
30
    @column = Hstore.columns.find { |c| c.name == 'tags' }
31 32 33 34 35 36
  end

  def teardown
    @connection.execute 'drop table if exists hstores'
  end

37 38 39 40 41
  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
42

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

54 55 56
    def test_column
      assert_equal :hstore, @column.type
    end
57

58 59 60 61 62 63 64 65 66 67
    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.find { |c| c.name == 'users' }
        assert_equal :hstore, column.type

        raise ActiveRecord::Rollback # reset the schema change
68
      end
69
    ensure
70 71 72
      Hstore.reset_column_information
    end

73 74 75 76 77 78
    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
79

80 81
    def test_type_cast_hstore
      assert @column
82

83 84 85 86
      data = "\"1\"=>\"2\""
      hash = @column.class.string_to_hstore data
      assert_equal({'1' => '2'}, hash)
      assert_equal({'1' => '2'}, @column.type_cast(data))
87

88 89 90 91
      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
92

93 94 95 96
    def test_with_store_accessors
      x = Hstore.new(language: "fr", timezone: "GMT")
      assert_equal "fr", x.language
      assert_equal "GMT", x.timezone
97

98 99 100 101
      x.save!
      x = Hstore.first
      assert_equal "fr", x.language
      assert_equal "GMT", x.timezone
102

103 104
      x.language = "de"
      x.save!
105

106 107 108 109
      x = Hstore.first
      assert_equal "de", x.language
      assert_equal "GMT", x.timezone
    end
110

111 112 113
    def test_gen1
      assert_equal(%q(" "=>""), @column.class.hstore_to_string({' '=>''}))
    end
114

115 116 117
    def test_gen2
      assert_equal(%q(","=>""), @column.class.hstore_to_string({','=>''}))
    end
118

119 120 121
    def test_gen3
      assert_equal(%q("="=>""), @column.class.hstore_to_string({'='=>''}))
    end
122

123 124 125
    def test_gen4
      assert_equal(%q(">"=>""), @column.class.hstore_to_string({'>'=>''}))
    end
126

127 128 129
    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
130

131 132 133
    def test_parse2
      assert_equal({" " => " "},  @column.type_cast("\\ =>\\ "))
    end
134

135 136 137
    def test_parse3
      assert_equal({"=" => ">"},  @column.type_cast("==>>"))
    end
138

139 140 141
    def test_parse4
      assert_equal({"=a"=>"q=w"},   @column.type_cast('\=a=>q=w'))
    end
142

143 144 145
    def test_parse5
      assert_equal({"=a"=>"q=w"},   @column.type_cast('"=a"=>q\=w'))
    end
146

147 148 149
    def test_parse6
      assert_equal({"\"a"=>"q>w"},  @column.type_cast('"\"a"=>q>w'))
    end
150

151 152 153
    def test_parse7
      assert_equal({"\"a"=>"q\"w"}, @column.type_cast('\"a=>q"w'))
    end
154

155 156 157 158 159 160
    def test_rewrite
      @connection.execute "insert into hstores (tags) VALUES ('1=>2')"
      x = Hstore.first
      x.tags = { '"a\'' => 'b' }
      assert x.save!
    end
161

162 163 164 165 166
    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 已提交
167

168 169 170 171 172
    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
173

174 175 176
    def test_create
      assert_cycle('a' => 'b', '1' => '2')
    end
A
Aaron Patterson 已提交
177

178 179 180
    def test_nil
      assert_cycle('a' => nil)
    end
181

182 183 184
    def test_quotes
      assert_cycle('a' => 'b"ar', '1"foo' => '2')
    end
A
Aaron Patterson 已提交
185

186 187 188
    def test_whitespace
      assert_cycle('a b' => 'b ar', '1"foo' => '2')
    end
A
Aaron Patterson 已提交
189

190 191 192
    def test_backslash
      assert_cycle('a\\b' => 'b\\ar', '1"foo' => '2')
    end
A
Aaron Patterson 已提交
193

194 195 196
    def test_comma
      assert_cycle('a, b' => 'bar', '1"foo' => '2')
    end
A
Aaron Patterson 已提交
197

198 199 200
    def test_arrow
      assert_cycle('a=>b' => 'bar', '1"foo' => '2')
    end
A
Aaron Patterson 已提交
201

202 203 204
    def test_quoting_special_characters
      assert_cycle('ca' => 'cà', 'ac' => 'àc')
    end
205

206 207 208
    def test_multiline
      assert_cycle("a\nb" => "c\nd")
    end
209 210
  end

A
Aaron Patterson 已提交
211
  private
212 213 214 215 216 217 218 219 220 221 222 223 224 225

    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
226
end