Advertisement
Guest User

TestQueue.py

a guest
Feb 25th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.81 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.  
  9. class TestQueue(unittest.TestCase):
  10.    
  11.     @classmethod
  12.     def setUpClass(cls):
  13.         # Set working directory to lambda-api
  14.         os.chdir(os.path.dirname(os.path.abspath(__file__)) + "/../../backend/lambda-api")
  15.         # Load database credentials to os.environ
  16.  
  17.         load_dotenv('./.env')
  18.         # Make database connection
  19.         connection = pymysql.connect(host='pickmo.cm8t4da2mqrz.us-west-2.rds.amazonaws.com',
  20.                                  user=os.environ['RDS_USERNAME'],
  21.                                  password=os.environ['RDS_PWD'],
  22.                                  db='pickmo_testdb')
  23.         # Clear PickerQueue
  24.         try:
  25.             with connection.cursor() as cursor:
  26.                 sql = "DELETE FROM PickerQueue"
  27.                 cursor.execute(sql)
  28.                 connection.commit()
  29.         # End connection
  30.         finally:
  31.             connection.close()
  32.  
  33.     def test_queue_1(self):  
  34.         # Test adding picker to queue
  35.         load_dotenv('./.env')
  36.         # Make database connection
  37.         connection = pymysql.connect(host='pickmo.cm8t4da2mqrz.us-west-2.rds.amazonaws.com',
  38.                                  user=os.environ['RDS_USERNAME'],
  39.                                  password=os.environ['RDS_PWD'],
  40.                                  db='pickmo_testdb')
  41.         # Invoke lambda code
  42.         data = {
  43.             'body':{
  44.                 'username':'new_user1'
  45.             },
  46.             'headers':{
  47.                 'test':'lambda'
  48.             },
  49.             'requestContext': {
  50.                 'authorizer': {
  51.                     'claims': {
  52.                         'email': 'new_user1'
  53.                     }
  54.                 }
  55.             }
  56.         }
  57.         bashCommand = "sls invoke local -f queuePicker --data '" + json.dumps(data) + "'"
  58.         self.assertEqual(subprocess.check_call( bashCommand, shell=True), 0)
  59.        
  60.         # Check database for newly added picker
  61.         try:
  62.             with connection.cursor() as cursor:
  63.                 sql = "SELECT * FROM PickerQueue WHERE username = 'new_user1'"
  64.                 cursor.execute(sql)    #
  65.                 result = cursor.fetchall()
  66.                 self.assertEqual(len(result), 1)
  67.                 self.assertEqual(result[0][0], 'new_user1')
  68.         finally:
  69.             connection.close()
  70.  
  71.     def test_queue_2(self):
  72.         # Test removing picker from queue
  73.         connection = pymysql.connect(host='pickmo.cm8t4da2mqrz.us-west-2.rds.amazonaws.com',
  74.                                  user=os.environ['RDS_USERNAME'],
  75.                                  password=os.environ['RDS_PWD'],
  76.                                  db='pickmo_testdb')
  77.         # Invoke lambda code
  78.         data = {
  79.             'pathParameters':{
  80.                 'username':'new_user1'
  81.             },
  82.             'headers':{
  83.                 'test':'lambda'
  84.             },
  85.             'requestContext': {
  86.                 'authorizer': {
  87.                     'claims': {
  88.                         'email': 'new_user1'
  89.                     }
  90.                 }
  91.             }
  92.         }
  93.         bashCommand = "sls invoke local -f dequeuePicker --data '" + json.dumps(data) + "'"
  94.         self.assertEqual(subprocess.check_call( bashCommand, shell=True), 0)
  95.        
  96.         # Check database for deleted picker
  97.         try:
  98.             with connection.cursor() as cursor:
  99.                 sql = "SELECT * FROM PickerQueue WHERE username = 'new_user1'"
  100.                 cursor.execute(sql)    #
  101.                 result = cursor.fetchall()
  102.                 self.assertEqual(len(result), 0)
  103.         finally:
  104.             connection.close()
  105.  
  106. if __name__ == '__main__':
  107.     unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement