Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. import unittest
  2. import os
  3. #from authenticated import *
  4. #from question_add import *
  5. from question_class2 import *
  6. from response_class import *
  7. from survey_class3 import *
  8. import sqlite3
  9. from sqlalchemy import exc,orm
  10. import csv
  11.  
  12. # not sure how to test authentication ...
  13. class TestAuthentication(unittest.TestCase):
  14. pass
  15.  
  16. class TestQuestionClass(unittest.TestCase):
  17.  
  18. def setUp(self):
  19. con = sqlite3.connect("test_library.db")
  20. cur = con.cursor()
  21. ############### test_question_pool ########################
  22. cur.execute("CREATE TABLE IF NOT EXISTS question_pool (QID,Question,Option1,Option2,Option3,Option4,Option5,Option6,Option7,Option8);")
  23. with open("questionexample.csv","r") as fin:
  24. dr = csv.DictReader(fin)
  25. to_db = [(i["QID"],i["Question"],i["Option1"],i["Option2"],i["Option3"],i["Option4"],i["Option5"],i["Option6"],i["Option7"],i["Option8"]) for i in dr]
  26.  
  27. cur.executemany("INSERT INTO question_pool (QID,Question,Option1,Option2,Option3,Option4,Option5,Option6,Option7,Option8) VALUES (?,?,?,?,?,?,?,?,?,?);", to_db)
  28. con.commit()
  29. ###########################################################
  30. # QID,Question,Option1,Option2,Option3,Option4,Option5,Option6,Option7,Option8
  31. # 1,What did you have for lunch?,test1,test2,test3,test4
  32. # 2,Test question,a1,a2,a3,a4,a5,a6,a7,a8
  33. # 3,Another question,__input
  34. ###########################################################
  35.  
  36. # I'm not sure how test no question title because 'itm1' will be interpreted as the title
  37. def test_add_question_no_title(self):
  38. with self.assertRaises(TypeError):
  39. self.question.add_question('itm1','itm2','itm3','itm4','itm5','itm6','itm7','itm8')
  40.  
  41.  
  42. def test_add_question_no_options(self):
  43. with self.assertRaises(TypeError):
  44. self.question.add_question('title')
  45.  
  46.  
  47. def test_add_question_with_invalid_title_and_options(self):
  48. with self.assertRaises(orm.exc.NoResultFound):
  49. self.question.add_question() # you cannot make a question with no title or options
  50.  
  51.  
  52. def test_successful_question_add(self):
  53. # add a question with correct inputs
  54. self.question.question_add('succcess','0','1','2','3','4','5','6','7')
  55. # check title
  56. self.assertEqual(question.question,'success')
  57. # check options
  58. self.assertEqual(question.answer[0],'0')
  59. self.assertEqual(question.answer[1],'1')
  60. self.assertEqual(question.answer[2],'2')
  61. self.assertEqual(question.answer[3],'3')
  62. self.assertEqual(question.answer[4],'4')
  63. self.assertEqual(question.answer[5],'5')
  64. self.assertEqual(question.answer[6],'6')
  65. self.assertEqual(question.answer[7],'7')
  66.  
  67. def tearDown(self):
  68. con = sqlite3.connect("test_library.db")
  69. cur = con.cursor()
  70. cur.execute("DROP TABLE IF EXISTS question_pool;")
  71. con.commit()
  72. con.close()
  73.  
  74.  
  75. class TestResponseClass(unittest.TestCase):
  76.  
  77. def setUp(self):
  78. # add a few dummy responses
  79. con = sqlite3.connect("test_library.db")
  80. cur = con.cursor()
  81. ############### test_survey_pool #########################
  82. cur.execute("CREATE TABLE IF NOT EXISTS response_pool (SID,userID,option1,option2,option3,option4,option5,ioption6,option7,option8);")
  83. with open("responseexample.csv","r") as fin:
  84. dr = csv.DictReader(fin)
  85. to_db = [(i["SID"],i["userID"],i["Option1"],i["Option2"],i["Option3"],i["Option4"],i["Option5"],i["Option6"],i["Option7"],i["Option8"]) for i in dr]
  86.  
  87. cur.executemany("INSERT INTO response_pool (SID,userID,Option1,Option2,Option3,Option4,Option5,Option6,Option7,Option8) VALUES (?,?,?,?,?,?,?,?,?,?);", to_db)
  88. con.commit()
  89. ###########################################################
  90. # SID,userID,Option1,Option2,Option3,Option4,Option5,Option6,Option7,Option8
  91. # 1,123,1,1,1,1,1,1,1,1
  92. # 1,124,2,2,2,2,2,2,2,2
  93. # 1,125,3,3,3,3,3,3,3,3
  94. ###########################################################
  95.  
  96. def test_duplicate_survey_response_same_user(self):
  97. with self.assertRaises(exc.IntegrityError):
  98. # creating a user 123 answering survey SID 1 again
  99. self.response_dup = Response()
  100. reponse_dup.add_response('1','123','dup1','dup2','dup3','dup4','dup5','dup6','dup7','dup8')
  101.  
  102.  
  103. def tearDown(self):
  104. con = sqlite3.connect("test_library.db")
  105. cur = con.cursor()
  106. cur.execute("DROP TABLE IF EXISTS response_pool;")
  107. con.commit()
  108. con.close()
  109.  
  110.  
  111. class TestSurveyClass(unittest.TestCase):
  112. def setUp(self):
  113. con = sqlite3.connect("test_library.db")
  114. cur = con.cursor()
  115. ############### test_survey_pool #########################
  116. cur.execute("CREATE TABLE IF NOT EXISTS survey_pool (SID,Name,Course,Offering,QIDs,Active);")
  117. with open("surveyexample.csv","r") as fin:
  118. dr = csv.DictReader(fin)
  119. to_db = [(i["SID"],i["Name"],i["Course"],i["Offering"],i["QIDs"],i["Active"]) for i in dr]
  120.  
  121. cur.executemany("INSERT INTO survey_pool (SID,Name,Course,Offering,QIDs,Active) VALUES (?,?,?,?,?,?);", to_db)
  122. con.commit()
  123. ###########################################################
  124.  
  125. # SID,Name,Course,Offering,QIDs,Active
  126. # 1,Survey 1,COMP1000,17S2,1 2,1
  127. # 2,Survey 2,SENG2011,18s2,3,0
  128. # 3,Survey 3,COMP1521,17s2,1 2 3,1
  129.  
  130. ############### test_question_pool ########################
  131. cur.execute("CREATE TABLE IF NOT EXISTS question_pool (QID,Question,Option1,Option2,Option3,Option4,Option5,Option6,Option7,Option8);")
  132. with open("questionexample.csv","r") as fin:
  133. dr = csv.DictReader(fin)
  134. to_db = [(i["QID"],i["Question"],i["Option1"],i["Option2"],i["Option3"],i["Option4"],i["Option5"],i["Option6"],i["Option7"],i["Option8"]) for i in dr]
  135.  
  136. cur.executemany("INSERT INTO question_pool (QID,Question,Option1,Option2,Option3,Option4,Option5,Option6,Option7,Option8) VALUES (?,?,?,?,?,?,?,?,?,?);", to_db)
  137. con.commit()
  138. ###########################################################
  139.  
  140. # QID,Question,Option1,Option2,Option3,Option4,Option5,Option6,Option7,Option8
  141. # 1,What did you have for lunch?,test1,test2,test3,test4
  142. # 2,Test question,a1,a2,a3,a4,a5,a6,a7,a8
  143. # 3,Another question,__input
  144.  
  145. # can't make a survey for a course offering that does not exist
  146. def test_invalid_course_offering(self):
  147. with self.assertRaises(exc.IntegrityError):
  148. self.survey_dup = Survey('fake survey','FAKE1000','17s2','1')
  149. survey_dup.add_survey('fake survey','FAKE1000','17s2','1',)
  150.  
  151.  
  152. # can't make a survey if a course offering already has a survey
  153. def test_duplicate_course_offering_survey(self):
  154. with self.assertRaises(exc.IntegrityError):
  155. self.survey_dup = Survey('duplicate','COMP1521','17s2','1')
  156. survey_dup.add_survey('duplicate','COMP1521','17s2','1')
  157.  
  158.  
  159. def tearDown(self):
  160. con = sqlite3.connect("test_library.db")
  161. cur = con.cursor()
  162. cur.execute("DROP TABLE IF EXISTS survey_pool;")
  163. con.commit()
  164. cur.execute("DROP TABLE IF EXISTS question_pool;")
  165. con.commit()
  166. con.close()
  167.  
  168.  
  169. if __name__=="__main__":
  170. unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement