def get_placebo_product(): """根据策略获取本次活动的体验金产品.""" product_ids = PlaceboProduct.get_ids_by_strategy( strategies.strategy_2016_spring.id_) if len(product_ids) != 1: raise RuntimeError('placebo products are dirty') product_id = first(product_ids) return PlaceboProduct.get(product_id)
def test_get_by_strategy(self): product = self._create_placebo_product() result_ids = PlaceboProduct.get_ids_by_strategy(self.odd_user_id.id_) assert result_ids == [product.id_] another_product = self._create_placebo_product() result_ids = PlaceboProduct.get_ids_by_strategy(self.odd_user_id.id_) assert result_ids == [another_product.id_, product.id_] another_product = self._create_placebo_product() result_ids = PlaceboProduct.get_ids_by_strategy(self.odd_user_id.id_)
def test_strategy(self): product = self._create_placebo_product() assert product.strategy.target('10001') assert not product.strategy.target('10002') assert product.strategy.target('10003') ids = [product.id_] assert PlaceboProduct.get_multi(ids) == [product] assert PlaceboProduct.get_multi(ids, user_id='2') == [] assert PlaceboProduct.get_multi(ids, user_id='3') == [product] assert PlaceboProduct.get_multi(ids, user_id='4') == []
def test_create_and_get(self): product = self._create_placebo_product() assert product.id_ > 0 assert PlaceboProduct.get(product.id_) == product assert product.min_amount == decimal.Decimal('100') assert product.max_amount == decimal.Decimal('200') assert PlaceboProduct.get_all_ids() == [product.id_] product2 = self._create_placebo_product() assert PlaceboProduct.get_all_ids() == [product2.id_, product.id_]
def main(): product = PlaceboProduct.add(strategy=strategies.strategy_2016_spring, min_amount=decimal.Decimal(8888), max_amount=decimal.Decimal(8888), start_sell_date=datetime.date.today(), end_sell_date=datetime.date.today() + datetime.timedelta(days=3), frozen_days=7, annual_rate=decimal.Decimal('6.6')) bcolors.run('success: %s' % product, key='placebo')
def _create_placebo_product( self, frozen_days=18, annual_rate=decimal.Decimal('8.8'), strategy=None): product = PlaceboProduct.add( strategy=strategy or self.odd_user_id, min_amount=decimal.Decimal('100.00'), max_amount=decimal.Decimal('200.00'), start_sell_date=datetime.date.today() - datetime.timedelta(days=1), end_sell_date=datetime.date.today() + datetime.timedelta(days=1), frozen_days=frozen_days, annual_rate=annual_rate) return product
def test_add_order_failed_by_outdated_product(self): outdated_product = PlaceboProduct.add( strategy=self.check_white_list, min_amount=decimal.Decimal('100.00'), max_amount=decimal.Decimal('100.00'), start_sell_date=datetime.date.today() - datetime.timedelta(days=2), end_sell_date=datetime.date.today() - datetime.timedelta(days=1), frozen_days=10, annual_rate=decimal.Decimal('12.2')) with raises(OffShelfError): PlaceboOrder.add(user_id=self.user.id_, product_id=outdated_product.id_, bankcard_id=self.bankcard.id_, amount=decimal.Decimal('100'))
def setUp(self): super(PlaceboOrderTestCase, self).setUp() self._original_strategy_storage = dict(PlaceboProduct.strategy_storage) @PlaceboProduct.strategy_storage.register(id_=1, name=u'白名单') def check_white_list(user_id): return user_id in self.white_list self.white_list = set() self.check_white_list = check_white_list self.user = self.add_account(mobile='13800138000') self.white_list.add(self.user.id_) self.product = PlaceboProduct.add( strategy=check_white_list, min_amount=decimal.Decimal('100.00'), max_amount=decimal.Decimal('100.00'), start_sell_date=datetime.date.today() - datetime.timedelta(days=1), end_sell_date=datetime.date.today() + datetime.timedelta(days=1), frozen_days=10, annual_rate=decimal.Decimal('12.2')) self.bankcard = self.add_bankcard(self.user.id_) self.identity = self.add_identity(self.user.id_, u'张无忌', u'44011320141005001X')
def test_get_nothing(self): assert PlaceboProduct.get(1) is None