예제 #1
0
def test_translate_function_specials(statement: str, translation: str,
                                     output_type: str) -> None:
    Translation = recursive_translation.RecursiveHiveToPresto()
    query = sqlparse.parse(statement)[0]  # Single query in this statement
    function = query.tokens[0]  # Single object in query
    assert Translation._translate_function(function.tokens) == (
        translation, output_type)  # Single query in this statement
예제 #2
0
def test_breakdown_parenthesis(query: str) -> None:
    Translation = recursive_translation.RecursiveHiveToPresto()
    tokens = sqlparse.parse(query)[0][0]
    expected = query.strip("()")
    if expected == "":
        expected = []
    else:
        expected = sqlparse.parse(expected)[0][0].tokens
    result = Translation._breakdown_parenthesis(tokens)

    # Validation
    assert len(result) == len(expected)
    assert all([
        result[idx].value == expected[idx].value for idx in range(len(result))
    ])
예제 #3
0
def test_apply_compositions_AssertionError(str_output_arguments: List,
                                           compositions: List[Dict]) -> None:
    Translation = recursive_translation.RecursiveHiveToPresto()
    with pytest.raises(AssertionError):
        Translation._apply_compositions(str_output_arguments, compositions)
예제 #4
0
def test_apply_compositions(str_output_arguments: List,
                            compositions: List[Dict], expected: List) -> None:
    Translation = recursive_translation.RecursiveHiveToPresto()
    Translation._apply_compositions(str_output_arguments,
                                    compositions) == expected
예제 #5
0
def test_breakdown_query(statement: str, expected: str) -> None:
    Translation = recursive_translation.RecursiveHiveToPresto()
    query = sqlparse.parse(statement)[0]  # Single query in this statement
    _, _, _, Translation.partition_info = utils.parse_hive_insertion(
        query.value)  # Set the partition information
    assert Translation._breakdown_query(query.tokens) == expected
예제 #6
0
def test_translate_query_ValueError() -> None:
    Translation = recursive_translation.RecursiveHiveToPresto()
    with pytest.raises(ValueError):
        Translation.translate_query(
            "INSERT INTO TABLE test_db.test_table select * from db1.table1;select * from db2.table2"
        )
예제 #7
0
def test_translate_function_ValueError(statement: str) -> None:
    Translation = recursive_translation.RecursiveHiveToPresto()
    query = sqlparse.parse(statement)[0]  # Single query in this statement
    function = query.tokens[0]  # Single object in query
    with pytest.raises(ValueError):
        Translation._translate_function(function.tokens)
예제 #8
0
def test_translate_query(query: str, has_insert_statement: bool,
                         expected: Dict) -> None:
    Translation = recursive_translation.RecursiveHiveToPresto()
    Translation._breakdown_query = MagicMock(return_value="")
    Translation.translate_query(query, has_insert_statement)
    assert Translation.partition_info == expected
예제 #9
0
from unittest.mock import MagicMock
import pytest
import os
from typing import Dict, List, Tuple
import sqlparse
from sqlparse.tokens import Name, Literal
from sql_translate.engine import recursive_translation
from sql_translate import utils

Translation = recursive_translation.RecursiveHiveToPresto()


def test_create_parent() -> None:
    _RecursiveTranslator = recursive_translation._RecursiveTranslator()


@pytest.mark.parametrize(['query', 'has_insert_statement', 'expected'], [(
    "INSERT INTO TABLE test_db.test_table PARTITION (a='3') select * from db.table",
    True, {
        'partition_name': 'a',
        'partition_value': "'3'"
    }), ("select * from db.table", False, {})])
def test_translate_query(query: str, has_insert_statement: bool,
                         expected: Dict) -> None:
    Translation = recursive_translation.RecursiveHiveToPresto()
    Translation._breakdown_query = MagicMock(return_value="")
    Translation.translate_query(query, has_insert_statement)
    assert Translation.partition_info == expected


def test_translate_query_ValueError() -> None: