@@ -12,101 +12,59 @@ def test_connection(connection):
1212
1313 cur = connection .cursor ()
1414 with suppress (dbapi .DatabaseError ):
15- cur .execute ("DROP TABLE foo" , context = { "isddl" : True } )
15+ cur .execute (dbapi . YdbQuery ( "DROP TABLE foo" , is_ddl = True ) )
1616
1717 assert not connection .check_exists ("/local/foo" )
1818 with pytest .raises (dbapi .ProgrammingError ):
1919 connection .describe ("/local/foo" )
2020
21- cur .execute ("CREATE TABLE foo(id Int64 NOT NULL, PRIMARY KEY (id))" , context = { "isddl" : True } )
21+ cur .execute (dbapi . YdbQuery ( "CREATE TABLE foo(id Int64 NOT NULL, PRIMARY KEY (id))" , is_ddl = True ) )
2222
2323 assert connection .check_exists ("/local/foo" )
2424
2525 col = connection .describe ("/local/foo" ).columns [0 ]
2626 assert col .name == "id"
2727 assert col .type == ydb .PrimitiveType .Int64
2828
29- cur .execute ("DROP TABLE foo" , context = { "isddl" : True } )
29+ cur .execute (dbapi . YdbQuery ( "DROP TABLE foo" , is_ddl = True ) )
3030 cur .close ()
3131
3232
33- def test_cursor (connection ):
33+ def test_cursor_raw_query (connection ):
3434 cur = connection .cursor ()
3535 assert cur
3636
3737 with suppress (dbapi .DatabaseError ):
38- cur .execute ("DROP TABLE test" , context = { "isddl" : True } )
38+ cur .execute (dbapi . YdbQuery ( "DROP TABLE test" , is_ddl = True ) )
3939
40- cur .execute (
41- "CREATE TABLE test(id Int64 NOT NULL, text Utf8, PRIMARY KEY (id))" ,
42- context = {"isddl" : True },
43- )
44-
45- cur .execute ('INSERT INTO test(id, text) VALUES (1, "foo")' )
46-
47- cur .execute ("SELECT id, text FROM test" )
48- assert cur .fetchone () == (1 , "foo" ), "fetchone is ok"
49-
50- cur .execute ("SELECT id, text FROM test WHERE id = %(id)s" , {"id" : 1 })
51- assert cur .fetchone () == (1 , "foo" ), "parametrized query is ok"
40+ cur .execute (dbapi .YdbQuery ("CREATE TABLE test(id Int64 NOT NULL, text Utf8, PRIMARY KEY (id))" , is_ddl = True ))
5241
5342 cur .execute (
54- "INSERT INTO test(id, text) VALUES (%(id1)s, %(text1)s), (%(id2)s, %(text2)s)" ,
55- {"id1" : 2 , "text1" : "" , "id2" : 3 , "text2" : "bar" },
56- )
57-
58- cur .execute ("UPDATE test SET text = %(t)s WHERE id = %(id)s" , {"id" : 2 , "t" : "foo2" })
59-
60- cur .execute ("SELECT id FROM test" )
61- assert set (cur .fetchall ()) == {(1 ,), (2 ,), (3 ,)}, "fetchall is ok"
62-
63- cur .execute ("SELECT id FROM test ORDER BY id DESC" )
64- assert cur .fetchmany (2 ) == [(3 ,), (2 ,)], "fetchmany is ok"
65- assert cur .fetchmany (1 ) == [(1 ,)]
66-
67- cur .execute ("SELECT id FROM test ORDER BY id LIMIT 2" )
68- assert cur .fetchall () == [(1 ,), (2 ,)], "limit clause without params is ok"
69-
70- # TODO: Failed to convert type: Int64 to Uint64
71- # cur.execute("SELECT id FROM test ORDER BY id LIMIT %(limit)s", {"limit": 2})
72- # assert cur.fetchall() == [(1,), (2,)], "limit clause with params is ok"
73-
74- cur2 = connection .cursor ()
75- cur2 .execute ("INSERT INTO test(id) VALUES (%(id1)s), (%(id2)s)" , {"id1" : 5 , "id2" : 6 })
76-
77- cur .execute ("SELECT id FROM test ORDER BY id" )
78- assert cur .fetchall () == [(1 ,), (2 ,), (3 ,), (5 ,), (6 ,)], "cursor2 commit changes"
79-
80- cur .execute ("SELECT text FROM test WHERE id > %(min_id)s" , {"min_id" : 3 })
81- assert cur .fetchall () == [(None ,), (None ,)], "NULL returns as None"
82-
83- cur .execute ("SELECT id, text FROM test WHERE text LIKE %(p)s" , {"p" : "foo%" })
84- assert set (cur .fetchall ()) == {(1 , "foo" ), (2 , "foo2" )}, "like clause works"
85-
86- cur .execute (
87- # DECLARE statement (DECLARE $data AS List<Struct<id:Int64,text:Utf8>>)
88- # will generate automatically
89- """INSERT INTO test SELECT id, text FROM AS_TABLE($data);""" ,
43+ dbapi .YdbQuery (
44+ """
45+ DECLARE $data AS List<Struct<id:Int64, text: Utf8>>;
46+
47+ INSERT INTO test SELECT id, text FROM AS_TABLE($data);
48+ """ ,
49+ parameters_types = {
50+ "$data" : ydb .ListType (
51+ ydb .StructType ()
52+ .add_member ("id" , ydb .PrimitiveType .Int64 )
53+ .add_member ("text" , ydb .PrimitiveType .Utf8 )
54+ )
55+ },
56+ ),
9057 {
91- "data" : [
58+ "$ data" : [
9259 {"id" : 17 , "text" : "seventeen" },
9360 {"id" : 21 , "text" : "twenty one" },
9461 ]
9562 },
9663 )
9764
98- cur .execute ("SELECT id FROM test ORDER BY id" )
99- assert cur .rowcount == 7 , "rowcount ok"
100- assert cur .fetchall () == [(1 ,), (2 ,), (3 ,), (5 ,), (6 ,), (17 ,), (21 ,)], "ok"
101-
102- cur .execute ("INSERT INTO test(id) VALUES (37)" )
103- cur .execute ("SELECT id FROM test WHERE text IS %(p)s" , {"p" : None })
104- assert not cur .fetchone () == (37 ,)
105-
106- cur .execute ("DROP TABLE test" , context = {"isddl" : True })
65+ cur .execute (dbapi .YdbQuery ("DROP TABLE test" , is_ddl = True ))
10766
10867 cur .close ()
109- cur2 .close ()
11068
11169
11270def test_errors (connection ):
@@ -116,25 +74,25 @@ def test_errors(connection):
11674 cur = connection .cursor ()
11775
11876 with suppress (dbapi .DatabaseError ):
119- cur .execute ("DROP TABLE test" , context = { "isddl" : True } )
77+ cur .execute (dbapi . YdbQuery ( "DROP TABLE test" , is_ddl = True ) )
12078
12179 with pytest .raises (dbapi .DataError ):
122- cur .execute ("SELECT 18446744073709551616" )
80+ cur .execute (dbapi . YdbQuery ( "SELECT 18446744073709551616" ) )
12381
12482 with pytest .raises (dbapi .DataError ):
125- cur .execute ("SELECT * FROM 拉屎" )
83+ cur .execute (dbapi . YdbQuery ( "SELECT * FROM 拉屎" ) )
12684
12785 with pytest .raises (dbapi .DataError ):
128- cur .execute ("SELECT floor(5 / 2)" )
86+ cur .execute (dbapi . YdbQuery ( "SELECT floor(5 / 2)" ) )
12987
13088 with pytest .raises (dbapi .ProgrammingError ):
131- cur .execute ("SELECT * FROM test" )
89+ cur .execute (dbapi . YdbQuery ( "SELECT * FROM test" ) )
13290
133- cur .execute ("CREATE TABLE test(id Int64, PRIMARY KEY (id))" , context = { "isddl" : True } )
91+ cur .execute (dbapi . YdbQuery ( "CREATE TABLE test(id Int64, PRIMARY KEY (id))" , is_ddl = True ) )
13492
135- cur .execute ("INSERT INTO test(id) VALUES(1)" )
93+ cur .execute (dbapi . YdbQuery ( "INSERT INTO test(id) VALUES(1)" ) )
13694 with pytest .raises (dbapi .IntegrityError ):
137- cur .execute ("INSERT INTO test(id) VALUES(1)" )
95+ cur .execute (dbapi . YdbQuery ( "INSERT INTO test(id) VALUES(1)" ) )
13896
139- cur .execute ("DROP TABLE test" , context = { "isddl" : True } )
97+ cur .execute (dbapi . YdbQuery ( "DROP TABLE test" , is_ddl = True ) )
14098 cur .close ()
0 commit comments