1- """The custom Rule example in README.md , run as written through the Rule harness."""
1+ """The introductory custom Rule examples , run as written through the Rule harness."""
22
33from __future__ import annotations
44
99from sqlclean .rules .types import RuleCheck
1010from sqlclean .testing import RuleCase , RuleResult , evaluate_rule
1111from tests .integration .src .sqlclean .rule_testing ._test_types import ReadmeRuleTestCase
12+ from tests .integration .src .sqlclean .rule_testing .helpers import document as document
1213from tests .integration .src .sqlclean .rule_testing .helpers import readme_rule
1314
1415
1516@pytest .mark .parametrize (
1617 "test_case" ,
1718 [
1819 ReadmeRuleTestCase (
19- description = "a bare email column compared with a mixed-case literal " ,
20- source = "SELECT id FROM customers c WHERE c.email = 'Alice@example.com' " ,
21- expected_texts = ("c.email " ,),
20+ description = "the documented SQL reports the function name " ,
21+ source = "SELECT IFNULL(discount, 0) FROM orders; " ,
22+ expected_texts = ("IFNULL " ,),
2223 ),
2324 ReadmeRuleTestCase (
24- description = "LOWER on one side only still compares with a mixed-case literal" ,
25- source = "SELECT id FROM customers WHERE LOWER(email) = 'Alice@Example.com'" ,
26- expected_texts = ("email" ,),
25+ description = "mixed-case calls nested in a CTE and subquery are reported" ,
26+ source = "WITH totals AS (SELECT ifnull(discount, 0) AS discount FROM orders) "
27+ "SELECT (SELECT IfNull(MAX(discount), 0) FROM totals)" ,
28+ expected_texts = ("ifnull" , "IfNull" ),
2729 ),
2830 ReadmeRuleTestCase (
29- description = "a designated column on the right of <> in upper case " ,
30- source = "SELECT id FROM customers WHERE 'Carol@example.com' <> BILLING_EMAIL " ,
31- expected_texts = ("BILLING_EMAIL " ,),
31+ description = "a later statement after a multibyte literal uses file character offsets " ,
32+ source = "SELECT 'é'; \n SELECT IFNULL(discount, 0) FROM orders; " ,
33+ expected_texts = ("IFNULL " ,),
3234 ),
3335 ReadmeRuleTestCase (
34- description = "UPPER does not normalize the other side to lower case" ,
35- source = "SELECT id FROM customers WHERE LOWER(email) = UPPER('x')" ,
36- expected_texts = ("email" ,),
37- ),
38- ReadmeRuleTestCase (
39- description = "a bare column against a lower-case literal is still not normalized" ,
40- source = "SELECT id FROM customers WHERE email = 'carol@example.com'" ,
41- expected_texts = ("email" ,),
42- ),
43- ReadmeRuleTestCase (
44- description = "a comparison inside a CTE after a multibyte literal" ,
45- source = "WITH u AS (SELECT 'é' AS m, email FROM users WHERE email = 'X@y.com') "
46- "SELECT * FROM u" ,
47- expected_texts = ("email" ,),
48- ),
49- ReadmeRuleTestCase (
50- description = "LOWER on both sides passes" ,
51- source = "SELECT * FROM u JOIN a ON LOWER(u.email) = LOWER(a.email)" ,
52- expected_texts = (),
53- ),
54- ReadmeRuleTestCase (
55- description = "LOWER against a lower-case literal passes" ,
56- source = "SELECT id FROM customers WHERE LOWER(email) = 'bob@example.com'" ,
36+ description = "the recommended COALESCE replacement passes" ,
37+ source = "SELECT COALESCE(discount, 0) FROM orders;" ,
5738 expected_texts = (),
5839 ),
5940 ReadmeRuleTestCase (
60- description = "comments, strings, other columns, LIKE and projections never match" ,
61- source = "-- email = 'A@b.c'\n SELECT email, 'email = x' AS note FROM customers "
62- "WHERE email_verified = TRUE AND email LIKE '%dave%'" ,
41+ description = "comments, strings, identifiers and longer function names never match" ,
42+ source = "-- IFNULL(discount, 0)\n "
43+ "SELECT 'IFNULL(discount, 0)' AS note, \" IFNULL\" , "
44+ "my_ifnull(discount, 0) FROM orders /* IFNULL(discount, 0) */" ,
6345 expected_texts = (),
6446 ),
6547 ],
6648 ids = lambda case : case .description ,
6749)
68- def test_given_readme_email_rule_when_evaluating_then_unnormalized_comparisons_are_reported (
69- tmp_path : Path , test_case : ReadmeRuleTestCase
50+ def test_given_introductory_rule_when_evaluating_then_only_ifnull_calls_are_reported (
51+ tmp_path : Path , document : Path , test_case : ReadmeRuleTestCase
7052) -> None :
71- rule : RuleCheck = readme_rule (tmp_path )
53+ rule : RuleCheck = readme_rule (tmp_path , document )
7254
7355 result : RuleResult = evaluate_rule (
7456 rule = rule ,
@@ -84,4 +66,5 @@ def test_given_readme_email_rule_when_evaluating_then_unnormalized_comparisons_a
8466 )
8567
8668 assert texts == test_case .expected_texts
69+ assert all (finding .edits == () for finding in result .findings )
8770 assert result .fixed_source == test_case .source
0 commit comments