def test_find_appointment_by_desc_exists(self): with self.app.app_context(): db = mainappbp.get_db(self.app) self.insert_sample(db) only_one = service.find_by_description("ins") two = service.find_by_description("#tag") assert len(only_one) == 1 assert len(two) == 2
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_get_by_description(self): with self.app.app_context(): self.insert_sample(mainappbp.get_db(self.app)) keyword = "serting" response = self.test_client.get('/api/v1.0/' + keyword) assert response.status_code == 200 assert response.content_type == "application/json" data = json.loads(response.data) assert len(data) == 1 assert data[0]['id'] == '1'
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 add_appointment(appointment): """ Adds appointment object to database @param Appointment """ try: db = mainappbp.get_db(current_app) cursor = db.cursor() cursor.execute('INSERT INTO '+__tbl_name__ +'(appointment_time,description) VALUES (?, ?)', list( [appointment.appointment_time,appointment.description] ) ) db.commit() except: if mainappbp.get_db(current_app)==None: raise RuntimeError("No db connection") if appointment.appointment_time == None or appointment.description == None : raise ValueError("Both attributes of appointment cannot be null")
def test_add_appointment_happyday(self): with self.app.app_context(): db = mainappbp.get_db(self.app) date = datetime.datetime(2017, 10, 21, 12, 00) appointment = Appointment(None, date, "Birthday") service.add_appointment(appointment) cursor = db.cursor() result = cursor.execute( "SELECT * from tbl_appointments").fetchone() assert result[0] == 1 # '2017-10-21 12:00:00' assert result[1] == date.strftime(utils.SQL_DATE_TIME_FORMAT) assert result[2] == "Birthday"
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 test_get_all_appointments(self): with self.app.app_context(): self.insert_sample(mainappbp.get_db(self.app)) response = self.test_client.get('/api/v1.0/') assert response.status_code == 200 assert response.content_type == "application/json" expected = [{ "id": "1", "appointment_time": "2017-09-13 08:08:00", "description": "inserting data #tag" }, { "id": "2", "appointment_time": "2017-09-13 08:08:30", "description": "another appointment #tag" }] data = json.loads(response.data) for exp, got in zip(expected, data): assert exp['id'] == got['id'] assert exp['appointment_time'] == got['appointment_time'] assert exp['description'] == got['description']
def test_get_db_connxn(self): with self.app.app_context(): connection = mainappbp.get_db(self.app) assert g.sqlite_db == mainappbp.get_db(self.app)
def test_find_appointment_by_desc_not_found(self): with self.app.app_context(): db = mainappbp.get_db(self.app) self.insert_sample(db) rs = service.find_by_description("%") assert service.find_by_description("%") == []