def create_recipe_phases_from_light_configurations( light_configurations: List[Any], recipe_id: int, end_date: datetime ) -> List[RecipePhase]: recipe_phases: List[RecipePhase] = [] for i, light_config in enumerate(light_configurations): start_date_str: str = light_config["start_date"] phase_start_date: datetime = iso8601_string_to_datetime(start_date_str) is_last_phase = i == len(light_configurations) - 1 # if this is the last phase, use `end_date`. Else, use the next phases start date (guaranteed to exist cause # this is therefore not the last phase) phase_end_date: datetime = end_date if is_last_phase else iso8601_string_to_datetime( light_configurations[i + 1]["start_date"] ) date_diff: timedelta = phase_end_date - phase_start_date # 60 seconds * 60 minutes = 3600 seconds in an hour num_hours: int = int(date_diff.total_seconds() / 3600) recipe_phase_num: int = i power_level: int = light_config["power_level"] red_level: int = light_config["red_level"] blue_level: int = light_config["blue_level"] recipe_phase: RecipePhase = RecipePhase( recipe_id, recipe_phase_num, num_hours, power_level, red_level, blue_level, ) recipe_phases.append(recipe_phase) return recipe_phases
def test_create_recipe_phase(): recipe_phase = RecipePhase(1, 2, 3, 4, 5, 6) assert recipe_phase.recipe_id == 1 assert recipe_phase.recipe_phase_num == 2 assert recipe_phase.num_hours == 3 assert recipe_phase.power_level == 4 assert recipe_phase.red_level == 5 assert recipe_phase.blue_level == 6
def test_to_json(): recipephase = RecipePhase(1, 2, 3, 4, 5, 6) assert recipephase.to_json() == { "recipe_id": 1, "recipe_phase_num": 2, "num_hours": 3, "power_level": 4, "red_level": 5, "blue_level": 6, }
def read_phases_from_recipe(conn, recipe_id: int) -> List[RecipePhase]: sql = "SELECT recipe_id, recipe_phase_num, num_hours, power_level, red_level, blue_level FROM recipe_phases WHERE recipe_id = %s" with conn.cursor() as cursor: cursor.execute(sql, (recipe_id)) found_recipe_phases = cursor.fetchall() recipe_phases = [ RecipePhase(rid, rpn, nh, pl, rl, bl) for (rid, rpn, nh, pl, rl, bl) in found_recipe_phases ] cursor.close() return recipe_phases
def read_phases_from_recipes(conn, recipe_ids: List[int]) -> List[RecipePhase]: values_list: List[str] = [str(rid) for rid in recipe_ids] sql = "SELECT recipe_id, recipe_phase_num, num_hours, power_level, red_level, blue_level FROM recipe_phases WHERE (recipe_id) IN ({})".format( ",".join(values_list)) with conn.cursor() as cursor: cursor.execute(sql) found_recipe_phases = cursor.fetchall() recipe_phases = [ RecipePhase(rid, rpn, nh, pl, rl, bl) for (rid, rpn, nh, pl, rl, bl) in found_recipe_phases ] cursor.close() return recipe_phases
def read_recipe_phases( conn, recipe_id_phase_num_pairs: List[Tuple[int, int]]) -> List[RecipePhase]: values_list: List[str] = [] for recipe_id, recipe_phase_num in recipe_id_phase_num_pairs: values_list.append("({},{})".format(recipe_id, recipe_phase_num)) sql = "SELECT recipe_id, recipe_phase_num, num_hours, power_level, red_level, blue_level FROM recipe_phases WHERE (recipe_id, recipe_phase_num) IN ({})".format( ",".join(values_list)) with conn.cursor() as cursor: cursor.execute(sql) found_recipe_phases = cursor.fetchall() recipe_phases = [ RecipePhase(rid, rpn, nh, pl, rl, bl) for (rid, rpn, nh, pl, rl, bl) in found_recipe_phases ] cursor.close() return recipe_phases
def test__eq__fail(): recipephase = RecipePhase(1, 2, 3, 4, 5, 6) recipephase2 = RecipePhase(1, 2, 3, 4, 6, 6) assert not recipephase == recipephase2
def test__eq__(): recipephase = RecipePhase(1, 2, 3, 4, 5, 6) recipephase2 = RecipePhase(1, 2, 3, 4, 5, 6) assert recipephase == recipephase2
def test__str__(): recipephase = RecipePhase(1, 2, 3, 4, 5, 6) recipephase2 = RecipePhase(1, 2, 3, 4, 5, 6) assert str(recipephase2) == str(recipephase)
def test__hash__fail(): recipephase = RecipePhase(1, 2, 3, 4, 5, 6) recipephase2 = RecipePhase(2, 3, 4, 5, 6, 7) assert not hash(recipephase) == hash(recipephase2)
def test__hash__(): recipephase = RecipePhase(1, 2, 3, 4, 5, 6) recipephase2 = RecipePhase(1, 2, 3, 4, 5, 6) assert hash(recipephase2) == hash(recipephase)