I am working on a web app using flask and postgresql. I am wrting unit tests for all my view functions. However, I look up the doc and some other stackoverflow posts like this for testing post request and it doesn't work.
This is a method in a class inherited from unittest.TestCase
def test__student_receive_data(self):
built_in_ids = populate_db()
role_student = query_role('Student')
student = query_user(built_in_ids['student_id'])
student.role = role_student
lab = query_lab(built_in_ids['lab_id'])
experiment = query_experiment(built_in_ids['experiment_id'])
self.login(student.username, student.password)
# Start a new session
student = query_user(student.id)
student.labs.append(lab)
ds.commit()
observationsForOneExperiment = [{'labId': lab.id,
'experimentName': experiment.name,
'observation': randlength_word()}]
observationsGroupByStudent = [{'studentName': student.name,
'observationsForOneExperiment': observationsForOneExperiment}]
data_to_be_sent = dict(observationsGroupByStudent=observationsGroupByStudent)
cprint(json.dumps(data_to_be_sent))
rv = self.app.post('/_student_receive_data', data=json.dumps(data_to_be_sent), follow_redirects=True)
self.assertEqual(rv.status_code, 200)
This is a view function which receives post request from the test function above
# store incoming data into the database
@app.route('/_student_receive_data', methods=['POST'])
@permission_required(m.Permission.DATA_ENTRY)
def _student_receive_data():
# get data from client
jsonData = request.get_json()
print(current_user)
print(jsonData)
print(request.form)
# check that data is in the appropriate format
err_msg = check_existence(jsonData, 'observationsGroupByStudent')
if err_msg != '':
return err_json(err_msg)
err_msg = add_observations_sent_by_students(jsonData['observationsGroupByStudent'])
if err_msg != '':
return err_json(err_msg)
return normal_json(jsonData)
The print out in the terminal(Clearly, nothing is received):
User<id: pip, username: pip, password: pip, role: Student, classes: ['bio101_16fall'], labs: ['test_bio101_amy']>
None
ImmutableMultiDict([])
Aucun commentaire:
Enregistrer un commentaire