Snippets

Frederik Banke MySQL example unit test

Updated by Frederik Banke

File snippet.BUILD Modified

  • Ignore whitespace
  • Hide word diff
 from mysql.connector.cursor import MySQLCursorPrepared
 
 
-class TestMongoDBConsumer(TestCase):
+class TestMySQL(TestCase):
     def setUp(self):
         self.connection = unittest.mock.create_autospec(mysql.connector.MySQLConnection)
         cursor = MagicMock(MySQLCursorPrepared)
Created by Frederik Banke

File snippet.BUILD Added

  • Ignore whitespace
  • Hide word diff
+import unittest.mock
+from unittest import TestCase
+from unittest.mock import MagicMock
+from mysql.connector.cursor import MySQLCursorPrepared
+
+
+class TestMongoDBConsumer(TestCase):
+    def setUp(self):
+        self.connection = unittest.mock.create_autospec(mysql.connector.MySQLConnection)
+        cursor = MagicMock(MySQLCursorPrepared)
+        cursor.fetchall = MagicMock(return_value=[[2], [3]])
+        self.connection.cursor = MagicMock(return_value=cursor)
+
+
+    def test_that_sum_is_calculated_correctly(self):
+        self.assertEqual(5, get_data_from_mysql(self.connection, "testkey"))
+
+
+if __name__ == '__main__':
+    unittest.main()
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.