def test_delete_empty_string_key(cql, table1): s = unique_key_string() # An empty-string clustering *is* allowed: cql.execute(f"DELETE FROM {table1} WHERE p='{s}' AND c=''") # But an empty-string partition key is *not* allowed, with a specific # error that a "Key may not be empty": with pytest.raises(InvalidRequest, match='Key may not be empty'): cql.execute(f"DELETE FROM {table1} WHERE p='' AND c='{s}'")
def test_filtering_eq_null(cassandra_bug, cql, table1): p = unique_key_string() cql.execute(f"INSERT INTO {table1} (p,c,v) VALUES ('{p}', '1', 'hello')") cql.execute(f"INSERT INTO {table1} (p,c,v) VALUES ('{p}', '2', '')") cql.execute(f"INSERT INTO {table1} (p,c) VALUES ('{p}', '3')") # As explained above, none of the above-inserted rows should match - # not even the one with an unset v: assert list(cql.execute(f"SELECT c FROM {table1} WHERE p='{p}' AND v=NULL ALLOW FILTERING")) == []
def test_null_char_in_blob(cql, table1): p = unique_key_string() v = random_bytes() + bytes([0]) + random_bytes() # sanity check: verify that Python actually put the null in the blob... assert 0 in v stmt = cql.prepare(f'INSERT INTO {table1} (p, b) VALUES (?, ?)') cql.execute(stmt, [p, v]) assert v == cql.execute(f"SELECT b FROM {table1} WHERE p='{p}'").one().b
def test_insert_json_empty_string_key(cql, table1): s = unique_key_string() # An empty-string clustering *is* allowed: cql.execute("""INSERT INTO %s JSON '{"p": "%s", "c": "", "v": "cat"}'""" % (table1, s)) assert list(cql.execute(f"SELECT v FROM {table1} WHERE p='{s}' AND c=''")) == [('cat',)] # But an empty-string partition key is *not* allowed, with a specific # error that a "Key may not be empty": with pytest.raises(InvalidRequest, match='Key may not be empty'): cql.execute("""INSERT INTO %s JSON '{"p": "", "c": "%s", "v": "cat"}'""" % (table1, s))
def test_update_empty_string_key(cql, table1): s = unique_key_string() # An empty-string clustering *is* allowed: cql.execute(f"UPDATE {table1} SET v = 'cat' WHERE p='{s}' AND c=''") assert list(cql.execute(f"SELECT v FROM {table1} WHERE p='{s}' AND c=''")) == [('cat',)] # But an empty-string partition key is *not* allowed, with a specific # error that a "Key may not be empty": with pytest.raises(InvalidRequest, match='Key may not be empty'): cql.execute(f"UPDATE {table1} SET v = 'dog' WHERE p='' AND c='{s}'")
def test_insert_empty_string_key(cql, table1): s = unique_key_string() # An empty-string clustering *is* allowed: cql.execute(f"INSERT INTO {table1} (p,c,v) VALUES ('{s}', '', 'cat')") assert list(cql.execute(f"SELECT v FROM {table1} WHERE p='{s}' AND c=''")) == [('cat',)] # But an empty-string partition key is *not* allowed, with a specific # error that a "Key may not be empty": with pytest.raises(InvalidRequest, match='Key may not be empty'): cql.execute(f"INSERT INTO {table1} (p,c,v) VALUES ('', '{s}', 'dog')")
def test_insert_missing_key(cql, table1): s = unique_key_string() # A clustering key is missing. Cassandra uses the message "Some clustering # keys are missing: c", and Scylla: "Missing mandatory PRIMARY KEY part c" with pytest.raises(InvalidRequest, match=re.compile('missing', re.IGNORECASE)): cql.execute(f"INSERT INTO {table1} (p) VALUES ('{s}')") # Similarly, a missing partition key with pytest.raises(InvalidRequest, match=re.compile('missing', re.IGNORECASE)): cql.execute(f"INSERT INTO {table1} (c) VALUES ('{s}')")
def test_filtering_inequality_null(cassandra_bug, cql, table1): p = unique_key_string() cql.execute(f"INSERT INTO {table1} (p,c,i) VALUES ('{p}', '1', 7)") cql.execute(f"INSERT INTO {table1} (p,c,i) VALUES ('{p}', '2', -3)") cql.execute(f"INSERT INTO {table1} (p,c) VALUES ('{p}', '3')") assert list(cql.execute(f"SELECT c FROM {table1} WHERE p='{p}' AND i>NULL ALLOW FILTERING")) == [] assert list(cql.execute(f"SELECT c FROM {table1} WHERE p='{p}' AND i>=NULL ALLOW FILTERING")) == [] assert list(cql.execute(f"SELECT c FROM {table1} WHERE p='{p}' AND i<NULL ALLOW FILTERING")) == [] assert list(cql.execute(f"SELECT c FROM {table1} WHERE p='{p}' AND i<=NULL ALLOW FILTERING")) == []
def test_null_char_in_string(cql, table1): for col in ['a', 't']: p = unique_key_string() v = random_string() + '\x00' + random_string() # sanity check: verify that Python actually put the null in the string... assert 0 in v.encode('utf-8') stmt = cql.prepare(f'INSERT INTO {table1} (p, {col}) VALUES (?, ?)') cql.execute(stmt, [p, v]) assert v == getattr( cql.execute(f"SELECT {col} FROM {table1} WHERE p='{p}'").one(), col)
def test_insert_null_key(cql, table1): s = unique_key_string() with pytest.raises(InvalidRequest, match='null value'): cql.execute(f"INSERT INTO {table1} (p,c) VALUES ('{s}', null)") with pytest.raises(InvalidRequest, match='null value'): cql.execute(f"INSERT INTO {table1} (p,c) VALUES (null, '{s}')") # Try the same thing with prepared statement, where a "None" stands for # a null. Note that this is completely different from UNSET_VALUE - only # with the latter should the insertion be ignored. stmt = cql.prepare(f"INSERT INTO {table1} (p,c) VALUES (?, ?)") with pytest.raises(InvalidRequest, match='null value'): cql.execute(stmt, [s, None]) with pytest.raises(InvalidRequest, match='null value'): cql.execute(stmt, [None, s])
def test_unicode_equivalence_like(scylla_only, cql, table1): u1 = "\u00F1" # Spanish ñ as one character u2 = "\u006E\u0303" # Two characters: n followed by combining tilde. # Confirm that u1 and u2 are different Unicode strings, but are # equivalent, i.e., have the same normalized value: assert u1 != u2 assert unicodedata.normalize('NFC', u1) == unicodedata.normalize('NFC', u2) insert = cql.prepare(f"INSERT INTO {table1} (k, c) VALUES (?, ?)") search = cql.prepare( f"SELECT k, c FROM {table1} WHERE k=? AND c LIKE ? ALLOW FILTERING") s = unique_key_string() # u1 does not match the pattern 'n%': cql.execute(insert, [s, u1]) assert set(cql.execute(search, [s, 'n%'])) == set() # u1 matches the pattern '_' (a single character though not a single byte) assert set(cql.execute(search, [s, '_'])) == set([(s, u1)]) # but u2 does match 'n%', but not '_': cql.execute(insert, [s, u2]) assert set(cql.execute(search, [s, 'n%'])) == set([(s, u2)]) assert set(cql.execute(search, [s, '_'])) == set([(s, u1)])
def test_unicode_equivalence(cql, table1): u1 = "\u00F1" # Spanish ñ as one character u2 = "\u006E\u0303" # Two characters: n followed by combining tilde. # Confirm that u1 and u2 are different Unicode strings, but are # equivalent, i.e., have the same normalized value: assert u1 != u2 assert unicodedata.normalize('NFC', u1) == unicodedata.normalize('NFC', u2) insert = cql.prepare(f"INSERT INTO {table1} (k, c) VALUES (?, ?)") search = cql.prepare(f"SELECT k, c FROM {table1} WHERE k=? and c=?") s = unique_key_string() # Test that writing u1 as a *clustering key* and looking up u2 will not # work. cql.execute(insert, [s, u1]) assert len(list(cql.execute(search, [s, u1]))) == 1 assert len(list(cql.execute(search, [s, u2]))) == 0 # Test that writing u1 as a *partition key* and looking up u2 will not # work. cql.execute(insert, [u1, s]) assert len(list(cql.execute(search, [u1, s]))) == 1 assert len(list(cql.execute(search, [u2, s]))) == 0
def test_filtering_contains_key_null(cassandra_bug, cql, table1): p = unique_key_string() cql.execute(f"INSERT INTO {table1} (p,c,m) VALUES ('{p}', '1', {{1: 2}})") cql.execute(f"INSERT INTO {table1} (p,c,m) VALUES ('{p}', '2', {{3: 4}})") cql.execute(f"INSERT INTO {table1} (p,c) VALUES ('{p}', '3')") assert list(cql.execute(f"SELECT c FROM {table1} WHERE p='{p}' AND m CONTAINS KEY NULL ALLOW FILTERING")) == []