def test_is_identified(self): with LogDir() as log_dir: KM.log_dir = log_dir with StdIO() as stdio: self.assertFalse(KM.is_identified()) KM.identify('id') self.assertTrue(KM.is_identified())
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(), '')
def assertQuery(self, type, data, update=True): 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(0, None) with self.mocker: yield with self.get_log('query') as log: data = dict(data, _t=str(_t), _k=_k) if update: data['_p'] = _p line = log.readline().strip() bits = urlparse.urlsplit(line) self.assertEqual(bits[2], '/%s' % urllib.quote(type)) self.assertEqual(dict(parse_qsl(bits[3])), data) self.assertEqual(log.readline(), '') os.unlink(KM.log_name('query'))
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')
def test_is_initialize_and_identified(self): with LogDir() as log_dir: KM.log_dir = log_dir with StdIO() as stdio: self.assertFalse(KM.is_initialized_and_identified()) KM.init('key') self.assertFalse(KM.is_initialized_and_identified()) KM.identify('id') self.assertTrue(KM.is_initialized_and_identified()) KM.init(None) self.assertFalse(KM.is_initialized_and_identified())
def test_reset(self): with LogDir() as log_dir: KM.init('key', to_stderr=False, log_dir=log_dir) KM.identify('id') KM.log_query('Query') self.assertEqual(KM._key, 'key') self.assertEqual(KM._id, 'id') self.assertEqual( KM._logs, {'query': os.path.join(log_dir, 'kissmetrics_query.log')}) KM.reset() self.assertEqual(KM._key, None) self.assertEqual(KM._id, None) self.assertEqual(KM._logs, {})
def test_reset(self): with LogDir() as log_dir: KM.init('key', to_stderr=False, log_dir=log_dir) KM.identify('id') KM.log_query('Query') self.assertEqual(KM._key, 'key') self.assertEqual(KM._id, 'id') self.assertEqual( KM._logs, {'query': os.path.join(log_dir, 'kissmetrics_query.log')} ) KM.reset() self.assertEqual(KM._key, None) self.assertEqual(KM._id, None) self.assertEqual(KM._logs, {})