diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-02-10 10:55:34 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-02-10 10:55:34 -0800 |
commit | f7b915b50718c86d3644941cd0bbe7df08888a5b (patch) | |
tree | 694847bdcdf374afc65eec9a81aa76fa63855ccf /activerecord/test/cases | |
parent | 522b72fd28b898c914cfbdbc31837e56702f60d5 (diff) | |
parent | 2dd0178229ccd5e7f19f120ded85fd4a80306cac (diff) | |
download | rails-f7b915b50718c86d3644941cd0bbe7df08888a5b.tar.gz rails-f7b915b50718c86d3644941cd0bbe7df08888a5b.tar.bz2 rails-f7b915b50718c86d3644941cd0bbe7df08888a5b.zip |
Merge branch 'joelhoffman-postgres_schema_builder' into instance_reader
* joelhoffman-postgres_schema_builder:
Also support writing the hstore back to the database
Hstore values are all strings
string_to_hstore / hstore_to_string, serializing
don't test schema where hstore not installed
schema dumper tests for hstore
Additional hstore tests, supporting null values, better compliance with postgres docs
add hstore to postgres native types and defaults
Conflicts:
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r-- | activerecord/test/cases/adapters/postgresql/hstore_test.rb | 76 | ||||
-rw-r--r-- | activerecord/test/cases/schema_dumper_test.rb | 7 |
2 files changed, 75 insertions, 8 deletions
diff --git a/activerecord/test/cases/adapters/postgresql/hstore_test.rb b/activerecord/test/cases/adapters/postgresql/hstore_test.rb index 33bf4478cc..1644a58d92 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 @@ -10,12 +12,13 @@ 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 return skip "do not test on PG without hstore" end + @column = Hstore.columns.find { |c| c.name == 'tags' } end def teardown @@ -23,21 +26,74 @@ 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.string_to_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.class.hstore_to_string({' '=>''})) + end + + def test_gen2 + assert_equal(%q(","=>""), @column.class.hstore_to_string({','=>''})) + end + + def test_gen3 + assert_equal(%q("="=>""), @column.class.hstore_to_string({'='=>''})) + end + + def test_gen4 + assert_equal(%q(">"=>""), @column.class.hstore_to_string({'>'=>''})) + 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_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 @@ -54,6 +110,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 diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index c79382a984..4de0999dbb 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -238,6 +238,13 @@ class SchemaDumperTest < ActiveRecord::TestCase 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 assert_match %r{t.tsvector "text_vector"}, output end |