“0c672dee2ec5577b49dcbd18e7357d1b2cfc6f70”上不存在“src/main/java/me/git@gitcode.net:java639/JustAuth.git”
hstore_test.rb 4.1 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 10 11 12 13

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

  def setup
    @connection = ActiveRecord::Base.connection
14 15
    unless @connection.extension_enabled?('hstore')
      @connection.enable_extension 'hstore'
16
      return skip "do not test on PG without hstore"
17
    end
18 19 20 21 22 23

    @connection.transaction do
      @connection.create_table('hstores') do |t|
        t.hstore 'tags', :default => ''
      end
    end
24
    @column = Hstore.columns.find { |c| c.name == 'tags' }
25 26 27 28 29 30
  end

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

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  def test_hstore_enabled
    assert @connection.extension_enabled?('hstore')
  end

  def test_disable_hstore
    if @connection.extension_enabled?('hstore')
      @connection.disable_extension 'hstore'
      assert_not @connection.extension_enabled?('hstore')
    end
  end

  def test_enable_hstore
    if @connection.extension_enabled?('hstore')
      @connection.disable_extension 'hstore'
    end

    assert_not @connection.extension_enabled?('hstore')
    @connection.enable_extension 'hstore'
    assert @connection.extension_enabled?('hstore')
  end

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

  def test_type_cast_hstore
57
    assert @column
58 59

    data = "\"1\"=>\"2\""
60
    hash = @column.class.string_to_hstore data
61
    assert_equal({'1' => '2'}, hash)
62 63 64 65 66 67 68 69
    assert_equal({'1' => '2'}, @column.type_cast(data))

    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

  def test_gen1
70
    assert_equal(%q(" "=>""), @column.class.hstore_to_string({' '=>''}))
71 72 73
  end

  def test_gen2
74
    assert_equal(%q(","=>""), @column.class.hstore_to_string({','=>''}))
75 76 77
  end

  def test_gen3
78
    assert_equal(%q("="=>""), @column.class.hstore_to_string({'='=>''}))
79 80 81
  end

  def test_gen4
82
    assert_equal(%q(">"=>""), @column.class.hstore_to_string({'>'=>''}))
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  end

  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

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

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

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

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

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

  def test_parse7
    assert_equal({"\"a"=>"q\"w"}, @column.type_cast('\"a=>q"w'))
111 112
  end

113 114
  def test_rewrite
    @connection.execute "insert into hstores (tags) VALUES ('1=>2')"
115
    x = Hstore.first
116 117 118 119 120
    x.tags = { '"a\'' => 'b' }
    assert x.save!
  end


121 122
  def test_select
    @connection.execute "insert into hstores (tags) VALUES ('1=>2')"
123
    x = Hstore.first
124 125
    assert_equal({'1' => '2'}, x.tags)
  end
A
Aaron Patterson 已提交
126 127 128

  def test_select_multikey
    @connection.execute "insert into hstores (tags) VALUES ('1=>2,2=>3')"
129
    x = Hstore.first
A
Aaron Patterson 已提交
130 131
    assert_equal({'1' => '2', '2' => '3'}, x.tags)
  end
132 133

  def test_create
A
Aaron Patterson 已提交
134
    assert_cycle('a' => 'b', '1' => '2')
A
Aaron Patterson 已提交
135 136
  end

137 138 139 140
  def test_nil
    assert_cycle('a' => nil)
  end

A
Aaron Patterson 已提交
141
  def test_quotes
A
Aaron Patterson 已提交
142
    assert_cycle('a' => 'b"ar', '1"foo' => '2')
A
Aaron Patterson 已提交
143 144 145
  end

  def test_whitespace
A
Aaron Patterson 已提交
146
    assert_cycle('a b' => 'b ar', '1"foo' => '2')
A
Aaron Patterson 已提交
147 148 149
  end

  def test_backslash
A
Aaron Patterson 已提交
150
    assert_cycle('a\\b' => 'b\\ar', '1"foo' => '2')
A
Aaron Patterson 已提交
151 152 153
  end

  def test_comma
A
Aaron Patterson 已提交
154
    assert_cycle('a, b' => 'bar', '1"foo' => '2')
A
Aaron Patterson 已提交
155 156 157
  end

  def test_arrow
A
Aaron Patterson 已提交
158
    assert_cycle('a=>b' => 'bar', '1"foo' => '2')
A
Aaron Patterson 已提交
159 160
  end

161 162 163 164
  def test_quoting_special_characters
    assert_cycle('ca' => 'cà', 'ac' => 'àc')
  end

A
Aaron Patterson 已提交
165
  private
A
Aaron Patterson 已提交
166
  def assert_cycle hash
167
    # test creation
168 169
    x = Hstore.create!(:tags => hash)
    x.reload
A
Aaron Patterson 已提交
170
    assert_equal(hash, x.tags)
A
Aaron Patterson 已提交
171

172 173
    # test updating
    x = Hstore.create!(:tags => {})
A
Aaron Patterson 已提交
174 175 176 177
    x.tags = hash
    x.save!
    x.reload
    assert_equal(hash, x.tags)
178
  end
179
end