def test_items(self): user = '******' body = '<elem>data0</elem>' expected = self._expected(user, body, items=2) response = FileRestoreResponse(user, True) response.append(body) response.finalize() self.assertEqual(expected, str(response))
def test_subsequent_syncs_when_job_complete(self): # First sync, return a timout. Ensure that the async_task_id gets set cache_id = restore_cache_key(ASYNC_RESTORE_CACHE_KEY_PREFIX, self.user.user_id) with mock.patch('casexml.apps.phone.restore.get_async_restore_payload' ) as task: delay = mock.MagicMock() delay.id = 'random_task_id' delay.get = mock.MagicMock( side_effect=TimeoutError()) # task not finished task.delay.return_value = delay restore_config = self._restore_config(async=True) initial_payload = restore_config.get_payload() self.assertIsNotNone(restore_config.cache.get(cache_id)) self.assertIsInstance(initial_payload, AsyncRestoreResponse) # new synclog should not have been created self.assertIsNone(restore_config.restore_state.current_sync_log) # Second sync, don't timeout (can't use AsyncResult in tests, so mock # the return value). file_restore_response = mock.MagicMock( return_value=FileRestoreResponse()) with mock.patch.object(AsyncResult, 'get', file_restore_response) as get_result: with mock.patch.object(AsyncResult, 'status', ASYNC_RESTORE_SENT): subsequent_restore = self._restore_config(async=True) self.assertIsNotNone(restore_config.cache.get(cache_id)) subsequent_restore.get_payload() # if the task actually ran, the cache should now not have the task id, # however, the task is not run in this test. See `test_completed_task_deletes_cache` # self.assertIsNone(restore_config.cache.get(cache_id)) get_result.assert_called_with(timeout=1)
def test_items(self): user = u'user1' body = b'<elem>data0</elem>' expected = self._expected(user, body, items=2) with FileRestoreResponse(user, True) as response: response.append(body) self.assertEqual(expected, str(response))
def test_no_items(self): user = '******' body = '<elem>data0</elem>' expected = self._expected(user, body, items=None) with FileRestoreResponse(user, False) as response: response.append(body) response.finalize() self.assertEqual(expected, str(response))
def test_add(self): user = '******' body1 = '<elem>data0</elem>' body2 = '<elem>data1</elem>' expected = self._expected(user, body1 + body2, items=3) response1 = FileRestoreResponse(user, True) response1.append(body1) response2 = FileRestoreResponse(user, True) response2.append(body2) added = response1 + response2 added.finalize() self.assertEqual(expected, str(added))