def test_register_get_order_total_non_empty_order(): register = Register('tmp/menu.txt', 'tmp/employees.txt', 'tmp/register_count.bin', 'tmp/') register.login_employee("admin") items = [ Item("Chocolate bar", 1.0, "001", category="Candy", shortcut=None), Item("gum", 0.47, "custom_gum", category="Custom", shortcut=None) ] order = {items[0]: 1, items[1]: 1} register.order = order assert register.get_order_total() == 1.47
def test_register_add_new_item_to_order(): register = Register('tmp/menu.txt', 'tmp/employees.txt', 'tmp/register_count.bin', 'tmp/') register.login_employee("admin") items = [ Item("Chocolate bar", 1.0, "001", category="Candy", shortcut=None), Item("Gum", 0.75, "002", category="Candy", shortcut=None) ] order = {items[0]: 1} register.order = order register._add_to_order(items[1]) assert register.order == {items[0]: 1, items[1]: 1}
def test_register_clear_order(): register = Register('tmp/menu.txt', 'tmp/employees.txt', 'tmp/register_count.bin', 'tmp/') register.login_employee("admin") items = [ Item("Chocolate bar", 1.0, "001", category="Candy", shortcut=None), Item("Gum", 0.75, "002", category="Candy", shortcut=None) ] initial_order = {items[0]: 1, items[1]: 1} final_order = {} register.order = initial_order register.clear_order() assert register.order == final_order
def test_register_remove_custom(): register = Register('tmp/menu.txt', 'tmp/employees.txt', 'tmp/register_count.bin', 'tmp/') register.login_employee("admin") items = [ Item("Chocolate bar", 1.0, "001", category="Candy", shortcut=None), Item("gum", 0.47, "custom_gum", category="Custom", shortcut=None) ] initial_order = {items[0]: 1, items[1]: 1} final_order = {items[0]: 1} register.order = initial_order register.remove("custom_gum") assert register.order == final_order
def test_register_find_by_shortcut(): register = Register('tmp/menu.txt', 'tmp/employees.txt', 'tmp/register_count.bin', 'tmp/') register.login_employee("admin") item = register._find_in_menu("hc") assert item == Item("Hot chocolate", 0.5, "003", category="Beverage", shortcut="hc")
def test_register_find_by_barcode(): register = Register('tmp/menu.txt', 'tmp/employees.txt', 'tmp/register_count.bin', 'tmp/') register.login_employee("admin") item = register._find_in_menu("001") assert item == Item("Chocolate bar", 1.0, "001", category="Candy", shortcut=None)
def test_register_order_to_string(): register = Register('tmp/menu.txt', 'tmp/employees.txt', 'tmp/register_count.bin', 'tmp/') register.login_employee("admin") items = [ Item("Chocolate bar", 1.0, "001", category="Candy", shortcut=None), Item("Gum", 0.75, "002", category="Candy", shortcut=None) ] order = {items[0]: 1, items[1]: 1} register.order = order representation = register.order_to_string().split('\n') correct_representation = ['Chocolate bar x 1', 'Gum x 1'] assert all([line in correct_representation for line in representation]) assert all([line in representation for line in correct_representation])
def test_register_remove_raises_exception_if_item_not_in_order(): register = Register('tmp/menu.txt', 'tmp/employees.txt', 'tmp/register_count.bin', 'tmp/') register.login_employee("admin") item = Item("Gum", 0.75, "002", category="Candy", shortcut=None) order = {} register.order = order register._remove_from_order(item)
def test_register_remove_unique_item_from_order(): register = Register('tmp/menu.txt', 'tmp/employees.txt', 'tmp/register_count.bin', 'tmp/') register.login_employee("admin") item = Item("Gum", 0.75, "002", category="Candy", shortcut=None) order = {item: 1} register.order = order register._remove_from_order(item) assert register.order == {}
def test_register_checkout_order(): register = Register('tmp/menu.txt', 'tmp/employees.txt', 'tmp/register_count.bin', 'tmp/') register.login_employee("admin") items = [ Item("Chocolate bar", 1.0, "001", category="Candy", shortcut=None), Item("Gum", 0.75, "002", category="Candy", shortcut=None) ] order = {items[0]: 1, items[1]: 1} register.register_count = 1.5 register.order = order register.checkout_order() assert register.register_count == 3.25 assert register.order == {} with io.open('tmp/register_count.bin', 'rb') as f: (register_count, ) = struct.unpack('d', f.read(8)) assert register_count == 3.25
def test_register_add_custom(): register = Register('tmp/menu.txt', 'tmp/employees.txt', 'tmp/register_count.bin', 'tmp/') register.login_employee("admin") register.add_custom("gum", 0.47) correct_added_item = Item("gum", 0.47, "custom_gum", category="Custom", shortcut=None) correct_quantity = 1 assert register.order == {correct_added_item: correct_quantity}
def test_register_add(): register = Register('tmp/menu.txt', 'tmp/employees.txt', 'tmp/register_count.bin', 'tmp/') register.login_employee("admin") register.add("001") correct_added_item = Item("Chocolate bar", 1.0, "001", category="Candy", shortcut=None) correct_quantity = 1 assert register.order == {correct_added_item: correct_quantity}
def test_register_reads_menu(): register = Register('tmp/menu.txt', 'tmp/employees.txt', 'tmp/register_count.bin', 'tmp/') menu = register.menu correct_menu = [ Item("Chocolate bar", 1.0, "001", category="Candy", shortcut=None), Item("Gum", 0.75, "002", category="Candy", shortcut=None), Item("Hot chocolate", 0.5, "003", category="Beverage", shortcut="hc") ] assert all([item in menu for item in correct_menu]) assert all([item in correct_menu for item in menu]) for item in correct_menu: corresponding_item = menu[menu.index(item)] assert item.get_name() == corresponding_item.get_name() assert item.get_price() == corresponding_item.get_price() assert item.get_barcode() == corresponding_item.get_barcode() assert item.get_category() == corresponding_item.get_category() assert item.get_shortcut() == corresponding_item.get_shortcut()
def add_custom(self, name, price): """ Adds a custom item to the order Parameters ---------- name : str Name of the custom item price : int or float Price of the custom item """ self._verify_credentials(self.employee, 1) check_is_valid_item_name(name) check_is_valid_item_price(price) item = Item(name=name, barcode="custom_" + name, category="Custom", price=price, shortcut=None) self._add_to_order(item)
def _load_menu(self, file_path): """ Loads and returns the menu. Parameters ---------- file_path : str Path to the menu file """ menu = [] with io.open(file_path, encoding='utf-8') as f: # Read all lines in the file lines = f.readlines() # Strip all lines from trailing white space and carriage returns lines = [line.strip() for line in lines] # Remove empty lines lines = [line for line in lines if line != ''] # Split lines into lists of tokens separated by '|' (items) items = [line.split('|') for line in lines] # This flag tells whether the file contains errors errors_detected = False current_category = "General" current_default_price = None # Loop over items and try to add them to the menu as MenuItem # instances. If errors occur, faulty lines are ignored and the # `errors_detected` flag is raised. for item in items: # Strip all tokens from leading and trailing white space item = [token.strip() for token in item] # Items must have at least two tokens (name and barcode) and at # most four (name, barcode, price and shortcut) if not len(item) in (2, 3, 4): errors_detected = True continue # If '#' is the first character of the line, this is a category # command to set a new category and a new default price. if item[0].startswith("#"): current_category = item[0][1:].strip() try: current_default_price = float(item[1]) continue except ValueError: errors_detected = True continue # The third token is always considered as the custom price, and # the fourth one is the shortcut. Items with a shortcut must # therefore have a price set. Items without a custom price must # belong to a category which sets a default price. if len(item) == 2 and current_default_price is None: errors_detected = True continue # Parse the actual item if len(item) == 2: barcode = item[0] name = item[1] category = current_category price = current_default_price shortcut = None if len(item) == 3: barcode = item[0] name = item[1] category = current_category try: price = float(item[2]) except ValueError: errors_detected = True continue shortcut = None if len(item) == 4: barcode = item[0] name = item[1] category = current_category try: price = float(item[2]) except ValueError: errors_detected = True continue shortcut = item[3] # Add item to menu it = Item(name=name, barcode=barcode, category=category, price=price, shortcut=shortcut) menu.append(it) if errors_detected: self.logger.warning("Some lines of the menu contained errors " + "and were ignored") return list(set(menu))