def test_accepts_complex_arg_annotations(): assert parse_function_definition( 'def Foo(arg: Tuple[str, int]) -> None: ...' ) == FunctionDefinition( name='Foo', args=[ ArgumentDefinition(name='arg'), ] )
def test_simple(): prefix = ('-- some strings not related to any method\n' '-- some more strings\n' '\n') foo_text = ('-- def foo() -> None: ...\n' 'SELECT\n' ' 1 + 1;\n') bar_text = ('-- def bar() -> None: ...\n' 'SELECT 2;\n') assert parse_queries_from_fd( io.StringIO(prefix + foo_text + bar_text)) == [ Query(func_def=FunctionDefinition(name='foo', args=[], returns=None), text=foo_text), Query(func_def=FunctionDefinition(name='bar', args=[], returns=None), text=bar_text) ]
def test_returns_outer_single(): assert parse_function_definition( 'def Foo() -> Single[Tuple]: ...' ) == FunctionDefinition( name='Foo', returns=ReturnValueDefinition( outer_format=ReturnValueOuterFormat.SINGLE, inner_format=ReturnValueInnerFormat.TUPLE ) )
def test_returns_outer_list(): assert parse_function_definition( 'def Foo() -> List[Tuple]: ...' ) == FunctionDefinition( name='Foo', returns=ReturnValueDefinition( outer_format=ReturnValueOuterFormat.LIST, inner_format=ReturnValueInnerFormat.TUPLE ) )
def test_returns_outer_iterator(): assert parse_function_definition( 'def Foo() -> Iterator[Tuple]: ...' ) == FunctionDefinition( name='Foo', returns=ReturnValueDefinition( outer_format=ReturnValueOuterFormat.ITERATOR, inner_format=ReturnValueInnerFormat.TUPLE ) )
def test_accepts_complex_return_annotations(): assert parse_function_definition( 'def Foo() -> Single[Value[bool]]: ...' ) == FunctionDefinition( name='Foo', returns=ReturnValueDefinition( outer_format=ReturnValueOuterFormat.SINGLE, inner_format=ReturnValueInnerFormat.VALUE ) ) assert parse_function_definition( 'def Foo() -> Single[Value[Optional[str]]]: ...' ) == FunctionDefinition( name='Foo', returns=ReturnValueDefinition( outer_format=ReturnValueOuterFormat.SINGLE, inner_format=ReturnValueInnerFormat.VALUE ) )
def test_returns_inner_dict(): assert parse_function_definition( 'def Foo() -> Single[Dict]: ...' ) == FunctionDefinition( name='Foo', returns=ReturnValueDefinition( outer_format=ReturnValueOuterFormat.SINGLE, inner_format=ReturnValueInnerFormat.DICT ) )
def test_default_args(): assert parse_function_definition( 'def Foo(a: int, b: int=2, c: int=3) -> None: ...' ) == FunctionDefinition( name='Foo', args=[ ArgumentDefinition(name='a'), ArgumentDefinition(name='b', has_default=True, default=2), ArgumentDefinition(name='c', has_default=True, default=3), ] )
def test_args(): assert parse_function_definition( 'def Foo(a: int, b: str, c: float) -> None: ...' ) == FunctionDefinition( name='Foo', args=[ ArgumentDefinition(name='a'), ArgumentDefinition(name='b'), ArgumentDefinition(name='c'), ] )
def test_args_unannotated(): assert parse_function_definition( 'def Foo(a, b, c) -> None: ...' ) == FunctionDefinition( name='Foo', args=[ ArgumentDefinition(name='a'), ArgumentDefinition(name='b'), ArgumentDefinition(name='c'), ] )
def test_multiline_definition(): text_foo = ('-- def foo(a) -> None:\n' '-- ...\n' 'SELECT 1;\n') text_bar = ('--\n' '-- Start bar function\n' '--\n' '-- def bar(\n' '-- a\n' '-- ) -> None: ...\n' '--\n' '-- End bar function\n' '--\n' '\n' 'SELECT 1;\n' '\n') text_baz = ('--------------------------------------------------\n' '-- def baz(\n' '-- a\n' '-- ) -> None:\n' '-- ...\n' '--------------------------------------------------\n' '\n' 'SELECT\n' '-- this following line calculates sum of 1 and 1\n' '1+1;\n') assert parse_queries_from_fd( io.StringIO(text_foo + text_bar + text_baz)) == [ Query(func_def=FunctionDefinition( name='foo', args=[ArgumentDefinition(name='a')], returns=None), text=text_foo), Query(func_def=FunctionDefinition( name='bar', args=[ArgumentDefinition(name='a')], returns=None), text=text_bar), Query(func_def=FunctionDefinition( name='baz', args=[ArgumentDefinition(name='a')], returns=None), text=text_baz), ]
def test_returns_outer_dict(): assert parse_function_definition( 'def Foo() -> Dict[0, Tuple]: ...' ) == FunctionDefinition( name='Foo', returns=ReturnValueDefinition( outer_format=ReturnValueOuterFormat.DICT, inner_format=ReturnValueInnerFormat.TUPLE, outer_dict_by=0 ) ) assert parse_function_definition( 'def Foo() -> Dict["colname", Tuple]: ...' ) == FunctionDefinition( name='Foo', returns=ReturnValueDefinition( outer_format=ReturnValueOuterFormat.DICT, inner_format=ReturnValueInnerFormat.TUPLE, outer_dict_by='colname' ) )
def test_simple(): assert parse_function_definition( 'def Foo() -> None: ...' ) == FunctionDefinition( name='Foo' )