33
33
from sqlalchemy .orm import Session
34
34
from sqlalchemy .testing import eq_ , in_
35
35
36
- fake_cursor = MagicMock (name = 'fake_cursor' )
37
- fake_cursor .description = (
38
- ('foo' , None , None , None , None , None , None ),
39
- )
40
36
FakeCursor = MagicMock (name = 'FakeCursor' , spec = Cursor )
41
- FakeCursor .return_value = fake_cursor
42
37
43
38
44
39
@patch ('crate.client.connection.Cursor' , FakeCursor )
45
40
class DialectTest (TestCase ):
46
41
47
42
def execute_wrapper (self , query , * args , ** kwargs ):
48
43
self .executed_statement = query
49
- return fake_cursor
44
+ return self . fake_cursor
50
45
51
46
def setUp (self ):
47
+
48
+ self .fake_cursor = MagicMock (name = 'fake_cursor' )
49
+ FakeCursor .return_value = self .fake_cursor
50
+
52
51
self .engine = sa .create_engine ('crate://' )
53
52
self .executed_statement = None
54
53
@@ -58,7 +57,7 @@ def setUp(self):
58
57
self .engine .execute = self .execute_wrapper
59
58
self .connection .execute = self .execute_wrapper
60
59
else :
61
- fake_cursor .execute = self .execute_wrapper
60
+ self . fake_cursor .execute = self .execute_wrapper
62
61
63
62
self .base = declarative_base (bind = self .engine )
64
63
@@ -73,31 +72,56 @@ class Character(self.base):
73
72
self .character = Character
74
73
self .session = Session ()
75
74
76
- def test_pks_are_retrieved_depending_on_version_set (self ):
75
+ def test_pks_are_retrieved_depending_on_version_set_old (self ):
76
+ """
77
+ Test the old pk retrieval.
78
+ """
77
79
meta = self .character .metadata
78
-
79
- # test the old pk retrieval
80
80
insp = inspect (meta .bind )
81
81
self .engine .dialect .server_version_info = (0 , 54 , 0 )
82
- fake_cursor .rowcount = 1
83
- fake_cursor .fetchone = MagicMock (return_value = [["id" , "id2" , "id3" ]])
82
+
83
+ # Setup fake cursor.
84
+ self .fake_cursor .rowcount = 1
85
+ self .fake_cursor .description = (
86
+ ('foo' , None , None , None , None , None , None ),
87
+ )
88
+ self .fake_cursor .fetchone = MagicMock (return_value = [["id" , "id2" , "id3" ]])
89
+
90
+ # Verify outcome.
84
91
eq_ (insp .get_pk_constraint ("characters" )['constrained_columns' ], {"id" , "id2" , "id3" })
85
- fake_cursor .fetchone .assert_called_once_with ()
92
+ self . fake_cursor .fetchone .assert_called_once_with ()
86
93
in_ ("information_schema.table_constraints" , self .executed_statement )
87
94
88
- # test the new pk retrieval
95
+ def test_pks_are_retrieved_depending_on_version_set_new (self ):
96
+ """
97
+ Test the new pk retrieval.
98
+ """
99
+ meta = self .character .metadata
89
100
insp = inspect (meta .bind )
90
101
self .engine .dialect .server_version_info = (2 , 3 , 0 )
91
- fake_cursor .rowcount = 3
92
- fake_cursor .fetchall = MagicMock (return_value = [["id" ], ["id2" ], ["id3" ]])
102
+
103
+ # Setup fake cursor.
104
+ self .fake_cursor .rowcount = 3
105
+ self .fake_cursor .description = (
106
+ ('foo' , None , None , None , None , None , None ),
107
+ )
108
+ self .fake_cursor .fetchall = MagicMock (return_value = [["id" ], ["id2" ], ["id3" ]])
109
+
110
+ # Verify outcome.
93
111
eq_ (insp .get_pk_constraint ("characters" )['constrained_columns' ], {"id" , "id2" , "id3" })
94
- fake_cursor .fetchall .assert_called_once_with ()
112
+ self . fake_cursor .fetchall .assert_called_once_with ()
95
113
in_ ("information_schema.key_column_usage" , self .executed_statement )
96
114
97
115
def test_get_table_names (self ):
98
- fake_cursor .rowcount = 1
99
- fake_cursor .fetchall = MagicMock (return_value = [["t1" ], ["t2" ]])
100
116
117
+ # Setup fake cursor.
118
+ self .fake_cursor .rowcount = 1
119
+ self .fake_cursor .description = (
120
+ ('foo' , None , None , None , None , None , None ),
121
+ )
122
+ self .fake_cursor .fetchall = MagicMock (return_value = [["t1" ], ["t2" ]])
123
+
124
+ # Verify outcome.
101
125
insp = inspect (self .character .metadata .bind )
102
126
self .engine .dialect .server_version_info = (2 , 0 , 0 )
103
127
eq_ (insp .get_table_names (schema = "doc" ),
@@ -117,11 +141,18 @@ def test_get_table_names(self):
117
141
in_ ("WHERE schema_name = ? ORDER BY" , self .executed_statement )
118
142
119
143
def test_get_view_names (self ):
120
- fake_cursor .rowcount = 1
121
- fake_cursor .fetchall = MagicMock (return_value = [["v1" ], ["v2" ]])
122
144
123
145
insp = inspect (self .character .metadata .bind )
124
146
self .engine .dialect .server_version_info = (2 , 0 , 0 )
147
+
148
+ # Setup fake cursor.
149
+ self .fake_cursor .rowcount = 1
150
+ self .fake_cursor .description = (
151
+ ('foo' , None , None , None , None , None , None ),
152
+ )
153
+ self .fake_cursor .fetchall = MagicMock (return_value = [["v1" ], ["v2" ]])
154
+
155
+ # Verify outcome.
125
156
eq_ (insp .get_view_names (schema = "doc" ),
126
157
['v1' , 'v2' ])
127
158
eq_ (self .executed_statement , "SELECT table_name FROM information_schema.views "
0 commit comments