44
55
66class Table ():
7- def __init__ (self , redis , name , values , key = "" ):
8- self .redis = redis
9- self .key = key
10- self .name = name
11- self .values = values
12-
13- def __enter__ (self ):
14- create_table = "CREATE TABLE {} {}" .format (self .name , self .values )
15- if self .key :
16- self .redis .execute_command ("REDISQL.EXEC" , self .key , create_table )
17- else :
18- self .redis .execute_command ("REDISQL.EXEC" , create_table )
19-
20- def __exit__ (self , type , value , traceback ):
21- drop_table = "DROP TABLE {}" .format (self .name )
22- if self .key :
23- self .redis .execute_command ("REDISQL.EXEC" , self .key , drop_table )
24- else :
25- self .redis .execute_command ("REDISQL.EXEC" , drop_table )
7+ def __init__ (self , redis , name , values , key = "" ):
8+ self .redis = redis
9+ self .key = key
10+ self .name = name
11+ self .values = values
12+
13+ def __enter__ (self ):
14+ create_table = "CREATE TABLE {} {}" .format (self .name , self .values )
15+ if self .key :
16+ self .redis .execute_command ("REDISQL.EXEC" , self .key , create_table )
17+ else :
18+ self .redis .execute_command ("REDISQL.EXEC" , create_table )
19+
20+ def __exit__ (self , type , value , traceback ):
21+ drop_table = "DROP TABLE {}" .format (self .name )
22+ if self .key :
23+ self .redis .execute_command ("REDISQL.EXEC" , self .key , drop_table )
24+ else :
25+ self .redis .execute_command ("REDISQL.EXEC" , drop_table )
2626
2727class DB ():
2828 def __init__ (self , redis , key ):
@@ -37,103 +37,116 @@ def __exit__(self, type, value, traceback):
3737
3838
3939class TestRediSQLWithExec (unittest .TestCase ):
40- @classmethod
41- def setUpClass (self ):
42- self .redis = redis .StrictRedis ()
40+ @classmethod
41+ def setUpClass (self ):
42+ self .redis = redis .StrictRedis ()
4343
44- def exec_cmd (self , * command ):
45- return self .redis .execute_command ("REDISQL.EXEC" , * command )
44+ def exec_cmd (self , * command ):
45+ return self .redis .execute_command ("REDISQL.EXEC" , * command )
4646
47- def create_db (self , key ):
48- return self .redis .execute_command ("REDISQL.CREATE_DB" , key )
47+ def create_db (self , key ):
48+ return self .redis .execute_command ("REDISQL.CREATE_DB" , key )
4949
50- def delete_db (self , key ):
51- return self .redis .execute_command ("REDISQL.DELETE_DB" , key )
50+ def delete_db (self , key ):
51+ return self .redis .execute_command ("REDISQL.DELETE_DB" , key )
5252
5353class TestRediSQLExec (TestRediSQLWithExec ):
54- def setUp (self ):
55- pass
54+ def setUp (self ):
55+ pass
5656
57- def test_ping (self ):
58- self .assertTrue (self .redis .ping ())
57+ def test_ping (self ):
58+ self .assertTrue (self .redis .ping ())
5959
60- def test_create_table (self ):
61- done = self .exec_cmd ("CREATE TABLE test1 (A INTEGER);" )
62- self .assertEquals (done , "DONE" )
63- done = self .exec_cmd ("DROP TABLE test1" )
60+ def test_create_table (self ):
61+ with DB (self .redis , "A" ):
62+ done = self .exec_cmd ("A" , "CREATE TABLE test1 (A INTEGER);" )
63+ self .assertEquals (done , "DONE" )
64+ done = self .exec_cmd ("A" , "DROP TABLE test1" )
65+ self .assertEquals (done , "DONE" )
66+
67+ def test_insert (self ):
68+ with DB (self .redis , "B" ):
69+ with Table (self .redis , "test2" , "(A INTEGER)" , key = "B" ):
70+ done = self .exec_cmd ("B" , "INSERT INTO test2 VALUES(2);" )
6471 self .assertEquals (done , "DONE" )
6572
66- def test_insert (self ):
67- with Table (self .redis , "test2" , "(A INTEGER)" ):
68- done = self .exec_cmd ("INSERT INTO test2 VALUES(2);" )
69- self .assertEquals (done , "DONE" )
70-
71- def test_select (self ):
72- with Table (self .redis , "test3" , "(A INTEGER)" ):
73- done = self .exec_cmd ("INSERT INTO test3 VALUES(2);" )
74- self .assertEquals (done , "DONE" )
75-
76- result = self .exec_cmd ("SELECT * from test3" )
77- self .assertEquals (result , [[2 ]])
78-
79- self .exec_cmd ("INSERT INTO test3 VALUES(3);" )
80- result = self .exec_cmd ("SELECT * from test3 ORDER BY A" )
81- self .assertEquals (result , [[2 ], [3 ]])
82-
83- self .exec_cmd ("INSERT INTO test3 VALUES(4);" )
84- result = self .exec_cmd ("SELECT * FROM test3 ORDER BY A" )
85- self .assertEquals (result , [[2 ], [3 ], [4 ]])
86-
87- def test_single_remove (self ):
88- with Table (self .redis , "test4" , "(A INTEGER)" ):
89- self .exec_cmd ("INSERT INTO test4 VALUES(2);" )
90- self .exec_cmd ("INSERT INTO test4 VALUES(3);" )
91- self .exec_cmd ("INSERT INTO test4 VALUES(4);" )
92- result = self .exec_cmd ("SELECT * FROM test4 ORDER BY A" )
93- self .assertEquals (result , [[2 ], [3 ], [4 ]])
94- self .exec_cmd ("DELETE FROM test4 WHERE A = 3;" )
95- result = self .exec_cmd ("SELECT * FROM test4 ORDER BY A" )
96- self .assertEquals (result , [[2 ], [4 ]])
73+ def test_select (self ):
74+ with DB (self .redis , "C" ):
75+ with Table (self .redis , "test3" , "(A INTEGER)" , key = "C" ):
76+ done = self .exec_cmd ("C" , "INSERT INTO test3 VALUES(2);" )
77+ self .assertEquals (done , "DONE" )
78+
79+ result = self .exec_cmd ("C" , "SELECT * from test3" )
80+ self .assertEquals (result , [[2 ]])
81+
82+ self .exec_cmd ("C" , "INSERT INTO test3 VALUES(3);" )
83+ result = self .exec_cmd ("C" , "SELECT * from test3 ORDER BY A" )
84+ self .assertEquals (result , [[2 ], [3 ]])
85+
86+ self .exec_cmd ("C" , "INSERT INTO test3 VALUES(4);" )
87+ result = self .exec_cmd ("C" , "SELECT * FROM test3 ORDER BY A" )
88+ self .assertEquals (result , [[2 ], [3 ], [4 ]])
89+
90+ def test_single_remove (self ):
91+ with DB (self .redis , "D" ):
92+ with Table (self .redis , "test4" , "(A INTEGER)" , key = "D" ):
93+ self .exec_cmd ("D" , "INSERT INTO test4 VALUES(2);" )
94+ self .exec_cmd ("D" , "INSERT INTO test4 VALUES(3);" )
95+ self .exec_cmd ("D" , "INSERT INTO test4 VALUES(4);" )
96+ result = self .exec_cmd ("D" , "SELECT * FROM test4 ORDER BY A" )
97+ self .assertEquals (result , [[2 ], [3 ], [4 ]])
98+ self .exec_cmd ("D" , "DELETE FROM test4 WHERE A = 3;" )
99+ result = self .exec_cmd ("D" , "SELECT * FROM test4 ORDER BY A" )
100+ self .assertEquals (result , [[2 ], [4 ]])
97101
98- def test_big_select (self ):
99- elements = 50
100- with Table (self .redis , "test5" , "(A INTERGER)" ):
101- pipe = self .redis .pipeline (transaction = False )
102- for i in xrange (elements ):
103- pipe .execute_command ("REDISQL.EXEC" ,
104- "INSERT INTO test5 VALUES({})" .format (i ))
105- pipe .execute ()
106- result = self .exec_cmd ("SELECT * FROM test5 ORDER BY A" )
107- self .assertEquals (result , [[x ] for x in xrange (elements )])
102+ def test_big_select (self ):
103+ elements = 50
104+ with DB (self .redis , "E" ):
105+ with Table (self .redis , "test5" , "(A INTERGER)" , key = "E" ):
106+ pipe = self .redis .pipeline (transaction = False )
107+ for i in xrange (elements ):
108+ pipe .execute_command ("REDISQL.EXEC" , "E" ,
109+ "INSERT INTO test5 VALUES({})" .format (i ))
110+ pipe .execute ()
111+ result = self .exec_cmd ("E" , "SELECT * FROM test5 ORDER BY A" )
112+ self .assertEquals (result , [[x ] for x in xrange (elements )])
108113
109- def test_multiple_row (self ):
110- with Table (self .redis , "test6" , "(A INTEGER, B REAL, C TEXT)" ):
111- self .exec_cmd ("INSERT INTO test6 VALUES(1, 1.0, '1point1')" )
112- self .exec_cmd ("INSERT INTO test6 VALUES(2, 2.0, '2point2')" )
113- self .exec_cmd ("INSERT INTO test6 VALUES(3, 3.0, '3point3')" )
114- self .exec_cmd ("INSERT INTO test6 VALUES(4, 4.0, '4point4')" )
115- self .exec_cmd ("INSERT INTO test6 VALUES(5, 5.0, '5point5')" )
116-
117- result = self .exec_cmd ("SELECT A, B, C FROM test6 ORDER BY A" )
118- result = [[A , float (B ), C ] for [A , B , C ] in result ]
119- self .assertEquals (result ,
120- [[1L , 1.0 , "1point1" ], [2L , 2.0 , '2point2' ],
121- [3L , 3.0 , '3point3' ], [4L , 4.0 , '4point4' ],
122- [5L , 5.0 , '5point5' ]])
123-
124- def test_join (self ):
125- with Table (self .redis , "testA" , "(A INTEGER, B INTEGER)" ):
126- with Table (self .redis , "testB" , "(C INTEGER, D INTEGER)" ):
127- self .exec_cmd ("INSERT INTO testA VALUES(1, 2)" )
128- self .exec_cmd ("INSERT INTO testA VALUES(3, 4)" )
129- self .exec_cmd ("INSERT INTO testB VALUES(1, 2)" )
130- self .exec_cmd ("INSERT INTO testB VALUES(3, 4)" )
131- result = self .exec_cmd ("SELECT A, B, C, D FROM testA, testB WHERE A = C ORDER BY A" )
132- self .assertEquals (result , [[1 , 2 , 1 , 2 ], [3 , 4 , 3 , 4 ]])
133-
134-
135- def runTest (self ):
136- pass
114+ def test_multiple_row (self ):
115+ with DB (self .redis , "F" ):
116+ with Table (self .redis , "test6" , "(A INTEGER, B REAL, C TEXT)" , key = "F" ):
117+ self .exec_cmd ("F" , "INSERT INTO test6 VALUES(1, 1.0, '1point1')" )
118+ self .exec_cmd ("F" , "INSERT INTO test6 VALUES(2, 2.0, '2point2')" )
119+ self .exec_cmd ("F" , "INSERT INTO test6 VALUES(3, 3.0, '3point3')" )
120+ self .exec_cmd ("F" , "INSERT INTO test6 VALUES(4, 4.0, '4point4')" )
121+ self .exec_cmd ("F" , "INSERT INTO test6 VALUES(5, 5.0, '5point5')" )
122+
123+ result = self .exec_cmd ("F" , "SELECT A, B, C FROM test6 ORDER BY A" )
124+ result = [[A , float (B ), C ] for [A , B , C ] in result ]
125+ self .assertEquals (result ,
126+ [[1L , 1.0 , "1point1" ], [2L , 2.0 , '2point2' ],
127+ [3L , 3.0 , '3point3' ], [4L , 4.0 , '4point4' ],
128+ [5L , 5.0 , '5point5' ]])
129+
130+ def test_join (self ):
131+ with DB (self .redis , "G" ):
132+ with Table (self .redis , "testA" , "(A INTEGER, B INTEGER)" , key = "G" ):
133+ with Table (self .redis , "testB" , "(C INTEGER, D INTEGER)" , key = "G" ):
134+ self .exec_cmd ("G" , "INSERT INTO testA VALUES(1, 2)" )
135+ self .exec_cmd ("G" , "INSERT INTO testA VALUES(3, 4)" )
136+ self .exec_cmd ("G" , "INSERT INTO testB VALUES(1, 2)" )
137+ self .exec_cmd ("G" , "INSERT INTO testB VALUES(3, 4)" )
138+ result = self .exec_cmd ("G" , "SELECT A, B, C, D FROM testA, testB WHERE A = C ORDER BY A" )
139+ self .assertEquals (result , [[1 , 2 , 1 , 2 ], [3 , 4 , 3 , 4 ]])
140+
141+
142+ def runTest (self ):
143+ pass
144+
145+ class NoDefaultDB (TestRediSQLWithExec ):
146+
147+ def test_that_we_need_a_key (self ):
148+ with self .assertRaises (redis .exceptions .ResponseError ):
149+ self .exec_cmd ("SELECT 'foo';" )
137150
138151class TestRediSQLKeys (TestRediSQLWithExec ):
139152
@@ -163,6 +176,6 @@ def test_insert_into_table(self):
163176
164177
165178if __name__ == '__main__' :
166- unittest .main ()
179+ unittest .main ()
167180
168181
0 commit comments