当前位置: 首页>>代码示例>>Python>>正文


Python Inventory.update_qty方法代码示例

本文整理汇总了Python中inventory.Inventory.update_qty方法的典型用法代码示例。如果您正苦于以下问题:Python Inventory.update_qty方法的具体用法?Python Inventory.update_qty怎么用?Python Inventory.update_qty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在inventory.Inventory的用法示例。


在下文中一共展示了Inventory.update_qty方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: CartTest

# 需要导入模块: from inventory import Inventory [as 别名]
# 或者: from inventory.Inventory import update_qty [as 别名]
class CartTest(unittest.TestCase):
    
    def setUp(self):
        self.cart = Cart()
        self.inv = Inventory()

    def tearDown(self):
        self.inv.close()

    def test_add_items_to_basket(self):
        # Test adding item, make sure it's in cart and inventory updates to reflect that
        old_qty= self.inv.view_qty('item050')
        self.cart.add_item('item050', 10)
        self.assertEqual(self.cart.basket('item050'), 10)
        self.assertEqual(self.inv.view_qty('item050'), old_qty-10)
        self.inv.update_qty('item050', 500)
        # Test adding more to cart than inventory contains
        self.cart._basket['item050'] = 0 # Temporary kluge 
        self.cart.add_item('item050', 600)
        self.assertEqual(self.cart.basket('item050'), 500)
        self.assertEqual(self.inv.view_qty('item050'), 0)
        self.inv.update_qty('item050', 500)

    def test_remove_items_from_basket(self):
        self.cart.add_item('item050', 10)
        self.cart.remove_item('item050',5)
        self.assertEqual(self.cart.basket('item050'), 5)
        self.assertEqual(self.inv.view_qty('item050'), 495)

    def test_edit_items(self):
        # Change to a quantity greater than you already have
        self.cart.edit_item('item050', 50)
        self.cart.edit_item('item050', 70)
        self.assertEqual(self.cart.basket('item050'), 70)
        self.assertEqual(self.inv.view_qty('item050'), 430)
        self.cart.remove_item('item050', 70)
        # Add more than are in inventory
        self.cart.edit_item('item050', 1000)
        self.assertEqual(self.inv.view_qty('item050'), 0)
        self.assertEqual(self.cart.basket('item050'), 500)
        self.cart.remove_item('item050', 500)
        # Change to a quantity less than what you already have
        self.cart.add_item('item050', 100)
        self.cart.edit_item('item050', 50)
        self.assertEqual(self.inv.view_qty('item050'), 450)
        self.assertEqual(self.cart.basket('item050'), 50)
        self.cart.remove_item('item050', 50)
        
    def test_items_total(self):
        self.cart.add_item('item100', 100)
        cost = 100 * self.inv.view_price('item100')
        self.assertEqual(self.cart.items_total(), cost)
        self.cart.remove_item('item100', 100)

    def test_list_items(self):
        self.cart.add_item('item100', 100)
        # Test listing sorted by item name
        for _tuple in self.cart.list_items('item'):
            self.assertEqual(_tuple, ('item100', 100, self.inv.view_price('item100')))
        # Test listing sorted by qty
        for _tuple in self.cart.list_items('price'):
            self.assertEqual(_tuple, ('item100', 100, self.inv.view_price('item100')))
        # Test listing sorted by price
            self.assertEqual(_tuple, ('item100', 100, self.inv.view_price('item100')))
        self.cart.remove_item('item100', 100)
开发者ID:timahutchinson,项目名称:curly-quack,代码行数:67,代码来源:unit_test.py

示例2: InventoryTest

# 需要导入模块: from inventory import Inventory [as 别名]
# 或者: from inventory.Inventory import update_qty [as 别名]
class InventoryTest(unittest.TestCase):
    
    def setUp(self):
        """Create inventory instance."""
        self.inv = Inventory()

    def tearDown(self):
        """Close database opened by self.inv."""
        self.inv.close()

    def test_add_record_to_database(self):
        nrecords = self.count_records()
        self.inv.add_item('test_item', 15.00, 200)
        self.assertEqual(self.count_records(), nrecords+1)

    def test_remove_record_from_database(self):
        nrecords = self.count_records()
        self.inv.remove_item('test_item')
        self.assertEqual(self.count_records(), nrecords-1)

    def test_query_sort(self):
        for field in ['item', 'price', 'qty', 'updated']:
            for direction in ['ascending', 'descending']:
                count = 0
                for row in self.inv.query_sort(field, direction):
                    count += 1
                self.assertEqual(count, 100)

    def test_count_records(self):
        self.assertEqual(self.count_records(), self.inv.nrecords)

    def test_next_page(self):
        pass

    def test_update_qty(self):
        self.inv.update_qty('item000', 250)
        self.inv.c.execute('SELECT qty FROM inventory WHERE item=?', ('item000',))
        self.assertEqual(self.inv.c.fetchone()[0], 250)
        self.inv.update_qty('item000', 500)

    def test_update_price(self):
        original_price = self.inv.c.execute('SELECT price FROM inventory WHERE item=?', ('item000',)).fetchone()[0]
        self.inv.update_price('item000', 10)
        self.inv.c.execute('SELECT price FROM inventory WHERE item=?', ('item000',))
        self.assertEqual(self.inv.c.fetchone()[0], 10)
        self.inv.update_price('item000', original_price)

    def test_query_by_price(self):
        original_price1 = self.inv.c.execute('SELECT price FROM inventory WHERE item=?', ('item050',)).fetchone()[0]
        original_price2 = self.inv.c.execute('SELECT price FROM inventory WHERE item=?', ('item150',)).fetchone()[0]
        self.inv.update_price('item050', 50)
        self.inv.update_price('item150', 70)
        # Check with low and high price range
        count = 0
        for row in self.inv.query_by_price(40,80):
            count += 1
        self.assertEqual(count, 2)
        # Check with only low price
        count = 0
        for row in self.inv.query_by_price(40):
            count += 1
        self.assertEqual(count, 2)
        # We also expect all records to be returned with no prices set
        count = 0
        for row in self.inv.query_by_price():
            count += 1
        self.assertEqual(count, self.count_records())

    def test_query_by_name(self):
        # Test should return same number of results regardless of sort
        count = 0
        for row in self.inv.query_by_name('item0', 0, 100, 'item'):
            count += 1
        self.assertEqual(count, 100)
        count = 0
        for row in self.inv.query_by_name('item0', 0, 100, 'price'):
            count += 1
        self.assertEqual(count, 100)
        count = 0
        for row in self.inv.query_by_name('item0', 0, 100):
            count += 1
        self.assertEqual(count, 100)
        count = 0
        for row in self.inv.query_by_name('item0'):
            count += 1
        self.assertEqual(count, 100)

    def test_get_qty(self):
        self.inv.update_qty('item100', 1000)
        self.assertEqual(self.inv.view_qty('item100'), 1000)
        self.inv.update_qty('item100', 1000)

    def count_records(self):
        return self.inv.c.execute('SELECT Count(*) FROM inventory').fetchone()[0]
开发者ID:timahutchinson,项目名称:curly-quack,代码行数:96,代码来源:unit_test.py

示例3: Cart

# 需要导入模块: from inventory import Inventory [as 别名]
# 或者: from inventory.Inventory import update_qty [as 别名]
class Cart(object):

    def __init__(self):
        self.inv = Inventory()
        self._basket = {}

    def to_JSON(self):
        # JSON serializability
        del self.inv.c
        del self.inv.conn
        print (json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4))
        self.inv = Inventory()

    def basket(self, name):
        try:
            return self._basket[name.lower()]
        except KeyError:
            print 'Item not in basket.'
            return 0

    def add_item(self, name, qty):
        if self.inv.view_qty(name.lower()) >= qty:
            if name.lower() in self._basket:
                self._basket[name.lower()] += qty
            else:
                self._basket[name.lower()] = qty
            self.inv.update_qty(name, self.inv.view_qty(name.lower())-qty)
        else:
            print 'Inventory only has %s of this item' % self.inv.view_qty(name.lower())
            if name.lower() in self._basket:
                self._basket[name.lower()] += self.inv.view_qty(name.lower())
            else:
                self._basket[name.lower()] = self.inv.view_qty(name.lower())
            self.inv.update_qty(name, 0)

    def remove_item(self, name, qty):
        if name.lower() in self._basket:
            if self.basket(name.lower()) >= qty:
                self._basket[name.lower()] -= qty
                self.inv.update_qty(name, self.inv.view_qty(name.lower())+qty)
            else:
                self.inv.update_qty(name, self.inv.view_qty(name.lower())+self._basket[name.lower()])
                self._basket[name.lower()] = 0

    def edit_item(self, name, qty):
        if qty < 0:
            return
        diff = qty - self.basket(name.lower())
        if diff > 0:
            # Need to add items from inventory
            if self.inv.view_qty(name.lower()) >= diff:
                self._basket[name.lower()] = qty
                self.inv.update_qty(name, self.inv.view_qty(name.lower())-diff)
            else:
                print 'Only %s in inventory' % self.inv.view_qty(name.lower())
                self._basket[name.lower()] = self.inv.view_qty(name.lower())
                self.inv.update_qty(name, 0)
        elif diff < 0:
            # Remove items from cart
            self._basket[name.lower()] = qty
            self.inv.update_qty(name, self.inv.view_qty(name.lower())-diff)
        else:
            # Cart already has qty items
            pass

    def items_total(self):
        """Return total cost of all items in cart."""
        total = 0
        for item in self._basket:
            total += self._basket[item] * self.inv.c.execute('SELECT price FROM inventory WHERE item=?', (item,)).fetchone()[0]
        return total

    def list_items(self, sortby='item'):
        list = []
        for key in self._basket:
            list.append( (key, self._basket[key], self.inv.view_price(key)) )
        if sortby.lower() == 'item':
            self.sort_list_of_tuples(list,0)
        elif sortby.lower() == 'qty':
            self.sort_list_of_tuples(list,1)
        elif sortby.lower() == 'price':
            self.sort_list_of_tuples(list,2)
        else:
            print 'Cart can only be sorted by "item", "qty", or "price".'
            return
        for _tuple in list:
            yield _tuple

    def query_by_price(self, lowprice=0, highprice=None, sortby='item'):
        list = []
        if highprice is not None:
            for key in self._basket:
                this_price = self.inv.view_price(key)
                if this_price >= lowprice and this_price <= highprice:
                    list.append( (key, self._basket[key], self.inv.view_price(key)) )
        else:
            for key in self._basket:
                this_price = self.inv.view_price(key)
                if this_price >= lowprice:
                    list.append( (key, self._basket[key], self.inv.view_price(key)) )
#.........这里部分代码省略.........
开发者ID:timahutchinson,项目名称:curly-quack,代码行数:103,代码来源:cart.py


注:本文中的inventory.Inventory.update_qty方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。