Esempio n. 1
0
 def test_generate_query_cron(self):
     host = 'example.com'
     _t = 1
     _k = 'key'
     _p = 'id'
     with LogDir() as log_dir:
         KM.init(_k,
                 host=host,
                 to_stderr=False,
                 log_dir=log_dir,
                 use_cron=True)
         KM.identify(_p)
         # Ensure time.time() always returns the same value
         time = self.mocker.replace('time.time')
         time()
         self.mocker.result(_t)
         self.mocker.count(1, None)
         with self.mocker:
             KM.generate_query('e', {}, update=False)
             KM.generate_query('e', {}, update=True)
             with self.get_log('query') as log:
                 self.assertEqual(log.readline(),
                                  '/e?_t=%s&_k=%s\n' % (_t, _k))
                 self.assertEqual(log.readline(),
                                  '/e?_t=%s&_k=%s&_p=%s\n' % (_t, _k, _p))
                 self.assertEqual(log.read(), '')
Esempio n. 2
0
 def test_generate_query(self):
     host = 'example.com'
     _t = 1
     _k = 'key'
     _p = 'id'
     with LogDir() as log_dir:
         KM.init(_k, host=host, to_stderr=False, log_dir=log_dir,
                 use_cron=False)
         KM.identify(_p)
         # Ensure time.time() always returns the same value
         time = self.mocker.replace('time.time')
         time()
         self.mocker.result(_t)
         self.mocker.count(1, None)
         urlopen = self.mocker.replace('urllib2.urlopen')
         # First
         urlopen('http://%s/e?_t=%d&_k=%s' % (host, _t, _k))
         self.mocker.result(None)
         # Second
         urlopen('http://%s/e?_t=%d&_k=%s&_p=%s' % (host, _t, _k, _p))
         self.mocker.result(None)
         # Third
         urlopen('http://%s/f?_t=%d&_k=%s&_p=%s' % (host, _t, _k, _p))
         self.mocker.throw(urllib2.HTTPError('', 500, 'Error', None, None))
         with self.mocker:
             # First
             KM.generate_query('e', {}, update=False)
             # Second
             KM.generate_query('e', {}, update=True)
             # Third
             KM.generate_query('f', {}, update=True)
             with self.get_log('error') as log:
                 self.assertEndsWith(log.read(),
                                     '> HTTP Error 500: Error\n')
Esempio n. 3
0
 def test_generate_query_cron(self):
     host = 'example.com'
     _t = 1
     _k = 'key'
     _p = 'id'
     with LogDir() as log_dir:
         KM.init(_k, host=host, to_stderr=False, log_dir=log_dir,
                 use_cron=True)
         KM.identify(_p)
         # Ensure time.time() always returns the same value
         time = self.mocker.replace('time.time')
         time()
         self.mocker.result(_t)
         self.mocker.count(1, None)
         with self.mocker:
             KM.generate_query('e', {}, update=False)
             KM.generate_query('e', {}, update=True)
             with self.get_log('query') as log:
                 self.assertEqual(log.readline(),
                                  '/e?_t=%s&_k=%s\n' % (_t, _k))
                 self.assertEqual(log.readline(),
                                  '/e?_t=%s&_k=%s&_p=%s\n' % (_t, _k, _p))
                 self.assertEqual(log.read(), '')
Esempio n. 4
0
 def test_generate_query(self):
     host = 'example.com'
     _t = 1
     _k = 'key'
     _p = 'id'
     with LogDir() as log_dir:
         KM.init(_k,
                 host=host,
                 to_stderr=False,
                 log_dir=log_dir,
                 use_cron=False)
         KM.identify(_p)
         # Ensure time.time() always returns the same value
         time = self.mocker.replace('time.time')
         time()
         self.mocker.result(_t)
         self.mocker.count(1, None)
         urlopen = self.mocker.replace('urllib2.urlopen')
         # First
         urlopen('http://%s/e?_t=%d&_k=%s' % (host, _t, _k))
         self.mocker.result(None)
         # Second
         urlopen('http://%s/e?_t=%d&_k=%s&_p=%s' % (host, _t, _k, _p))
         self.mocker.result(None)
         # Third
         urlopen('http://%s/f?_t=%d&_k=%s&_p=%s' % (host, _t, _k, _p))
         self.mocker.throw(urllib2.HTTPError('', 500, 'Error', None, None))
         with self.mocker:
             # First
             KM.generate_query('e', {}, update=False)
             # Second
             KM.generate_query('e', {}, update=True)
             # Third
             KM.generate_query('f', {}, update=True)
             with self.get_log('error') as log:
                 self.assertEndsWith(log.read(),
                                     '> HTTP Error 500: Error\n')