def test_from_and_to_dict(self): utl.add_categories_to_db() category = utl.CATEGORIES[-1] utl.add_sensors_to_db() sensor = utl.SENSORS[-1] r = Reading() reading = { "data": 123, "category": category["name"], "sensor": sensor["name"], "timestamp": 123, } # Test the from_dict() method r.from_dict(reading) self.assertEqual(r.data, reading["data"]) self.assertEqual(r.category.name, reading["category"]) self.assertEqual(r.sensor.name, reading["sensor"]) self.assertEqual(r.timestamp, reading["timestamp"]) # Test the to_dict() method r_dict = r.to_dict() self.assertEqual(r_dict["data"], reading["data"]) self.assertEqual(r_dict["category"]["name"], reading["category"]) self.assertEqual(r_dict["sensor"]["name"], reading["sensor"]) self.assertEqual(r_dict["timestamp"], reading["timestamp"])
def test_add_category(self): utl.add_categories_to_db() category = utl.CATEGORIES[-1] r = Reading() # Test adding a category to a reading r.add_category(category["name"]) self.assertEqual(r.category.name, category["name"])
def test_add_sensor(self): utl.add_categories_to_db() utl.add_sensors_to_db() sensor = utl.SENSORS[-1] r = Reading() # Test adding a sensor to a reading r.add_sensor(sensor["name"]) self.assertEqual(r.sensor.name, sensor["name"])
def get(cls, id:int): user = UserModel.find_by_id(id) if not user: return {'message':'User not found'}, 400 #reading_list = reading_list_schema.dump(ReadingModel.find_user_list(id, include_notebook_info=True)) reading_list = ReadingModel.find_user_list(id, include_notebook_info=True) return {"readings": reading_notebook_list_schema.dump(reading_list)}, 200
def get(cls): user = request.args['user'] notebook = request.args['notebook'] reading = ReadingModel.find(user, notebook) if reading is None: return {'message': 'Reading do not exists'}, 400 return {'data': reading_schema.dump(reading)}
def delete(cls, id): reading = ReadingModel.find_by_id(id) if reading is None: return {'message': 'Reading do not exists'}, 400 reading.delete_from_db() return {'message': 'Deleted'}, 200
def post(cls): reading_json = request.get_json() user_id = get_jwt_identity() if ReadingModel.exists(user_id, reading_json["notebook_id"]): return {'message': 'Reading exists'}, 400 reading_json['user_id'] = user_id reading = reading_schema.load(reading_json) reading.save_to_db() return { 'message': 'Reading created successfully ', 'data': reading_schema.dump(reading) }, 201
def store_readings(self, readings): db.session.add(Reading(readings)) db.session.commit()
def get(cls, id): reading = ReadingModel.find_by_id(id) if reading is None: return {'message': 'Reading do not exists'}, 400 return {'data': reading_schema.dump(reading)}, 200