def __init__(self, station_id, name, color, direction_a=None, direction_b=None): self.name = name station_name = ( self.name.lower() .replace("/", "_and_") .replace(" ", "_") .replace("-", "_") .replace("'", "") ) # # # TODO: Complete the below by deciding on a topic name, number of partitions, and number of # replicas # # topic_name = f"{station_name}" # TODO: Come up with a better topic name super().__init__( topic_name, key_schema=Station.key_schema, value_schema=Station.value_schema, # TODO: Uncomment once schema is defined num_partitions=1, num_replicas=1, ) self.station_id = int(station_id) self.color = color self.dir_a = direction_a self.dir_b = direction_b self.a_train = None self.b_train = None self.turnstile = Turnstile(self)
def test_logs(): reg = Register('Metropoliten3') t = Turnstile(reg, id_=1, fare=8.0) c3 = reg.create_card(CardType.BalanceLimit, 12) c4 = reg.create_card(CardType.BalanceLimit, 3) t(c3.id) t(c4.id) log = [ID for ID, _, _ in reg.log] assert c3.id in log and c4.id in log
class Station(Producer): """Defines a single station""" key_schema = avro.load(f"{Path(__file__).parents[0]}/schemas/arrival_key.json") # # TODO: Define this value schema in `schemas/station_value.json, then uncomment the below # value_schema = avro.load(f"{Path(__file__).parents[0]}/schemas/arrival_value.json") def __init__(self, station_id, name, color, direction_a=None, direction_b=None): self.name = name station_name = ( self.name.lower() .replace("/", "_and_") .replace(" ", "_") .replace("-", "_") .replace("'", "") ) # # # TODO: Complete the below by deciding on a topic name, number of partitions, and number of # replicas # # topic_name = f"{station_name}" # TODO: Come up with a better topic name super().__init__( topic_name, key_schema=Station.key_schema, value_schema=Station.value_schema, # TODO: Uncomment once schema is defined num_partitions=1, num_replicas=1, ) self.station_id = int(station_id) self.color = color self.dir_a = direction_a self.dir_b = direction_b self.a_train = None self.b_train = None self.turnstile = Turnstile(self) def run(self, train, direction, prev_station_id, prev_direction): val = json.dumps( {"station_id": self.station_id, "train_id": train, "direction": direction, "prev_station_id": prev_station_id, "prev_direction": prev_direction } ) """Simulates train arrivals at this station""" # # # TODO: Complete this function by producing an arrival message to Kafka # # logger.info("arrival kafka integration incomplete - skipping") self.producer.produce( topic=self.topic_name, key={"timestamp": self.time_millis()}, value= val ) def __str__(self): return "Station | {:^5} | {:<30} | Direction A: | {:^5} | departing to {:<30} | Direction B: | {:^5} | departing to {:<30} | ".format( self.station_id, self.name, self.a_train.train_id if self.a_train is not None else "---", self.dir_a.name if self.dir_a is not None else "---", self.b_train.train_id if self.b_train is not None else "---", self.dir_b.name if self.dir_b is not None else "---", ) def __repr__(self): return str(self) def arrive_a(self, train, prev_station_id, prev_direction): """Denotes a train arrival at this station in the 'a' direction""" self.a_train = train self.run(train, "a", prev_station_id, prev_direction) def arrive_b(self, train, prev_station_id, prev_direction): """Denotes a train arrival at this station in the 'b' direction""" self.b_train = train self.run(train, "b", prev_station_id, prev_direction) def close(self): """Prepares the producer for exit by cleaning up the producer""" self.turnstile.close() super(Station, self).close()
def turnstile_object(): turnstile = Turnstile() return turnstile
def test_pushing_locked_turnstile_remains_locked(): turnstile = Turnstile() turnstile.push() assert turnstile.current_state() == "locked"
def test_amount_increases_when_inserting_coin(): turnstile = Turnstile(amount_for_passing=10) turnstile.insert_coin(5) assert turnstile.current_amount() == 5
def test_amount_in_turnstile_is_reset_after_unlocking(): turnstile = Turnstile(amount_for_passing=10) turnstile.insert_coin(15) assert turnstile.current_amount() == 0
def test_push_locks_previously_unlocked_turnstile(): turnstile = Turnstile(amount_for_passing=10) turnstile.change_state(Unlocked(turnstile)) turnstile.push() assert turnstile.current_state() == "locked"
def test_turnstile_is_locked_after_creation(): turnstile = Turnstile() assert turnstile.current_state() == "locked"
def test_insert_coins_keeps_unlocked_turnstile_unlocked(): turnstile = Turnstile(amount_for_passing=10) turnstile.change_state(Unlocked(turnstile)) turnstile.insert_coin(5) assert turnstile.current_state() == "unlocked"
def test_insert_more_than_sufficient_coins_opens_turnstile(): turnstile = Turnstile(amount_for_passing=10) turnstile.insert_coin(15) assert turnstile.current_state() == "unlocked"
def test_inserting_half_the_required_amount_twice_unlocks_turnstile(): turnstile = Turnstile(amount_for_passing=10) turnstile.insert_coin(5) assert turnstile.current_state() == "locked" turnstile.insert_coin(5) assert turnstile.current_state() == "unlocked"
def test_insert_insufficient_coins_keeps_turnstile_locked(): turnstile = Turnstile(amount_for_passing=10) turnstile.insert_coin(5) assert turnstile.current_state() == "locked"