def _start_end_range_reverse(self, batch_id, start, end): if start is not None: start = to_reverse_timestamp(start) if end is not None: end = to_reverse_timestamp(end) # The index is an inverse timestamp so the start and end range values # are swapped. start, end = end, start return self._start_end_range(batch_id, start, end)
def test_to_reverse_timestamp(self): """ to_reverse_timestamp() turns a vumi_date-formatted string into a reverse timestamp. """ self.assertEqual("FFAAE41F25", to_reverse_timestamp("2015-04-01 12:13:14")) self.assertEqual("FFAAE41F25", to_reverse_timestamp("2015-04-01 12:13:14.000000")) self.assertEqual("FFAAE41F25", to_reverse_timestamp("2015-04-01 12:13:14.999999")) self.assertEqual("FFAAE41F24", to_reverse_timestamp("2015-04-01 12:13:15")) self.assertEqual("F0F9025FA5", to_reverse_timestamp("4015-04-01 12:13:14"))
def test_add_ack_event_to_new_batch(self): """ When an existing event is added with a new batch identifier, it belongs to the new batch as well as batches it already belonged to. """ events = self.manager.proxy(Event) msg = self.msg_helper.make_outbound("apples") ack = self.msg_helper.make_ack(msg) yield self.backend.add_event(ack, batch_ids=["mybatch"]) yield self.backend.add_event(ack, batch_ids=["yourbatch"]) stored_event = yield events.load(ack["event_id"]) self.assertEqual(stored_event.event, ack) self.assertEqual(sorted(stored_event.batches.keys()), ["mybatch", "yourbatch"]) # Make sure we're writing the right indexes. timestamp = format_vumi_date(msg['timestamp']) reverse_ts = to_reverse_timestamp(timestamp) self.assertEqual( stored_event._riak_object.get_indexes(), set([ ("message_bin", ack["user_message_id"]), ("batches_bin", "mybatch"), ('batches_bin', "yourbatch"), ("message_with_status_bin", "%s$%s$%s" % (ack["user_message_id"], ack["timestamp"], "ack")), ("batches_with_statuses_reverse_bin", "%s$%s$%s" % ("mybatch", reverse_ts, "ack")), ("batches_with_statuses_reverse_bin", "%s$%s$%s" % ("yourbatch", reverse_ts, "ack")), ]))
def test_add_outbound_message_to_new_batch(self): """ When an existing outbound message is added with a new batch identifier, it belongs to the new batch as well as batches it already belonged to. """ outbound_messages = self.manager.proxy(OutboundMessage) msg = self.msg_helper.make_outbound("apples") yield self.backend.add_outbound_message(msg, batch_ids=["mybatch"]) yield self.backend.add_outbound_message(msg, batch_ids=["yourbatch"]) stored_msg = yield outbound_messages.load(msg["message_id"]) self.assertEqual(stored_msg.msg, msg) self.assertEqual(sorted(stored_msg.batches.keys()), ["mybatch", "yourbatch"]) # Make sure we're writing the right indexes. timestamp = format_vumi_date(msg['timestamp']) reverse_ts = to_reverse_timestamp(timestamp) self.assertEqual( stored_msg._riak_object.get_indexes(), set([ ('batches_bin', "mybatch"), ('batches_bin', "yourbatch"), ('batches_with_addresses_bin', "%s$%s$%s" % ("mybatch", timestamp, msg['to_addr'])), ('batches_with_addresses_bin', "%s$%s$%s" % ("yourbatch", timestamp, msg['to_addr'])), ('batches_with_addresses_reverse_bin', "%s$%s$%s" % ("mybatch", reverse_ts, msg['to_addr'])), ('batches_with_addresses_reverse_bin', "%s$%s$%s" % ("yourbatch", reverse_ts, msg['to_addr'])), ]))
def test_add_inbound_message_with_multiple_batch_ids(self): """ When an inbound message is added with multiple batch identifiers, it belongs to all the specified batches. """ inbound_messages = self.manager.proxy(InboundMessage) msg = self.msg_helper.make_inbound("apples") yield self.backend.add_inbound_message( msg, batch_ids=["mybatch", "yourbatch"]) stored_msg = yield inbound_messages.load(msg["message_id"]) self.assertEqual(stored_msg.msg, msg) self.assertEqual(sorted(stored_msg.batches.keys()), ["mybatch", "yourbatch"]) # Make sure we're writing the right indexes. timestamp = format_vumi_date(msg['timestamp']) reverse_ts = to_reverse_timestamp(timestamp) self.assertEqual( stored_msg._riak_object.get_indexes(), set([ ('batches_bin', "mybatch"), ('batches_bin', "yourbatch"), ('batches_with_addresses_bin', "%s$%s$%s" % ("mybatch", timestamp, msg['from_addr'])), ('batches_with_addresses_bin', "%s$%s$%s" % ("yourbatch", timestamp, msg['from_addr'])), ('batches_with_addresses_reverse_bin', "%s$%s$%s" % ("mybatch", reverse_ts, msg['from_addr'])), ('batches_with_addresses_reverse_bin', "%s$%s$%s" % ("yourbatch", reverse_ts, msg['from_addr'])), ]))
def test_add_outbound_message_with_batch_id(self): """ When an outbound message is added with a batch identifier, that batch identifier is stored with it and indexed. """ outbound_messages = self.manager.proxy(OutboundMessage) msg = self.msg_helper.make_outbound("apples") yield self.backend.add_outbound_message(msg, batch_ids=["mybatch"]) stored_msg = yield outbound_messages.load(msg["message_id"]) self.assertEqual(stored_msg.msg, msg) self.assertEqual(stored_msg.batches.keys(), ["mybatch"]) # Make sure we're writing the right indexes. timestamp = format_vumi_date(msg['timestamp']) reverse_ts = to_reverse_timestamp(timestamp) self.assertEqual( stored_msg._riak_object.get_indexes(), set([ ('batches_bin', "mybatch"), ('batches_with_addresses_bin', "%s$%s$%s" % ("mybatch", timestamp, msg['to_addr'])), ('batches_with_addresses_reverse_bin', "%s$%s$%s" % ("mybatch", reverse_ts, msg['to_addr'])), ]))