hstore_test.rb 3.6 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 16
    begin
      @connection.transaction do
        @connection.create_table('hstores') do |t|
17
          t.hstore 'tags', :default => ''
18 19 20 21
        end
      end
    rescue ActiveRecord::StatementInvalid
      return skip "do not test on PG without hstore"
22
    end
23
    @column = Hstore.columns.find { |c| c.name == 'tags' }
24 25 26 27 28 29 30
  end

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

  def test_column
31
    assert_equal :hstore, @column.type
32
  end
33 34

  def test_type_cast_hstore
35
    assert @column
36 37

    data = "\"1\"=>\"2\""
38
    hash = @column.class.string_to_hstore data
39
    assert_equal({'1' => '2'}, hash)
40 41 42 43 44 45 46 47
    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
48
    assert_equal(%q(" "=>""), @column.class.hstore_to_string({' '=>''}))
49 50 51
  end

  def test_gen2
52
    assert_equal(%q(","=>""), @column.class.hstore_to_string({','=>''}))
53 54 55
  end

  def test_gen3
56
    assert_equal(%q("="=>""), @column.class.hstore_to_string({'='=>''}))
57 58 59
  end

  def test_gen4
60
    assert_equal(%q(">"=>""), @column.class.hstore_to_string({'>'=>''}))
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  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'))
89 90
  end

91 92
  def test_rewrite
    @connection.execute "insert into hstores (tags) VALUES ('1=>2')"
93
    x = Hstore.first
94 95 96 97 98
    x.tags = { '"a\'' => 'b' }
    assert x.save!
  end


99 100
  def test_select
    @connection.execute "insert into hstores (tags) VALUES ('1=>2')"
101
    x = Hstore.first
102 103
    assert_equal({'1' => '2'}, x.tags)
  end
A
Aaron Patterson 已提交
104 105 106

  def test_select_multikey
    @connection.execute "insert into hstores (tags) VALUES ('1=>2,2=>3')"
107
    x = Hstore.first
A
Aaron Patterson 已提交
108 109
    assert_equal({'1' => '2', '2' => '3'}, x.tags)
  end
110 111

  def test_create
A
Aaron Patterson 已提交
112
    assert_cycle('a' => 'b', '1' => '2')
A
Aaron Patterson 已提交
113 114
  end

115 116 117 118
  def test_nil
    assert_cycle('a' => nil)
  end

A
Aaron Patterson 已提交
119
  def test_quotes
A
Aaron Patterson 已提交
120
    assert_cycle('a' => 'b"ar', '1"foo' => '2')
A
Aaron Patterson 已提交
121 122 123
  end

  def test_whitespace
A
Aaron Patterson 已提交
124
    assert_cycle('a b' => 'b ar', '1"foo' => '2')
A
Aaron Patterson 已提交
125 126 127
  end

  def test_backslash
A
Aaron Patterson 已提交
128
    assert_cycle('a\\b' => 'b\\ar', '1"foo' => '2')
A
Aaron Patterson 已提交
129 130 131
  end

  def test_comma
A
Aaron Patterson 已提交
132
    assert_cycle('a, b' => 'bar', '1"foo' => '2')
A
Aaron Patterson 已提交
133 134 135
  end

  def test_arrow
A
Aaron Patterson 已提交
136
    assert_cycle('a=>b' => 'bar', '1"foo' => '2')
A
Aaron Patterson 已提交
137 138
  end

139 140 141 142
  def test_quoting_special_characters
    assert_cycle('ca' => 'cà', 'ac' => 'àc')
  end

A
Aaron Patterson 已提交
143
  private
A
Aaron Patterson 已提交
144
  def assert_cycle hash
145
    # test creation
146 147
    x = Hstore.create!(:tags => hash)
    x.reload
A
Aaron Patterson 已提交
148
    assert_equal(hash, x.tags)
A
Aaron Patterson 已提交
149

150 151
    # test updating
    x = Hstore.create!(:tags => {})
A
Aaron Patterson 已提交
152 153 154 155
    x.tags = hash
    x.save!
    x.reload
    assert_equal(hash, x.tags)
156
  end
157
end