##// END OF EJS Templates
cleanup sqlitedb temporary db file after tests
MinRK -
Show More
@@ -1,224 +1,233 b''
1 1 """Tests for db backends
2 2
3 3 Authors:
4 4
5 5 * Min RK
6 6 """
7 7
8 8 #-------------------------------------------------------------------------------
9 9 # Copyright (C) 2011 The IPython Development Team
10 10 #
11 11 # Distributed under the terms of the BSD License. The full license is in
12 12 # the file COPYING, distributed as part of this software.
13 13 #-------------------------------------------------------------------------------
14 14
15 15 #-------------------------------------------------------------------------------
16 16 # Imports
17 17 #-------------------------------------------------------------------------------
18 18
19 19 from __future__ import division
20 20
21 import os
21 22 import tempfile
22 23 import time
23 24
24 25 from datetime import datetime, timedelta
25 26 from unittest import TestCase
26 27
27 28 from IPython.parallel import error
28 29 from IPython.parallel.controller.dictdb import DictDB
29 30 from IPython.parallel.controller.sqlitedb import SQLiteDB
30 31 from IPython.parallel.controller.hub import init_record, empty_record
31 32
32 33 from IPython.testing import decorators as dec
33 34 from IPython.zmq.session import Session
34 35
35 36
36 37 #-------------------------------------------------------------------------------
37 38 # TestCases
38 39 #-------------------------------------------------------------------------------
39 40
40 41 class TestDictBackend(TestCase):
41 42 def setUp(self):
42 43 self.session = Session()
43 44 self.db = self.create_db()
44 45 self.load_records(16)
45 46
46 47 def create_db(self):
47 48 return DictDB()
48 49
49 50 def load_records(self, n=1):
50 51 """load n records for testing"""
51 52 #sleep 1/10 s, to ensure timestamp is different to previous calls
52 53 time.sleep(0.1)
53 54 msg_ids = []
54 55 for i in range(n):
55 56 msg = self.session.msg('apply_request', content=dict(a=5))
56 57 msg['buffers'] = []
57 58 rec = init_record(msg)
58 59 msg_id = msg['header']['msg_id']
59 60 msg_ids.append(msg_id)
60 61 self.db.add_record(msg_id, rec)
61 62 return msg_ids
62 63
63 64 def test_add_record(self):
64 65 before = self.db.get_history()
65 66 self.load_records(5)
66 67 after = self.db.get_history()
67 68 self.assertEquals(len(after), len(before)+5)
68 69 self.assertEquals(after[:-5],before)
69 70
70 71 def test_drop_record(self):
71 72 msg_id = self.load_records()[-1]
72 73 rec = self.db.get_record(msg_id)
73 74 self.db.drop_record(msg_id)
74 75 self.assertRaises(KeyError,self.db.get_record, msg_id)
75 76
76 77 def _round_to_millisecond(self, dt):
77 78 """necessary because mongodb rounds microseconds"""
78 79 micro = dt.microsecond
79 80 extra = int(str(micro)[-3:])
80 81 return dt - timedelta(microseconds=extra)
81 82
82 83 def test_update_record(self):
83 84 now = self._round_to_millisecond(datetime.now())
84 85 #
85 86 msg_id = self.db.get_history()[-1]
86 87 rec1 = self.db.get_record(msg_id)
87 88 data = {'stdout': 'hello there', 'completed' : now}
88 89 self.db.update_record(msg_id, data)
89 90 rec2 = self.db.get_record(msg_id)
90 91 self.assertEquals(rec2['stdout'], 'hello there')
91 92 self.assertEquals(rec2['completed'], now)
92 93 rec1.update(data)
93 94 self.assertEquals(rec1, rec2)
94 95
95 96 # def test_update_record_bad(self):
96 97 # """test updating nonexistant records"""
97 98 # msg_id = str(uuid.uuid4())
98 99 # data = {'stdout': 'hello there'}
99 100 # self.assertRaises(KeyError, self.db.update_record, msg_id, data)
100 101
101 102 def test_find_records_dt(self):
102 103 """test finding records by date"""
103 104 hist = self.db.get_history()
104 105 middle = self.db.get_record(hist[len(hist)//2])
105 106 tic = middle['submitted']
106 107 before = self.db.find_records({'submitted' : {'$lt' : tic}})
107 108 after = self.db.find_records({'submitted' : {'$gte' : tic}})
108 109 self.assertEquals(len(before)+len(after),len(hist))
109 110 for b in before:
110 111 self.assertTrue(b['submitted'] < tic)
111 112 for a in after:
112 113 self.assertTrue(a['submitted'] >= tic)
113 114 same = self.db.find_records({'submitted' : tic})
114 115 for s in same:
115 116 self.assertTrue(s['submitted'] == tic)
116 117
117 118 def test_find_records_keys(self):
118 119 """test extracting subset of record keys"""
119 120 found = self.db.find_records({'msg_id': {'$ne' : ''}},keys=['submitted', 'completed'])
120 121 for rec in found:
121 122 self.assertEquals(set(rec.keys()), set(['msg_id', 'submitted', 'completed']))
122 123
123 124 def test_find_records_msg_id(self):
124 125 """ensure msg_id is always in found records"""
125 126 found = self.db.find_records({'msg_id': {'$ne' : ''}},keys=['submitted', 'completed'])
126 127 for rec in found:
127 128 self.assertTrue('msg_id' in rec.keys())
128 129 found = self.db.find_records({'msg_id': {'$ne' : ''}},keys=['submitted'])
129 130 for rec in found:
130 131 self.assertTrue('msg_id' in rec.keys())
131 132 found = self.db.find_records({'msg_id': {'$ne' : ''}},keys=['msg_id'])
132 133 for rec in found:
133 134 self.assertTrue('msg_id' in rec.keys())
134 135
135 136 def test_find_records_in(self):
136 137 """test finding records with '$in','$nin' operators"""
137 138 hist = self.db.get_history()
138 139 even = hist[::2]
139 140 odd = hist[1::2]
140 141 recs = self.db.find_records({ 'msg_id' : {'$in' : even}})
141 142 found = [ r['msg_id'] for r in recs ]
142 143 self.assertEquals(set(even), set(found))
143 144 recs = self.db.find_records({ 'msg_id' : {'$nin' : even}})
144 145 found = [ r['msg_id'] for r in recs ]
145 146 self.assertEquals(set(odd), set(found))
146 147
147 148 def test_get_history(self):
148 149 msg_ids = self.db.get_history()
149 150 latest = datetime(1984,1,1)
150 151 for msg_id in msg_ids:
151 152 rec = self.db.get_record(msg_id)
152 153 newt = rec['submitted']
153 154 self.assertTrue(newt >= latest)
154 155 latest = newt
155 156 msg_id = self.load_records(1)[-1]
156 157 self.assertEquals(self.db.get_history()[-1],msg_id)
157 158
158 159 def test_datetime(self):
159 160 """get/set timestamps with datetime objects"""
160 161 msg_id = self.db.get_history()[-1]
161 162 rec = self.db.get_record(msg_id)
162 163 self.assertTrue(isinstance(rec['submitted'], datetime))
163 164 self.db.update_record(msg_id, dict(completed=datetime.now()))
164 165 rec = self.db.get_record(msg_id)
165 166 self.assertTrue(isinstance(rec['completed'], datetime))
166 167
167 168 def test_drop_matching(self):
168 169 msg_ids = self.load_records(10)
169 170 query = {'msg_id' : {'$in':msg_ids}}
170 171 self.db.drop_matching_records(query)
171 172 recs = self.db.find_records(query)
172 173 self.assertEquals(len(recs), 0)
173 174
174 175 def test_null(self):
175 176 """test None comparison queries"""
176 177 msg_ids = self.load_records(10)
177 178
178 179 query = {'msg_id' : None}
179 180 recs = self.db.find_records(query)
180 181 self.assertEquals(len(recs), 0)
181 182
182 183 query = {'msg_id' : {'$ne' : None}}
183 184 recs = self.db.find_records(query)
184 185 self.assertTrue(len(recs) >= 10)
185 186
186 187 def test_pop_safe_get(self):
187 188 """editing query results shouldn't affect record [get]"""
188 189 msg_id = self.db.get_history()[-1]
189 190 rec = self.db.get_record(msg_id)
190 191 rec.pop('buffers')
191 192 rec['garbage'] = 'hello'
192 193 rec2 = self.db.get_record(msg_id)
193 194 self.assertTrue('buffers' in rec2)
194 195 self.assertFalse('garbage' in rec2)
195 196
196 197 def test_pop_safe_find(self):
197 198 """editing query results shouldn't affect record [find]"""
198 199 msg_id = self.db.get_history()[-1]
199 200 rec = self.db.find_records({'msg_id' : msg_id})[0]
200 201 rec.pop('buffers')
201 202 rec['garbage'] = 'hello'
202 203 rec2 = self.db.find_records({'msg_id' : msg_id})[0]
203 204 self.assertTrue('buffers' in rec2)
204 205 self.assertFalse('garbage' in rec2)
205 206
206 207 def test_pop_safe_find_keys(self):
207 208 """editing query results shouldn't affect record [find+keys]"""
208 209 msg_id = self.db.get_history()[-1]
209 210 rec = self.db.find_records({'msg_id' : msg_id}, keys=['buffers'])[0]
210 211 rec.pop('buffers')
211 212 rec['garbage'] = 'hello'
212 213 rec2 = self.db.find_records({'msg_id' : msg_id})[0]
213 214 self.assertTrue('buffers' in rec2)
214 215 self.assertFalse('garbage' in rec2)
215 216
216 217
217 218 class TestSQLiteBackend(TestDictBackend):
218 219
219 220 @dec.skip_without('sqlite3')
220 221 def create_db(self):
221 222 return SQLiteDB(location=tempfile.gettempdir())
222 223
223 224 def tearDown(self):
224 225 self.db._db.close()
226
227
228 def teardown():
229 """cleanup task db file after all tests have run"""
230 try:
231 os.remove(os.path.join(tempfile.gettempdir(), 'tasks.db'))
232 except:
233 pass
General Comments 0
You need to be logged in to leave comments. Login now