简体   繁体   中英

Python read_text() adding extra strings

I've created a function that takes in a file with dynamic SQL (and parameters) then returns the end SQL result. It seems to be adding extra strings because my unit test is failing and it shows extra strings.

My function:

def file_to_sql(filename, kwargs=None):
    """Read file and return SQL statement.

    Parameters
    ----------
    filename: str
        Name of the file to read.
    kwargs : dict
        Keyword arguments to be passed into your SQL statement.
    """

    python_dir = Path('logic/extract/python3')
    python_file_path = python_dir / '{}'.format(filename)
    sql = python_file_path.read_text().format(**kwargs)

return sql

My unit test:

def test_python_file_with_kwargs_to_sql():
    sql = file_to_sql(filename="select_all_from_{table}.py", kwargs={
        'table': 'sandbox.test_table'
        })

    assert sql == "SELECT * FROM sandbox.test_table;"

select_all_from_{table}.py:

"SELECT * FROM {table};"

Unit test failure message:

================================== FAILURES ===================================
_____________________ test_python_file_with_kwargs_to_sql _____________________

    def test_python_file_with_kwargs_to_sql():
        sql = file_to_sql(filename="select_all_from_{table}.py", kwargs={
            'table': 'sandbox.test_table'
            })

>       assert sql == "SELECT * FROM sandbox.test_table;"
E       assert '"SELECT * FR....test_table;"' == 'select * from...x.test_table;'
E         - "SELECT * FROM sandbox.test_table;"
E         + SELECT * FROM sandbox.test_table;

tests\unit_tests\transform\test_preprocess.py:19: AssertionError
================ 1 failed, 2 passed, 4 skipped in 1.40 seconds ================

There are two reasons your unit test is failing:

  1. Your sql string is in mixed cases ("SELECT" and "FROM" are upper cases) whereas your comparison string is in all lower cases.

  2. The text within select_all_from_{table}.py are in double quotes but your comparison string doesn't have the double quotes.

Since read_text() reads the file content as str you don't need the double quotes. Remove the double quotes from your file and for good practice, do a case insensitive test:

sql.lower() == 'select * from sandbox.test_table'.lower()

Or just sql.lower() == 'select * from sandbox.test_table' will suffice in this particular case.

For unit tests it is best you be vigilant about your input and output and make sure your tests are exact.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM