test/attr_encrypted_test.rb in symmetric-encryption-3.3 vs test/attr_encrypted_test.rb in symmetric-encryption-3.4.0

- old
+ new

@@ -229,19 +229,27 @@ assert_equal true, @user.valid? @user.bank_account_number = '123' assert_equal true, @user.valid? end + + context "with saved user" do setup do @user.save! end teardown do @user.destroy end + should "return correct data type before save" do + u = User.new(:integer_value => "5") + assert_equal 5, u.integer_value + assert u.integer_value.kind_of?(Integer) + end + should "handle gsub! for non-encrypted_field" do @user.name.gsub!('a', 'v') new_name = @name.gsub('a', 'v') assert_equal new_name, @user.name @user.reload @@ -288,10 +296,16 @@ should "return correct data type" do assert_equal @integer_value, @user_clone.integer_value assert @user.clone.integer_value.kind_of?(Integer) end + should "coerce data type before save" do + u = User.new(:integer_value => "5") + assert_equal 5, u.integer_value + assert u.integer_value.kind_of?(Integer) + end + should "permit replacing value with nil" do @user_clone.integer_value = nil @user_clone.save! @user.reload @@ -313,10 +327,16 @@ should "return correct data type" do assert_equal @float_value, @user_clone.float_value assert @user.clone.float_value.kind_of?(Float) end + should "coerce data type before save" do + u = User.new(:float_value => "5.6") + assert_equal 5.6, u.float_value + assert u.float_value.kind_of?(Float) + end + should "permit replacing value with nil" do @user_clone.float_value = nil @user_clone.save! @user.reload @@ -338,10 +358,16 @@ should "return correct data type" do assert_equal @decimal_value, @user_clone.decimal_value assert @user.clone.decimal_value.kind_of?(BigDecimal) end + should "coerce data type before save" do + u = User.new(:decimal_value => "99.95") + assert_equal BigDecimal.new("99.95"), u.decimal_value + assert u.decimal_value.kind_of?(BigDecimal) + end + should "permit replacing value with nil" do @user_clone.decimal_value = nil @user_clone.save! @user.reload @@ -363,10 +389,17 @@ should "return correct data type" do assert_equal @datetime_value, @user_clone.datetime_value assert @user.clone.datetime_value.kind_of?(DateTime) end + should "coerce data type before save" do + now = Time.now + u = User.new(:datetime_value => now) + assert_equal now, u.datetime_value + assert u.datetime_value.kind_of?(DateTime) + end + should "permit replacing value with nil" do @user_clone.datetime_value = nil @user_clone.save! @user.reload @@ -388,10 +421,17 @@ should "return correct data type" do assert_equal @time_value, @user_clone.time_value assert @user.clone.time_value.kind_of?(Time) end + should "coerce data type before save" do + now = Time.now + u = User.new(:time_value => now) + assert_equal now, u.time_value + assert u.time_value.kind_of?(Time) + end + should "permit replacing value with nil" do @user_clone.time_value = nil @user_clone.save! @user.reload @@ -413,10 +453,17 @@ should "return correct data type" do assert_equal @date_value, @user_clone.date_value assert @user.clone.date_value.kind_of?(Date) end + should "coerce data type before save" do + now = Time.now + u = User.new(:date_value => now) + assert_equal now.to_date, u.date_value + assert u.date_value.kind_of?(Date) + end + should "permit replacing value with nil" do @user_clone.date_value = nil @user_clone.save! @user.reload @@ -438,10 +485,16 @@ should "return correct data type" do assert_equal true, @user_clone.true_value assert @user.clone.true_value.kind_of?(TrueClass) end + should "coerce data type before save" do + u = User.new(:true_value => "1") + assert_equal true, u.true_value + assert u.true_value.kind_of?(TrueClass) + end + should "permit replacing value with nil" do @user_clone.true_value = nil @user_clone.save! @user.reload @@ -463,10 +516,16 @@ should "return correct data type" do assert_equal false, @user_clone.false_value assert @user.clone.false_value.kind_of?(FalseClass) end + should "coerce data type before save" do + u = User.new(:false_value => "0") + assert_equal false, u.false_value + assert u.false_value.kind_of?(FalseClass) + end + should "permit replacing value with nil" do @user_clone.false_value = nil @user_clone.save! @user.reload @@ -497,10 +556,16 @@ should "return correct data type" do assert_equal @h, @user_clone.data_json assert @user.clone.data_json.kind_of?(Hash) end + should "not coerce data type (leaves as hash) before save" do + u = User.new(:data_json => @h) + assert_equal @h, u.data_json + assert u.data_json.kind_of?(Hash) + end + should "permit replacing value with nil" do @user_clone.data_json = nil @user_clone.save! @user.reload @@ -521,9 +586,15 @@ context "YAML Serialization" do should "return correct data type" do assert_equal @h, @user_clone.data_yaml assert @user.clone.data_yaml.kind_of?(Hash) + end + + should "not coerce data type (leaves as hash) before save" do + u = User.new(:data_yaml => @h) + assert_equal @h, u.data_yaml + assert u.data_yaml.kind_of?(Hash) end should "permit replacing value with nil" do @user_clone.data_yaml = nil @user_clone.save! \ No newline at end of file