def setUp(self): super(IndexRequestsTests, self).setUp() self.good_1 = goods_types.test_hero_good.create_good('good-#-1') self.good_2 = goods_types.test_hero_good.create_good('good-#-2') self.good_3 = goods_types.test_hero_good.create_good('good-#-3') self.good_4 = goods_types.test_hero_good.create_good('good-#-4') self.price_1 = 1 self.price_2 = 2 self.price_3 = 3 self.price_4 = 4 self.lot_1 = logic.reserve_lot(self.account_1.id, self.good_1, price=self.price_1) self.lot_2 = logic.reserve_lot(self.account_1.id, self.good_2, price=self.price_2) self.lot_3 = logic.reserve_lot(self.account_1.id, self.good_3, price=self.price_3) self.lot_4 = logic.reserve_lot(self.account_2.id, self.good_4, price=self.price_4) self.lot_1.state = relations.LOT_STATE.ACTIVE self.lot_2.state = relations.LOT_STATE.ACTIVE self.lot_4.state = relations.LOT_STATE.ACTIVE logic.save_lot(self.lot_1) logic.save_lot(self.lot_2) logic.save_lot(self.lot_4) self.requested_url = url('market:')
def test_close_lots_by_timeout(self): lot_2 = logic.reserve_lot(self.account_1.id, self.good_2, price=2) lot_2.state = relations.LOT_STATE.ACTIVE logic.save_lot(lot_2) lot_3 = logic.reserve_lot(self.account_1.id, self.good_3, price=3) self.assertFalse(lot_3.state.is_ACTIVE) lot_4 = logic.reserve_lot(self.account_1.id, self.good_4, price=4) lot_4.state = relations.LOT_STATE.ACTIVE logic.save_lot(lot_4) models.Lot.objects.filter( id__in=[self.lot_1.id, lot_3.id, lot_4.id]).update( created_at=datetime.datetime.now() - datetime.timedelta(days=conf.settings.LOT_LIVE_TIME + 1)) with mock.patch( 'the_tale.finances.market.workers.market_manager.Worker.cmd_logic_task' ) as cmd_logic_task: with self.check_delta( postponed_tasks_models.PostponedTask.objects.count, 2): tasks = logic.close_lots_by_timeout() self.assertEqual(cmd_logic_task.call_count, 2) self.assertEqual( set((tasks[0].internal_logic.lot_id, tasks[1].internal_logic.lot_id)), set((self.lot_1.id, lot_4.id)))
def test_lot_exists(self): logic.reserve_lot(self.account_1.id, self.goods_1.get_good(self.good_1.uid), price=777) self.request_login(self.account_1.email) self.check_html_ok(self.request_ajax_html(self.requested_url), texts=[('pgf-error-lot_exists')])
def test_has_lot(self): logic.reserve_lot(self.account_1.id, self.goods_1.get_good(self.good_1_uid), price=777) self.assertEqual(self.task.process(self.main_task), POSTPONED_TASK_LOGIC_RESULT.ERROR) self.assertTrue(self.task.state.is_ALREADY_RESERVED) self.assertTrue(self.task.step.is_UNPROCESSED)
def test_has_lot__other_good(self): good_2 = goods_types.test_hero_good.create_good('good-2') self.goods_1.add_good(good_2) logic.save_goods(self.goods_1) logic.reserve_lot(self.account_1.id, self.goods_1.get_good('good-2'), price=777) self.assertEqual(self.task.process(self.main_task), POSTPONED_TASK_LOGIC_RESULT.CONTINUE) self.assertTrue(self.task.state.is_UNPROCESSED) self.assertTrue(self.task.step.is_RESERVED)
def test_lot_exists(self): logic.reserve_lot(self.account_1.id, self.goods_1.get_good(self.good_1.uid), price=777) self.request_login(self.account_1.email) with self.check_not_changed( postponed_tasks_models.PostponedTask.objects.count): self.check_ajax_error( self.post_ajax_json(self.requested_url, {'price': 100}), 'lot_exists')
def test_has_lot__other_account(self): account_2 = self.accounts_factory.create_account() good_2 = goods_types.test_hero_good.create_good(self.good_1_uid) goods_2 = logic.load_goods(account_2.id) goods_2.add_good(good_2) logic.save_goods(goods_2) logic.reserve_lot(account_2.id, goods_2.get_good(self.good_1_uid), price=777) self.assertEqual(self.task.process(self.main_task), POSTPONED_TASK_LOGIC_RESULT.CONTINUE) self.assertTrue(self.task.state.is_UNPROCESSED) self.assertTrue(self.task.step.is_RESERVED)
def setUp(self): super(TaskTests, self).setUp() create_test_map() goods_types.test_hero_good._clear() self.good_1_uid = 'good-1' self.account_1 = self.accounts_factory.create_account() self.goods_1 = logic.load_goods(self.account_1.id) self.good_1 = goods_types.test_hero_good.create_good(self.good_1_uid) self.goods_1.add_good(self.good_1) logic.save_goods(self.goods_1) self.logic_storage = logic_storage.LogicStorage() self.hero_1 = self.logic_storage.load_account_data(self.account_1) self.price = 666 self.lot_1 = logic.reserve_lot(self.account_1.id, self.good_1, price=self.price) self.lot_1.state = relations.LOT_STATE.ACTIVE logic.save_lot(self.lot_1) self.task = postponed_tasks.CloseLotByTimoutTask(lot_id=self.lot_1.id) self.main_task = mock.Mock(comment=None, id=777)
def test_reserve_lot(self): with self.check_delta(models.Lot.objects.count, 1): lot = logic.reserve_lot(account_id=self.account_1.id, good=self.good_2, price=777) lot_model = models.Lot.objects.latest() self.assertEqual(lot.id, lot_model.id) self.assertEqual(lot.type, lot_model.type) self.assertEqual(lot.seller_id, lot_model.seller_id) self.assertEqual(lot.buyer_id, lot_model.buyer_id) self.assertEqual(lot.name, lot_model.name) self.assertEqual(lot.state, lot_model.state) self.assertEqual(lot.price, lot_model.price) self.assertTrue(lot.closed_at-lot.created_at > datetime.timedelta(days=conf.settings.LOT_LIVE_TIME, seconds=-1)) self.assertEqual(lot.good.serialize(), s11n.from_json(lot_model.data)['good']) self.assertEqual(lot.type, self.good_2.type) self.assertEqual(lot.seller_id, self.account_1.id) self.assertEqual(lot.buyer_id, None) self.assertEqual(lot.name, self.good_2.name) self.assertTrue(lot.state.is_RESERVED) self.assertEqual(lot.price, 777) self.assertEqual(lot.commission, 54) self.assertTrue(lot.closed_at-lot.created_at > datetime.timedelta(days=conf.settings.LOT_LIVE_TIME, seconds=-1)) self.assertEqual(lot.good.serialize(), self.good_2.serialize())
def setUp(self): super(LogicTests, self).setUp() create_test_map() self.account_1 = self.accounts_factory.create_account() self.account_2 = self.accounts_factory.create_account() self.goods_1 = logic.load_goods(self.account_1.id) self.good_1 = goods_types.test_hero_good.create_good('good-1') self.good_2 = goods_types.test_hero_good.create_good('good-2') self.good_3 = goods_types.test_hero_good.create_good('good-3') self.good_4 = goods_types.test_hero_good.create_good('good-4') self.goods_1.add_good(self.good_1) self.goods_1.add_good(self.good_2) self.goods_1.add_good(self.good_3) self.goods_1.add_good(self.good_4) logic.save_goods(self.goods_1) self.price = 666 self.lot_1 = logic.reserve_lot(self.account_1.id, self.good_1, price=self.price) self.lot_1.state = relations.LOT_STATE.ACTIVE logic.save_lot(self.lot_1)
def test_close_lots_by_timeout(self): lot_2 = logic.reserve_lot(self.account_1.id, self.good_2, price=2) lot_2.state = relations.LOT_STATE.ACTIVE logic.save_lot(lot_2) lot_3 = logic.reserve_lot(self.account_1.id, self.good_3, price=3) self.assertFalse(lot_3.state.is_ACTIVE) lot_4 = logic.reserve_lot(self.account_1.id, self.good_4, price=4) lot_4.state = relations.LOT_STATE.ACTIVE logic.save_lot(lot_4) models.Lot.objects.filter(id__in=[self.lot_1.id, lot_3.id, lot_4.id]).update(created_at=datetime.datetime.now()-datetime.timedelta(days=conf.settings.LOT_LIVE_TIME+1)) with mock.patch('the_tale.finances.market.workers.market_manager.Worker.cmd_logic_task') as cmd_logic_task: with self.check_delta(postponed_tasks_models.PostponedTask.objects.count, 2): tasks = logic.close_lots_by_timeout() self.assertEqual(cmd_logic_task.call_count, 2) self.assertEqual(set((tasks[0].internal_logic.lot_id, tasks[1].internal_logic.lot_id)), set((self.lot_1.id, lot_4.id)))
def setUp(self): super(LotTests, self).setUp() create_test_map() self.account_1 = self.accounts_factory.create_account() self.good_1 = goods_types.test_hero_good.create_good('good-1') self.price_1 = 666 self.lot_1 = logic.reserve_lot(self.account_1.id, self.good_1, price=self.price_1)
def setUp(self): super(TaskTests, self).setUp() create_test_map() self.good_1_uid = 'good-1' self.account_1 = self.accounts_factory.create_account() self.goods_1 = logic.load_goods(self.account_1.id) self.account_2 = self.accounts_factory.create_account() self.goods_2 = logic.load_goods(self.account_2.id) self.good_1 = goods_types.test_hero_good.create_good(self.good_1_uid) self.goods_1.add_good(self.good_1) logic.save_goods(self.goods_1) self.logic_storage = logic_storage.LogicStorage() self.hero_1 = self.logic_storage.load_account_data(self.account_1) self.hero_2 = self.logic_storage.load_account_data(self.account_2) self.price = 666 self.lot_1 = logic.reserve_lot(self.account_1.id, self.good_1, price=self.price) self.lot_1.state = relations.LOT_STATE.ACTIVE logic.save_lot(self.lot_1) self.invoice = bank_prototypes.InvoicePrototype.create( recipient_type=bank_relations.ENTITY_TYPE.GAME_ACCOUNT, recipient_id=self.account_1.id, sender_type=bank_relations.ENTITY_TYPE.GAME_ACCOUNT, sender_id=self.account_2.id, currency=bank_relations.CURRENCY_TYPE.PREMIUM, amount=self.price, description_for_sender='transaction-description-for_sender', description_for_recipient='transaction-description-for-recipient', operation_uid='transaction-operation-ui') self.transaction = bank_transaction.Transaction(self.invoice.id) self.task = postponed_tasks.BuyLotTask(seller_id=self.account_1.id, buyer_id=self.account_2.id, lot_id=self.lot_1.id, transaction=self.transaction) self.main_task = mock.Mock(comment=None, id=777)
def setUp(self): super(PurchaseRequestsTests, self).setUp() self.good_1 = goods_types.test_hero_good.create_good('good-1') self.goods_1 = logic.load_goods(self.account_1.id) self.goods_1.add_good(self.good_1) logic.save_goods(self.goods_1) self.lot_1 = logic.reserve_lot(self.account_1.id, self.goods_1.get_good(self.good_1.uid), price=777) self.lot_1.state = relations.LOT_STATE.ACTIVE logic.save_lot(self.lot_1) self.requested_url = url('market:purchase', self.lot_1.id)
def setUp(self): super(TaskTests, self).setUp() create_test_map() self.good_1_uid = 'good-1' self.account_1 = self.accounts_factory.create_account() self.goods_1 = logic.load_goods(self.account_1.id) self.account_2 = self.accounts_factory.create_account() self.goods_2 = logic.load_goods(self.account_2.id) self.good_1 = goods_types.test_hero_good.create_good(self.good_1_uid) self.goods_1.add_good(self.good_1) logic.save_goods(self.goods_1) self.logic_storage = logic_storage.LogicStorage() self.hero_1 = self.logic_storage.load_account_data(self.account_1) self.hero_2 = self.logic_storage.load_account_data(self.account_2) self.price = 666 self.lot_1 = logic.reserve_lot(self.account_1.id, self.good_1, price=self.price) self.lot_1.state = relations.LOT_STATE.ACTIVE logic.save_lot(self.lot_1) self.invoice = bank_prototypes.InvoicePrototype.create(recipient_type=bank_relations.ENTITY_TYPE.GAME_ACCOUNT, recipient_id=self.account_1.id, sender_type=bank_relations.ENTITY_TYPE.GAME_ACCOUNT, sender_id=self.account_2.id, currency=bank_relations.CURRENCY_TYPE.PREMIUM, amount=self.price, description_for_sender='transaction-description-for_sender', description_for_recipient='transaction-description-for-recipient', operation_uid='transaction-operation-ui') self.transaction = bank_transaction.Transaction(self.invoice.id) self.task = postponed_tasks.BuyLotTask(seller_id=self.account_1.id, buyer_id=self.account_2.id, lot_id=self.lot_1.id, transaction=self.transaction) self.main_task = mock.Mock(comment=None, id=777)
def process(self, main_task, storage=None): # pylint: disable=R0911 good_type = goods_types.get_type(self.good_type) if self.step.is_UNPROCESSED: account = account_prototypes.AccountPrototype.get_by_id(self.account_id) if account.is_ban_game: main_task.comment = 'account is banned' self.state = self.STATE.BANNED return POSTPONED_TASK_LOGIC_RESULT.ERROR goods = logic.load_goods(self.account_id) if not goods.has_good(self.good_uid): main_task.comment = 'account has no good %s' % self.good_uid self.state = self.STATE.NO_GOOD return POSTPONED_TASK_LOGIC_RESULT.ERROR if logic.has_lot(self.account_id, self.good_uid): main_task.comment = 'account %d has lot for <%s> %s' % (self.account_id, self.good_type, self.good_uid) self.state = self.STATE.ALREADY_RESERVED return POSTPONED_TASK_LOGIC_RESULT.ERROR lot = logic.reserve_lot(self.account_id, goods.get_good(self.good_uid), price=self.price) self.lot_id = lot.id main_task.extend_postsave_actions((lambda: environment.workers.supervisor.cmd_logic_task(self.account_id, main_task.id),)) self.step = self.STEP.RESERVED return POSTPONED_TASK_LOGIC_RESULT.CONTINUE if self.step.is_RESERVED: hero = storage.accounts_to_heroes[self.account_id] if not good_type.has_good(hero, self.good_uid): main_task.comment = 'hero has no good %s' % self.good_uid self.state = self.STATE.NO_GOOD self.step = self.STEP.ROLLBACK main_task.extend_postsave_actions((lambda: environment.workers.market_manager.cmd_logic_task(self.account_id, main_task.id),)) return POSTPONED_TASK_LOGIC_RESULT.CONTINUE good_type.extract_good(hero, self.good_uid) storage.save_bundle_data(hero.actions.current_action.bundle_id) main_task.extend_postsave_actions((lambda: environment.workers.market_manager.cmd_logic_task(self.account_id, main_task.id),)) self.step = self.STEP.GOTTEN return POSTPONED_TASK_LOGIC_RESULT.CONTINUE if self.step.is_ROLLBACK: goods = logic.load_goods(self.account_id) lot = logic.load_lot(self.lot_id) lot.state = relations.LOT_STATE.CLOSED_BY_ERROR logic.save_lot(lot) self.step = self.STEP.ROLLBACKED self.state = self.STATE.PROCESSED return POSTPONED_TASK_LOGIC_RESULT.SUCCESS if self.step.is_GOTTEN: goods = logic.load_goods(self.account_id) lot = logic.load_lot(self.lot_id) lot.state = relations.LOT_STATE.ACTIVE logic.save_lot(lot) goods.remove_good(self.good_uid) logic.save_goods(goods) self.step = self.STEP.ACTIVATED self.state = self.STATE.PROCESSED return POSTPONED_TASK_LOGIC_RESULT.SUCCESS
def test_lot_exists(self): logic.reserve_lot(self.account_1.id, self.goods_1.get_good(self.good_1.uid), price=777) self.request_login(self.account_1.email) with self.check_not_changed(postponed_tasks_models.PostponedTask.objects.count): self.check_ajax_error(self.post_ajax_json(self.requested_url, {'price': 100}), 'lot_exists')
def process(self, main_task, storage=None): # pylint: disable=R0911 good_type = goods_types.get_type(self.good_type) if self.step.is_UNPROCESSED: account = account_prototypes.AccountPrototype.get_by_id( self.account_id) if account.is_ban_game: main_task.comment = 'account is banned' self.state = self.STATE.BANNED return POSTPONED_TASK_LOGIC_RESULT.ERROR goods = logic.load_goods(self.account_id) if not goods.has_good(self.good_uid): main_task.comment = 'account has no good %s' % self.good_uid self.state = self.STATE.NO_GOOD return POSTPONED_TASK_LOGIC_RESULT.ERROR if logic.has_lot(self.account_id, self.good_uid): main_task.comment = 'account %d has lot for <%s> %s' % ( self.account_id, self.good_type, self.good_uid) self.state = self.STATE.ALREADY_RESERVED return POSTPONED_TASK_LOGIC_RESULT.ERROR lot = logic.reserve_lot(self.account_id, goods.get_good(self.good_uid), price=self.price) self.lot_id = lot.id main_task.extend_postsave_actions( (lambda: environment.workers.supervisor.cmd_logic_task( self.account_id, main_task.id), )) self.step = self.STEP.RESERVED return POSTPONED_TASK_LOGIC_RESULT.CONTINUE if self.step.is_RESERVED: hero = storage.accounts_to_heroes[self.account_id] if not good_type.has_good(hero, self.good_uid): main_task.comment = 'hero has no good %s' % self.good_uid self.state = self.STATE.NO_GOOD self.step = self.STEP.ROLLBACK main_task.extend_postsave_actions( (lambda: environment.workers.market_manager.cmd_logic_task( self.account_id, main_task.id), )) return POSTPONED_TASK_LOGIC_RESULT.CONTINUE good_type.extract_good(hero, self.good_uid) storage.save_bundle_data(hero.actions.current_action.bundle_id) main_task.extend_postsave_actions( (lambda: environment.workers.market_manager.cmd_logic_task( self.account_id, main_task.id), )) self.step = self.STEP.GOTTEN return POSTPONED_TASK_LOGIC_RESULT.CONTINUE if self.step.is_ROLLBACK: goods = logic.load_goods(self.account_id) lot = logic.load_lot(self.lot_id) lot.state = relations.LOT_STATE.CLOSED_BY_ERROR logic.save_lot(lot) self.step = self.STEP.ROLLBACKED self.state = self.STATE.PROCESSED return POSTPONED_TASK_LOGIC_RESULT.SUCCESS if self.step.is_GOTTEN: goods = logic.load_goods(self.account_id) lot = logic.load_lot(self.lot_id) lot.state = relations.LOT_STATE.ACTIVE logic.save_lot(lot) goods.remove_good(self.good_uid) logic.save_goods(goods) self.step = self.STEP.ACTIVATED self.state = self.STATE.PROCESSED return POSTPONED_TASK_LOGIC_RESULT.SUCCESS