|
| 1 | +from logfire._internal.db_statement_summary import message_from_db_statement |
| 2 | + |
| 3 | + |
| 4 | +def test_no_db_statement(): |
| 5 | + assert message_from_db_statement({}, None, 'x') is None |
| 6 | + |
| 7 | + |
| 8 | +def test_short_db_statement(): |
| 9 | + assert message_from_db_statement({'db.statement': 'SELECT * FROM table'}, None, 'x') == 'SELECT * FROM table' |
| 10 | + |
| 11 | + |
| 12 | +def test_message_same(): |
| 13 | + assert ( |
| 14 | + message_from_db_statement({'db.statement': 'SELECT * FROM table'}, 'SELECT', 'SELECT') == 'SELECT * FROM table' |
| 15 | + ) |
| 16 | + |
| 17 | + |
| 18 | +def test_message_different(): |
| 19 | + assert message_from_db_statement({'db.statement': 'SELECT * FROM table'}, 'SELECT', 'x') is None |
| 20 | + |
| 21 | + |
| 22 | +def test_message_not_in_db_statement(): |
| 23 | + q = 'SELECT apple, banana, carrot, durian, egg, fig FROM table WHERE apple = 1' |
| 24 | + assert message_from_db_statement({'db.statement': q}, 'not in statement', 'not in statement') is None |
| 25 | + |
| 26 | + |
| 27 | +def test_message_multiword(): |
| 28 | + q = 'SELECT apple, banana, carrot, durian, egg, fig FROM table WHERE apple = 1' |
| 29 | + assert message_from_db_statement({'db.statement': q}, 'SELECT apple', 'SELECT apple') is None |
| 30 | + |
| 31 | + |
| 32 | +def test_ok_after_clean(): |
| 33 | + q = """ |
| 34 | +-- this is a long comment about the sql |
| 35 | +SELECT apple, banana, carrot, durian, egg, fig FROM table |
| 36 | +""" |
| 37 | + # insert_assert(message_from_db_statement({'db.statement': q}, None, 'x')) |
| 38 | + assert ( |
| 39 | + message_from_db_statement({'db.statement': q}, None, 'x') |
| 40 | + == 'SELECT apple, banana, carrot, durian, egg, fig FROM table' |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +def attrs(q: str): |
| 45 | + return {'db.statement': q, 'db.system': 'postgresql'} |
| 46 | + |
| 47 | + |
| 48 | +def test_query_rewritten(): |
| 49 | + q = 'SELECT apple, banana, carrot, durian, egg, fig FROM table WHERE apple = 1 and banana = 2' |
| 50 | + # insert_assert(message_from_db_statement(attrs(q), None, 'SELECT')) |
| 51 | + assert message_from_db_statement(attrs(q), None, 'SELECT') == 'SELECT apple, ban…, egg, fig FROM table WHERE …' |
| 52 | + |
| 53 | + |
| 54 | +def test_invalid_sql(): |
| 55 | + q = 'SELECT apple, banana, carrot, durian, egg, fig FROM "table WHERE apple = 1 offset 12345678901234567890' |
| 56 | + # insert_assert(message_from_db_statement(attrs(q), None, 'SELECT')) |
| 57 | + assert message_from_db_statement(attrs(q), None, 'SELECT') == 'SELECT apple, ban…, egg, fig FROM "table WHERE …' |
| 58 | + |
| 59 | + |
| 60 | +def test_one_cte(): |
| 61 | + q = 'WITH foobar AS (SELECT apple, banana, carrot, durian FROM table) SELECT * FROM foobar where x = 1' |
| 62 | + # insert_assert(message_from_db_statement(attrs(q), None, 'SELECT')) |
| 63 | + assert message_from_db_statement(attrs(q), None, 'SELECT') == 'WITH foobar AS (…) SELECT * FROM foobar WHERE …' |
| 64 | + |
| 65 | + |
| 66 | +def test_one_cte_long(): |
| 67 | + q = 'WITH foobar_foobar_foobar AS (SELECT apple, banana, carrot FROM table) SELECT * FROM foobar where x = 1' |
| 68 | + # insert_assert(message_from_db_statement(attrs(q), None, 'SELECT')) |
| 69 | + assert message_from_db_statement(attrs(q), None, 'SELECT') == 'WITH fooba…oobar AS (…) SELECT * FROM foobar WHERE …' |
| 70 | + |
| 71 | + |
| 72 | +def test_two_ctes(): |
| 73 | + q = 'WITH foo AS (SELECT * FROM table), bar AS (SELECT apple, banana, carrot, durian FROM foo) SELECT * FROM bar' |
| 74 | + # insert_assert(message_from_db_statement(attrs(q), None, 'SELECT')) |
| 75 | + assert message_from_db_statement(attrs(q), None, 'SELECT') == 'WITH …[2 CTEs] SELECT * FROM bar' |
| 76 | + |
| 77 | + |
| 78 | +def test_long_select(): |
| 79 | + q = '\nSELECT apple, banana, carrot, durian, egg, fig, grape FROM table offset 12345678901234567890' |
| 80 | + # insert_assert(message_from_db_statement(attrs(q), None, 'SELECT')) |
| 81 | + assert message_from_db_statement(attrs(q), None, 'SELECT') == 'SELECT apple, ban…fig, grape FROM table' |
| 82 | + |
| 83 | + |
| 84 | +def test_from_subquery(): |
| 85 | + q = 'select * from (select * from table) as sub where aaaaa_bbbb_cccccc=1 offset 12345678901234567890' |
| 86 | + # insert_assert(message_from_db_statement(attrs(q), None, 'SELECT')) |
| 87 | + assert message_from_db_statement(attrs(q), None, 'SELECT') == 'SELECT * FROM (select * from table) AS sub WHERE …' |
| 88 | + |
| 89 | + |
| 90 | +def test_from_quoted(): |
| 91 | + q = 'select * from "foo.bar" as sub where aaaaa_bbbb_cccccc=1 offset 12345678901234567890' |
| 92 | + # insert_assert(message_from_db_statement(attrs(q), None, 'SELECT')) |
| 93 | + assert message_from_db_statement(attrs(q), None, 'SELECT') == 'SELECT * FROM "foo.bar" WHERE …' |
| 94 | + |
| 95 | + |
| 96 | +def test_from_long(): |
| 97 | + q = 'select * from "aaaaa.bbbb.cccccc" as sub where aaaaa_bbbb_cccccc=1 offset 12345678901234567890' |
| 98 | + # insert_assert(message_from_db_statement(attrs(q), None, 'SELECT')) |
| 99 | + assert message_from_db_statement(attrs(q), None, 'SELECT') == 'SELECT * FROM "aaaaa.bbbb.cccccc" WHERE …' |
| 100 | + |
| 101 | + |
| 102 | +def test_one_join(): |
| 103 | + q = ' SELECT apple, banana, carrot FROM table JOIN other ON table.id = other.id offset 12345678901234567890' |
| 104 | + # insert_assert(message_from_db_statement(attrs(q), None, 'SELECT')) |
| 105 | + assert ( |
| 106 | + message_from_db_statement(attrs(q), None, 'SELECT') == 'SELECT apple, ban…na, carrot FROM table JOIN other ON …' |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +def test_one_join_long(): |
| 111 | + q = ' SELECT apple, banana, carrot FROM table JOIN other_other_other ON table.id = other_other_other.id' |
| 112 | + # insert_assert(message_from_db_statement(attrs(q), None, 'SELECT')) |
| 113 | + assert ( |
| 114 | + message_from_db_statement(attrs(q), None, 'SELECT') |
| 115 | + == 'SELECT apple, ban…na, carrot FROM table JOIN other…other ON …' |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +def test_two_joins(): |
| 120 | + q = 'SELECT * FROM table JOIN other ON table.id = other.id JOIN another ON table.id = another.id' |
| 121 | + # insert_assert(message_from_db_statement(attrs(q), None, 'SELECT')) |
| 122 | + assert message_from_db_statement(attrs(q), None, 'SELECT') == 'SELECT * FROM table …[2 JOINs]' |
| 123 | + |
| 124 | + |
| 125 | +def test_where(): |
| 126 | + q = 'SELECT apple, banana, carrot, durian, egg FROM table where a = 1 and b = 2 and c =3' |
| 127 | + # insert_assert(message_from_db_statement(attrs(q), None, 'SELECT')) |
| 128 | + assert message_from_db_statement(attrs(q), None, 'SELECT') == 'SELECT apple, ban…urian, egg FROM table WHERE …' |
| 129 | + |
| 130 | + |
| 131 | +def test_limit(): |
| 132 | + q = 'SELECT apple, banana, carrot, durian, egg, fig, grape FROM table where apple=12345678901234567890 limit 10' |
| 133 | + # insert_assert(message_from_db_statement(attrs(q), None, 'SELECT')) |
| 134 | + assert ( |
| 135 | + message_from_db_statement(attrs(q), None, 'SELECT') |
| 136 | + == 'SELECT apple, ban…fig, grape FROM table WHERE … LIMIT 10' |
| 137 | + ) |
| 138 | + |
| 139 | + |
| 140 | +def test_update(): |
| 141 | + q = 'UPDATE table set apple = 1 where banana = 2 and carrrrrrrot = 3 and durian = 4 and egg = 5 and fig = 6' |
| 142 | + # insert_assert(message_from_db_statement(attrs(q), None, 'UPDATE')) |
| 143 | + assert ( |
| 144 | + message_from_db_statement(attrs(q), None, 'UPDATE') |
| 145 | + == 'UPDATE table set apple = 1 where banan … and durian = 4 and egg = 5 and fig = 6' |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +def test_insert(): |
| 150 | + q = 'INSERT INTO table (apple, banana, carrot, durian, egg, fig) VALUES (1, 2, 3, 4, 5, 6)' |
| 151 | + # insert_assert(message_from_db_statement(attrs(q), None, 'INSERT')) |
| 152 | + assert ( |
| 153 | + message_from_db_statement(attrs(q), None, 'INSERT') |
| 154 | + == 'INSERT INTO table (apple, bana…n, egg, fig) VALUES (1, 2, 3, 4, 5, 6)' |
| 155 | + ) |
0 commit comments