Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. import math
  2.  
  3. class Checkout():
  4.  
  5. def __init__(self, pricing_rules):
  6. self.pricing_rules = pricing_rules
  7. self.basket = {}
  8.  
  9. def scan(self, item):
  10. self.basket[item] = self.basket[item] + 1 if item in self.basket else 1
  11.  
  12. def total(self):
  13. basket_total = 0
  14. for item_type, quantity in self.basket.items():
  15. basket_total = basket_total + self.pricing_rules[item_type](quantity)
  16. return basket_total
  17.  
  18.  
  19. def bogof(quantity):
  20. return math.ceil(quantity / 2.0)
  21.  
  22. def bulkbuy(threshold_quantity, original_price, new_price, actual_quantity):
  23. return original_price if actual_quantity < threshold_quantity else new_price
  24.  
  25. #### test.py###
  26. import math
  27. from do_something import Checkout, bogof, bulkbuy
  28.  
  29. def cf1_rule(quantity):
  30. return 1123 * quantity
  31.  
  32. def fr1_rule(quantity):
  33. return 311 * bogof(quantity)
  34.  
  35. def sr1_rule(quantity):
  36. return quantity * bulkbuy(3, 500, 450, quantity)
  37.  
  38. def test_fr1():
  39. checkout = Checkout({'FR1': fr1_rule})
  40. checkout.scan('FR1')
  41. assert checkout.total() == 311
  42.  
  43. def test_dataset_1():
  44. """
  45. Second test from provided data
  46. """
  47. checkout = Checkout({
  48. 'FR1': fr1_rule,
  49. 'SR1': sr1_rule,
  50. 'CF1': cf1_rule
  51. })
  52. checkout.scan('FR1')
  53. checkout.scan('SR1')
  54. checkout.scan('FR1')
  55. checkout.scan('FR1')
  56. checkout.scan('CF1')
  57. assert checkout.total() == 2245
  58.  
  59. def test_dataset_2():
  60. """
  61. Second test from provided data
  62. """
  63. checkout = Checkout({'FR1': fr1_rule})
  64. checkout.scan('FR1')
  65. checkout.scan('FR1')
  66. assert checkout.total() == 311
  67.  
  68. def test_dataset_3():
  69. """
  70. third test from provided data
  71. """
  72. checkout = Checkout({
  73. 'FR1': fr1_rule,
  74. 'SR1': sr1_rule
  75. })
  76. checkout.scan('SR1')
  77. checkout.scan('SR1')
  78. checkout.scan('FR1')
  79. checkout.scan('SR1')
  80. assert checkout.total() == 1661
  81.  
  82. def test_scan_single():
  83. checkout = Checkout({})
  84. checkout.scan('FR1')
  85. assert checkout.basket == {'FR1': 1}
  86.  
  87. def test_scan_same_item():
  88. checkout = Checkout({})
  89. checkout.scan('FR1')
  90. checkout.scan('FR1')
  91. assert checkout.basket == {'FR1': 2}
  92.  
  93. def test_scan_same_item():
  94. checkout = Checkout({})
  95. checkout.scan('FR1')
  96. checkout.scan('CF1')
  97. assert checkout.basket == {'FR1': 1, 'CF1': 1}
  98.  
  99.  
  100. def test_bogof():
  101. assert bogof(2) == 1
  102.  
  103. def test_bogof_2():
  104. assert bogof(3) == 2
  105.  
  106. def test_bogof_1():
  107. assert bogof(1) == 1
  108.  
  109. def test_not_bulkbuy():
  110. assert bulkbuy(3, 500, 450, 2) == 500
  111.  
  112. def test_bulkbuy():
  113. assert bulkbuy(3, 500, 450, 3) == 450
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement