def get_all_appointments(): """ Returns appointment object in DB. """ db = mainappbp.get_db(current_app) cursor = db.cursor() cursor.execute('SELECT * FROM '+ __tbl_name__) result_set = cursor.fetchall() return [Appointment(result[0],utils.format_date(result[1]),result[2]) for result in result_set]
def test_appointment_serialize(self): appointment = Appointment(1, utils.format_date('2017-09-12 08:20:00'), 'this is test data') data = json.dumps(appointment.serialize()) expected = json.dumps({ "id": "1", "appointment_time": "2017-09-12 08:20:00", "description": "this is test data" }) assert expected == data
def find_by_description(keyword): """ Returns list of appointment objects selectively. @param keyword """ db = mainappbp.get_db(current_app) cursor = db.cursor() cursor.execute("SELECT * FROM "+ __tbl_name__+" WHERE instr(description,?)",(keyword,)) result_set = cursor.fetchall() return [Appointment(result[0],utils.format_date(result[1]),result[2]) for result in result_set]
def test_get_all_appointment_db_with_values(self): with self.app.app_context(): db = mainappbp.get_db(self.app) self.insert_sample(db) expected = [{ 'time': utils.format_date('2017-09-13 08:08:00'), 'desc': 'inserting data #tag' }, { 'time': utils.format_date('2017-09-13 08:08:30'), 'desc': 'another appointment #tag' }] #should return all the rows converted to appointments model result_set = service.get_all_appointments() for ex, result in zip(expected, result_set): assert ex['time'] == result.appointment_time assert ex['desc'] == result.description
def create_appointment(): """ validates incoming json data and saves """ print request.json if not request.json: abort(400) if not 'appointment_time' in request.json: abort(400) if not 'description' in request.json: abort(400) appointment = Appointment( None, utils.format_date(request.json['appointment_time']), request.json['description']) app_repository.add_appointment(appointment) return jsonify({'message': 'Appointment Saved !'}), 200
def test_add_appointments_invalid_request(self): with self.app.app_context(): #empty data response = self.test_client.post('/api/v1.0/', data="", content_type="application/json") assert response.status_code == 400 #with content type default appointment = Appointment(1, utils.format_date('2017-09-12 08:20:00'), 'this is test data') data = json.dumps(appointment.serialize()) response = self.test_client.post('/api/v1.0/', data=data) assert response.status_code == 400 #with Appointment.appointment_time None data = json.dumps({"description": "this is test data"}) response = self.test_client.post('/api/v1.0/', data=data) assert response.status_code == 400 #with Appointment.description None data = json.dumps({"appointment_time": "2017-09-12 08:20:00"}) response = self.test_client.post('/api/v1.0/', data=data) assert response.status_code == 400