2020 sql , sql_cursor_get_page ,
2121 cache_get_configuration ,
2222)
23+ from pyignite .datatypes .cache_config import CacheMode
2324from pyignite .datatypes .prop_codes import *
2425from pyignite .exceptions import SQLError
2526from pyignite .utils import entity_id
2627from pyignite .binary import unwrap_binary
2728
28-
2929initial_data = [
3030 ('John' , 'Doe' , 5 ),
3131 ('Jane' , 'Roe' , 4 ),
@@ -59,9 +59,10 @@ def test_sql(client):
5959
6060 result = sql_fields (
6161 conn ,
62- 'PUBLIC' ,
62+ 0 ,
6363 create_query ,
6464 page_size ,
65+ schema = 'PUBLIC' ,
6566 include_field_names = True
6667 )
6768 assert result .status == 0 , result .message
@@ -70,9 +71,10 @@ def test_sql(client):
7071 fname , lname , grade = data_line
7172 result = sql_fields (
7273 conn ,
73- 'PUBLIC' ,
74+ 0 ,
7475 insert_query ,
7576 page_size ,
77+ schema = 'PUBLIC' ,
7678 query_args = [i , fname , lname , grade ],
7779 include_field_names = True
7880 )
@@ -108,7 +110,7 @@ def test_sql(client):
108110 assert data .type_id == entity_id (binary_type_name )
109111
110112 # repeat cleanup
111- result = sql_fields (conn , 'PUBLIC' , drop_query , page_size )
113+ result = sql_fields (conn , 0 , drop_query , page_size , schema = 'PUBLIC' )
112114 assert result .status == 0
113115
114116
@@ -121,9 +123,10 @@ def test_sql_fields(client):
121123
122124 result = sql_fields (
123125 conn ,
124- 'PUBLIC' ,
126+ 0 ,
125127 create_query ,
126128 page_size ,
129+ schema = 'PUBLIC' ,
127130 include_field_names = True
128131 )
129132 assert result .status == 0 , result .message
@@ -132,19 +135,21 @@ def test_sql_fields(client):
132135 fname , lname , grade = data_line
133136 result = sql_fields (
134137 conn ,
135- 'PUBLIC' ,
138+ 0 ,
136139 insert_query ,
137140 page_size ,
141+ schema = 'PUBLIC' ,
138142 query_args = [i , fname , lname , grade ],
139143 include_field_names = True
140144 )
141145 assert result .status == 0 , result .message
142146
143147 result = sql_fields (
144148 conn ,
145- 'PUBLIC' ,
149+ 0 ,
146150 select_query ,
147151 page_size ,
152+ schema = 'PUBLIC' ,
148153 include_field_names = True
149154 )
150155 assert result .status == 0
@@ -159,7 +164,7 @@ def test_sql_fields(client):
159164 assert result .value ['more' ] is False
160165
161166 # repeat cleanup
162- result = sql_fields (conn , 'PUBLIC' , drop_query , page_size )
167+ result = sql_fields (conn , 0 , drop_query , page_size , schema = 'PUBLIC' )
163168 assert result .status == 0
164169
165170
@@ -176,7 +181,7 @@ def test_long_multipage_query(client):
176181
177182 client .sql ('DROP TABLE LongMultipageQuery IF EXISTS' )
178183
179- client .sql ("CREATE TABLE LongMultiPageQuery (%s, %s)" % \
184+ client .sql ("CREATE TABLE LongMultiPageQuery (%s, %s)" %
180185 (fields [0 ] + " INT(11) PRIMARY KEY" , "," .join (map (lambda f : f + " INT(11)" , fields [1 :]))))
181186
182187 for id in range (1 , 21 ):
@@ -193,6 +198,63 @@ def test_long_multipage_query(client):
193198 client .sql (drop_query )
194199
195200
196- def test_sql_not_create_cache (client ):
201+ def test_sql_not_create_cache_with_schema (client ):
197202 with pytest .raises (SQLError , match = r".*Cache does not exist.*" ):
198- client .sql (schema = 'IS_NOT_EXISTING' , query_str = 'select * from IsNotExisting' )
203+ client .sql (schema = None , cache = 'NOT_EXISTING' , query_str = 'select * from NotExisting' )
204+
205+
206+ def test_sql_not_create_cache_with_cache (client ):
207+ with pytest .raises (SQLError , match = r".*Failed to set schema.*" ):
208+ client .sql (schema = 'NOT_EXISTING' , query_str = 'select * from NotExisting' )
209+
210+
211+ def test_query_with_cache (client ):
212+ test_key = 42
213+ test_value = 'Lorem ipsum'
214+
215+ cache_name = test_query_with_cache .__name__ .upper ()
216+ schema_name = f'{ cache_name } _schema' .upper ()
217+ table_name = f'{ cache_name } _table' .upper ()
218+
219+ cache = client .create_cache ({
220+ PROP_NAME : cache_name ,
221+ PROP_SQL_SCHEMA : schema_name ,
222+ PROP_CACHE_MODE : CacheMode .PARTITIONED ,
223+ PROP_QUERY_ENTITIES : [
224+ {
225+ 'table_name' : table_name ,
226+ 'key_field_name' : 'KEY' ,
227+ 'value_field_name' : 'VALUE' ,
228+ 'key_type_name' : 'java.lang.Long' ,
229+ 'value_type_name' : 'java.lang.String' ,
230+ 'query_indexes' : [],
231+ 'field_name_aliases' : [],
232+ 'query_fields' : [
233+ {
234+ 'name' : 'KEY' ,
235+ 'type_name' : 'java.lang.Long' ,
236+ 'is_key_field' : True ,
237+ 'is_notnull_constraint_field' : True ,
238+ },
239+ {
240+ 'name' : 'VALUE' ,
241+ 'type_name' : 'java.lang.String' ,
242+ },
243+ ],
244+ },
245+ ],
246+ })
247+
248+ cache .put (test_key , test_value )
249+
250+ args_to_check = [
251+ ('schema' , schema_name ),
252+ ('cache' , cache ),
253+ ('cache' , cache .name ),
254+ ('cache' , cache .cache_id )
255+ ]
256+
257+ for param , value in args_to_check :
258+ page = client .sql (f'select value from { table_name } ' , ** {param : value })
259+ received = next (page )[0 ]
260+ assert test_value == received
0 commit comments