|
| 1 | +import warnings |
| 2 | + |
| 3 | +from stripe._stripe_object import StripeObject |
| 4 | +from stripe._encode import _api_encode |
| 5 | + |
| 6 | + |
| 7 | +class TestApiEncode: |
| 8 | + def test_encode_stripe_object_without_id_no_deprecation_warning(self): |
| 9 | + """ |
| 10 | + Test that encoding a StripeObject without an id (like metadata) |
| 11 | + does not trigger a deprecation warning. |
| 12 | + Regression test for issue #1651. |
| 13 | + """ |
| 14 | + metadata = StripeObject() |
| 15 | + metadata["key1"] = "value1" |
| 16 | + metadata["key2"] = "value2" |
| 17 | + |
| 18 | + with warnings.catch_warnings(record=True) as w: |
| 19 | + warnings.simplefilter("always") |
| 20 | + result = list(_api_encode({"metadata": metadata})) |
| 21 | + |
| 22 | + # Check no deprecation warnings were raised |
| 23 | + deprecation_warnings = [ |
| 24 | + warning |
| 25 | + for warning in w |
| 26 | + if issubclass(warning.category, DeprecationWarning) |
| 27 | + ] |
| 28 | + assert len(deprecation_warnings) == 0, ( |
| 29 | + f"Expected no deprecation warnings, but got {len(deprecation_warnings)}" |
| 30 | + ) |
| 31 | + |
| 32 | + # Verify the metadata was encoded correctly as nested dict |
| 33 | + assert result == [ |
| 34 | + ("metadata[key1]", "value1"), |
| 35 | + ("metadata[key2]", "value2"), |
| 36 | + ] |
| 37 | + |
| 38 | + def test_encode_stripe_object_with_id_extracts_id(self): |
| 39 | + """ |
| 40 | + Test that encoding a StripeObject with an id (like a customer reference) |
| 41 | + correctly extracts just the id value. |
| 42 | + """ |
| 43 | + customer = StripeObject() |
| 44 | + customer["id"] = "cus_123" |
| 45 | + customer["name"] = "Test Customer" |
| 46 | + |
| 47 | + with warnings.catch_warnings(record=True) as w: |
| 48 | + warnings.simplefilter("always") |
| 49 | + result = list(_api_encode({"customer": customer})) |
| 50 | + |
| 51 | + # Check no deprecation warnings were raised |
| 52 | + deprecation_warnings = [ |
| 53 | + warning |
| 54 | + for warning in w |
| 55 | + if issubclass(warning.category, DeprecationWarning) |
| 56 | + ] |
| 57 | + assert len(deprecation_warnings) == 0, ( |
| 58 | + f"Expected no deprecation warnings, but got {len(deprecation_warnings)}" |
| 59 | + ) |
| 60 | + |
| 61 | + # Should encode to just the ID |
| 62 | + assert result == [("customer", "cus_123")] |
| 63 | + |
| 64 | + def test_encode_regular_dict(self): |
| 65 | + """Test that regular dicts are encoded as nested dicts.""" |
| 66 | + regular_dict = {"key1": "value1", "key2": "value2"} |
| 67 | + result = list(_api_encode({"data": regular_dict})) |
| 68 | + |
| 69 | + assert result == [ |
| 70 | + ("data[key1]", "value1"), |
| 71 | + ("data[key2]", "value2"), |
| 72 | + ] |
| 73 | + |
| 74 | + def test_encode_none_value_skipped(self): |
| 75 | + """Test that None values are skipped during encoding.""" |
| 76 | + result = list(_api_encode({"field": None})) |
| 77 | + assert result == [] |
| 78 | + |
| 79 | + def test_encode_string_value(self): |
| 80 | + """Test that string values are encoded directly.""" |
| 81 | + result = list(_api_encode({"name": "John Doe"})) |
| 82 | + assert result == [("name", "John Doe")] |
| 83 | + |
| 84 | + def test_encode_boolean_value(self): |
| 85 | + """Test that boolean values are encoded as lowercase strings.""" |
| 86 | + result = list(_api_encode({"active": True, "deleted": False})) |
| 87 | + assert result == [("active", "true"), ("deleted", "false")] |
| 88 | + |
| 89 | + def test_encode_list_value(self): |
| 90 | + """Test that list values are encoded with indexed keys.""" |
| 91 | + result = list(_api_encode({"items": ["item1", "item2", "item3"]})) |
| 92 | + assert result == [ |
| 93 | + ("items[0]", "item1"), |
| 94 | + ("items[1]", "item2"), |
| 95 | + ("items[2]", "item3"), |
| 96 | + ] |
0 commit comments