def test_UserActivityWorker(self) -> None: fake_client = self.FakeClient() user = self.example_user("hamlet") UserActivity.objects.filter( user_profile=user.id, client=get_client("ios"), ).delete() data = dict( user_profile_id=user.id, client_id=get_client("ios").id, time=time.time(), query="send_message", ) fake_client.enqueue("user_activity", data) # The block below adds an event using the old format, # having the client name instead of id, to test the queue # worker handles it correctly. That compatibility code can # be deleted in a later release, and this test should then be cleaned up. data_old_format = dict( user_profile_id=user.id, client="ios", time=time.time(), query="send_message", ) fake_client.enqueue("user_activity", data_old_format) with simulated_queue_client(lambda: fake_client): worker = queue_processors.UserActivityWorker() worker.setup() worker.start() activity_records = UserActivity.objects.filter( user_profile=user.id, client=get_client("ios"), ) self.assert_length(activity_records, 1) self.assertEqual(activity_records[0].count, 2) # Now process the event a second time and confirm count goes # up. Ideally, we'd use an event with a slightly newer # time, but it's not really important. fake_client.enqueue("user_activity", data) with simulated_queue_client(lambda: fake_client): worker = queue_processors.UserActivityWorker() worker.setup() worker.start() activity_records = UserActivity.objects.filter( user_profile=user.id, client=get_client("ios"), ) self.assert_length(activity_records, 1) self.assertEqual(activity_records[0].count, 3)
def test_UserActivityWorker(self) -> None: fake_client = self.FakeClient() user = self.example_user('hamlet') UserActivity.objects.filter(user_profile=user.id, client=get_client('ios')).delete() data = dict(user_profile_id=user.id, client_id=get_client('ios').id, time=time.time(), query='send_message') fake_client.queue.append(('user_activity', data)) # The block below adds an event using the old format, # having the client name instead of id, to test the queue # worker handles it correctly. That compatibility code can # be deleted in a later release, and this test should then be cleaned up. data_old_format = dict(user_profile_id=user.id, client='ios', time=time.time(), query='send_message') fake_client.queue.append(('user_activity', data_old_format)) with loopworker_sleep_mock: with simulated_queue_client(lambda: fake_client): worker = queue_processors.UserActivityWorker() worker.setup() try: worker.start() except AbortLoop: pass activity_records = UserActivity.objects.filter( user_profile=user.id, client=get_client('ios')) self.assertEqual(len(activity_records), 1) self.assertEqual(activity_records[0].count, 2) # Now process the event a second time and confirm count goes # up. Ideally, we'd use an event with a slightly newer # time, but it's not really important. fake_client.queue.append(('user_activity', data)) with loopworker_sleep_mock: with simulated_queue_client(lambda: fake_client): worker = queue_processors.UserActivityWorker() worker.setup() try: worker.start() except AbortLoop: pass activity_records = UserActivity.objects.filter( user_profile=user.id, client=get_client('ios')) self.assertEqual(len(activity_records), 1) self.assertEqual(activity_records[0].count, 3)
def test_UserActivityWorker(self) -> None: fake_client = self.FakeClient() user = self.example_user('hamlet') UserActivity.objects.filter( user_profile = user.id, client = get_client('ios') ).delete() data = dict( user_profile_id = user.id, client = 'ios', time = time.time(), query = 'send_message' ) fake_client.queue.append(('user_activity', data)) with loopworker_sleep_mock: with simulated_queue_client(lambda: fake_client): worker = queue_processors.UserActivityWorker() worker.setup() try: worker.start() except AbortLoop: pass activity_records = UserActivity.objects.filter( user_profile = user.id, client = get_client('ios') ) self.assertTrue(len(activity_records), 1) self.assertTrue(activity_records[0].count, 1) # Now process the event a second time and confirm count goes # up to 2. Ideally, we'd use an event with a slightly never # time, but it's not really important. fake_client.queue.append(('user_activity', data)) with loopworker_sleep_mock: with simulated_queue_client(lambda: fake_client): worker = queue_processors.UserActivityWorker() worker.setup() try: worker.start() except AbortLoop: pass activity_records = UserActivity.objects.filter( user_profile = user.id, client = get_client('ios') ) self.assertTrue(len(activity_records), 1) self.assertTrue(activity_records[0].count, 2)
def test_UserActivityWorker(self) -> None: fake_client = self.FakeClient() user = self.example_user('hamlet') UserActivity.objects.filter( user_profile = user.id, client = get_client('ios') ).delete() data = dict( user_profile_id = user.id, client = 'ios', time = time.time(), query = 'send_message' ) fake_client.queue.append(('user_activity', data)) with simulated_queue_client(lambda: fake_client): worker = queue_processors.UserActivityWorker() worker.setup() worker.start() activity_records = UserActivity.objects.filter( user_profile = user.id, client = get_client('ios') ) self.assertTrue(len(activity_records), 1) self.assertTrue(activity_records[0].count, 1)