def test4(self): app = tested_app.test_client() reply = app.post('/doodles', data=json.dumps({ "title": "test4", "options": [] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['pollnumber'], 4) reply = app.delete('/doodles/4') app = tested_app.test_client() reply = app.post('/doodles', data=json.dumps({ "title": "test5", "options": [] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['pollnumber'], 5) app = tested_app.test_client() reply = app.post('/doodles', data=json.dumps({ "title": "test6", "options": [] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['pollnumber'], 6) reply = app.delete('/doodles/6') app = tested_app.test_client() reply = app.post('/doodles', data=json.dumps({ "title": "test6", "options": [] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['pollnumber'], 7)
def test4(self): # test POLLNUMBER never decreases app = tested_app.test_client() # Create a new poll reply = app.post('/doodles', data=json.dumps({ "title": "poll1", "options": ["1", "2", "3"] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) # Save poll number pollNumber = body['pollnumber'] # Create another poll reply = app.post('/doodles', data=json.dumps({ "title": "poll1", "options": ["1", "2", "3"] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) # Check the new poll number is greater then the previous self.assertGreater(int(body['pollnumber']), int(pollNumber)) # Delete the last poll reply = app.delete('/doodles/' + str(body['pollnumber']), content_type='application/json') # Create a new poll reply = app.post('/doodles', data=json.dumps({ "title": "poll1", "options": ["1", "2", "3"] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) # Verify the new poll number is greater then the saved one self.assertGreater(int(body['pollnumber']), int(pollNumber)) # Delete the last poll reply = app.delete('/doodles/' + str(body['pollnumber']), content_type='application/json') # Create a new poll reply = app.post('/doodles', data=json.dumps({ "title": "poll1", "options": ["1", "2", "3"] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) # Verify the new poll number is greater then the saved one self.assertGreater(int(body['pollnumber']), int(pollNumber))
def test1(self): # allpolls app = tested_app.test_client() reply = app.get('/calc/sum?m=1&n=2') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['result'], '3') reply = app.get('/calc/diff?m=1&n=2') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['result'], '-1')
def test4(self): # app = tested_app.test_client() reply =app.post('/doodles', data=json.dumps({"title" : "pooltest", "options" : ["1", "2", "3"] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) firstPOLLNUMBER = body['pollnumber'] reply = app.post('/doodles', data=json.dumps({"title" : "pooltest2", "options" : ["1", "2", "3"] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) secondPOLLNUMBER = body['pollnumber'] self.assertGreater( secondPOLLNUMBER,firstPOLLNUMBER) reply = app.delete('/doodles/'+str(firstPOLLNUMBER)) reply = app.post('/doodles', data=json.dumps({"title" : "pooltest3", "options" : ["1", "2", "3"] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) thirdPOLLNUMBER = body['pollnumber'] self.assertGreater(thirdPOLLNUMBER, secondPOLLNUMBER) self.assertGreater(thirdPOLLNUMBER, firstPOLLNUMBER) reply = app.delete('/doodles/'+str(secondPOLLNUMBER)) reply = app.post('/doodles', data=json.dumps({"title" : "pooltest4", "options" : ["1", "2", "3"] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) fouthPOLLNUMBER = body['pollnumber'] self.assertGreater(fouthPOLLNUMBER, firstPOLLNUMBER) self.assertGreater(fouthPOLLNUMBER, secondPOLLNUMBER) self.assertGreater(fouthPOLLNUMBER, thirdPOLLNUMBER)
def test_home_view(self): # creating a FlaskClient instance to interact with the app tested_app.testing = True client = tested_app.test_client() # calling /hello endpoint hello = client.get('/hello') # asserting the body body = json.loads(str(hello.data, 'utf8')) self.assertEqual(body['Hello'], 'World')
def test_home_view(self): # creating a FlaskClient instance to interact with the app tested_app.testing = True client = tested_app.test_client() # calling / endpoint home = client.get('/') # asserting the body body = json.loads(str(home.data, 'utf8')) self.assertEqual(len(body), 0)
def test_guests(self): app = tested_app.test_client() # check number of guests reply = app.get('/reservation/1/guests') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body["nguests"], 0) # change the list of guests new_guests = ["Lucia", "Elisabetta"] reply = app.put('/reservation/1/guests', data=json.dumps({ "guests": new_guests, "password": "******" }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['guests'], new_guests) # attempt changing list of guests with wrong password new_guests = ["Filippa", "Paolino"] reply = app.put('/reservation/1/guests', data=json.dumps({ "guests": new_guests, "password": "******" }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['error'], "wrong password") # attempt changing list of guests of past reservation # time passed set_test_time("01/04/2020 23:30") new_guests = ["Lucia", "Elisabetta"] reply = app.put('/reservation/1/guests', data=json.dumps({ "guests": new_guests, "password": "******" }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body["error"], 'past reservation')
def test3(self): app = tested_app.test_client() reply = app.post('/quizzes', data=json.dumps({ "questions": [ { "question": "What's the answer to all questions?", "answers": [ { "answer": "33", "correct": 0 }, { "answer": "42", "correct": 1 }, { "answer": "1", "correct": 0 } ] }, { "question": "What's the answer to all questions?", "answers": [ { "answer": "33", "correct": 0 }, { "answer": "42", "correct": 1 }, { "answer": "1", "correct": 0 } ] } ] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['quiznumber'], 0) # delete it pass
def setUp(self): app = tested_app.test_client() set_test_time("01/04/2020 10:30") # no loaded reservation reply = app.get('/reservations/count') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['nreservations'], 0) # create 3 reservations reply = app.post('/reservations', data=json.dumps({ "name" : "Stefano", "password" : "ciaone", "date" : "01/04/2020 20:30", "guests": ["Remo", "Davide"], }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['reservationid'], 0) reply = app.post('/reservations', data=json.dumps({ "name" : "Antonio", "password" : "password", "date" : "01/04/2020 22:30", "guests": [], }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['reservationid'], 1) reply = app.post('/reservations', data=json.dumps({ "name" : "Franco", "password" : "franco", "date" : "01/04/2020 12:30", "guests": ["Carlo", "Chiara"], }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['reservationid'], 2)
def test_create_reservations(self): app = tested_app.test_client() # get the number of future reservations reply = app.get('/reservations/count') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['nreservations'], 3) # time passes set_test_time("01/04/2020 14:00") # get the number of future reservations reply = app.get('/reservations/count') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['nreservations'], 2)
def test3_quizNumberInvariant(self): # allpolls # given app = tested_app.test_client() # when reply = app.post('/quizzes', data=self.dummyJsonQuiz(), content_type='application/json') actualnumber = json.loads(str(reply.data, 'utf8'))['quiznumber'] app.delete('/quiz/0') # then reply = app.post('/quizzes', data=self.dummyJsonQuiz(), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['quiznumber'], actualnumber + 1)
def test4(self): #allpolls app = tested_app.test_client() #create a doodle 4 reply = app.post('/doodles', data=json.dumps({ "title": "poll1", "options": ["1", "2", "3"] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['pollnumber'], 4) #create a doodle 5 reply = app.post('/doodles', data=json.dumps({ "title": "poll1", "options": ["1", "2", "3"] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['pollnumber'], 5) # delete the doodle reply = app.delete('/doodles/5') body = json.loads(str(reply.data, 'utf8')) # delete the doodle reply = app.delete('/doodles/4') body = json.loads(str(reply.data, 'utf8')) #create a doodle reply = app.post('/doodles', data=json.dumps({ "title": "poll1", "options": ["1", "2", "3"] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) #check ID is only increasing self.assertEqual(body['pollnumber'], 6)
def test_retrieve_reservations(self): app = tested_app.test_client() #get the second reservation reply = app.get('/reservation/1') body = json.loads(str(reply.data, 'utf8')) assertion = { "reservation" : { "id" : 1, "name" : "Antonio", "date" : "01/04/2020 22:30", "guests" : [] } } self.assertEqual(body, assertion) #time passed set_test_time("01/04/2020 15:30") # get future reservations reply = app.get('/reservations') body = json.loads(str(reply.data, 'utf8')) assertion = { "futurereservations": [ { "id" : 0, "name" : "Stefano", "date" : "01/04/2020 20:30", "guests" : ["Remo", "Davide"], }, { "id" : 1, "name" : "Antonio", "date" : "01/04/2020 22:30", "guests" : [], }, ] } self.assertEqual(body, assertion) #try to retrieve a non-existing reservation reply = app.get('/reservation/100') self.assertEqual(reply.status_code, 404)
def test_delete_reservations(self): app = tested_app.test_client() # delete a reservation reply = app.delete('/reservation/0', data=json.dumps({ "password" : "ciaone" }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body["reservationid"], '0') #try to retrieve the deleted reservation reply = app.get('/reservation/0') self.assertEqual(reply.status_code, 410) # attempt deleting a reservation with wrong password reply = app.delete('/reservation/1', data=json.dumps({ "password" : "a_wrong_psw" }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body["error"], 'wrong password') # attempt deleting a past reservation # time passed set_test_time("01/04/2020 23:30") reply = app.delete('/reservation/1', data=json.dumps({ "password" : "password" }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body["error"], 'past reservation')
def test1(self): #allpolls app = tested_app.test_client() #create 3 doodles reply = app.post('/doodles', data=json.dumps({ "title": "poll1", "options": ["1", "2", "3"] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['pollnumber'], 1) reply = app.post('/doodles', data=json.dumps({ "title": "poll2", "options": ["1", "2"] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['pollnumber'], 2) reply = app.post('/doodles', data=json.dumps({ "title": "poll3", "options": ["pizza", "disco"] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['pollnumber'], 3) #get the three doodles reply = app.get('/doodles') body = json.loads(str(reply.data, 'utf8')) self.assertEqual( body, { "activepolls": [{ "id": 1, "options": { "1": [], "2": [], "3": [] }, "title": "poll1", "winners": ["1", "2", "3"] }, { "id": 2, "options": { "1": [], "2": [] }, "title": "poll2", "winners": ["1", "2"] }, { "id": 3, "options": { "disco": [], "pizza": [] }, "title": "poll3", "winners": ["pizza", "disco"] }] })
def test3(self): # /quiz app = tested_app.test_client() reply = app.post('/quizzes', data=json.dumps({ "questions": [{ "question": "What's the answer to all questions?", "answers": [{ "answer": "33", "correct": 0 }, { "answer": "42", "correct": 1 }, { "answer": "1", "correct": 0 }] }, { "question": "What's the answer to all questions?", "answers": [{ "answer": "33", "correct": 0 }, { "answer": "42", "correct": 1 }, { "answer": "1", "correct": 0 }] }] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) id = body['quiznumber'] reply = app.delete('/quiz/' + str(id)) reply = app.post('/quizzes', data=json.dumps({ "questions": [{ "question": "What's the answer to all questions?", "answers": [{ "answer": "33", "correct": 0 }, { "answer": "42", "correct": 1 }, { "answer": "1", "correct": 0 }] }, { "question": "What's the answer to all questions?", "answers": [{ "answer": "33", "correct": 0 }, { "answer": "42", "correct": 1 }, { "answer": "1", "correct": 0 }] }] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) id_new = body['quiznumber'] self.assertLess(id, id_new, 'message') print('' + str(id) + ' < ' + str(id_new))
def test3(self): #person poll app = tested_app.test_client() # vote ok reply = app.put('/doodles/1', data=json.dumps({ "person": "fred", "option": "1" }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body, {"winners": ["1"]}) # vote ok reply = app.put('/doodles/1', data=json.dumps({ "person": "fred", "option": "2" }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body, {"winners": ["1", "2"]}) # vote ok reply = app.put('/doodles/1', data=json.dumps({ "person": "barney", "option": "1" }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body, {"winners": ["1"]}) #get votes from a person who voted reply = app.get('/doodles/1/fred') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body, {'votedoptions': ['1', '2']}) #get votes from a person who did not vote reply = app.get('/doodles/1/wilma') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body, {'votedoptions': []}) #delete votes from a person who voted reply = app.delete('/doodles/1/fred') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body, {'removed': True}) reply = app.get('/doodles/1/fred') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body, {'votedoptions': []}) #delete votes from a person who did not vote reply = app.delete('/doodles/1/wilma') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body, {'removed': False}) #get votes from a non-existing poll reply = app.get('/doodles/13/fred') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 404) self.assertEqual( body, { "code": 404, "description": "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.", "message": "404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again." }) #get votes from a previously existing poll reply = app.get('/doodles/2/fred') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 410) self.assertEqual( body, { "code": 410, "description": "The requested URL is no longer available on this server and there is no forwarding address. If you followed a link from a foreign page, please contact the author of this page.", "message": "410 Gone: The requested URL is no longer available on this server and there is no forwarding address. If you followed a link from a foreign page, please contact the author of this page." })
def test2(self): # /quiz app = tested_app.test_client() # retrieve existing quiz reply = app.get('/quiz/2') body = json.loads(str(reply.data, 'utf8')) self.assertEqual( body, { "id": 2, "questions": [{ "answers": [{ "answer": "Fred" }, { "answer": "Barney" }, { "answer": "Dyno" }], "question": "Who's Wilma's husband?" }, { "answers": [{ "answer": "Wilma" }, { "answer": "Pebbles" }, { "answer": "Betty" }], "question": "Who's Fred's daughter?" }, { "answers": [{ "answer": "Dyno" }, { "answer": "Brontosaure" }, { "answer": "BamBam" }], "question": "Who's Flintstones' pet?" }] }) # retrieve non-existing quiz GET reply = app.get('/quiz/12') self.assertEqual(reply.status_code, 404) body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['code'], 404) # get question reply = app.get('/quiz/2/question', content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual( body, { "answers": [{ "answer": "Fred" }, { "answer": "Barney" }, { "answer": "Dyno" }], "question": "Who's Wilma's husband?" }) # correct answer reply = app.put('/quiz/2/question/Fred', content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body, {"msg": 1}) # get question reply = app.get('/quiz/2/question', content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual( body, { "answers": [{ "answer": "Wilma" }, { "answer": "Pebbles" }, { "answer": "Betty" }], "question": "Who's Fred's daughter?" }) # answer ok reply = app.put('/quiz/2/question/Pebbles', content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body, {"msg": 2}) # answer ok: won a million clams reply = app.put('/quiz/2/question/Dyno', content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body, {"msg": "you won 1 million clams!"}) # double call to complete quiz reply = app.get('/quiz/2/question', content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body, {"msg": "completed quiz"}) # double call to complete quiz question with answer reply = app.put('/quiz/2/question/ciao', content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body, {"msg": "completed quiz"}) # vote non-existing option reply = app.put('/quiz/1/question/35', content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body, {"msg": "non-existing answer!"}) # correct answer reply = app.put('/quiz/0/question/42', content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body, {"msg": 1}) # wrong answer reply = app.put('/quiz/0/question/1', content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body, {"msg": "you lost!"}) # double call to lost quiz reply = app.put('/quiz/0/question/21', content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body, {"msg": "you lost!"}) # triple call to lost quiz reply = app.get('/quiz/0/question', content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body, {"msg": "you lost!"}) # delete quiz reply = app.delete('/quiz/2') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body, {"answered_questions": 4, "total_questions": 3}) # two loaded quizzes reply = app.get('/quizzes/loaded') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['loaded_quizzes'], 2) reply = app.get('/quizzes') body = json.loads(str(reply.data, 'utf8')) self.assertEqual( body, { "loadedquizzes": [{ "id": 0, "questions": [{ "answers": [{ "answer": "33" }, { "answer": "42" }, { "answer": "1" }], "question": "What's the answer to all questions?" }, { "answers": [{ "answer": "33" }, { "answer": "42" }, { "answer": "1" }], "question": "What's the answer to all questions?" }] }, { "id": 1, "questions": [{ "answers": [{ "answer": "Fred" }, { "answer": "Barney" }, { "answer": "Dyno" }], "question": "Who's Wilma's husband?" }, { "answers": [{ "answer": "Wilma" }, { "answer": "Pebbles" }, { "answer": "Betty" }], "question": "Who's Fred's daughter?" }, { "answers": [{ "answer": "Dyno" }, { "answer": "Brontosaure" }, { "answer": "BamBam" }], "question": "Who's Flintstones' pet?" }] }] }) # delete previously deleted quiz reply = app.delete('/quiz/2') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 410) # delete non-existing quiz reply = app.delete('/quiz/12') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 404) # get previously existing quiz reply = app.get('/quiz/2') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 410) # create new quiz after deletion reply = app.post('/quizzes', data=json.dumps({ "questions": [{ "question": "What's the answer to all questions?", "answers": [{ "answer": "33", "correct": 0 }, { "answer": "42", "correct": 1 }, { "answer": "1", "correct": 0 }] }, { "question": "What's the answer to all questions?", "answers": [{ "answer": "33", "correct": 0 }, { "answer": "42", "correct": 1 }, { "answer": "1", "correct": 0 }] }] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['quiznumber'], 3) reply = app.get('/quizzes') body = json.loads(str(reply.data, 'utf8')) self.assertEqual( body, { "loadedquizzes": [{ "id": 0, "questions": [{ "answers": [{ "answer": "33" }, { "answer": "42" }, { "answer": "1" }], "question": "What's the answer to all questions?" }, { "answers": [{ "answer": "33" }, { "answer": "42" }, { "answer": "1" }], "question": "What's the answer to all questions?" }] }, { "id": 1, "questions": [{ "answers": [{ "answer": "Fred" }, { "answer": "Barney" }, { "answer": "Dyno" }], "question": "Who's Wilma's husband?" }, { "answers": [{ "answer": "Wilma" }, { "answer": "Pebbles" }, { "answer": "Betty" }], "question": "Who's Fred's daughter?" }, { "answers": [{ "answer": "Dyno" }, { "answer": "Brontosaure" }, { "answer": "BamBam" }], "question": "Who's Flintstones' pet?" }] }, { "id": 3, "questions": [{ "answers": [{ "answer": "33" }, { "answer": "42" }, { "answer": "1" }], "question": "What's the answer to all questions?" }, { "answers": [{ "answer": "33" }, { "answer": "42" }, { "answer": "1" }], "question": "What's the answer to all questions?" }] }] }) # delete quiz reply = app.delete('/quiz/1') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body, {"answered_questions": 0, "total_questions": 3})
def test1(self): # allpolls app = tested_app.test_client() # no loaded quiz reply = app.get('/quizzes/loaded') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['loaded_quizzes'], 0) # create 3 quizzes reply = app.post('/quizzes', data=json.dumps({ "questions": [{ "question": "What's the answer to all questions?", "answers": [{ "answer": "33", "correct": 0 }, { "answer": "42", "correct": 1 }, { "answer": "1", "correct": 0 }] }, { "question": "What's the answer to all questions?", "answers": [{ "answer": "33", "correct": 0 }, { "answer": "42", "correct": 1 }, { "answer": "1", "correct": 0 }] }] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['quiznumber'], 0) reply = app.post('/quizzes', data=json.dumps({ "questions": [{ "question": "Who's Wilma's husband?", "answers": [{ "answer": "Fred", "correct": 1 }, { "answer": "Barney", "correct": 0 }, { "answer": "Dyno", "correct": 0 }] }, { "question": "Who's Fred's daughter?", "answers": [{ "answer": "Wilma", "correct": 0 }, { "answer": "Pebbles", "correct": 1 }, { "answer": "Betty", "correct": 0 }] }, { "question": "Who's Flintstones' pet?", "answers": [{ "answer": "Dyno", "correct": 1 }, { "answer": "Brontosaure", "correct": 0 }, { "answer": "BamBam", "correct": 0 }] }] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['quiznumber'], 1) reply = app.post('/quizzes', data=json.dumps({ "questions": [{ "question": "Who's Wilma's husband?", "answers": [{ "answer": "Fred", "correct": 1 }, { "answer": "Barney", "correct": 0 }, { "answer": "Dyno", "correct": 0 }] }, { "question": "Who's Fred's daughter?", "answers": [{ "answer": "Wilma", "correct": 0 }, { "answer": "Pebbles", "correct": 1 }, { "answer": "Betty", "correct": 0 }] }, { "question": "Who's Flintstones' pet?", "answers": [{ "answer": "Dyno", "correct": 1 }, { "answer": "Brontosaure", "correct": 0 }, { "answer": "BamBam", "correct": 0 }] }] }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['quiznumber'], 2) # get the three quizzes reply = app.get('/quizzes/loaded') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body['loaded_quizzes'], 3) # get the three quizzes reply = app.get('/quizzes') body = json.loads(str(reply.data, 'utf8')) self.assertEqual( body, { "loadedquizzes": [{ "id": 0, "questions": [{ "answers": [{ "answer": "33" }, { "answer": "42" }, { "answer": "1" }], "question": "What's the answer to all questions?" }, { "answers": [{ "answer": "33" }, { "answer": "42" }, { "answer": "1" }], "question": "What's the answer to all questions?" }] }, { "id": 1, "questions": [{ "answers": [{ "answer": "Fred" }, { "answer": "Barney" }, { "answer": "Dyno" }], "question": "Who's Wilma's husband?" }, { "answers": [{ "answer": "Wilma" }, { "answer": "Pebbles" }, { "answer": "Betty" }], "question": "Who's Fred's daughter?" }, { "answers": [{ "answer": "Dyno" }, { "answer": "Brontosaure" }, { "answer": "BamBam" }], "question": "Who's Flintstones' pet?" }] }, { "id": 2, "questions": [{ "answers": [{ "answer": "Fred" }, { "answer": "Barney" }, { "answer": "Dyno" }], "question": "Who's Wilma's husband?" }, { "answers": [{ "answer": "Wilma" }, { "answer": "Pebbles" }, { "answer": "Betty" }], "question": "Who's Fred's daughter?" }, { "answers": [{ "answer": "Dyno" }, { "answer": "Brontosaure" }, { "answer": "BamBam" }], "question": "Who's Flintstones' pet?" }] }] })
def test2(self): #single poll app = tested_app.test_client() # retrieve existing doodle GET reply = app.get('/doodles/2') body = json.loads(str(reply.data, 'utf8')) self.assertEqual( body, { "id": 2, "options": { "1": [], "2": [] }, "title": "poll2", "winners": ["1", "2"] }) #retrieve non-existing doodle GET reply = app.get('/doodles/12') self.assertEqual(reply.status_code, 404) body = json.loads(str(reply.data, 'utf8')) self.assertEqual( body, { "code": 404, "description": "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.", "message": "404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again." }) # vote ok reply = app.put('/doodles/2', data=json.dumps({ "person": "fred", "option": "1" }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body, {"winners": ["1"]}) # vote ok reply = app.put('/doodles/2', data=json.dumps({ "person": "fred", "option": "2" }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body, {"winners": ["1", "2"]}) # vote ok reply = app.put('/doodles/2', data=json.dumps({ "person": "barney", "option": "1" }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body, {"winners": ["1"]}) # vote replica reply = app.put('/doodles/2', data=json.dumps({ "person": "barney", "option": "1" }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 400) self.assertEqual( body, { "code": 400, "description": "The browser (or proxy) sent a request that this server could not understand.", "message": "400 Bad Request: The browser (or proxy) sent a request that this server could not understand." }) # vote non-existing option reply = app.put('/doodles/2', data=json.dumps({ "person": "wilma", "option": "8" }), content_type='application/json') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 400) self.assertEqual( body, { "code": 400, "description": "The browser (or proxy) sent a request that this server could not understand.", "message": "400 Bad Request: The browser (or proxy) sent a request that this server could not understand." }) # delete doodle reply = app.delete('/doodles/2') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(body, {"winners": ["1"]}) reply = app.get('/doodles') body = json.loads(str(reply.data, 'utf8')) self.assertEqual( body, { "activepolls": [{ "id": 1, "options": { "1": [], "2": [], "3": [] }, "title": "poll1", "winners": ["1", "2", "3"] }, { "id": 3, "options": { "disco": [], "pizza": [] }, "title": "poll3", "winners": ["pizza", "disco"] }] }) # delete previously doodle reply = app.delete('/doodles/2') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 410) self.assertEqual( body, { "code": 410, "description": "The requested URL is no longer available on this server and there is no forwarding address. If you followed a link from a foreign page, please contact the author of this page.", "message": "410 Gone: The requested URL is no longer available on this server and there is no forwarding address. If you followed a link from a foreign page, please contact the author of this page." }) #delete non-existing doodle reply = app.delete('/doodles/12') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 404) self.assertEqual( body, { "code": 404, "description": "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.", "message": "404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again." }) #get previously existing doodle reply = app.get('/doodles/2') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 410) self.assertEqual( body, { "code": 410, "description": "The requested URL is no longer available on this server and there is no forwarding address. If you followed a link from a foreign page, please contact the author of this page.", "message": "410 Gone: The requested URL is no longer available on this server and there is no forwarding address. If you followed a link from a foreign page, please contact the author of this page." })