def test_line_is_ready_one_occupant() -> None: """ Test line is ready with exactly one customer.""" store = GroceryStore(GROCERY_STORE) item_list = [Item('bananas', 6)] belinda = Customer('Belinda', item_list) store.enter_line(belinda) assert store.line_is_ready(0)
def test_close_line_one_customer() -> None: """ Test whether line with one customer returns customers on close.""" store = GroceryStore(GROCERY_STORE) item_list = [Item('bananas', 6)] belinda = Customer('Belinda', item_list) store.enter_line(belinda) assert store.close_line(0) == []
def test_start_checkout_non_empty() -> None: """ Test whether non empty line starts checkout.""" store = GroceryStore(GROCERY_STORE) item_list = [Item('bananas', 6), Item('apples', 5), Item('carrots', 3)] belinda = Customer('Belinda', item_list) store.enter_line(belinda) assert store.start_checkout(0) == 14
def test_complete_checkout_one() -> None: """ Test whether customers left to checkout after checkout when line has one customer.""" store = GroceryStore(GROCERY_STORE) item_list = [Item('bananas', 6)] belinda = Customer('Belinda', item_list) store.enter_line(belinda) assert not store.complete_checkout(0)
def test_enter_line() -> None: """ Test whether customer enters correct line with empty lines.""" store = GroceryStore(GROCERY_STORE) item_list = [Item('bananas', 6)] belinda = Customer('Belinda', item_list) assert len(store._lines[0]) == 0 assert store.enter_line(belinda) == 0 assert len(store._lines[0]) == 1
def test_get_first_in_line_many() -> None: """ Test whether get first in line returns correct customer with one customer in line.""" store = GroceryStore(GROCERY_STORE) item_list = [Item('bananas', 6)] belinda = Customer('Belinda', item_list) store.enter_line(belinda) assert store.get_first_in_line(0) == belinda
def test_enter_first_occupied() -> None: """ Test whether customer enters correct line when first is occupied.""" store = GroceryStore(GROCERY_STORE) item_list = [Item('bananas', 6)] belinda = Customer('Belinda', item_list) item_list_2 = [Item('apples', 5)] arnold = Customer('Arnold', item_list_2) store.enter_line(arnold) assert store.enter_line(belinda) == 1
def test_start_checkout_self_serve_line() -> None: """check for the checkout method for self serve line""" store1 = GroceryStore(StringIO(CONFIG_FILE3)) item1 = Item("banana", 10) item2 = Item("apple", 2) item3 = Item("orange", 9) customer1 = Customer("bruce", [item1, item2, item3]) assert store1.enter_line(customer1) == 0 assert store1.start_checkout(0) == 42
def test_line_is_ready() -> None: """check the method of line is ready""" store1 = GroceryStore(StringIO(CONFIG_FILE2)) customer1 = Customer("bruce", []) assert store1.line_is_ready(0) is False assert store1.line_is_ready(1) is False assert store1.enter_line(customer1) == 0 assert store1.line_is_ready(0) is True assert store1.line_is_ready(1) is False
def test_enter_line_lines_full() -> None: """ Test whether customer enters line with full line(s).""" store = GroceryStore(GROCERY_STORE_1_LINE_1_CAP) item_list = [Item('bananas', 6)] belinda = Customer('Belinda', item_list) item_list_2 = [Item('apples', 5)] arnold = Customer('Arnold', item_list_2) store.enter_line(arnold) assert store.enter_line(belinda) == -1
def test_enter_over_express_limit() -> None: """ Test whether customer enters express line when they have too many items.""" store = GroceryStore(GROCERY_STORE_EXPRESS) item_list = [Item('apples', 5)] for i in range(EXPRESS_LIMIT): item_list.append(Item('apples', i)) arnold = Customer('Arnold', item_list) store.enter_line(arnold) assert store.enter_line(arnold) == -1
def test_cannot_enter_line() -> None: """test for when the line capacity is full""" store1 = GroceryStore(StringIO(CONFIG_FILE2)) customer1 = Customer("bruce", []) customer2 = Customer("tom", []) customer3 = Customer("mary", []) assert store1.enter_line(customer1) == 0 assert store1.enter_line(customer2) == 1 assert store1.enter_line(customer3) == -1 assert customer3.arrival_time == -1
def test_complete_checkout_more() -> None: """ Test whether customers left to checkout after checkout when line is empty.""" store = GroceryStore(GROCERY_STORE_1_LINE_2_CAP) item_list = [Item('bananas', 6)] belinda = Customer('Belinda', item_list) charlie = Customer('Charlie', item_list) store.enter_line(belinda) store.enter_line(charlie) assert store.complete_checkout(0)
def test_get_first_in_line() -> None: """check get first method""" store1 = GroceryStore(StringIO(CONFIG_FILE4)) customer1 = Customer("bruce", []) customer2 = Customer("tom", []) customer3 = Customer("mary", []) assert store1.enter_line(customer1) == 0 assert store1.enter_line(customer2) == 1 assert store1.enter_line(customer3) == 0 assert store1.get_first_in_line(0) == customer1 assert store1.get_first_in_line(1) == customer2
def test_close_line_people_in_line() -> None: """check close line with people in line""" store1 = GroceryStore(StringIO(CONFIG_FILE4)) customer1 = Customer("bruce", []) customer2 = Customer("tom", []) customer3 = Customer("mary", []) assert store1.enter_line(customer1) == 0 assert store1.enter_line(customer2) == 1 assert store1.enter_line(customer3) == 0 assert store1.close_line(0) == [customer3] assert store1.close_line(1) == []
def test_start_checkout() -> None: """Test the start_checkout function of the GroceryStore Class.""" gs = GroceryStore(StringIO(ONE_LINE_FILE_CONTENTS)) gs2 = GroceryStore(StringIO(SELF_SERVE_FILE_CONTENTS)) c1 = Customer('A', [Item('bananas', 7), Item('apple', 2), \ Item('orange', 5), Item('grapes', 10)]) c2 = Customer('B', [Item('apple', 2)]) c3 = Customer('C', [Item('orange', 5)]) gs.enter_line(c1) gs.enter_line(c2) gs2.enter_line(c3) assert gs.start_checkout(0) == 24 assert gs2.start_checkout(0) == 10
def test_enter_line2() -> None: """check if people can enter line correctly.""" store = GroceryStore(StringIO(CONFIG_FILE2)) customer1 = Customer('bruce', []) customer2 = Customer('tom', [Item('banana', 1), Item('banana', 1), Item('banana', 1), Item('banana', 1), Item('banana', 1), Item('banana', 1), Item('banana', 1), Item('banana', 1)]) customer3 = Customer('jerry', []) customer4 = Customer('mary', []) assert store.enter_line(customer1) == 0 assert store.enter_line(customer2) == -1 assert store.enter_line(customer3) == 1 assert store.enter_line(customer4) == -1
def test_get_first_in_line(): f = StringIO(CONFIG_FILE_100_03) store4 = GroceryStore(f) customer1 = Customer('b', [Item('a', 29)]) customer2 = Customer('c', [Item('a', 2)]) store4.enter_line(customer1) store4.enter_line(customer2) assert store4.get_first_in_line(0) == customer1 assert store4.complete_checkout(0) assert store4.get_first_in_line(0) == customer2 assert not store4.complete_checkout(0) assert store4.get_first_in_line(0) is None
def do(self, store: GroceryStore) -> List[Event]: """Return a list of events representing this customer joining a line in GroceryStore. A customer cannot join a line that does not have capacity for them. When there are no lines the customer can join, the “new customer” event should go back into the container, and have its timestamp increased by 1 (representing trying to join a line again at the next time interval.) If a new customer joins an empty checkout line, a new “begin checking out” event is added with the same timestamp as the join event. """ line_entered = store.enter_line(self.customer) result = [] marker = True while marker is True: if line_entered == -1: self.timestamp += 1 else: event = CustomerArrival(self.timestamp, self.customer) result.append(event) marker = False return result
def do(self, store: GroceryStore) -> List[Event]: """ return a CheckoutComplete event of this customer """ return [CheckoutCompleted(self.timestamp + store.start_checkout\ (self.line_number), self.line_number,\ store.get_first_in_line(self.line_number))]
def do(self, store: GroceryStore) -> List[Event]: """ return a CustomerArrival event if this customer cannot get in line with the timestamp plus one, return CheckoutStarted event if the line is empty or return an empty list if this customer is wait in line """ self._line_num = GroceryStore.enter_line(store, self.customer) if self._line_num == -1: self.timestamp += 1 return [CustomerArrival(self.timestamp, self.customer)] else: self.customer.arrival_time = self.timestamp if store.get_first_in_line(self._line_num) == self.customer: return [CheckoutStarted(self.timestamp, self._line_num)] else: return []
def __init__(self, store_file): """Initialize a GroceryStoreSimulation from a file. @type store_file: str A file containing the configuration of the grocery store. @rtype: None """ self._events = PriorityQueue() self._store = GroceryStore(store_file)
def test_grocery_store_init() -> None: """ Test store initialization with correct lines.""" store = GroceryStore(StringIO(GROCERY_STORE)) assert store._regular_count == 3 assert store._express_count == 2 assert store._self_serve_count == 4 assert len(store._lines) == 9 for line in store._lines: assert line.capacity == 10
def do(self, store: GroceryStore) -> List[Event]: """ return an empty list if there is no customer in line or return a CheckoutStarted event of this line when there is customer behind """ lens = store.complete_checkout(self.line_number) if lens == 0: return [] else: return [CheckoutStarted(self.timestamp, self.line_number)]
def test_is_ready_more_occupants() -> None: """ Test line is ready with more than one customer.""" store = GroceryStore(GROCERY_STORE_1_LINE_2_CAP) item_list = [Item('bananas', 6)] belinda = Customer('Belinda', item_list) item_list_2 = [Item('apples', 5)] arnold = Customer('Arnold', item_list_2) store.enter_line(belinda) store.enter_line(arnold) assert not store.line_is_ready(0)
def test_complete_checkout() -> None: """Test the complete_checkout function of the GroceryStore Class.""" gs = GroceryStore(StringIO(SHORT_FILE_CONTENTS)) c1 = Customer('A', [Item('bananas', 7)]) c2 = Customer('B', [Item('apple', 2)]) c3 = Customer('C', [Item('orange', 5)]) c4 = Customer('D', [Item('grapes', 10)]) gs.enter_line(c1) gs.enter_line(c2) gs.enter_line(c3) gs.enter_line(c4) assert gs.complete_checkout(0) == True assert gs.complete_checkout(1) == False assert gs.complete_checkout(2) == False
def do(self, store: GroceryStore) -> List[Event]: """Return an event representing when checkout will be completed If a customer begins checking out, a new “finish checking out” event is added with the same timestamp as the “begin” timestamp, plus the appropriate amount of time based on the type of checkout line and the time required by the customer’s items. """ total_waiting_time = self.timestamp #should encompass wait time of entire queue for customer in store.get_line_list()[0].queue[:-1]: total_waiting_time += customer.get_item_time() event = CheckoutStarted(total_waiting_time, self.line_number)
def do(self, store: GroceryStore) -> List[Event]: """ return a list of CustomerArrival events of all the other customers behind with timestamps have difference of 1, or an empty list if there is no customer behind """ output = [] self._name_list.extend(store.close_line(self.line_number)) lens = len(self._name_list) for i in self._name_list: output.append(CustomerArrival(self.timestamp + lens, i)) lens -= 1 return output
def test_get_first_in_line_one() -> None: """ Test whether get first in line returns correct customer with more than one customer in line.""" store = GroceryStore(GROCERY_STORE_1_LINE_2_CAP) item_list = [Item('bananas', 6)] belinda = Customer('Belinda', item_list) store.enter_line(belinda) item_list_2 = [Item('cherries', 5)] charlie = Customer('Charlie', item_list_2) store.enter_line(charlie) assert store.get_first_in_line(0) == belinda
def test_close_line_more_occupants() -> None: """ Test whether line with more than one customer returns correct customers on close.""" store = GroceryStore(GROCERY_STORE_1_LINE_2_CAP) item_list = [Item('bananas', 6)] belinda = Customer('Belinda', item_list) store.enter_line(belinda) item_list_2 = [Item('cherries', 5)] charlie = Customer('Charlie', item_list_2) store.enter_line(charlie) assert store.close_line(0) == [charlie]