本文整理汇总了Python中inventory.Inventory.query_sort方法的典型用法代码示例。如果您正苦于以下问题:Python Inventory.query_sort方法的具体用法?Python Inventory.query_sort怎么用?Python Inventory.query_sort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类inventory.Inventory
的用法示例。
在下文中一共展示了Inventory.query_sort方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: InventoryTest
# 需要导入模块: from inventory import Inventory [as 别名]
# 或者: from inventory.Inventory import query_sort [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]