|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require File.expand_path("../test_helper", __dir__) |
| 4 | + |
| 5 | +module Stripe |
| 6 | + class StripeObjectInt64Test < Test::Unit::TestCase |
| 7 | + # A test resource with an int64_string-encoded field. |
| 8 | + class Int64Resource < Stripe::StripeObject |
| 9 | + def self.field_encodings |
| 10 | + { amount: :int64_string } |
| 11 | + end |
| 12 | + end |
| 13 | + |
| 14 | + # A nested inner class that has its own field_encodings. |
| 15 | + class InnerDetail < Stripe::StripeObject |
| 16 | + def self.field_encodings |
| 17 | + { quantity: :int64_string } |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + # A parent resource referencing the inner class via inner_class_types. |
| 22 | + # This demonstrates that nested coercion works through each StripeObject |
| 23 | + # applying its own field_encodings during update_attributes (not through |
| 24 | + # V2TypeCoercion recursing into StripeObjects). |
| 25 | + class ParentResource < Stripe::StripeObject |
| 26 | + def self.field_encodings |
| 27 | + { total: :int64_string } |
| 28 | + end |
| 29 | + |
| 30 | + def self.inner_class_types |
| 31 | + { detail: InnerDetail } |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + context "response-side int64_string coercion via construct_from" do |
| 36 | + should "deserialize a string field to Integer" do |
| 37 | + obj = Int64Resource.construct_from(id: "obj_1", amount: "42") |
| 38 | + assert_equal 42, obj.amount |
| 39 | + assert_instance_of Integer, obj.amount |
| 40 | + end |
| 41 | + |
| 42 | + should "handle large int64 values" do |
| 43 | + obj = Int64Resource.construct_from(id: "obj_2", amount: "9223372036854775807") |
| 44 | + assert_equal 9_223_372_036_854_775_807, obj.amount |
| 45 | + end |
| 46 | + |
| 47 | + should "preserve negative values" do |
| 48 | + obj = Int64Resource.construct_from(id: "obj_3", amount: "-100") |
| 49 | + assert_equal(-100, obj.amount) |
| 50 | + end |
| 51 | + |
| 52 | + should "preserve zero" do |
| 53 | + obj = Int64Resource.construct_from(id: "obj_4", amount: "0") |
| 54 | + assert_equal 0, obj.amount |
| 55 | + assert_instance_of Integer, obj.amount |
| 56 | + end |
| 57 | + |
| 58 | + should "pass through nil without error" do |
| 59 | + obj = Int64Resource.construct_from(id: "obj_5", amount: nil) |
| 60 | + assert_nil obj.amount |
| 61 | + end |
| 62 | + |
| 63 | + should "leave non-encoded fields unaffected" do |
| 64 | + obj = Int64Resource.construct_from(id: "obj_6", amount: "10", name: "test") |
| 65 | + assert_equal "test", obj.name |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + context "nested resource coercion (each layer applies its own field_encodings)" do |
| 70 | + should "coerce both parent and nested inner-class fields" do |
| 71 | + obj = ParentResource.construct_from( |
| 72 | + id: "parent_1", |
| 73 | + total: "500", |
| 74 | + detail: { quantity: "25", label: "items" } |
| 75 | + ) |
| 76 | + |
| 77 | + assert_equal 500, obj.total |
| 78 | + assert_instance_of Integer, obj.total |
| 79 | + |
| 80 | + assert_equal 25, obj.detail.quantity |
| 81 | + assert_instance_of Integer, obj.detail.quantity |
| 82 | + |
| 83 | + assert_equal "items", obj.detail.label |
| 84 | + end |
| 85 | + |
| 86 | + should "handle nil nested object" do |
| 87 | + obj = ParentResource.construct_from(id: "parent_2", total: "100", detail: nil) |
| 88 | + assert_equal 100, obj.total |
| 89 | + assert_nil obj.detail |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | +end |
0 commit comments