From 1917392c269d43962c22ca0f3ec36042522c0909 Mon Sep 17 00:00:00 2001 From: "Ricardo Chimal, Jr" Date: Thu, 21 Aug 2008 14:59:38 -0700 Subject: [PATCH] verify the database encoding is utf8 or unicode --- lib/yaml_db.rb | 20 ++++++++++++++++++++ spec/yaml_dump_spec.rb | 1 + spec/yaml_load_spec.rb | 1 + spec/yaml_spec.rb | 27 +++++++++++++++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 spec/yaml_spec.rb diff --git a/lib/yaml_db.rb b/lib/yaml_db.rb index 1cd485e..9219ed1 100644 --- a/lib/yaml_db.rb +++ b/lib/yaml_db.rb @@ -5,12 +5,16 @@ module YamlDb def self.dump(filename) + verify_utf8 + disable_logger YamlDb::Dump.dump(File.new(filename, "w")) reenable_logger end def self.load(filename) + verify_utf8 + disable_logger YamlDb::Load.load(File.new(filename, "r")) reenable_logger @@ -24,6 +28,22 @@ def self.disable_logger def self.reenable_logger ActiveRecord::Base.logger = @@old_logger end + + class EncodingException < RuntimeError; end + + def self.verify_utf8 + raise "RAILS_ENV is not defined" unless defined?(RAILS_ENV) + + unless ActiveRecord::Base.configurations[RAILS_ENV].has_key?('encoding') + raise EncodingException, "Your database.yml configuration needs to specify encoding" + end + + unless ['unicode', 'utf8'].include?(ActiveRecord::Base.configurations[RAILS_ENV]['encoding']) + raise EncodingException, "Your database encoding must be utf8 (mysql) or unicode (postgres)" + end + + true + end end diff --git a/spec/yaml_dump_spec.rb b/spec/yaml_dump_spec.rb index dd47a9f..144e92d 100644 --- a/spec/yaml_dump_spec.rb +++ b/spec/yaml_dump_spec.rb @@ -11,6 +11,7 @@ ActiveRecord::Base.connection.stub!(:select_one).and_return({"count"=>"2"}) ActiveRecord::Base.connection.stub!(:select_all).and_return([ { 'a' => 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ]) YamlDb::Utils.stub!(:quote_table).with('mytable').and_return('mytable') + YamlDb.stub!(:verify_utf8) end before(:each) do diff --git a/spec/yaml_load_spec.rb b/spec/yaml_load_spec.rb index b567bab..ff01220 100644 --- a/spec/yaml_load_spec.rb +++ b/spec/yaml_load_spec.rb @@ -3,6 +3,7 @@ describe YamlDb::Load do before do YamlDb::Utils.stub!(:quote_table).with('mytable').and_return('mytable') + YamlDb.stub!(:verify_utf8) ActiveRecord::Base = mock('ActiveRecord::Base', :null_object => true) ActiveRecord::Base.connection = mock('connection') diff --git a/spec/yaml_spec.rb b/spec/yaml_spec.rb new file mode 100644 index 0000000..809e795 --- /dev/null +++ b/spec/yaml_spec.rb @@ -0,0 +1,27 @@ +require File.dirname(__FILE__) + '/base' + +describe YamlDb do + before do + ActiveRecord::Base = mock('ActiveRecord::Base', :null_object => true) + ActiveRecord::Base.stub!(:configurations).and_return(mock('configurations')) + RAILS_ENV = "test" unless defined?(RAILS_ENV) + end + + it "verifies that the connection is encoded with unicode or utf8" do + @config = { 'encoding' => 'utf8' } + ActiveRecord::Base.configurations.stub!(:[]).with('test').and_return(@config) + lambda { YamlDb.verify_utf8 }.should_not raise_error(YamlDb::EncodingException) + end + + it "raises an exception if encoding is not set" do + @config = { } + ActiveRecord::Base.configurations.stub!(:[]).with('test').and_return(@config) + lambda { YamlDb.verify_utf8 }.should raise_error(YamlDb::EncodingException) + end + + it "raises an exception if encoding is not utf8 or unicode" do + @config = { 'encoding' => 'latin1' } + ActiveRecord::Base.configurations.stub!(:[]).with('test').and_return(@config) + lambda { YamlDb.verify_utf8 }.should raise_error(YamlDb::EncodingException) + end +end