1
1
import re
2
- from typing import List
2
+ from typing import List , Union
3
3
4
4
import pytest
5
5
6
+ from dbally .context import BaseCallerContext
6
7
from dbally .iql import IQLArgumentParsingError , IQLQuery , IQLUnsupportedSyntaxError , syntax
7
8
from dbally .iql ._exceptions import IQLArgumentValidationError , IQLFunctionNotExists
8
9
from dbally .iql ._processor import IQLProcessor
9
10
from dbally .views .exposed_functions import ExposedFunction , MethodParamWithTyping
10
11
11
12
13
+ class TestCustomContext (BaseCallerContext ):
14
+ city : str
15
+
16
+
17
+ class AnotherTestCustomContext (BaseCallerContext ):
18
+ some_field : str
19
+
20
+
12
21
async def test_iql_parser ():
22
+ custom_context = TestCustomContext (city = "cracow" )
23
+ custom_context2 = AnotherTestCustomContext (some_field = "aaa" )
24
+
13
25
parsed = await IQLQuery .parse (
14
- "not (filter_by_name(['John', 'Anne']) and filter_by_city('cracow' ) and filter_by_company('deepsense.ai'))" ,
26
+ "not (filter_by_name(['John', 'Anne']) and filter_by_city(BaseCallerContext() ) and filter_by_company('deepsense.ai'))" ,
15
27
allowed_functions = [
16
28
ExposedFunction (
17
29
name = "filter_by_name" , description = "" , parameters = [MethodParamWithTyping (name = "name" , type = List [str ])]
18
30
),
19
31
ExposedFunction (
20
- name = "filter_by_city" , description = "" , parameters = [MethodParamWithTyping (name = "city" , type = str )]
32
+ name = "filter_by_city" ,
33
+ description = "" ,
34
+ parameters = [MethodParamWithTyping (name = "city" , type = Union [str , TestCustomContext ])],
35
+ context_class = TestCustomContext ,
21
36
),
22
37
ExposedFunction (
23
38
name = "filter_by_company" , description = "" , parameters = [MethodParamWithTyping (name = "company" , type = str )]
24
39
),
25
40
],
41
+ contexts = [custom_context , custom_context2 ],
26
42
)
27
43
28
44
not_op = parsed .root
@@ -37,7 +53,7 @@ async def test_iql_parser():
37
53
assert name_filter .arguments [0 ] == ["John" , "Anne" ]
38
54
39
55
assert isinstance (city_filter , syntax .FunctionCall )
40
- assert city_filter .arguments [0 ] == "cracow"
56
+ assert city_filter .arguments [0 ] is custom_context
41
57
42
58
assert isinstance (company_filter , syntax .FunctionCall )
43
59
assert company_filter .arguments [0 ] == "deepsense.ai"
0 commit comments