diff --git a/src/app/routes.py b/src/app/routes.py index b65bf9f..64af156 100644 --- a/src/app/routes.py +++ b/src/app/routes.py @@ -47,15 +47,22 @@ def fluff_results(): # dialect must be a dialect label for `load_raw_dialect`. VALID_DIALECTS is a # dictionary of dialect labels to dialect names. If we have a name, we need to # get the label. + # + # However, the frontend logic runs on dialect names, so we need to convert the + # label back to a name for the frontend. dialect = request.args["dialect"] if dialect in VALID_DIALECTS.values(): - dialect = next( + dialect_name = dialect + dialect_label = next( label for label, name in VALID_DIALECTS.items() if name == dialect ) + else: + dialect_label = dialect + dialect_name = VALID_DIALECTS[dialect] try: - linted = lint(sql, dialect=dialect) - fixed_sql = fix(sql, dialect=dialect) + linted = lint(sql, dialect=dialect_label) + fixed_sql = fix(sql, dialect=dialect_label) except RuntimeError as e: linted = [ { @@ -70,7 +77,7 @@ def fluff_results(): "index.html", results=True, sql=sql, - dialect=dialect, + dialect=dialect_name, lint_errors=linted, fixed_sql=fixed_sql, ) diff --git a/test/test_app.py b/test/test_app.py index 3a7e72e..02ef1e5 100644 --- a/test/test_app.py +++ b/test/test_app.py @@ -39,7 +39,7 @@ def test_post_redirect(client): @pytest.mark.parametrize("dialect", ["sparksql", "Apache Spark SQL"]) def test_results_no_errors(client, dialect): """Test that the results is good to go when there is no error. - + Parameterized dialect asserts that either the formatted name or label can be used as the dialect parameter. """ @@ -50,6 +50,14 @@ def test_results_no_errors(client, dialect): assert "fixed sql" in html assert "select * from table" in html + # Test that the dialect is correctly selected in the results page. + selected_dialect = ( + BeautifulSoup(html, "html.parser") + .find("select", {"id": "sql_dialect"}) + .find("option", {"selected": "selected"}) + ) + assert selected_dialect.text.strip() == "apache spark sql" + def test_results_some_errors(client): """Test that the results is good to go with one obvious error."""