def record_event(self, consumer_id, event_type, event_details=None): """ @ivar consumer_id: identifies the consumer @type id: str @param type: event type @type type: str @param details: event details @type details: dict @raises MissingResource: if the given consumer does not exist @raises InvalidValue: if any of the fields is unacceptable """ # Check that consumer exists for all except registration event existing_consumer = Consumer.get_collection().find_one( {'id': consumer_id}) if not existing_consumer and event_type != TYPE_CONSUMER_UNREGISTERED: raise MissingResource(consumer=consumer_id) invalid_values = [] if event_type not in TYPES: invalid_values.append('event_type') if event_details is not None and not isinstance(event_details, dict): invalid_values.append('event_details') if invalid_values: raise InvalidValue(invalid_values) event = ConsumerHistoryEvent(consumer_id, self._originator(), event_type, event_details) ConsumerHistoryEvent.get_collection().save(event)
def test_leave_unexpired_entries(self, getfloat): chec = ConsumerHistoryEvent.get_collection() event = ConsumerHistoryEvent('consumer', 'originator', 'consumer_registered', {}) chec.insert(event) self.assertTrue(chec.find({'_id': event['_id']}).count() == 1) # Let's mock getfloat to pretend that the user said they want to reap things that are a day # old. This means that the event should not get reaped. getfloat.return_value = 1.0 reaper.reap_expired_documents() # The event should still exist self.assertTrue(chec.find({'_id': event['_id']}).count() == 1)
def test_remove_expired_entries(self, getfloat): chec = ConsumerHistoryEvent.get_collection() event = ConsumerHistoryEvent('consumer', 'originator', 'consumer_registered', {}) chec.insert(event) self.assertTrue(chec.find({'_id': event['_id']}).count() == 1) # Let's mock getfloat to pretend that the user said they want to reap things from the # future, which should make the event we just created look old enough to delete getfloat.return_value = -1.0 reaper.reap_expired_documents() # The event should no longer exist self.assertTrue(chec.find({'_id': event['_id']}).count() == 0)
def test_remove_expired_entries(self): event = ConsumerHistoryEvent('consumer', 'originator', 'consumer_registered', {}) self.collection.insert(event, safe=True) self.assertTrue( self.collection.find({ '_id': event['_id'] }).count() == 1) expired_oid = self.reaper._create_expired_object_id( timedelta(seconds=-1)) self.reaper._remove_expired_entries(self.collection, expired_oid) self.assertTrue( self.collection.find({ '_id': event['_id'] }).count() == 0)