|
22 | 22 | from cassandra import InvalidRequest |
23 | 23 |
|
24 | 24 | from cassandra.cluster import Cluster |
25 | | -from cassandra.query import PreparedStatement |
| 25 | +from cassandra.query import PreparedStatement, UNSET_VALUE |
26 | 26 |
|
27 | 27 |
|
28 | 28 | def setup_module(): |
@@ -222,6 +222,54 @@ def test_none_values(self): |
222 | 222 |
|
223 | 223 | cluster.shutdown() |
224 | 224 |
|
| 225 | + def test_unset_values(self): |
| 226 | + """ |
| 227 | + Test to validate that UNSET_VALUEs are bound, and have the expected effect |
| 228 | +
|
| 229 | + Prepare a statement and insert all values. Then follow with execute excluding |
| 230 | + parameters. Verify that the original values are unaffected. |
| 231 | +
|
| 232 | + @since 2.6.0 |
| 233 | +
|
| 234 | + @jira_ticket PYTHON-317 |
| 235 | + @expected_result UNSET_VALUE is implicitly added to bind parameters, and properly encoded, leving unset values unaffected. |
| 236 | +
|
| 237 | + @test_category prepared_statements:binding |
| 238 | + """ |
| 239 | + if PROTOCOL_VERSION < 4: |
| 240 | + raise unittest.SkipTest("Binding UNSET values is not supported in protocol version < 4") |
| 241 | + |
| 242 | + cluster = Cluster(protocol_version=PROTOCOL_VERSION) |
| 243 | + session = cluster.connect() |
| 244 | + |
| 245 | + # table with at least two values so one can be used as a marker |
| 246 | + session.execute("CREATE TABLE IF NOT EXISTS test1rf.test_unset_values (k int PRIMARY KEY, v0 int, v1 int)") |
| 247 | + insert = session.prepare( "INSERT INTO test1rf.test_unset_values (k, v0, v1) VALUES (?, ?, ?)") |
| 248 | + select = session.prepare( "SELECT * FROM test1rf.test_unset_values WHERE k=?") |
| 249 | + |
| 250 | + bind_expected = [ |
| 251 | + # initial condition |
| 252 | + ((0, 0, 0), (0, 0, 0)), |
| 253 | + # unset implicit |
| 254 | + ((0, 1,), (0, 1, 0)), |
| 255 | + ({'k': 0, 'v0': 2}, (0, 2, 0)), |
| 256 | + ({'k': 0, 'v1': 1}, (0, 2, 1)), |
| 257 | + # unset explicit |
| 258 | + ((0, 3, UNSET_VALUE), (0, 3, 1)), |
| 259 | + ((0, UNSET_VALUE, 2), (0, 3, 2)), |
| 260 | + ({'k': 0, 'v0': 4, 'v1': UNSET_VALUE}, (0, 4, 2)), |
| 261 | + ({'k': 0, 'v0': UNSET_VALUE, 'v1': 3}, (0, 4, 3)), |
| 262 | + # nulls still work |
| 263 | + ((0, None, None), (0, None, None)), |
| 264 | + ] |
| 265 | + |
| 266 | + for params, expected in bind_expected: |
| 267 | + session.execute(insert, params) |
| 268 | + results = session.execute(select, (0,)) |
| 269 | + self.assertEqual(results[0], expected) |
| 270 | + |
| 271 | + cluster.shutdown() |
| 272 | + |
225 | 273 | def test_no_meta(self): |
226 | 274 | cluster = Cluster(protocol_version=PROTOCOL_VERSION) |
227 | 275 | session = cluster.connect() |
|
0 commit comments