Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import unittest
  3. from qscu import QSCU
  4.  
  5.  
  6. class TestQSCU(unittest.TestCase):
  7.     def setup_test(self):
  8.         self.shop = QSCU(":memory:")
  9.         self.shop.setup_db()
  10.  
  11.     def test_no_sold_items(self):
  12.         """Test if the quantity of items is set to zero before any item is sold"""
  13.         self.setup_test()
  14.  
  15.         rows = self.shop.cursor.execute(
  16.             """
  17.            SELECT name, sold
  18.            FROM sales
  19.        """
  20.         )
  21.  
  22.         for name, sold in rows:
  23.             self.assertEqual(sold, 0)
  24.  
  25.     def test_sell_item(self):
  26.         """Test if the sell item function correctly updates the database"""
  27.         self.setup_test()
  28.  
  29.         expected_result = {1: 4, 2: 5}
  30.  
  31.         self.shop.sell_item(1, 1)
  32.         self.shop.sell_item(2, 2)
  33.         self.shop.sell_item(1, 3)
  34.         self.shop.sell_item(2, 3)
  35.  
  36.         rows = self.shop.cursor.execute(
  37.             """
  38.            SELECT product_id, sold
  39.            FROM sales
  40.        """
  41.         )
  42.  
  43.         for product_id, sold in rows:
  44.             if product_id in expected_result:
  45.                 self.assertEqual(sold, expected_result[product_id])
  46.             else:
  47.                 self.assertEqual(sold, 0)
  48.  
  49.     def test_most_sold(self):
  50.         """Test if the get_most_sold functions returns the most sold item"""
  51.         self.setup_test()
  52.  
  53.         # Case when no item has been sold: it's ok if we return any, but it must tell sold 0 times
  54.         self.assertEqual(self.shop.get_most_sold()[1], 0)
  55.  
  56.         # Another case: every item has been sold one time
  57.         self.shop.sell_item(1, 3)
  58.         self.shop.sell_item(2, 5)
  59.         self.shop.sell_item(3, 2)
  60.  
  61.         self.assertEqual(self.shop.get_most_sold(), ("Macchiato", 5))
  62.  
  63.         # More elaborate case: does our function work when update values again?
  64.         self.shop.sell_item(1, 5)
  65.         self.shop.sell_item(2, 2)
  66.         self.assertEqual(self.shop.get_most_sold(), ("Espresso", 8))
  67.  
  68.  
  69. if __name__ == "__main__":
  70.     unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement