11from unittest import TestCase , skip
22
33import numpy
4+ import numpy as np
45import pandas as pd
56import pytest
67
@@ -12,27 +13,28 @@ def setUp(self) -> None:
1213 self .con = monetdbe .connect ()
1314
1415 def test_regular_selection (self ):
15- con = monetdbe .connect ()
16- monetdbe . sql ('CREATE TABLE pylite00 (i INTEGER)' , client = con )
17- con .execute ('INSERT INTO pylite00 VALUES (1), (2), (3), (4), (5)' )
18- result = con .execute ('SELECT * FROM pylite00' ).fetchdf ()
19- assert len (result ['i' ]) == 5 , "Incorrect result"
16+ with monetdbe .connect () as con :
17+ con . execute ('CREATE TABLE pylite00 (i INTEGER)' )
18+ con .execute ('INSERT INTO pylite00 VALUES (1), (2), (3), (4), (5)' )
19+ result = con .execute ('SELECT * FROM pylite00' ).fetchdf ()
20+ assert len (result ['i' ]) == 5 , "Incorrect result"
2021
2122 def test_monetdbe_create (self ):
22- cur = monetdbe .create ('pylite01' , {'i' : numpy .arange (1000 )})
23- result = monetdbe . sql ('select * from pylite01' , client = cur . connection )
23+ cur = monetdbe .connect (). cursor (). create ('pylite01' , {'i' : numpy .arange (1000 )})
24+ result = cur . execute ('select * from pylite01' ). fetchdf ( )
2425 assert len (result ['i' ]) == 1000 , "Incorrect result"
2526
2627 def test_monetdbe_insert (self ):
27- cur = monetdbe .create ('pylite02' , {'i' : numpy .arange (1000 )})
28+ cur = monetdbe .connect (). cursor (). create ('pylite02' , {'i' : numpy .arange (1000 )})
2829 cur .insert ('pylite02' , {'i' : numpy .arange (1000 )})
2930 result = cur .execute ('select * from pylite02' ).fetchdf ()
3031 assert len (result ['i' ]) == 2000 , "Incorrect result"
3132
3233 def test_monetdbe_create_multiple_columns (self ):
3334 arrays = numpy .arange (100000 ).reshape ((5 , 20000 ))
34- cur = monetdbe .create ('pylite03' ,
35- {'i' : arrays [0 ], 'j' : arrays [1 ], 'k' : arrays [2 ], 'l' : arrays [3 ], 'm' : arrays [4 ]})
35+ cur = monetdbe .connect ().cursor ().create ('pylite03' ,
36+ {'i' : arrays [0 ], 'j' : arrays [1 ], 'k' : arrays [2 ], 'l' : arrays [3 ],
37+ 'm' : arrays [4 ]})
3638 result = cur .execute ('select * from pylite03' ).fetchnumpy ()
3739 assert len (result ) == 5 , "Incorrect amount of columns"
3840 assert len (result ['i' ]) == 20000 , "Incorrect amount of rows"
@@ -61,72 +63,71 @@ def test_connections(self):
6163 cur .create ('pylite05' , {'i' : numpy .arange (1000 )})
6264
6365 # check that table was successfully created
64- result = monetdbe .sql ('SELECT MIN(i) AS minimum FROM pylite05' , client = conn )
66+ result = monetdbe .connect (). execute ('SELECT MIN(i) AS minimum FROM pylite05' , client = conn )
6567 assert result ['minimum' ][0 ] == 0 , "Incorrect result"
6668 # attempt to query the table from another client
6769 with pytest .raises (monetdbe .DatabaseError ):
68- monetdbe .sql ('SELECT * FROM pylite05' , client = conn2 )
70+ monetdbe .connect (). execute ('SELECT * FROM pylite05' , client = conn2 )
6971
7072 # now commit the table
71- monetdbe .sql ('COMMIT' , client = conn )
73+ monetdbe .connect (). execute ('COMMIT' , client = conn )
7274 # query the table again from the other client, this time it should be there
73- result = monetdbe .sql ('SELECT MIN(i) AS minimum FROM pylite05' , client = conn2 )
75+ result = monetdbe .connect (). execute ('SELECT MIN(i) AS minimum FROM pylite05' , client = conn2 )
7476 assert result ['minimum' ][0 ] == 0 , "Incorrect result"
7577
7678 def test_non_existent_table (self , ):
7779 # select from non-existent table
7880 with pytest .raises (monetdbe .DatabaseError ):
79- monetdbe .sql ('select * from nonexistenttable' )
81+ monetdbe .connect (). execute ('select * from nonexistenttable' )
8082
8183 def test_invalid_connection_object (self ):
8284 # invalid connection object
8385 with pytest .raises (TypeError ):
84- monetdbe .sql ('select * from tables' , client = 33 )
86+ monetdbe .connect (). execute ('select * from tables' , client = 33 )
8587
8688 def test_invalid_colnames (self ):
8789 # invalid colnames
8890 with pytest .raises (monetdbe .DatabaseError ):
89- monetdbe .create ('pylite06' , {33 : []})
91+ monetdbe .connect (). cursor (). create ('pylite06' , {33 : []})
9092
9193 def test_empty_colnames (self ):
9294 # empty colnames
9395 with pytest .raises (monetdbe .DatabaseError ):
94- monetdbe .create ('pylite07' , {'' : []})
96+ monetdbe .connect (). cursor (). create ('pylite07' , {'' : []})
9597
9698 def test_invalid_key_dict (self ):
9799 # dictionary with invalid keys
98100 d = dict ()
99101 d [33 ] = 44
100102 with pytest .raises (monetdbe .ProgrammingError ):
101- monetdbe .create ('pylite08' , d )
103+ monetdbe .connect (). cursor (). create ('pylite08' , d )
102104
103105 def test_missing_dict_key (self ):
104106 # missing dict key in insert
105- cur = monetdbe .create ('pylite09' , dict (a = [], b = [], c = []))
107+ cur = monetdbe .connect (). cursor (). create ('pylite09' , dict (a = [], b = [], c = []))
106108 with pytest .raises (monetdbe .DatabaseError ):
107109 cur .insert ('pylite09' , dict (a = 33 , b = 44 ))
108110
109111 def test_bad_column_number (self ):
110112 # too few columns in insert
111- cur = monetdbe .create ('pylite10' , dict (a = [], b = [], c = []))
113+ cur = monetdbe .connect (). cursor (). create ('pylite10' , dict (a = [], b = [], c = []))
112114 with pytest .raises (monetdbe .DatabaseError ):
113115 cur .insert ('pylite10' , {'a' : [33 ], 'b' : [44 ]})
114116
115117 def test_many_sql_statements (self ):
116118 for i in range (5 ): # FIXME 1000
117- conn = monetdbe .connect ()
118- cur = conn .execute ('CREATE TABLE pylite11 (i INTEGER)' )
119- cur .insert ('pylite11' , {'i' : numpy .arange (10 ).astype (numpy .int32 )})
120- result = cur .execute ('SELECT * FROM pylite11' ).fetchdf ()
121- assert result ['i' ][0 ] == 0 , "Invalid result"
122- monetdbe .sql ('DROP TABLE pylite11' , client = conn )
123- monetdbe .sql ('ROLLBACK' , client = conn )
124- del conn
119+ with monetdbe .connect () as conn :
120+ cur = conn .execute ('CREATE TABLE pylite11 (i INTEGER)' )
121+ cur .insert ('pylite11' , {'i' : numpy .arange (10 ).astype (numpy .int32 )})
122+ result = cur .execute ('SELECT * FROM pylite11' ).fetchdf ()
123+ assert result ['i' ][0 ] == 0 , "Invalid result"
124+ conn .execute ('DROP TABLE pylite11' )
125+ conn .execute ('ROLLBACK' )
125126
126127 def test_null_string_insertion_bug (self ):
127- con = monetdbe .connect ()
128- monetdbe . sql ("CREATE TABLE pylite12 (s varchar(2))" , client = con )
129- monetdbe .insert ('pylite12' , {'s' : ['a' , None ]}, client = con )
130- result = monetdbe . sql ("SELECT * FROM pylite12" , client = con )
131- expected = numpy .ma .masked_array (['a' , 'a' ], mask = [0 , 1 ])
132- numpy .testing .assert_array_equal (result ['s' ], expected )
128+ with monetdbe .connect () as con :
129+ cur = con . execute ("CREATE TABLE pylite12 (s varchar(2))" )
130+ cur .insert ('pylite12' , {'s' : np . array ( ['a' , None ])} )
131+ result = cur . execute ("SELECT * FROM pylite12" ). fetchnumpy ( )
132+ expected = numpy .ma .masked_array (['a' , 'a' ], mask = [0 , 1 ])
133+ numpy .testing .assert_array_equal (result ['s' ], expected )
0 commit comments