Best Python code snippet using autotest_python
scheduler_lib_unittest.py
Source:scheduler_lib_unittest.py
1#!/usr/bin/python2#3# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.4# Use of this source code is governed by a BSD-style license that can be5# found in the LICENSE file.6import mock7import unittest8import common9from autotest_lib.database import database_connection10from autotest_lib.frontend import setup_django_environment11from autotest_lib.frontend.afe import readonly_connection12from autotest_lib.server import utils as server_utils13from autotest_lib.scheduler import scheduler_lib14from django.db import utils as django_utils15class ConnectionManagerTests(unittest.TestCase):16 """Connection manager unittests."""17 def setUp(self):18 self.connection_manager = None19 readonly_connection.set_globally_disabled = mock.MagicMock()20 setup_django_environment.enable_autocommit = mock.MagicMock()21 server_utils.Singleton._instances = {}22 def tearDown(self):23 readonly_connection.set_globally_disabled.reset_mock()24 setup_django_environment.enable_autocommit.reset_mock()25 def testConnectionDisconnect(self):26 """Test connection and disconnecting from the database."""27 # Test that the connection manager only opens a connection once.28 connection_manager = scheduler_lib.ConnectionManager()29 connection_manager.open_connection = mock.MagicMock()30 connection = connection_manager.get_connection()31 connection_manager.open_connection.assert_called_once_with()32 connection_manager.open_connection.reset_mock()33 connection = connection_manager.get_connection()34 self.assertTrue(35 connection_manager.open_connection.call_count == 0)36 connection_manager.open_connection.reset_mock()37 # Test that del on the connection manager closes the connection38 connection_manager.disconnect = mock.MagicMock()39 connection_manager.__del__()40 connection_manager.disconnect.assert_called_once_with()41 def testConnectionReconnect(self):42 """Test that retries don't destroy the connection."""43 database_connection._DjangoBackend.execute = mock.MagicMock()44 database_connection._DjangoBackend.execute.side_effect = (45 django_utils.DatabaseError('Database Error'))46 connection_manager = scheduler_lib.ConnectionManager()47 connection = connection_manager.get_connection()48 self.assertRaises(django_utils.DatabaseError,49 connection.execute, *('', None, True))50 self.assertTrue(51 database_connection._DjangoBackend.execute.call_count == 2)52 database_connection._DjangoBackend.execute.reset_mock()53 self.assertTrue(connection_manager.db_connection ==54 connection_manager.get_connection())55 def testConnectionManagerSingleton(self):56 """Test that the singleton works as expected."""57 # Confirm that instantiating the class applies global db settings.58 connection_manager = scheduler_lib.ConnectionManager()59 readonly_connection.set_globally_disabled.assert_called_once_with(True)60 setup_django_environment.enable_autocommit.assert_called_once_with()61 readonly_connection.set_globally_disabled.reset_mock()62 setup_django_environment.enable_autocommit.reset_mock()63 # Confirm that instantiating another connection manager doesn't change64 # the database settings, and in fact, returns the original manager.65 connection_manager_2 = scheduler_lib.ConnectionManager()66 self.assertTrue(connection_manager == connection_manager_2)67 self.assertTrue(68 readonly_connection.set_globally_disabled.call_count == 0)69 self.assertTrue(70 setup_django_environment.enable_autocommit.call_count == 0)71 # Confirm that we don't open the connection when the class is72 # instantiated.73 self.assertTrue(connection_manager.db_connection is None)74if __name__ == '__main__':...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!