|
| 1 | +import base64 |
1 | 2 | from unittest import mock |
2 | 3 |
|
3 | 4 | import psycopg2 |
@@ -92,3 +93,104 @@ def test_make_postgres_uri(self): |
92 | 93 | assert "postgres://test:PASSWORD@localhost:5432/test" == _make_postgres_uri( |
93 | 94 | **connection_info |
94 | 95 | ) |
| 96 | + |
| 97 | + def test_binary_serialization_roundtrip(self): |
| 98 | + """Tests that binary data (bytes) converts to a string with a prefix |
| 99 | + when saving, and converts back to bytes when reading. |
| 100 | + """ |
| 101 | + original_data = { |
| 102 | + "normal_text": "test", |
| 103 | + "binary_data": b"Test binary", |
| 104 | + } |
| 105 | + |
| 106 | + # 1. Simulate save (session_to_str) |
| 107 | + serialized = self.session_store.session_to_str(original_data) |
| 108 | + # Verify that the prefix was added and encoded in base64 |
| 109 | + expected_b64 = base64.b64encode(b"Test binary").decode("utf-8") |
| 110 | + self.assertEqual( |
| 111 | + serialized["binary_data"], |
| 112 | + f"base64::{expected_b64}", |
| 113 | + "Binary data should be serialized with the base64:: prefix", |
| 114 | + ) |
| 115 | + self.assertEqual(serialized["normal_text"], "test") |
| 116 | + |
| 117 | + # 2. Simulate read (str_to_session) |
| 118 | + deserialized = self.session_store.str_to_session(serialized) |
| 119 | + # Verify that we recover the 'bytes' type |
| 120 | + self.assertEqual(deserialized["binary_data"], b"Test binary") |
| 121 | + self.assertIsInstance(deserialized["binary_data"], bytes) |
| 122 | + |
| 123 | + def test_numeric_conversion(self): |
| 124 | + """Tests that strings looking like numbers convert to int/float.""" |
| 125 | + data_from_json = { |
| 126 | + "integer_str": "42", |
| 127 | + "float_str": "3.1416", |
| 128 | + "negative_int": "-10", |
| 129 | + "negative_float": "-0.01", |
| 130 | + "plain_text": "123-abc", # Should not convert |
| 131 | + } |
| 132 | + result = self.session_store.str_to_session(data_from_json) |
| 133 | + # Integer validations |
| 134 | + self.assertEqual(result["integer_str"], 42) |
| 135 | + self.assertIsInstance(result["integer_str"], int) |
| 136 | + self.assertEqual(result["negative_int"], -10) |
| 137 | + # Float validations |
| 138 | + self.assertEqual(result["float_str"], 3.1416) |
| 139 | + self.assertIsInstance(result["float_str"], float) |
| 140 | + self.assertEqual(result["negative_float"], -0.01) |
| 141 | + # Text validations that must not change |
| 142 | + self.assertEqual(result["plain_text"], "123-abc") |
| 143 | + self.assertIsInstance(result["plain_text"], str) |
| 144 | + |
| 145 | + def test_debug_param_exception(self): |
| 146 | + """Verifies that the "debug" key ALWAYS remains a string, |
| 147 | + even if it looks like an integer ("1"). |
| 148 | + """ |
| 149 | + data = { |
| 150 | + "debug": "1", # Special case: must remain string |
| 151 | + "assets_debug": "1", # Normal case: must be int |
| 152 | + "debug_nested": {"debug": "0"}, # Recursive test |
| 153 | + } |
| 154 | + result = self.session_store.str_to_session(data) |
| 155 | + |
| 156 | + # "debug" must be string "1", NOT integer 1 |
| 157 | + self.assertEqual(result["debug"], "1") |
| 158 | + self.assertIsInstance(result["debug"], str) |
| 159 | + # Other keys do convert |
| 160 | + self.assertEqual(result["assets_debug"], 1) |
| 161 | + self.assertIsInstance(result["assets_debug"], int) |
| 162 | + # Verify exception applies recursively if the key is "debug" |
| 163 | + self.assertEqual(result["debug_nested"]["debug"], "0") |
| 164 | + self.assertIsInstance(result["debug_nested"]["debug"], str) |
| 165 | + |
| 166 | + def test_recursive_traversal(self): |
| 167 | + """Tests that conversion works in nested structures (lists and dicts).""" |
| 168 | + data = {"list_of_data": [b"binary_in_list", "100", {"deep_key": "50.5"}]} |
| 169 | + # 1. Serialize (Bytes -> Str) |
| 170 | + serialized = self.session_store.session_to_str(data) |
| 171 | + self.assertTrue(serialized["list_of_data"][0].startswith("base64::")) |
| 172 | + # 2. Deserialize (Str -> Bytes/Int/Float) |
| 173 | + # Simulate that JSON already loaded the string "100" and "50.5" |
| 174 | + # Note: session_to_str does not convert ints to strings, json.dumps does that later. |
| 175 | + # Here we test str_to_session with data appearing to come from JSON. |
| 176 | + input_for_read = { |
| 177 | + "list_of_data": [ |
| 178 | + serialized["list_of_data"][0], # The base64 string |
| 179 | + "100", |
| 180 | + {"deep_key": "50.5"}, |
| 181 | + ] |
| 182 | + } |
| 183 | + result = self.session_store.str_to_session(input_for_read) |
| 184 | + |
| 185 | + self.assertEqual(result["list_of_data"][0], b"binary_in_list") |
| 186 | + self.assertEqual(result["list_of_data"][1], 100) |
| 187 | + self.assertEqual(result["list_of_data"][2]["deep_key"], 50.5) |
| 188 | + |
| 189 | + def test_invalid_base64_fallback(self): |
| 190 | + """If a string has the base64:: prefix but the content is invalid, |
| 191 | + it must return the original value without crashing. |
| 192 | + """ |
| 193 | + invalid_data = {"bad_binary": "base64::TESTS"} |
| 194 | + result = self.session_store.str_to_session(invalid_data) |
| 195 | + # Should return the original string intact |
| 196 | + self.assertEqual(result["bad_binary"], "base64::TESTS") |
0 commit comments