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 return_all_entities_listener(message) -> None: print("Received message in entities_listener:", message) assert "rooms" in message assert "racks" in message assert "shelves" in message assert "grows" in message assert "recipes" in message assert "recipe_phases" in message found_rooms = [Room.from_json(r) for r in message["rooms"]] found_racks = [Rack.from_json(r) for r in message["racks"]] found_shelves = [Shelf.from_json(s) for s in message["shelves"]] found_grows = [Grow.from_json(g) for g in message["grows"]] found_recipes = [Recipe.from_json(r) for r in message["recipes"]] found_recipe_phases = [ RecipePhase.from_json(rp) for rp in message["recipe_phases"] ] assert collections.Counter(found_rooms) == collections.Counter(rooms) assert collections.Counter(found_racks) == collections.Counter(racks) assert collections.Counter(found_shelves) == collections.Counter( shelves) assert collections.Counter(found_grows) == collections.Counter(grows) assert collections.Counter(found_recipes) == collections.Counter( recipes) assert collections.Counter(found_recipe_phases) == collections.Counter( recipe_phases) flag.append(True)
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 return_all_entities_listener(message) -> None: message_dict = json.loads(message) print("Received message in entities_listener:", message_dict) assert "rooms" in message_dict assert "racks" in message_dict assert "shelves" in message_dict assert "grows" in message_dict assert "grow_phases" in message_dict assert "recipes" in message_dict assert "recipe_phases" in message_dict found_rooms = [Room.from_json(r) for r in message_dict["rooms"]] found_racks = [Rack.from_json(r) for r in message_dict["racks"]] found_shelves = [Shelf.from_json(s) for s in message_dict["shelves"]] found_grows = [Grow.from_json(g) for g in message_dict["grows"]] found_grow_phases = [ GrowPhase.from_json(g) for g in message_dict["grow_phases"] ] found_recipes = [Recipe.from_json(r) for r in message_dict["recipes"]] found_recipe_phases = [ RecipePhase.from_json(rp) for rp in message_dict["recipe_phases"] ] assert collections.Counter(found_rooms) == collections.Counter(rooms) assert collections.Counter(found_racks) == collections.Counter(racks) assert collections.Counter(found_shelves) == collections.Counter( shelves ) assert len(found_grows) > 0 x = len(found_grows) assert ( found_grows[x - 1].start_datetime.strftime("%Y-%m-%d %H:%M:%S") == start ) assert ( found_grows[x - 1].estimated_end_datetime.strftime( "%Y-%m-%d %H:%M:%S" ) == end ) i = len(found_grow_phases) assert ( found_grow_phases[i - 1].phase_start_datetime.strftime( "%Y-%m-%d %H:%M:%S" ) == start ) assert ( found_grow_phases[i - 1].phase_end_datetime.strftime( "%Y-%m-%d %H:%M:%S" ) == end ) j = len(found_recipes) assert found_recipe_phases[j - 1].power_level == p_level assert found_recipe_phases[j - 1].red_level == r_level assert found_recipe_phases[j - 1].blue_level == b_level flag.append(True) grow.append(found_grows[0].grow_id)
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 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 test_create_recipe_phase_from_json(): recipephase = RecipePhase.from_json({ "recipe_id": 1, "recipe_phase_num": 2, "num_hours": 3, "power_level": 4, "red_level": 5, "blue_level": 6, }) assert recipephase.recipe_id == 1 assert recipephase.recipe_phase_num == 2 assert recipephase.num_hours == 3 assert recipephase.power_level == 4 assert recipephase.red_level == 5 assert recipephase.blue_level == 6
def _test_send_recipe(sio): recipe_dict = { "recipe": { "recipe_id": 1, "recipe_name": "purp", "recipe_phases": [ { "recipe_id": 1, "recipe_phase_num": 1, "num_hours": 10, "power_level": 9, "red_level": 8, "blue_level": 7, }, { "recipe_id": 1, "recipe_phase_num": 2, "num_hours": 69, "power_level": 69, "red_level": 68, "blue_level": 67, }, ], }, } flag = [] @sio.on("create_new_recipe_response") def create_new_recipe_response(message) -> None: assert "succeeded" in message assert message["succeeded"] flag.append(True) sio.emit("create_new_recipe", recipe_dict) wait_for_event(sio, flag, 1, 5, "test_create_recipe_with_phases") print("Created new recipe with phases") recipe = Recipe.from_json(recipe_dict["recipe"]) recipe_phases = [ RecipePhase.from_json(recipe_dict["recipe"]["recipe_phases"][0]) ] return recipe, 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 create_new_recipe(message) -> None: if "recipe" not in message: send_message_to_namespace_if_specified( socketio, message, "create_new_recipe_response", { "succeeded": False, "reason": "no recipe" }, ) elif "recipe_phases" not in message["recipe"]: send_message_to_namespace_if_specified( socketio, message, "create_new_recipe_response", { "succeeded": False, "reason": "no recipe phases" }, ) recipe_json = message["recipe"] recipe = Recipe.from_json(recipe_json) recipe_phases: List[RecipePhase] = [] json_recipe_phases = recipe_json["recipe_phases"] for rpl in json_recipe_phases: recipe_phase = RecipePhase.from_json(rpl) recipe_phases.append(recipe_phase) app_config.logger.debug(recipe) app_config.logger.debug(recipe_phases) print("Saw recipe in message") app_config.db.write_recipe_with_phases(recipe, recipe_phases) print("CREATED RECIPE WITH PHASES") send_message_to_namespace_if_specified(socketio, message, "create_new_recipe_response", {"succeeded": True})
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)