@@ -103,15 +103,121 @@ def test_is_lwt_propagates_from_statements(self):
103103 batch_with_bound .add (bound_lwt )
104104 assert batch_with_bound .is_lwt () is True
105105
106- class LwtSimpleStatement (SimpleStatement ):
107- def __init__ (self ):
108- super (LwtSimpleStatement , self ).__init__ (
109- "INSERT INTO test.table (id) VALUES (2) IF NOT EXISTS"
110- )
111-
112- def is_lwt (self ):
113- return True
106+ # SimpleStatement now detects LWT from query string (no subclass needed)
107+ lwt_simple = SimpleStatement (
108+ "INSERT INTO test.table (id) VALUES (2) IF NOT EXISTS"
109+ )
110+ assert lwt_simple .is_lwt () is True
114111
115112 batch_with_simple = BatchStatement ()
116- batch_with_simple .add (LwtSimpleStatement () )
113+ batch_with_simple .add (lwt_simple )
117114 assert batch_with_simple .is_lwt () is True
115+
116+ class SimpleStatementIsLwtTest (unittest .TestCase ):
117+ """Tests for SimpleStatement.is_lwt() CQL-based LWT detection."""
118+
119+ # --- INSERT IF NOT EXISTS ---
120+
121+ def test_insert_if_not_exists (self ):
122+ s = SimpleStatement ("INSERT INTO ks.t (a) VALUES (1) IF NOT EXISTS" )
123+ assert s .is_lwt () is True
124+
125+ def test_insert_if_not_exists_lowercase (self ):
126+ s = SimpleStatement ("insert into ks.t (a) values (1) if not exists" )
127+ assert s .is_lwt () is True
128+
129+ def test_insert_if_not_exists_mixed_case (self ):
130+ s = SimpleStatement ("INSERT INTO ks.t (a) VALUES (1) If Not Exists" )
131+ assert s .is_lwt () is True
132+
133+ # --- UPDATE IF EXISTS ---
134+
135+ def test_update_if_exists (self ):
136+ s = SimpleStatement ("UPDATE ks.t SET a=1 WHERE k=1 IF EXISTS" )
137+ assert s .is_lwt () is True
138+
139+ # --- DELETE IF EXISTS ---
140+
141+ def test_delete_if_exists (self ):
142+ s = SimpleStatement ("DELETE FROM ks.t WHERE k=1 IF EXISTS" )
143+ assert s .is_lwt () is True
144+
145+ # --- Conditional UPDATE (IF <column> = <value>) ---
146+
147+ def test_conditional_update_equals (self ):
148+ s = SimpleStatement ("UPDATE ks.t SET a=1 WHERE k=1 IF a = 2" )
149+ assert s .is_lwt () is True
150+
151+ def test_conditional_update_not_equals (self ):
152+ s = SimpleStatement ("UPDATE ks.t SET a=1 WHERE k=1 IF a != 2" )
153+ assert s .is_lwt () is True
154+
155+ def test_conditional_update_greater_than (self ):
156+ s = SimpleStatement ("UPDATE ks.t SET a=1 WHERE k=1 IF a > 2" )
157+ assert s .is_lwt () is True
158+
159+ def test_conditional_update_multiple_conditions (self ):
160+ s = SimpleStatement (
161+ "UPDATE ks.t SET a=1 WHERE k=1 IF a = 2 AND b = 3" )
162+ assert s .is_lwt () is True
163+
164+ # --- Conditional DELETE ---
165+
166+ def test_conditional_delete (self ):
167+ s = SimpleStatement ("DELETE FROM ks.t WHERE k=1 IF a = 2" )
168+ assert s .is_lwt () is True
169+
170+ # --- Non-LWT queries (should return False) ---
171+
172+ def test_select_not_lwt (self ):
173+ s = SimpleStatement ("SELECT * FROM ks.t WHERE k=1" )
174+ assert s .is_lwt () is False
175+
176+ def test_insert_without_if (self ):
177+ s = SimpleStatement ("INSERT INTO ks.t (a) VALUES (1)" )
178+ assert s .is_lwt () is False
179+
180+ def test_update_without_if (self ):
181+ s = SimpleStatement ("UPDATE ks.t SET a=1 WHERE k=1" )
182+ assert s .is_lwt () is False
183+
184+ def test_delete_without_if (self ):
185+ s = SimpleStatement ("DELETE FROM ks.t WHERE k=1" )
186+ assert s .is_lwt () is False
187+
188+ def test_create_table_with_if_not_exists (self ):
189+ """DDL IF NOT EXISTS should also match — this is harmless since
190+ DDL queries don't use token-aware routing anyway."""
191+ s = SimpleStatement ("CREATE TABLE IF NOT EXISTS ks.t (a int PRIMARY KEY)" )
192+ assert s .is_lwt () is True # False positive but harmless
193+
194+ # --- Caching ---
195+
196+ def test_result_is_cached (self ):
197+ s = SimpleStatement ("INSERT INTO ks.t (a) VALUES (1) IF NOT EXISTS" )
198+ assert s .is_lwt () is True
199+ assert s .is_lwt () is True # should use cache
200+ assert s ._cached_is_lwt is True
201+
202+ def test_non_lwt_result_is_cached (self ):
203+ s = SimpleStatement ("SELECT * FROM ks.t" )
204+ assert s .is_lwt () is False
205+ assert s ._cached_is_lwt is False
206+
207+ # --- Edge cases ---
208+
209+ def test_multiline_query (self ):
210+ s = SimpleStatement ("""
211+ INSERT INTO ks.t (a, b)
212+ VALUES (1, 2)
213+ IF NOT EXISTS
214+ """ )
215+ assert s .is_lwt () is True
216+
217+ def test_extra_whitespace (self ):
218+ s = SimpleStatement ("UPDATE ks.t SET a=1 WHERE k=1 IF EXISTS" )
219+ assert s .is_lwt () is True
220+
221+ def test_tab_separated (self ):
222+ s = SimpleStatement ("DELETE FROM ks.t WHERE k=1\t IF\t EXISTS" )
223+ assert s .is_lwt () is True
0 commit comments