Skip to content

Commit

Permalink
fix: failed to convert active model type to spanner type under certai…
Browse files Browse the repository at this point in the history
…n condition (#299)

* fix: failed to convert active model type to spanner type. under certain condition

Added support for the case where a DelegateClass such as ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter is passed to type.

* test: add test for DelegateClass conversions

---------

Co-authored-by: Knut Olav Løite <koloite@gmail.com>
  • Loading branch information
ruzia and olavloite committed Feb 19, 2024
1 parent 2f5ffd0 commit bacb4ef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def self.serialize_with_transaction_isolation_level type, value, isolation_level
##
# Converts an ActiveModel::Type to a Spanner type code.
def self.convert_active_model_type_to_spanner type # rubocop:disable Metrics/CyclomaticComplexity
# Unwrap the underlying object if the type is a DelegateClass.
type = type.__getobj__ if type.respond_to? :__getobj__

case type
when NilClass then nil
when ActiveModel::Type::Integer, ActiveModel::Type::BigInteger then :INT64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,23 @@ def test_create_singer_with_last_performance_as_time
assert_equal "2021-05-12T08:30:00.000000000Z", request.params["p3"]
end

def test_create_singer_with_time_zone_aware_attributes
Singer.time_zone_aware_attributes=true

insert_sql = "INSERT INTO `singers` (`first_name`, `last_name`, `last_performance`, `id`) VALUES (@p1, @p2, @p3, @p4)"
@mock.put_statement_result insert_sql, StatementResult.new(1)

Singer.transaction do
Singer.create(first_name: "Dave", last_name: "Allison", last_performance: ::Time.parse("2021-05-12T10:30:00+02:00"))
end

request = @mock.requests.select {|req| req.is_a?(Google::Cloud::Spanner::V1::ExecuteSqlRequest) && req.sql == insert_sql }.first
assert_equal :TIMESTAMP, request.param_types["p3"].code
assert_equal "2021-05-12T08:30:00.000000000Z", request.params["p3"]
ensure
Singer.time_zone_aware_attributes=false
end

def test_create_singer_with_last_performance_as_non_iso_string
insert_sql = "INSERT INTO `singers` (`first_name`, `last_name`, `last_performance`, `id`) VALUES (@p1, @p2, @p3, @p4)"
@mock.put_statement_result insert_sql, StatementResult.new(1)
Expand Down

0 comments on commit bacb4ef

Please sign in to comment.