Ejemplo n.º 1
0
    def testExchange(self):
        from db.models import Exchange, User, Item
        from server.item import exchange

        s = current_session()

        s.rollback()
        exchange.sell(uid=1, item_id=1, price=500)
        eq_(s.query(Item).filter(Item.owner_id != None).count(), 1)  # noqa
        e = s.query(Exchange).first()
        eq_(e.item.sku, 'foo')
        eid = e.id

        s.rollback()
        exchange.buy(uid=2, entry_id=eid)

        u1, u2 = s.query(User).order_by(User.id.asc()).all()
        eq_(u1.ppoint, 1500)
        eq_(u2.ppoint, 500)

        i1, i2 = s.query(Item).order_by(Item.id.asc()).all()
        eq_(i1.owner_id, 2)
        eq_(i2.owner_id, 2)

        with assert_raises(exceptions.UserNotFound):
            exchange.buy(uid=3, entry_id=3)

        with assert_raises(exceptions.ItemNotFound):
            exchange.buy(uid=1, entry_id=3)

        s.rollback()
        id = s.query(Item).first().id
        exchange.sell(uid=2, item_id=id, price=5000)
        s.rollback()
        id = s.query(Exchange).first().id

        with assert_raises(exceptions.InsufficientFunds):
            exchange.buy(uid=1, entry_id=id)

        exchange.cancel_sell(uid=2, entry_id=id)

        i1, i2 = s.query(Item).order_by(Item.id.asc()).all()
        eq_(i1.owner_id, 2)
        eq_(i2.owner_id, 2)

        exchange.list()
Ejemplo n.º 2
0
    def testExchange(self):
        from db.models import Exchange, User, Item
        from server.item import exchange

        s = current_session()

        s.rollback()
        exchange.sell(uid=1, item_id=1, price=500)
        eq_(s.query(Item).filter(Item.owner_id != None).count(), 1)  # noqa
        e = s.query(Exchange).first()
        eq_(e.item.sku, 'foo')
        eid = e.id

        s.rollback()
        exchange.buy(uid=2, entry_id=eid)

        u1, u2 = s.query(User).order_by(User.id.asc()).all()
        eq_(u1.ppoint, 1500)
        eq_(u2.ppoint, 500)

        i1, i2 = s.query(Item).order_by(Item.id.asc()).all()
        eq_(i1.owner_id, 2)
        eq_(i2.owner_id, 2)

        with assert_raises(exceptions.UserNotFound):
            exchange.buy(uid=3, entry_id=3)

        with assert_raises(exceptions.ItemNotFound):
            exchange.buy(uid=1, entry_id=3)

        s.rollback()
        id = s.query(Item).first().id
        exchange.sell(uid=2, item_id=id, price=5000)
        s.rollback()
        id = s.query(Exchange).first().id

        with assert_raises(exceptions.InsufficientFunds):
            exchange.buy(uid=1, entry_id=id)

        exchange.cancel_sell(uid=2, entry_id=id)

        i1, i2 = s.query(Item).order_by(Item.id.asc()).all()
        eq_(i1.owner_id, 2)
        eq_(i2.owner_id, 2)

        exchange.list()
Ejemplo n.º 3
0
 def buy(self, user, entry_id):
     exchange.buy(user.account.userid, entry_id)
     user.write(['message_info', 'success'])