Exemplo n.º 1
0
    def test_substitute_variables_four_braces_transformed_to_two(self):
        input_text = "{var1} sat on {{ sittingObject }}."
        escaped_text = "{var1} sat on {{{{ sittingObject }}}}."
        user_ns = {"var1": "The cat"}

        formatter = VvpFormatter(input_text, user_ns)
        formatted = formatter._substitute_variables(escaped_text)

        assert formatted == "The cat sat on {{ sittingObject }}."
Exemplo n.º 2
0
    def test_substitute_variables_works_in_simple_case(self):
        input_text = "{var1} sat on {var2}."
        escaped_text = input_text
        user_ns = {"var1": "The cat", "var2": "the mat"}

        formatter = VvpFormatter(input_text, user_ns)
        formatted = formatter._substitute_variables(escaped_text)

        assert formatted == "The cat sat on the mat."
Exemplo n.º 3
0
    def test_substitute_user_variables_ambiguous_throws(self):
        input_text = "{var1} sat on {{var2}."
        user_ns = {"var1": "The cat"}
        formatter = VvpFormatter(input_text, user_ns)

        with self.assertRaises(VariableSyntaxException) as exception:
            formatter.substitute_user_variables()

        assert exception.exception.bad_text == "{{var2}"
Exemplo n.º 4
0
    def test_prepare_escaped_variables_throws_in_ambiguous_case(self):
        input_text = "{{ good }} and {also_good} and {{bad_because_no_spaces}}"
        user_ns = {"also_good": "dummy_value"}
        formatter = VvpFormatter(input_text, user_ns)

        with self.assertRaises(VariableSyntaxException) as exception:
            formatter.substitute_user_variables()

        assert exception.exception.bad_text == "{{bad_because_no_spaces}"
Exemplo n.º 5
0
    def test_substitute_user_variables_undefined_variable_throws(self):
        input_text = "{var1} sat on {var2}."
        user_ns = {"var1": "The cat"}
        formatter = VvpFormatter(input_text, user_ns)

        with self.assertRaises(NonExistentVariableException) as exception:
            formatter.substitute_user_variables()

        assert exception.exception.variable_name == "var2"
Exemplo n.º 6
0
    def test_substitute_user_variables_works(self):
        input_text = """
        INSERT INTO {{ namespace }}_{resultsTable}
        SELECT * FROM {{ namespace }}_{tableName}
        """
        user_ns = {"resultsTable": "table1", "tableName": "table2"}
        formatter = VvpFormatter(input_text, user_ns)

        expected_output = """
        INSERT INTO {{ namespace }}_table1
        SELECT * FROM {{ namespace }}_table2
        """
        actual_output = formatter.substitute_user_variables()
        assert actual_output == expected_output
Exemplo n.º 7
0
def run_query(session, raw_cell, shell, args):
    cell = VvpFormatter(raw_cell, shell.user_ns).substitute_user_variables()
    validation_response = _validate_sql(cell, session)
    if validation_response.status_code != 200:
        raise FlinkSqlRequestException(
            "Bad HTTP request, return code {}".format(
                validation_response.status_code),
            sql=cell)
    json_response = json.loads(validation_response.text)

    if is_invalid_request(json_response):
        validation_result = json_response["validationResult"]
        message = "Unknown validation result: {}".format(validation_result)
        raise FlinkSqlRequestException(message, sql=cell)

    if is_supported_in(ddl_responses, json_response):
        execute_command_response = _execute_sql(cell, session)
        json_data = json.loads(execute_command_response.text)
        return json_convert_to_dataframe(json_data)

    if is_supported_in(dml_responses, json_response):
        return Deployments.make_deployment(cell, session, shell, args)

    if json_response["validationResult"] == dql_response:
        error_message = "SELECT statements are currently not supported."
    else:
        error_message = json_response["errorDetails"]["message"]

    raise SqlSyntaxException(
        "Invalid or unsupported SQL statement: {}".format(error_message),
        sql=cell,
        response=validation_response)
Exemplo n.º 8
0
    def test_get_ambiguous_syntax_does_not_parse_inside_brackets(self):
        test_data = {
            "{{    myvar }}": None,
            "{{ myvar myvar2 }}": None,
        }

        for test_input in test_data.keys():
            assert VvpFormatter._get_ambiguous_syntax(
                test_input) == test_data[test_input]
Exemplo n.º 9
0
    def test_get_ambiguous_syntax_finds_nesting(self):
        test_data = {
            "{ {myvar} }": "{ {myvar}",
            "{{     {myvar } }}":
            "{     {myvar }"  # inside double braces not parsed, but nesting detected
        }

        for input_data in test_data.keys():
            assert VvpFormatter._get_ambiguous_syntax(
                input_data) == test_data[input_data]
Exemplo n.º 10
0
    def test_get_ambiguous_syntax_finds_missing_spaces(self):
        test_data = {
            "{{myvar}}": "{{myvar}",  # missing space {
            "{{myvar": "{{myvar",  # missing space; no closing brace match
            "myvar}}": "myvar}}",  # missing space }
            "{ { myvar}}": "{ myvar}}",  # only get up to next brace back
            "{{ myvar}}": "{ myvar}}",  # same even if double braces
            "{ {{ myvar}}":
            "{ myvar}}"  # matches missing spaces before nesting
        }

        for test_input in test_data.keys():
            assert VvpFormatter._get_ambiguous_syntax(
                test_input) == test_data[test_input]
Exemplo n.º 11
0
 def test_get_ambiguous_syntax_returns_nothing_if_correct(self):
     input_text = "{good} and {{ good }}"
     assert VvpFormatter._get_ambiguous_syntax(input_text) is None
Exemplo n.º 12
0
    def test_prepare_escaped_variables_works_in_simple_case(self):
        input_text = "{{ variable }} and {{ another }} with { ignore }"
        expected = "{{{{ variable }}}} and {{{{ another }}}} with { ignore }"

        assert VvpFormatter._prepare_escaped_variables(input_text) == expected
Exemplo n.º 13
0
 def test_get_ambiguous_syntax_finds_multiple_braces(self):
     input_text = "{{{ myvar }}}"
     assert VvpFormatter._get_ambiguous_syntax(input_text) == "{{{ myvar }"