def test_repr(self): place = Place("id", "type", "name", Coordinates(46.0748, 11.1217)) self.assertEqual(place, Place.from_repr(place.to_repr())) raw_place = { Place.TYPE_KEY: "type", Place.NAME_KEY: "name", Place.COORDINATES_KEY: { Coordinates.LAT_KEY: 46.0748, Coordinates.LON_KEY: 11.1217 } } self.assertIsInstance(Place.from_repr(raw_place), Place)
def test_get_place_sensors_not_found(self): place_id = "id" place = Place.from_repr({ Place.ID_KEY: "id", Place.TYPE_KEY: "type", Place.NAME_KEY: "name", Place.COORDINATES_KEY: { Coordinates.LAT_KEY: 46.0748, Coordinates.LON_KEY: 11.1217 } }) sensors = [ Sensor.from_repr({ Sensor.ID_KEY: "id", Sensor.TYPE_KEY: "type", Sensor.PLACE_ID_KEY: "place_id" }) ] self.dao_collector.place_dao.get_place = Mock( side_effect=DaoEntryNotFound) self.dao_collector.sensor_dao.search_place_sensors = Mock( return_value=sensors) response = self.client.get( f"/place/sensor?{PlaceSensorsResource.PLACE_ID_KEY}={place_id}") self.assertEqual(404, response.status_code)
def test_get_place_sensors(self): place_id = "id" place = Place.from_repr({ Place.ID_KEY: "id", Place.TYPE_KEY: "type", Place.NAME_KEY: "name", Place.COORDINATES_KEY: { Coordinates.LAT_KEY: 46.0748, Coordinates.LON_KEY: 11.1217 } }) sensors = [ Sensor.from_repr({ Sensor.ID_KEY: "id", Sensor.TYPE_KEY: "type", Sensor.PLACE_ID_KEY: "place_id" }) ] self.dao_collector.place_dao.get_place = Mock(return_value=place) self.dao_collector.sensor_dao.search_place_sensors = Mock( return_value=sensors) response = self.client.get( f"/place/sensor?{PlaceSensorsResource.PLACE_ID_KEY}={place_id}") self.assertEqual(200, response.status_code) self.assertEqual([sensor.to_repr() for sensor in sensors], json.loads(response.data))
def save_place(self, place: Place) -> None: """ Save a place :param place: the place to save """ self._places[place.get_id()] = place
def update_place(self, place: Place) -> None: """ Update a place if present, if not create one :param place: the place to update """ try: place_to_update = self.get_place(place.get_id()) place_to_update.update(place) except DaoEntryNotFound: place_to_update = place self.save_place(place_to_update)
def test_get_place_missing_parameter(self): place = Place.from_repr({ Place.ID_KEY: "id", Place.TYPE_KEY: "type", Place.NAME_KEY: "name", Place.COORDINATES_KEY: { Coordinates.LAT_KEY: 46.0748, Coordinates.LON_KEY: 11.1217 } }) self.dao_collector.place_dao.get_place = Mock(return_value=place) response = self.client.get("/place") self.assertEqual(400, response.status_code)
def test_get_place(self): place_id = "id" place = Place.from_repr({ Place.ID_KEY: "id", Place.TYPE_KEY: "type", Place.NAME_KEY: "name", Place.COORDINATES_KEY: { Coordinates.LAT_KEY: 46.0748, Coordinates.LON_KEY: 11.1217 } }) self.dao_collector.place_dao.get_place = Mock(return_value=place) response = self.client.get( f"/place?{PlaceResource.PLACE_ID_KEY}={place_id}") self.assertEqual(200, response.status_code) self.assertEqual(place.to_repr(), json.loads(response.data))
def test_get_place_sensors_missing_parameter(self): place_id = "id" place = Place.from_repr({ Place.ID_KEY: "id", Place.TYPE_KEY: "type", Place.NAME_KEY: "name", Place.COORDINATES_KEY: { Coordinates.LAT_KEY: 46.0748, Coordinates.LON_KEY: 11.1217 } }) sensors = [ Sensor.from_repr({ Sensor.ID_KEY: "id", Sensor.TYPE_KEY: "type", Sensor.PLACE_ID_KEY: "place_id" }) ] self.dao_collector.place_dao.get_place = Mock(return_value=place) self.dao_collector.sensor_dao.search_place_sensors = Mock( return_value=sensors) response = self.client.get("/place/sensor") self.assertEqual(400, response.status_code)
def put(self): posted_data = request.get_json() if posted_data is None: return {"message": "Malformed request: data is missing"}, 400 try: place: Place = Place.from_repr(posted_data) except KeyError: return { "message": "Malformed request: could not parse posted data" }, 400 except Exception: return { "message": "Internal server error: could not parse posted data" }, 500 try: self._dao_collector.place_dao.update_place(place) except Exception: return { "message": "Internal server error: could not update the place" }, 500 return {}
def test_get_coordinates(self): place = Place("id", "type", "name", Coordinates(46.0748, 11.1217)) self.assertEqual(Coordinates(46.0748, 11.1217), place.get_coordinates())
def test_update(self): place = Place("id", "type", "name", Coordinates(46.0748, 11.1217)) place_updated = Place("id", "type2", "name2", Coordinates(46.0750, 11.1220)) self.assertEqual(place_updated, place.update(place_updated))