def test_to_json(): rack = Rack(1, 2, 40, None, True) assert rack.to_json() == { "rack_id": 1, "room_id": 2, "voltage": 40, "is_on": None, "is_connected": True, }
def return_racks_in_room_listener(message) -> None: found_racks = message["racks"] room_id = message["room_id"] print("found_racks:", found_racks, rack_dict) assert len(found_racks) == 1 assert Rack.from_json(found_racks[0]) == Rack.from_json( rack_dict["rack"]) assert room_id == room.room_id flag.append(True)
def mock_rack(): return Rack.from_json({ "rack_id": 1, "room_id": 2, "is_on": True, "is_connected": True })
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 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_rack_from_json(): rack = Rack.from_json({"rack_id": 1, "room_id": 2}) assert rack.rack_id == 1 assert rack.room_id == 2 assert rack.voltage == None # auto-initialized to None assert rack.is_on == None # auto-initialized to None assert rack.is_connected == None # auto-initialized to None
def test_create_rack(): rack = Rack(1, 2, 40, False, False) assert rack.rack_id == 1 assert rack.room_id == 2 assert rack.voltage == 40 assert rack.is_on == False assert rack.is_connected == False
def read_all_racks(conn) -> List[Rack]: sql = "SELECT rack_id, room_id, voltage, is_on, is_connected FROM racks" with conn.cursor() as cursor: cursor.execute(sql) all_racks = cursor.fetchall() racks = [ Rack(rack_id, room_id, voltage, bool(is_on), bool(is_connected)) for (rack_id, room_id, voltage, is_on, is_connected) in all_racks ] cursor.close() return racks
def read_racks_in_room(conn, room_id: int) -> List[Rack]: sql = "SELECT rack_id, room_id, voltage, is_on, is_connected FROM racks WHERE room_id=%s" with conn.cursor() as cursor: cursor.execute(sql, (room_id)) all_racks = cursor.fetchall() racks = [ Rack(rack_id, room_id, voltage, bool(is_on), bool(is_connected)) for (rack_id, room_id, voltage, is_on, is_connected) in all_racks ] cursor.close() return racks
def _test_send_rack(sio, room): # send initial rack update with created room id rack_dict = { "rack": { "rack_id": 2, "room_id": room.room_id, "voltage": 100, "is_on": True, "is_connected": True, }, } sio.emit("message_sent", rack_dict) sio.sleep(1) flag = [] @sio.on("return_racks_in_room") def return_racks_in_room_listener(message) -> None: found_racks = message["racks"] room_id = message["room_id"] print("found_racks:", found_racks, rack_dict) assert len(found_racks) == 1 assert Rack.from_json(found_racks[0]) == Rack.from_json( rack_dict["rack"]) assert room_id == room.room_id flag.append(True) sio.emit( "read_all_racks_in_room", {"room": { "room_id": room.room_id }}, ) wait_for_event(sio, flag, 1, 10, "test_send_rack.read_all_racks_in_room.1") print("first rack found and returned") # update same rack to on rack_dict["rack"]["is_on"] = not rack_dict["rack"]["is_on"] sio.emit("message_sent", rack_dict) sio.emit( "read_all_racks_in_room", {"room": { "room_id": room.room_id }}, ) wait_for_event(sio, flag, 2, 10, "test_send_rack.read_all_racks_in_room.2") print("second rack found and returned") return Rack.from_json(rack_dict["rack"])
def message_sent(message): logging.debug("message sent:", message) entities_processed = [] print("message:", message) if "room" in message: # a room is contained in this update entities_processed.append("room") room_json = message["room"] room = Room.from_json(room_json) app_config.logger.debug(room) print("Saw room in message") app_config.db.write_room(room) if "rack" in message: # a rack is contained in this update entities_processed.append("rack") rack_json = message["rack"] rack = Rack.from_json(rack_json) app_config.logger.debug(rack) print("Saw rack in message") app_config.db.write_rack(rack) if "recipe" in message: # a recipe is contained in this update entities_processed.append("recipe") recipe_json = message["recipe"] recipe = Recipe.from_json(recipe_json) app_config.logger.debug(recipe) print("Saw recipe in message") app_config.db.write_recipe(recipe) if "shelf" in message: # a shelf is contained in this update entities_processed.append("shelf") shelf_json = message["shelf"] shelf = Shelf.from_json(shelf_json) app_config.logger.debug(shelf) print("Saw shelf in message") app_config.db.write_shelf(shelf) if "plant" in message: # a plant is contained in this update entities_processed.append("plant") plant_json = message["plant"] plant = Plant.from_json(plant_json) app_config.logger.debug(plant) print("Saw plant in message") app_config.db.write_plant(plant) send_message_to_namespace_if_specified( socketio, message, "message_received", {"processed": entities_processed})
def _test_send_rack(sio, room): # send initial rack update with created room id rack_dict = { "rack": { "rack_id": 4, "room_id": room.room_id, "voltage": 100, "is_on": True, "is_connected": True, } } sio.emit("message_sent", rack_dict) sio.sleep(1) return Rack.from_json(rack_dict["rack"])
def _test_send_rack(sio, room): # send initial rack update with created room id rack_dict = { "rack": { "rack_id": 2, "room_id": room.room_id, "voltage": 100, "is_on": True, "is_connected": True, }, NAMESPACE: INTEGRATION_NAMESPACE, } sio.emit("create_object", rack_dict) sio.sleep(1) return Rack.from_json(rack_dict["rack"])
def create_object(message): entities_processed = [] print("received message:", message) if "room" in message: # a room is contained in this update entities_processed.append("room") room_json = message["room"] room = Room.from_json(room_json) print("Saw room in message", room) app_config.db.write_room(room) if "rack" in message: # a rack is contained in this update entities_processed.append("rack") rack_json = message["rack"] rack = Rack.from_json(rack_json) print("Saw rack in message", rack) app_config.db.write_rack(rack) if "recipe" in message: # a recipe is contained in this update entities_processed.append("recipe") recipe_json = message["recipe"] recipe = Recipe.from_json(recipe_json) print("Saw recipe in message", recipe) app_config.db.write_recipe(recipe) if "shelf" in message: # a shelf is contained in this update entities_processed.append("shelf") shelf_json = message["shelf"] shelf = Shelf.from_json(shelf_json) print("Saw shelf in message", shelf) app_config.db.write_shelf(shelf) send_message_to_namespace_if_specified( socketio, message, "create_object_success", {"processed": entities_processed}, )
def test__eq__fail(): rack = Rack(1, 2, 49, False, None) rack2 = Rack(1, 3, 49, True, False) assert not rack == rack2
def test__eq__(): rack = Rack(1, 2, 49, False, None) rack2 = Rack(1, 2, 49, False, None) assert rack == rack2
def test__hash__fail(): rack = Rack(1, 3, 50, None, True) rack2 = Rack(1, 2, 49, False, None) assert not hash(rack) == hash(rack2)
def test__hash__(): rack = Rack(1, 3, 50, None, True) rack2 = Rack(1, 3, 50, None, True) assert hash(rack2) == hash(rack)
def test__str__(): rack = Rack(1, 2, 49, False, None) rack2 = Rack(1, 2, 49, False, None) assert str(rack) == str(rack2)