def test_is_near_now(self): """ Test is_near_now for now """ now = datetime.datetime.now(tz=pytz.UTC) assert is_near_now(now) is True later = now + datetime.timedelta(0, 6) assert is_near_now(later) is False earlier = now - datetime.timedelta(0, 6) assert is_near_now(earlier) is False
def test_batch_update(mocker, db): # pylint: disable=unused-argument """ batch_update_user_data should create a group of tasks operating on chunks of users to refresh their caches """ users = SocialUserFactory.create_batch(25) calc_mock = mocker.patch('dashboard.tasks.calculate_users_to_refresh_in_bulk', autospec=True, return_value=[ user.id for user in users ]) lock_mock_init = mocker.patch('dashboard.tasks.Lock', autospec=True) lock_mock = lock_mock_init.return_value token = b'token' lock_mock.token = token lock_mock.acquire.return_value = True refresh_mock = mocker.patch('dashboard.tasks.refresh_user_data', autospec=True) release_mock = mocker.patch('dashboard.tasks.release_lock', autospec=True) batch_update_user_data() assert lock_mock_init.call_args[0][0] == LOCK_ID assert is_near_now(lock_mock_init.call_args[0][1] - timedelta(hours=5)) calc_mock.assert_called_once_with() lock_mock.acquire.assert_called_once_with() assert refresh_mock.call_count == len(users) for user in users: refresh_mock.assert_any_call(user.id) release_mock.assert_called_once_with(LOCK_ID, token)
def test_income_validation(self, original_income, original_currency, income_threshold, auto_approved): """ Tests FinancialAidRequestView post endpoint """ CountryIncomeThreshold.objects.filter(country_code=self.profile.country).update( income_threshold=income_threshold ) data = { "original_income": original_income, "original_currency": original_currency, "program_id": self.program.id, } assert FinancialAid.objects.exclude(status=FinancialAidStatus.RESET).count() == 0 assert FinancialAidAudit.objects.count() == 0 self.make_http_request(self.client.post, self.request_url, status.HTTP_201_CREATED, data=data) assert FinancialAid.objects.exclude(status=FinancialAidStatus.RESET).count() == 1 assert FinancialAidAudit.objects.count() == 1 financial_aid = FinancialAid.objects.exclude(status=FinancialAidStatus.RESET).first() income_usd = determine_income_usd(original_income, original_currency) assert financial_aid.tier_program == determine_tier_program(self.program, income_usd) if not auto_approved: assert financial_aid.status == FinancialAidStatus.PENDING_DOCS else: assert financial_aid.status == FinancialAidStatus.AUTO_APPROVED self.assertAlmostEqual(financial_aid.income_usd, income_usd) assert financial_aid.user == self.profile.user self.assertAlmostEqual(financial_aid.original_income, original_income) assert financial_aid.original_currency == original_currency assert financial_aid.country_of_income == self.profile.country assert financial_aid.country_of_residence == self.profile.country assert is_near_now(financial_aid.date_exchange_rate)
def test_batch_update(mocker, db): # pylint: disable=unused-argument """ batch_update_user_data should create a group of tasks operating on chunks of users to refresh their caches """ users = SocialUserFactory.create_batch(25) calc_mock = mocker.patch( 'dashboard.tasks.calculate_users_to_refresh_in_bulk', autospec=True, return_value=[user.id for user in users]) lock_mock_init = mocker.patch('dashboard.tasks.Lock', autospec=True) lock_mock = lock_mock_init.return_value token = b'token' lock_mock.token = token lock_mock.acquire.return_value = True refresh_mock = mocker.patch('dashboard.tasks.refresh_user_data', autospec=True) release_mock = mocker.patch('dashboard.tasks.release_lock', autospec=True) batch_update_user_data() assert lock_mock_init.call_args[0][0] == LOCK_ID assert is_near_now(lock_mock_init.call_args[0][1] - timedelta(hours=5)) calc_mock.assert_called_once_with() lock_mock.acquire.assert_called_once_with() assert refresh_mock.call_count == len(users) for user in users: refresh_mock.assert_any_call(user.id) release_mock.assert_called_once_with(LOCK_ID, token)
def test_nothing_to_do(mocker): """ If there's nothing to update batch_update_user_date should only acquire and release the lock """ calc_mock = mocker.patch( 'dashboard.tasks.calculate_users_to_refresh_in_bulk', autospec=True, return_value=[]) lock_mock_init = mocker.patch('dashboard.tasks.Lock', autospec=True) lock_mock = lock_mock_init.return_value token = b'token' lock_mock.token = token lock_mock.acquire.return_value = True refresh_mock = mocker.patch('dashboard.tasks.refresh_user_data', autospec=True) release_mock = mocker.patch('dashboard.tasks.release_lock', autospec=True) batch_update_user_data() assert lock_mock_init.call_args[0][0] == LOCK_ID assert is_near_now(lock_mock_init.call_args[0][1] - timedelta(hours=5)) calc_mock.assert_called_once_with() lock_mock.acquire.assert_called_once_with() assert refresh_mock.called is False assert release_mock.call_count == 1 release_mock.assert_called_once_with(LOCK_ID, token)
def test_sync_channel_memberships_lock_name(settings, mocker): """sync_channel_memberships should use the proper lock name and expiration""" lock_mock = mocker.patch('discussions.tasks.Lock', autospec=True) tasks.sync_channel_memberships.delay() assert lock_mock.call_count == 1 assert lock_mock.call_args[0][0] == tasks.SYNC_MEMBERSHIPS_LOCK_NAME assert is_near_now(lock_mock.call_args[0][1] - timedelta( seconds=tasks.SYNC_MEMBERSHIPS_LOCK_TTL_SECONDS))
def test_skipped_financialaid_object_created(self): """ Tests that the user can create a skipped FinancialAid if it doesn't already exist """ assert FinancialAidAudit.objects.count() == 0 assert FinancialAid.objects.exclude(status=FinancialAidStatus.RESET).count() == 0 self.make_http_request(self.client.patch, self.skip_url, status.HTTP_200_OK) assert FinancialAidAudit.objects.count() == 1 assert FinancialAid.objects.exclude(status=FinancialAidStatus.RESET).count() == 1 financial_aid = FinancialAid.objects.exclude(status=FinancialAidStatus.RESET).first() assert financial_aid.tier_program == get_no_discount_tier_program(self.program) assert financial_aid.user == self.profile.user assert financial_aid.status == FinancialAidStatus.SKIPPED assert is_near_now(financial_aid.date_exchange_rate) assert financial_aid.country_of_income == self.profile.country assert financial_aid.country_of_residence == self.profile.country
def test_failed_to_acquire(mocker): """ If the lock is held there should be nothing else done """ calc_mock = mocker.patch('dashboard.tasks.calculate_users_to_refresh_in_bulk', autospec=True, return_value=[]) lock_mock_init = mocker.patch('dashboard.tasks.Lock', autospec=True) lock_mock = lock_mock_init.return_value lock_mock.acquire.return_value = False refresh_mock = mocker.patch('dashboard.tasks.refresh_user_data', autospec=True) release_mock = mocker.patch('dashboard.tasks.release_lock', autospec=True) batch_update_user_data() assert lock_mock_init.call_args[0][0] == LOCK_ID assert is_near_now(lock_mock_init.call_args[0][1] - timedelta(hours=5)) assert calc_mock.called is False lock_mock.acquire.assert_called_once_with() assert refresh_mock.called is False assert release_mock.called is False
def test_nothing_to_do(mocker): """ If there's nothing to update batch_update_user_date should only acquire and release the lock """ calc_mock = mocker.patch('dashboard.tasks.calculate_users_to_refresh_in_bulk', autospec=True, return_value=[]) lock_mock_init = mocker.patch('dashboard.tasks.Lock', autospec=True) lock_mock = lock_mock_init.return_value token = b'token' lock_mock.token = token lock_mock.acquire.return_value = True refresh_mock = mocker.patch('dashboard.tasks.refresh_user_data', autospec=True) release_mock = mocker.patch('dashboard.tasks.release_lock', autospec=True) batch_update_user_data() assert lock_mock_init.call_args[0][0] == LOCK_ID assert is_near_now(lock_mock_init.call_args[0][1] - timedelta(hours=5)) calc_mock.assert_called_once_with() lock_mock.acquire.assert_called_once_with() assert refresh_mock.called is False assert release_mock.call_count == 1 release_mock.assert_called_once_with(LOCK_ID, token)
def test_failed_to_acquire(mocker): """ If the lock is held there should be nothing else done """ calc_mock = mocker.patch( 'dashboard.tasks.calculate_users_to_refresh_in_bulk', autospec=True, return_value=[]) lock_mock_init = mocker.patch('dashboard.tasks.Lock', autospec=True) lock_mock = lock_mock_init.return_value lock_mock.acquire.return_value = False refresh_mock = mocker.patch('dashboard.tasks.refresh_user_data', autospec=True) release_mock = mocker.patch('dashboard.tasks.release_lock', autospec=True) batch_update_user_data() assert lock_mock_init.call_args[0][0] == LOCK_ID assert is_near_now(lock_mock_init.call_args[0][1] - timedelta(hours=5)) assert calc_mock.called is False lock_mock.acquire.assert_called_once_with() assert refresh_mock.called is False assert release_mock.called is False
def test_now_in_utc(): """now_in_utc() should return the current time set to the UTC time zone""" now = now_in_utc() assert is_near_now(now) assert now.tzinfo == pytz.UTC