def test_and_both_same_key(model, index, key_name): """AND with 2 conditions, but both conditions are on the same key""" key_column = getattr(index or model.Meta, key_name) condition = key_column == "value" key = AndCondition(condition, condition) with pytest.raises(InvalidSearch): validate_key_condition(model, index, key)
def test_single_key_failure(model, index, key_lambda): """No other single key condition (except AND) will succeed""" # Get the correct hash key so only the condition type is wrong hash_key_column = (index or model.Meta).hash_key key = key_lambda(column=hash_key_column) with pytest.raises(InvalidSearch): validate_key_condition(model, index, key)
def test_hash_and_range_key_success(model, index, range_condition_lambda): """AND(hash, range) + AND(range, hash) for valid hash and range key conditions""" hash_condition = (index or model.Meta).hash_key == "value" range_condition = range_condition_lambda( column=(index or model.Meta).range_key) validate_key_condition(model, index, AndCondition(hash_condition, range_condition)) validate_key_condition(model, index, AndCondition(range_condition, hash_condition))
def test_and_bad_range_key(model, index, range_condition_lambda): """AND with valid hash range condition but bad hash key condition""" hash_condition = (index or model.Meta).hash_key == "value" range_condition = range_condition_lambda(column=(index or model.Meta).range_key) with pytest.raises(InvalidKeyCondition): validate_key_condition(model, index, AndCondition(hash_condition, range_condition)) with pytest.raises(InvalidKeyCondition): validate_key_condition(model, index, AndCondition(range_condition, hash_condition))
def test_and_bad_hash_key(model, index, range_condition_lambda): """AND with valid range key condition but bad hash key condition""" hash_condition = (index or model.Meta).hash_key.between("bad", "condition") range_condition = range_condition_lambda(column=(index or model.Meta).range_key) with pytest.raises(InvalidKeyCondition): validate_key_condition(model, index, AndCondition(hash_condition, range_condition)) with pytest.raises(InvalidKeyCondition): validate_key_condition(model, index, AndCondition(range_condition, hash_condition))
def test_and_not_two(model, index, count): """AND on hash+range fails if there aren't exactly 2 key conditions""" hash_key_column = (index or model.Meta).hash_key key = AndCondition(*[hash_key_column == "value"] * count) with pytest.raises(InvalidSearch): validate_key_condition(model, index, key)
def test_single_hash_key_success(model, index): """Single key condition: equality comparison on hash key""" query_on = index or model.Meta key = query_on.hash_key == "value" validate_key_condition(model, index, key)