Advertisement
Guest User

TestGetFlaggedImages.py

a guest
Mar 19th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.00 KB | None | 0 0
  1. import unittest
  2. import subprocess
  3. import os
  4. import sys
  5. import pymysql
  6. from dotenv import load_dotenv, find_dotenv
  7. import json
  8. import validators
  9.  
  10. # The tests I wrote extended Jacob's test. I wrote the tests test_get_flagged_images_3, test_get_flagged_images_4, test_get_flagged_images_6. But I also came up with the way of testing serverless lambda functions locally, which uses the python subprocess library. This makes it so we emulate exactly how the lambda functions will be called by the API Gateway.
  11.  
  12. class TestGetImage(unittest.TestCase):
  13. @classmethod
  14. def setUpClass(cls):
  15. # Set working directory to lambda-api
  16. os.chdir(os.path.dirname(os.path.abspath(__file__)) + "/../../backend/lambda-api")
  17. # Load database credentials to os.environ
  18.  
  19. load_dotenv('./.env')
  20. # Make database connection
  21. connection = pymysql.connect(host='pickmo.cm8t4da2mqrz.us-west-2.rds.amazonaws.com',
  22. user=os.environ['RDS_USERNAME'],
  23. password=os.environ['RDS_PWD'],
  24. db='pickmo_testdb')
  25. # Clear Tables
  26. try:
  27. with connection.cursor() as cursor:
  28. sql = "DELETE FROM Connections"
  29. cursor.execute(sql)
  30. connection.commit()
  31. with connection.cursor() as cursor:
  32. sql = "DELETE FROM Robots"
  33. cursor.execute(sql)
  34. connection.commit()
  35. with connection.cursor() as cursor:
  36. sql = "DELETE FROM FlaggedImages"
  37. cursor.execute(sql)
  38. connection.commit()
  39. with connection.cursor() as cursor:
  40. sql = "DELETE FROM Flags"
  41. cursor.execute(sql)
  42. connection.commit()
  43. with connection.cursor() as cursor:
  44. sql = "DELETE FROM FlagCoordinates"
  45. cursor.execute(sql)
  46. connection.commit()
  47. with connection.cursor() as cursor:
  48. sql = "DELETE FROM RandomSamplingInfo"
  49. cursor.execute(sql)
  50. connection.commit()
  51. # Write test data to use for tests
  52. with connection.cursor() as cursor:
  53. sql = "INSERT INTO RandomSamplingInfo " \
  54. "(id, percentage) " \
  55. "VALUES (0, 50)"
  56. cursor.execute(sql)
  57. connection.commit()
  58. with connection.cursor() as cursor:
  59. sql = "INSERT INTO Robots (id) VALUES ('Robot1')"
  60. cursor.execute(sql)
  61. connection.commit()
  62. with connection.cursor() as cursor:
  63. sql = "INSERT INTO Connections (username, robotID) VALUES ('new_user1','Robot1')"
  64. cursor.execute(sql)
  65. connection.commit()
  66. with connection.cursor() as cursor:
  67. sql = "INSERT INTO Flags " \
  68. "(flagName) " \
  69. "VALUES ('flag1')"
  70. cursor.execute(sql)
  71. connection.commit()
  72. with connection.cursor() as cursor:
  73. sql = "SELECT id FROM Flags WHERE flagName = 'flag1'"
  74. cursor.execute(sql)
  75. flag_id = cursor.fetchone()[0]
  76. with connection.cursor() as cursor:
  77. sql = "INSERT INTO FlagCoordinates " \
  78. "(line1AX, line1AY, line1BX, line1BY, line2AX, line2AY, line2BX, line2BY, circleX, circleY) " \
  79. "VALUES (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)"
  80. cursor.execute(sql)
  81. connection.commit()
  82. with connection.cursor() as cursor:
  83. sql = "SELECT id FROM FlagCoordinates"
  84. cursor.execute(sql)
  85. flag_coords_id = cursor.fetchone()[0]
  86. with connection.cursor() as cursor:
  87. sql = "INSERT INTO FlaggedImages (username, timestamp, flagID, imgPath, flagCoordinatesID, lat, lon) " \
  88. "VALUES ('new_user1', 12345, " + str(flag_id) + ", 'Robot1/2018 07 07 - 17:02:1518023271.png', " + str(flag_coords_id)+ ", 5, 7)"
  89. cursor.execute(sql)
  90. connection.commit()
  91. with connection.cursor() as cursor:
  92. sql = "INSERT INTO FlaggedImages (username, timestamp, flagID, imgPath, flagCoordinatesID, lat, lon) " \
  93. "VALUES ('new_user2', 32345, " + str(flag_id) + ", 'Robot1/2018 08 06 - 17:02:1518023271.png', " + str(flag_coords_id)+ ", 5, 7)"
  94. cursor.execute(sql)
  95. connection.commit()
  96. # End connection
  97. finally:
  98. connection.close()
  99.  
  100. def test_get_flagged_images_3(self):
  101. # Test getting flagged images of a certain sample percentage
  102. data = {
  103. "headers": {
  104. "test": "lambda"
  105. },
  106. "queryStringParameters": {
  107. "sample": 50
  108. }
  109. }
  110. bash_command = "sls invoke local -f getFlaggedImages --data '" + json.dumps(data) + "' > getFImagesRes.json"
  111. self.assertEqual(subprocess.check_call( bash_command, shell=True), 0)
  112.  
  113. with open('getFImagesRes.json', 'r') as f:
  114. data = json.load(f)
  115. self.assertEqual(len(data["body"]), 1)
  116. # Remove the response file
  117. os.remove('getFImagesRes.json')
  118.  
  119. def test_get_flagged_images_4(self):
  120. # Test getting flagged images of a certain sample percentage
  121. data = {
  122. "headers": {
  123. "test": "lambda"
  124. },
  125. "queryStringParameters": {
  126. "flag": "flag1"
  127. }
  128. }
  129. bash_command = "sls invoke local -f getFlaggedImages --data '" + json.dumps(data) + "' > getFImagesRes.json"
  130. self.assertEqual(subprocess.check_call( bash_command, shell=True), 0)
  131.  
  132. with open('getFImagesRes.json', 'r') as f:
  133. data = json.load(f)
  134. self.assertEqual(len(data["body"]), 2)
  135. # Remove the response file
  136. os.remove('getFImagesRes.json')
  137.  
  138. def test_get_flagged_images_5(self):
  139. # Test getting flagged images of a certain sample percentage
  140. data = {
  141. "headers": {
  142. "test": "lambda"
  143. },
  144. "queryStringParameters": {
  145. "flag": "flag2"
  146. }
  147. }
  148. bash_command = "sls invoke local -f getFlaggedImages --data '" + json.dumps(data) + "' > getFImagesRes.json"
  149. self.assertEqual(subprocess.check_call( bash_command, shell=True), 0)
  150.  
  151. with open('getFImagesRes.json', 'r') as f:
  152. data = json.load(f)
  153. self.assertEqual(len(data["body"]), 0)
  154. # Remove the response file
  155. os.remove('getFImagesRes.json')
  156.  
  157. def test_get_flagged_images_6(self):
  158. # Test getting flagged images of a certain sample percentage
  159. data = {
  160. "headers": {
  161. "test": "lambda"
  162. },
  163. "queryStringParameters": {
  164. "sample": 50,
  165. "flag": "flag1"
  166. }
  167. }
  168. bash_command = "sls invoke local -f getFlaggedImages --data '" + json.dumps(data) + "' > getFImagesRes.json"
  169. self.assertEqual(subprocess.check_call( bash_command, shell=True), 0)
  170.  
  171. with open('getFImagesRes.json', 'r') as f:
  172. data = json.load(f)
  173. self.assertEqual(len(data["body"]), 1)
  174. # Remove the response file
  175. os.remove('getFImagesRes.json')
  176.  
  177. @classmethod
  178. def tearDownClass(cls):
  179. # Load database credentials to os.environ
  180. load_dotenv('./.env')
  181. # Make database connection
  182. connection = pymysql.connect(host='pickmo.cm8t4da2mqrz.us-west-2.rds.amazonaws.com',
  183. user=os.environ['RDS_USERNAME'],
  184. password=os.environ['RDS_PWD'],
  185. db='pickmo_testdb')
  186. # Clear test data
  187. try:
  188. with connection.cursor() as cursor:
  189. sql = "DELETE FROM Connections"
  190. cursor.execute(sql)
  191. connection.commit()
  192. with connection.cursor() as cursor:
  193. sql = "DELETE FROM Robots"
  194. cursor.execute(sql)
  195. connection.commit()
  196. with connection.cursor() as cursor:
  197. sql = "DELETE FROM FlaggedImages"
  198. cursor.execute(sql)
  199. connection.commit()
  200. with connection.cursor() as cursor:
  201. sql = "DELETE FROM Flags"
  202. cursor.execute(sql)
  203. connection.commit()
  204. with connection.cursor() as cursor:
  205. sql = "DELETE FROM FlagCoordinates"
  206. cursor.execute(sql)
  207. connection.commit()
  208. # End connection
  209. finally:
  210. connection.close()
  211.  
  212. if __name__ == '__main__':
  213. unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement