PageRenderTime 671ms CodeModel.GetById 71ms app.highlight 418ms RepoModel.GetById 61ms app.codeStats 1ms

/historical/sqllog_test.py

https://bitbucket.org/lindenlab/apiary/
Python | 335 lines | 306 code | 5 blank | 24 comment | 0 complexity | a95af46a40e69dfbb819e17bb8787a15 MD5 | raw file
  1#
  2# $LicenseInfo:firstyear=2010&license=mit$
  3# 
  4# Copyright (c) 2010, Linden Research, Inc.
  5# 
  6# Permission is hereby granted, free of charge, to any person obtaining a copy
  7# of this software and associated documentation files (the "Software"), to deal
  8# in the Software without restriction, including without limitation the rights
  9# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10# copies of the Software, and to permit persons to whom the Software is
 11# furnished to do so, subject to the following conditions:
 12# 
 13# The above copyright notice and this permission notice shall be included in
 14# all copies or substantial portions of the Software.
 15# 
 16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22# THE SOFTWARE.
 23# $/LicenseInfo$
 24#
 25
 26from StringIO import StringIO
 27import unittest
 28
 29from sqllog import *
 30from timestamp import TimeStamp
 31    
 32
 33class TestEvent(unittest.TestCase):
 34
 35    def testEventStr(self):
 36        e = Event(123456.789, '888', 'sim', 'QueryStart', 'SELECT * FROM foo')
 37        self.assertEqual(str(e), """\
 38123456.789000\t888\tsim\tQueryStart
 39SELECT * FROM foo
 40**************************************
 41""")
 42
 43    def testOrdering(self):
 44        e1 = Event(10001.700, '999', 'sim', 'QueryStart', 'SELECT')
 45        e2 = Event(10001.717, '999', 'sim', 'QueryResponse', 'SELECT')
 46        e3 = Event(10001.717, '999', 'sim', 'Quit', 'Quit')
 47        e4 = Event(10001.729, '878', 'sim', 'QueryStart', 'SELECT')
 48        
 49        self.assert_(e1 < e2)
 50        self.assert_(e1 < e3)
 51        self.assert_(e1 < e4)
 52        self.assert_(e2 < e3)
 53        self.assert_(e2 < e4)
 54        self.assert_(e3 < e4)
 55
 56
 57class TestParsing(unittest.TestCase):
 58
 59    def testSimpleStanza(self):
 60        f = StringIO("""\
 611237237351.064861\t10.2.231.65:40784\tsim\tQueryResponse
 62SELECT owner_id, is_owner_group FROM parcel WHERE parcel_id='9d50d6eb-a623-2a30-a8e0-840189fabff7'
 63**************************************
 641237237351.065393\t10.6.6.97:39706\tsim\tQueryStart
 65SELECT u.username, uln.name, u.group_id  FROM user u, user_last_name uln  WHERE u.agent_id = '8c7d410a-ec70-430a-a3ef-1b44afe94cc2'  AND uln.last_name_id = u.last_name_id
 66**************************************
 67""")
 68        s = parse_stanza(f)
 69        self.assert_(s is not None)
 70        self.assertEqual(s.time, TimeStamp(1237237351.064861))
 71        self.assertEqual(s.id, '10.2.231.65:40784')
 72        self.assertEqual(s.state, 'QueryResponse')
 73        self.assertEqual(s.body, """\
 74SELECT owner_id, is_owner_group FROM parcel WHERE parcel_id='9d50d6eb-a623-2a30-a8e0-840189fabff7'
 75""")
 76
 77    def testEmptyStanza(self):
 78        f = StringIO('')
 79        s = parse_stanza(f)
 80        self.assert_(s is None)
 81    
 82    def testMissingStanzaEnd(self):
 83        f = StringIO("""\
 841237237351.064861\t10.2.231.65:40784\tsim\tQueryResponse
 85SELECT owner_id, is_owner_group FROM parcel WHERE parcel_id='9d50d6eb-a623-2a30-a8e0-840189fabff7'
 86""")
 87        s = parse_stanza(f)
 88        self.assert_(s is not None)
 89        self.assertEqual(s.time, TimeStamp(1237237351.064861))
 90        self.assertEqual(s.id, '10.2.231.65:40784')
 91        self.assertEqual(s.state, 'QueryResponse')
 92        self.assertEqual(s.body, """\
 93SELECT owner_id, is_owner_group FROM parcel WHERE parcel_id='9d50d6eb-a623-2a30-a8e0-840189fabff7'
 94""")
 95
 96    def testJunkLeadInStanza(self):
 97        f = StringIO("""\
 98SELECT u.username, uln.name, u.group_id  FROM user u, user_last_name uln  WHERE u.agent_id = '8c7d410a-ec70-430a-a3ef-1b44afe94cc2'  AND uln.last_name_id = u.last_name_id
 99**************************************
1001237237351.064861\t10.2.231.65:40784\tsim\tQueryResponse
101SELECT owner_id, is_owner_group FROM parcel WHERE parcel_id='9d50d6eb-a623-2a30-a8e0-840189fabff7'
102**************************************
103""")
104        s = parse_stanza(f)
105        self.assert_(s is not None)
106        self.assertEqual(s.time, TimeStamp(1237237351.064861))
107        self.assertEqual(s.id, '10.2.231.65:40784')
108        self.assertEqual(s.state, 'QueryResponse')
109        self.assertEqual(s.body, """\
110SELECT owner_id, is_owner_group FROM parcel WHERE parcel_id='9d50d6eb-a623-2a30-a8e0-840189fabff7'
111""")
112
113
114class TestSequence(unittest.TestCase):
115
116    def testTime(self):
117        seq = Sequence()
118        seq.note(Event(10001.700, '999', 'sim', 'QueryStart', 'SELECT'))
119        seq.note(Event(10001.703, '999', 'sim', 'QueryResult', 'SELECT'))
120        seq.note(Event(10001.717, '999', 'sim', 'Quit', 'Quit'))
121        
122        self.assertEqual(seq.count(), 3)
123        self.assertEqual(seq.time(), TimeStamp(0.017))
124    
125    def testQuit(self):
126        seq = Sequence()
127        seq.note(Event(10001.700, '999', 'sim', 'QueryStart', 'SELECT'))
128        self.assert_(not seq.ended())
129        seq.note(Event(10001.703, '999', 'sim', 'QueryResult', 'SELECT'))
130        self.assert_(not seq.ended())
131        seq.note(Event(10001.717, '999', 'sim', 'Quit', 'Quit'))
132        self.assert_(seq.ended())
133    
134    def testGenerateEnd(self):
135        seq = Sequence()
136        seq.note(Event(10001.700, '999', 'sim', 'QueryStart', 'SELECT'))
137        seq.note(Event(10001.703, '999', 'sim', 'QueryResult', 'SELECT'))
138        self.assert_(not seq.ended())
139        e = seq.generateEnd()
140        self.assertEqual(e.time, TimeStamp(10001.703))
141        self.assertEqual(e.id, '999')
142        self.assertEqual(e.state, 'Quit')
143    
144    def testTimeTo(self):
145        seq = Sequence()
146        e1 = Event(10001.700, '999', 'sim', 'QueryStart', 'SELECT')
147        e2 = Event(10001.717, '999', 'sim', 'Quit', 'Quit')
148        
149        self.assert_(seq.timeto(e1) is None)
150        
151        seq.note(e1)
152        self.assertEqual(seq.timeto(e1), TimeStamp(0))
153        self.assertEqual(seq.timeto(e2), TimeStamp(0.017))
154        
155
156
157class SimpleCoalesce(CoalesceSequences):
158    def __init__(self):
159        CoalesceSequences.__init__(self)
160        self.sequences = []
161        
162    def fullSequence(self, e):
163        self.sequences.append(e)
164        
165class TestCoalesce(unittest.TestCase):
166
167    def assertEvent(self, e, time, id, state, body=None):
168        if time is not None:
169            if not isinstance(time, TimeStamp):
170                time = TimeStamp(time)
171            self.assertEqual(e.time, time)
172        if id is not None:
173            self.assertEqual(e.id, id)
174        if state is not None:
175            self.assertEqual(e.state, state)
176        if body is not None:
177            self.assertEqual(e.body, body)
178            
179    def testOne(self):
180        c = CoalescedEvent()
181        c.add(Event(10001.500, '999', 'sim', 'QueryStart', 'SELECT "foo"'))
182        c.add(Event(10001.600, '999', 'sim', 'QueryStart', 'SELECT "bar"'))
183        c.add(Event(10001.700, '999', 'sim', 'Quit', 'Quit'))
184        
185        self.assertEvent(c, 10001.500, '999', 'Sequence',
186            '10001.500000:SELECT "foo"\n+++\n'
187            '10001.600000:SELECT "bar"\n+++\n'
188            '10001.700000:Quit\n+++\n')
189
190    def testTwoSequential(self):
191        l = []
192        l.append(Event(10001.500, '999', 'sim', 'QueryStart', 'SELECT "foo"'))
193        l.append(Event(10001.600, '999', 'sim', 'QueryStart', 'SELECT "bar"'))
194        l.append(Event(10001.700, '999', 'sim', 'Quit', 'Quit'))
195        l.append(Event(10002.500, '888', 'sim', 'QueryStart', 'SELECT "oof"'))
196        l.append(Event(10002.600, '888', 'sim', 'QueryStart', 'SELECT "rab"'))
197        l.append(Event(10002.700, '888', 'sim', 'Quit', 'Quit'))
198        
199        sc = SimpleCoalesce()
200        sc.replay(l)
201        
202        self.assertEqual(len(sc.sequences), 2)
203        self.assertEvent(sc.sequences[0], 10001.500, '999', 'Sequence',
204            '10001.500000:SELECT "foo"\n+++\n'
205            '10001.600000:SELECT "bar"\n+++\n'
206            '10001.700000:Quit\n+++\n')
207        self.assertEvent(sc.sequences[1], 10002.500, '888', 'Sequence',
208            '10002.500000:SELECT "oof"\n+++\n'
209            '10002.600000:SELECT "rab"\n+++\n'
210            '10002.700000:Quit\n+++\n')
211        
212    def testTwoInterleaved(self):
213        l = []
214        l.append(Event(10001.500, '999', 'sim', 'QueryStart', 'SELECT "foo"'))
215        l.append(Event(10001.520, '888', 'sim', 'QueryStart', 'SELECT "oof"'))
216        l.append(Event(10001.600, '999', 'sim', 'QueryStart', 'SELECT "bar"'))
217        l.append(Event(10001.620, '888', 'sim', 'QueryStart', 'SELECT "rab"'))
218        l.append(Event(10001.700, '999', 'sim', 'Quit', 'Quit'))
219        l.append(Event(10001.720, '888', 'sim', 'Quit', 'Quit'))
220        
221        sc = SimpleCoalesce()
222        sc.replay(l)
223        
224        self.assertEqual(len(sc.sequences), 2)
225        self.assertEvent(sc.sequences[0], 10001.500, '999', 'Sequence',
226            '10001.500000:SELECT "foo"\n+++\n'
227            '10001.600000:SELECT "bar"\n+++\n'
228            '10001.700000:Quit\n+++\n')
229        self.assertEvent(sc.sequences[1], 10001.520, '888', 'Sequence',
230            '10001.520000:SELECT "oof"\n+++\n'
231            '10001.620000:SELECT "rab"\n+++\n'
232            '10001.720000:Quit\n+++\n')
233        
234    def testTwoNested(self):
235        l = []
236        l.append(Event(10001.500, '999', 'sim', 'QueryStart', 'SELECT "foo"'))
237        l.append(Event(10002.500, '888', 'sim', 'QueryStart', 'SELECT "oof"'))
238        l.append(Event(10002.600, '888', 'sim', 'QueryStart', 'SELECT "rab"'))
239        l.append(Event(10002.700, '888', 'sim', 'Quit', 'Quit'))
240        l.append(Event(10003.600, '999', 'sim', 'QueryStart', 'SELECT "bar"'))
241        l.append(Event(10003.700, '999', 'sim', 'Quit', 'Quit'))
242        
243        sc = SimpleCoalesce()
244        sc.replay(l)
245        
246        self.assertEqual(len(sc.sequences), 2)
247        self.assertEvent(sc.sequences[0], 10001.500, '999', 'Sequence',
248            '10001.500000:SELECT "foo"\n+++\n'
249            '10003.600000:SELECT "bar"\n+++\n'
250            '10003.700000:Quit\n+++\n')
251        self.assertEvent(sc.sequences[1], 10002.500, '888', 'Sequence',
252            '10002.500000:SELECT "oof"\n+++\n'
253            '10002.600000:SELECT "rab"\n+++\n'
254            '10002.700000:Quit\n+++\n')
255        
256    def testManyNested(self):
257        l = []
258        l.append(Event(10001.500, '111', 'sim', 'QueryStart', 'SELECT "one"'))
259        l.append(Event(10002.500, '222', 'sim', 'QueryStart', 'SELECT "two"'))
260        l.append(Event(10002.700, '222', 'sim', 'Quit', 'Quit'))
261        l.append(Event(10003.500, '333', 'sim', 'QueryStart', 'SELECT "three"'))
262        l.append(Event(10003.700, '333', 'sim', 'Quit', 'Quit'))
263        l.append(Event(10004.500, '444', 'sim', 'QueryStart', 'SELECT "four"'))
264        l.append(Event(10004.700, '444', 'sim', 'Quit', 'Quit'))
265        l.append(Event(10005.500, '555', 'sim', 'QueryStart', 'SELECT "five"'))
266        l.append(Event(10005.700, '111', 'sim', 'Quit', 'Quit'))
267        l.append(Event(10006.500, '666', 'sim', 'QueryStart', 'SELECT "six"'))
268        l.append(Event(10006.700, '666', 'sim', 'Quit', 'Quit'))
269        l.append(Event(10007.500, '777', 'sim', 'QueryStart', 'SELECT "seven"'))
270        l.append(Event(10007.700, '777', 'sim', 'Quit', 'Quit'))
271        l.append(Event(10008.500, '888', 'sim', 'QueryStart', 'SELECT "eight"'))
272        l.append(Event(10008.700, '888', 'sim', 'Quit', 'Quit'))
273        l.append(Event(10009.700, '555', 'sim', 'Quit', 'Quit'))
274
275        sc = SimpleCoalesce()
276        sc.replay(l)
277
278        self.assertEqual(len(sc.sequences), 8)
279        self.assertEqual(sc.sequences[0].id, '111')
280        self.assertEqual(sc.sequences[1].id, '222')
281        self.assertEqual(sc.sequences[2].id, '333')
282        self.assertEqual(sc.sequences[3].id, '444')
283        self.assertEqual(sc.sequences[4].id, '555')
284        self.assertEqual(sc.sequences[5].id, '666')
285        self.assertEqual(sc.sequences[6].id, '777')
286        self.assertEqual(sc.sequences[7].id, '888')
287    
288    def testMissingEnd(self):
289        l = []
290        l.append(Event(10001.500, '111', 'sim', 'QueryStart', 'SELECT "one"'))
291        l.append(Event(10002.500, '222', 'sim', 'QueryStart', 'SELECT "two"'))
292        l.append(Event(10002.700, '222', 'sim', 'Quit', 'Quit'))
293        l.append(Event(10003.500, '333', 'sim', 'QueryStart', 'SELECT "three"'))
294    
295        sc = SimpleCoalesce()
296        sc.replay(l)
297        self.assertEqual(len(sc.sequences), 3)
298        self.assertEqual(sc.sequences[0].id, '111')
299        self.assertEqual(sc.sequences[1].id, '222')
300        self.assertEqual(sc.sequences[2].id, '333')
301
302        es = sc.sequences[0].events()
303        self.assertEqual(len(es), 2)
304        self.assertEvent(es[0], 10001.500, '111', Event.Query, 'SELECT "one"')
305        self.assertEvent(es[1], 10001.500, '111', Event.End)
306        es = sc.sequences[1].events()
307        self.assertEqual(len(es), 2)
308        self.assertEvent(es[0], 10002.500, '222', Event.Query, 'SELECT "two"')
309        self.assertEvent(es[1], 10002.700, '222', Event.End)
310        es = sc.sequences[2].events()
311        self.assertEqual(len(es), 2)
312        self.assertEvent(es[0], 10003.500, '333', Event.Query, 'SELECT "three"')
313        self.assertEvent(es[1], 10003.500, '333', Event.End)
314
315    def testSplitApart(self):
316        c = CoalescedEvent()
317        c.add(Event(10001.500, '999', 'sim', 'QueryStart', 'SELECT "foo"'))
318        c.add(Event(10001.600, '999', 'sim', 'QueryStart', 'SELECT "bar"\n'))
319        c.add(Event(10001.700, '999', 'sim', 'QueryStart', '\nSELECT "baz"'))
320        c.add(Event(10001.800, '999', 'sim', 'Quit', 'Quit'))
321        
322        e = parse_stanza(StringIO(str(c)))
323        self.assertEqual(e.id, '999')
324        self.assertEqual(e.state, CoalescedEvent.Sequence)
325
326        es = e.events();
327        self.assertEqual(len(es), 4)
328        
329        self.assertEvent(es[0], 10001.500, '999', Event.Query, 'SELECT "foo"')
330        self.assertEvent(es[1], 10001.600, '999', Event.Query, 'SELECT "bar"\n')
331        self.assertEvent(es[2], 10001.700, '999', Event.Query, '\nSELECT "baz"')
332        self.assertEvent(es[3], 10001.800, '999', Event.End)
333
334if __name__ == '__main__':
335    unittest.main()