hstore_test.rb 3.3 KB
Newer Older
1
require "cases/helper"
2 3
require 'active_record/base'
require 'active_record/connection_adapters/postgresql_adapter'
4 5 6 7 8 9 10 11

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

  def setup
    @connection = ActiveRecord::Base.connection
12 13 14
    begin
      @connection.transaction do
        @connection.create_table('hstores') do |t|
15
          t.hstore 'tags', :default => ''
16 17 18 19
        end
      end
    rescue ActiveRecord::StatementInvalid
      return skip "do not test on PG without hstore"
20
    end
21
    @column = Hstore.columns.find { |c| c.name == 'tags' }
22 23 24 25 26 27 28
  end

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

  def test_column
29
    assert_equal :hstore, @column.type
30
  end
31 32

  def test_type_cast_hstore
33
    assert @column
34 35

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

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

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

  def test_gen4
58
    assert_equal(%q(">"=>""), @column.class.hstore_to_string({'>'=>''}))
59 60 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
  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'))
87 88 89 90 91 92 93
  end

  def test_select
    @connection.execute "insert into hstores (tags) VALUES ('1=>2')"
    x = Hstore.find :first
    assert_equal({'1' => '2'}, x.tags)
  end
A
Aaron Patterson 已提交
94 95 96 97 98 99

  def test_select_multikey
    @connection.execute "insert into hstores (tags) VALUES ('1=>2,2=>3')"
    x = Hstore.find :first
    assert_equal({'1' => '2', '2' => '3'}, x.tags)
  end
100 101

  def test_create
A
Aaron Patterson 已提交
102
    assert_cycle('a' => 'b', '1' => '2')
A
Aaron Patterson 已提交
103 104
  end

105 106 107 108
  def test_nil
    assert_cycle('a' => nil)
  end

A
Aaron Patterson 已提交
109
  def test_quotes
A
Aaron Patterson 已提交
110
    assert_cycle('a' => 'b"ar', '1"foo' => '2')
A
Aaron Patterson 已提交
111 112 113
  end

  def test_whitespace
A
Aaron Patterson 已提交
114
    assert_cycle('a b' => 'b ar', '1"foo' => '2')
A
Aaron Patterson 已提交
115 116 117
  end

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

  def test_comma
A
Aaron Patterson 已提交
122
    assert_cycle('a, b' => 'bar', '1"foo' => '2')
A
Aaron Patterson 已提交
123 124 125
  end

  def test_arrow
A
Aaron Patterson 已提交
126
    assert_cycle('a=>b' => 'bar', '1"foo' => '2')
A
Aaron Patterson 已提交
127 128 129
  end

  private
A
Aaron Patterson 已提交
130
  def assert_cycle hash
131 132
    x = Hstore.create!(:tags => hash)
    x.reload
A
Aaron Patterson 已提交
133
    assert_equal(hash, x.tags)
A
Aaron Patterson 已提交
134 135 136 137 138 139

    # make sure updates work
    x.tags = hash
    x.save!
    x.reload
    assert_equal(hash, x.tags)
140
  end
141
end