Skip to content

Commit

Permalink
Don’t dedent multi-line strings
Browse files Browse the repository at this point in the history
  • Loading branch information
samdoran committed Dec 20, 2023
1 parent e85efae commit 2681795
Showing 1 changed file with 29 additions and 31 deletions.
60 changes: 29 additions & 31 deletions koku/koku/test_trino_db_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import textwrap
import uuid

from django.test import TestCase
Expand Down Expand Up @@ -30,32 +29,32 @@ def test_executescript(self):
Test execution of a buffer containing multiple statements
"""
sqlscript = """
drop table if exists hive.{{schema | sqlsafe}}.__test_{{uuid | sqlsafe}};
create table hive.{{schema | sqlsafe}}.__test_{{uuid | sqlsafe}}
(
id varchar,
i_data integer,
t_data varchar
);
drop table if exists hive.{{schema | sqlsafe}}.__test_{{uuid | sqlsafe}};
create table hive.{{schema | sqlsafe}}.__test_{{uuid | sqlsafe}}
(
id varchar,
i_data integer,
t_data varchar
);
insert into hive.{{schema | sqlsafe}}.__test_{{uuid | sqlsafe}} (id, i_data, t_data)
values (cast(uuid() as varchar), 10, 'default');
insert into hive.{{schema | sqlsafe}}.__test_{{uuid | sqlsafe}} (id, i_data, t_data)
values (cast(uuid() as varchar), 10, 'default');
insert into hive.{{schema | sqlsafe}}.__test_{{uuid | sqlsafe}} (id, i_data, t_data)
values (cast(uuid() as varchar), {{int_data}}, {{txt_data}});
insert into hive.{{schema | sqlsafe}}.__test_{{uuid | sqlsafe}} (id, i_data, t_data)
values (cast(uuid() as varchar), {{int_data}}, {{txt_data}});
select t_data from hive.{{schema | sqlsafe}}.__test_{{uuid | sqlsafe}} where i_data = {{int_data}};
select t_data from hive.{{schema | sqlsafe}}.__test_{{uuid | sqlsafe}} where i_data = {{int_data}};
drop table if exists hive.{{schema | sqlsafe}}.__test_{{uuid | sqlsafe}};
"""
drop table if exists hive.{{schema | sqlsafe}}.__test_{{uuid | sqlsafe}};
"""
conn = FakeTrinoConn()
params = {
"uuid": str(uuid.uuid4()).replace("-", "_"),
"schema": self.schema_name,
"int_data": 255,
"txt_data": "This is a test",
}
results = executescript(conn, textwrap.dedent(sqlscript), params=params, preprocessor=JinjaSql().prepare_query)
results = executescript(conn, sqlscript, params=params, preprocessor=JinjaSql().prepare_query)
self.assertEqual(results, [["eek"], ["eek"], ["eek"], ["eek"], ["eek"], ["eek"]])

def test_executescript_preprocessor_error(self):
Expand All @@ -64,22 +63,22 @@ def test_executescript_preprocessor_error(self):
"""
conn = FakeTrinoConn()
sqlscript = """
select * from eek where val1 in {{val_list}};
"""
select * from eek where val1 in {{val_list}};
"""
params = {"val_list": (1, 2, 3, 4, 5)}
with self.assertRaises(PreprocessStatementError):
executescript(conn, textwrap.dedent(sqlscript), params=params, preprocessor=JinjaSql().prepare_query)
executescript(conn, sqlscript, params=params, preprocessor=JinjaSql().prepare_query)

def test_executescript_no_preprocessor_error(self):
"""
Test executescript will not raise a preprocessor error
"""
sqlscript = """
select x from y;
select a from b;
"""
select x from y;
select a from b;
"""
conn = FakeTrinoConn()
res = executescript(conn, textwrap.dedent(sqlscript))
res = executescript(conn, sqlscript)
self.assertEqual(res, [["eek"], ["eek"]])

def test_executescript_trino_error(self):
Expand Down Expand Up @@ -111,14 +110,13 @@ def t_preprocessor(*args):
raise TypeError("This is a test")

sqlscript = """
\n
select x from y;
select a from b;
"""
select x from y;
select a from b;
"""
params = {"eek": 1}
conn = FakeTrinoConn()
with self.assertRaises(PreprocessStatementError):
executescript(conn, textwrap.dedent(sqlscript), params=params, preprocessor=t_preprocessor)
executescript(conn, sqlscript, params=params, preprocessor=t_preprocessor)

def test_executescript_error(self):
def t_exec_error(*args, **kwargs):
Expand All @@ -133,9 +131,9 @@ def cursor(self):
return FakerFakeTrinoCur()

sqlscript = """
select x from y;
select a from b;
"""
select x from y;
select a from b;
"""
with self.assertRaises(ValueError):
conn = FakerFakeTrinoConn()
executescript(conn, sqlscript)
Expand Down

0 comments on commit 2681795

Please sign in to comment.