def test_send_sampling(self): libhoney._xmit = mock.MagicMock() libhoney._should_drop = mock.MagicMock(return_value=True) # test that send() drops when should_drop is true ev = libhoney.Event() ev.send() libhoney._xmit.send.assert_not_called() libhoney._should_drop.assert_called_with(1) ev = libhoney.Event() ev.sample_rate = 5 ev.send() libhoney._xmit.send.assert_not_called() libhoney._should_drop.assert_called_with(5) # and actually sends them along when should_drop is false libhoney._should_drop = mock.MagicMock(return_value=False) ev = libhoney.Event() ev.add_field("f", "g") ev.api_host = "myhost" ev.writekey = "letmewrite" ev.dataset = "storeme" ev.send() libhoney._xmit.send.assert_called_with(ev) libhoney._should_drop.assert_called_with(1) ev.sample_rate = 5 ev.send() libhoney._xmit.send.assert_called_with(ev) libhoney._should_drop.assert_called_with(5) # test that send_presampled() does not drop libhoney._should_drop.reset_mock() ev.send_presampled() libhoney._xmit.send.assert_called_with(ev) libhoney._should_drop.assert_not_called() ev.sample_rate = 5 ev.send_presampled() libhoney._xmit.send.assert_called_with(ev) libhoney._should_drop.assert_not_called()
def test_str(self): ev = libhoney.Event() ev.add_field("obj", {"a": 1}) ev.add_field("string", "a:1") ev.add_field("number", 5) ev.add_field("boolean", True) ev.add_field("null", None) serialized = str(ev) self.assertTrue('"obj": {"a": 1}' in serialized) self.assertTrue('"string": "a:1"' in serialized) self.assertTrue('"number": 5' in serialized) self.assertTrue('"boolean": true' in serialized) self.assertTrue('"null": null' in serialized)
def test_send(self): libhoney._xmit = mock.MagicMock() ev = libhoney.Event() with self.assertRaises(libhoney.SendError) as c1: ev.send() self.assertTrue("No metrics added to event. Won't send empty event." in str(c1.exception)) ev = libhoney.Event() ev.add_field("f", "g") with self.assertRaises(libhoney.SendError) as c2: ev.send() self.assertTrue("No api_host for Honeycomb." in str(c2.exception)) ev.api_host = "myhost" with self.assertRaises(libhoney.SendError) as c2: ev.send() self.assertTrue("No write_key specified." in str(c2.exception)) ev.writekey = "letmewrite" with self.assertRaises(libhoney.SendError) as c2: ev.send() self.assertTrue("No dataset for Honeycomb." in str(c2.exception)) ev.dataset = "storeme" ev.send() libhoney._xmit.send.assert_called_with(ev)
def test_timer(self): class fakeDate: def setNow(self, time): self.time = time def now(self): return self.time def utcnow(self): return self.time fakeStart = datetime.datetime(2016, 1, 2, 3, 4, 5, 6) fakeEnd = fakeStart + datetime.timedelta(milliseconds=5) fd = fakeDate() fd.setNow(fakeStart) real_datetime = datetime.datetime libhoney.datetime.datetime = fd ev = libhoney.Event() with ev.timer("howlong"): fd.setNow(fakeEnd) self.assertEqual(ev._fields._data, {"howlong": 5}) self.assertEqual(ev.created_at, fakeStart) libhoney.datetime.datetime = real_datetime