From cf572114608b7813cacd88076dc272ac2a2a7955 Mon Sep 17 00:00:00 2001 From: Joel Date: Sat, 4 Feb 2012 23:51:03 -0800 Subject: Additional hstore tests, supporting null values, better compliance with postgres docs --- .../test/cases/adapters/postgresql/hstore_test.rb | 66 +++++++++++++++++++--- 1 file changed, 59 insertions(+), 7 deletions(-) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/adapters/postgresql/hstore_test.rb b/activerecord/test/cases/adapters/postgresql/hstore_test.rb index 33bf4478cc..28b5a50126 100644 --- a/activerecord/test/cases/adapters/postgresql/hstore_test.rb +++ b/activerecord/test/cases/adapters/postgresql/hstore_test.rb @@ -1,4 +1,6 @@ require "cases/helper" +require 'active_record/base' +require 'active_record/connection_adapters/postgresql_adapter' class PostgresqlHstoreTest < ActiveRecord::TestCase class Hstore < ActiveRecord::Base @@ -16,6 +18,7 @@ class PostgresqlHstoreTest < ActiveRecord::TestCase rescue ActiveRecord::StatementInvalid return skip "do not test on PG without hstore" end + @column = Hstore.columns.find { |c| c.name == 'tags' } end def teardown @@ -23,19 +26,64 @@ class PostgresqlHstoreTest < ActiveRecord::TestCase end def test_column - column = Hstore.columns.find { |c| c.name == 'tags' } - assert column - assert_equal :hstore, column.type + assert_equal :hstore, @column.type end def test_type_cast_hstore - column = Hstore.columns.find { |c| c.name == 'tags' } - assert column + assert @column data = "\"1\"=>\"2\"" - hash = column.class.cast_hstore data + hash = @column.class.cast_hstore data assert_equal({'1' => '2'}, hash) - assert_equal({'1' => '2'}, column.type_cast(data)) + 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 + assert_equal(%q(" "=>""), @column.type_cast({' '=>''})) + end + + def test_gen2 + assert_equal(%q(","=>""), @column.type_cast({','=>''})) + end + + def test_gen3 + assert_equal(%q("="=>""), @column.type_cast({'='=>''})) + end + + def test_gen4 + assert_equal(%q(">"=>""), @column.type_cast({'>'=>''})) + 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')) end def test_select @@ -54,6 +102,10 @@ class PostgresqlHstoreTest < ActiveRecord::TestCase assert_cycle('a' => 'b', '1' => '2') end + def test_nil + assert_cycle('a' => nil) + end + def test_quotes assert_cycle('a' => 'b"ar', '1"foo' => '2') end -- cgit v1.2.3 From 102ef98d32a40e1c7a7526d1d0d3165885315f1c Mon Sep 17 00:00:00 2001 From: Joel Date: Sun, 5 Feb 2012 11:52:50 -0800 Subject: schema dumper tests for hstore --- activerecord/test/cases/schema_dumper_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index abeb56fd3f..ddddce069f 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -227,6 +227,13 @@ class SchemaDumperTest < ActiveRecord::TestCase end end + def test_schema_dump_includes_tsvector_shorthand_definition + output = standard_dump + if %r{create_table "postgresql_hstores"} =~ output + assert_match %r{t.hstore "hash_store", default => ""}, output + end + end + def test_schema_dump_includes_tsvector_shorthand_definition output = standard_dump if %r{create_table "postgresql_tsvectors"} =~ output -- cgit v1.2.3 From b04880cecc6fdc452aeea5d64d078d90dfa2693a Mon Sep 17 00:00:00 2001 From: Joel Date: Sun, 5 Feb 2012 20:36:22 -0800 Subject: string_to_hstore / hstore_to_string, serializing --- activerecord/test/cases/adapters/postgresql/hstore_test.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/adapters/postgresql/hstore_test.rb b/activerecord/test/cases/adapters/postgresql/hstore_test.rb index 28b5a50126..82c52b38a3 100644 --- a/activerecord/test/cases/adapters/postgresql/hstore_test.rb +++ b/activerecord/test/cases/adapters/postgresql/hstore_test.rb @@ -12,7 +12,7 @@ class PostgresqlHstoreTest < ActiveRecord::TestCase begin @connection.transaction do @connection.create_table('hstores') do |t| - t.hstore 'tags' + t.hstore 'tags', :default => '' end end rescue ActiveRecord::StatementInvalid @@ -33,7 +33,7 @@ class PostgresqlHstoreTest < ActiveRecord::TestCase assert @column data = "\"1\"=>\"2\"" - hash = @column.class.cast_hstore data + hash = @column.class.string_to_hstore data assert_equal({'1' => '2'}, hash) assert_equal({'1' => '2'}, @column.type_cast(data)) @@ -43,19 +43,19 @@ class PostgresqlHstoreTest < ActiveRecord::TestCase end def test_gen1 - assert_equal(%q(" "=>""), @column.type_cast({' '=>''})) + assert_equal(%q(" "=>""), @column.class.hstore_to_string({' '=>''})) end def test_gen2 - assert_equal(%q(","=>""), @column.type_cast({','=>''})) + assert_equal(%q(","=>""), @column.class.hstore_to_string({','=>''})) end def test_gen3 - assert_equal(%q("="=>""), @column.type_cast({'='=>''})) + assert_equal(%q("="=>""), @column.class.hstore_to_string({'='=>''})) end def test_gen4 - assert_equal(%q(">"=>""), @column.type_cast({'>'=>''})) + assert_equal(%q(">"=>""), @column.class.hstore_to_string({'>'=>''})) end def test_parse1 -- cgit v1.2.3 From 2dd0178229ccd5e7f19f120ded85fd4a80306cac Mon Sep 17 00:00:00 2001 From: Joel Date: Wed, 8 Feb 2012 14:21:10 -0800 Subject: Also support writing the hstore back to the database --- activerecord/test/cases/adapters/postgresql/hstore_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/adapters/postgresql/hstore_test.rb b/activerecord/test/cases/adapters/postgresql/hstore_test.rb index 82c52b38a3..1644a58d92 100644 --- a/activerecord/test/cases/adapters/postgresql/hstore_test.rb +++ b/activerecord/test/cases/adapters/postgresql/hstore_test.rb @@ -86,6 +86,14 @@ class PostgresqlHstoreTest < ActiveRecord::TestCase assert_equal({"\"a"=>"q\"w"}, @column.type_cast('\"a=>q"w')) end + def test_rewrite + @connection.execute "insert into hstores (tags) VALUES ('1=>2')" + x = Hstore.find :first + x.tags = { '"a\'' => 'b' } + assert x.save! + end + + def test_select @connection.execute "insert into hstores (tags) VALUES ('1=>2')" x = Hstore.find :first -- cgit v1.2.3