Best Python code snippet using autotest_python
scheduler_models_unittest.py
Source:scheduler_models_unittest.py
...18 def _do_query(self, sql):19 self._database.execute(sql)20 def _set_monitor_stubs(self):21 # Clear the instance cache as this is a brand new database.22 scheduler_models.DBObject._clear_instance_cache()23 self._database = (24 database_connection.TranslatingDatabase.get_test_database(25 translators=scheduler_lib._DB_TRANSLATORS))26 self._database.connect(db_type='django')27 self._database.debug = _DEBUG28 self.god.stub_with(scheduler_models, '_db', self._database)29 def setUp(self):30 self._frontend_common_setup()31 self._set_monitor_stubs()32 def tearDown(self):33 self._database.disconnect()34 self._frontend_common_teardown()35 def _update_hqe(self, set, where=''):36 query = 'UPDATE afe_host_queue_entries SET ' + set37 if where:38 query += ' WHERE ' + where39 self._do_query(query)40class DBObjectTest(BaseSchedulerModelsTest):41 def test_compare_fields_in_row(self):42 host = scheduler_models.Host(id=1)43 fields = list(host._fields)44 row_data = [getattr(host, fieldname) for fieldname in fields]45 self.assertEqual({}, host._compare_fields_in_row(row_data))46 row_data[fields.index('hostname')] = 'spam'47 self.assertEqual({'hostname': ('host1', 'spam')},48 host._compare_fields_in_row(row_data))49 row_data[fields.index('id')] = 2350 self.assertEqual({'hostname': ('host1', 'spam'), 'id': (1, 23)},51 host._compare_fields_in_row(row_data))52 def test_compare_fields_in_row_datetime_ignores_microseconds(self):53 datetime_with_us = datetime.datetime(2009, 10, 07, 12, 34, 56, 7890)54 datetime_without_us = datetime.datetime(2009, 10, 07, 12, 34, 56, 0)55 class TestTable(scheduler_models.DBObject):56 _table_name = 'test_table'57 _fields = ('id', 'test_datetime')58 tt = TestTable(row=[1, datetime_without_us])59 self.assertEqual({}, tt._compare_fields_in_row([1, datetime_with_us]))60 def test_always_query(self):61 host_a = scheduler_models.Host(id=2)62 self.assertEqual(host_a.hostname, 'host2')63 self._do_query('UPDATE afe_hosts SET hostname="host2-updated" '64 'WHERE id=2')65 host_b = scheduler_models.Host(id=2, always_query=True)66 self.assert_(host_a is host_b, 'Cached instance not returned.')67 self.assertEqual(host_a.hostname, 'host2-updated',68 'Database was not re-queried')69 # If either of these are called, a query was made when it shouldn't be.70 host_a._compare_fields_in_row = lambda _: self.fail('eek! a query!')71 host_a._update_fields_from_row = host_a._compare_fields_in_row72 host_c = scheduler_models.Host(id=2, always_query=False)73 self.assert_(host_a is host_c, 'Cached instance not returned')74 def test_delete(self):75 host = scheduler_models.Host(id=3)76 host.delete()77 host = self.assertRaises(scheduler_models.DBError, scheduler_models.Host, id=3,78 always_query=False)79 host = self.assertRaises(scheduler_models.DBError, scheduler_models.Host, id=3,80 always_query=True)81 def test_save(self):82 # Dummy Job to avoid creating a one in the HostQueueEntry __init__.83 class MockJob(object):84 def __init__(self, id, row):85 pass86 def tag(self):87 return 'MockJob'88 self.god.stub_with(scheduler_models, 'Job', MockJob)89 hqe = scheduler_models.HostQueueEntry(90 new_record=True,91 row=[0, 1, 2, 'Queued', None, 0, 0, 0, '.', None, False, None,92 None])93 hqe.save()94 new_id = hqe.id95 # Force a re-query and verify that the correct data was stored.96 scheduler_models.DBObject._clear_instance_cache()97 hqe = scheduler_models.HostQueueEntry(id=new_id)98 self.assertEqual(hqe.id, new_id)99 self.assertEqual(hqe.job_id, 1)100 self.assertEqual(hqe.host_id, 2)101 self.assertEqual(hqe.status, 'Queued')102 self.assertEqual(hqe.meta_host, None)103 self.assertEqual(hqe.active, False)104 self.assertEqual(hqe.complete, False)105 self.assertEqual(hqe.deleted, False)106 self.assertEqual(hqe.execution_subdir, '.')107 self.assertEqual(hqe.started_on, None)108 self.assertEqual(hqe.finished_on, None)109class HostTest(BaseSchedulerModelsTest):110 def setUp(self):...
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!!