Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from __future__ import print_function
  4.  
  5. try:
  6. import mock
  7. except ImportError:
  8. from unittest import mock
  9. import unittest
  10.  
  11. import psycopg2
  12.  
  13.  
  14. def ex(user_id):
  15. with psycopg2.connect("") as conn:
  16. with conn.cursor() as cursor:
  17. cursor.execute("SELECT first_name FROM users WHERE id = %s", (user_id,))
  18. return cursor.fetchall()
  19.  
  20.  
  21. class TestPatchConn(unittest.TestCase):
  22. def setUp(self):
  23. self.user_id = 16
  24.  
  25. @mock.patch('psycopg2.connect')
  26. def test_with_context(self, mock_connect):
  27. mock_connect().__enter__().cursor().__enter__().fetchall.return_value = ['Stacy']
  28. ex(self.user_id)
  29. mock_connect().__enter__().cursor().__enter__().execute.assert_called_with('SELECT first_name FROM users WHERE id = %s', (self.user_id, ))
  30.  
  31.  
  32. if __name__ == '__main__':
  33. unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement