Best Python code snippet using autotest_python
models_unittest.py
Source:models_unittest.py  
...11from django.db import models as dbmodels12class AclGroupTest(unittest.TestCase,13                   test_utils.FrontendTestMixin):14    def setUp(self):15        self._frontend_common_setup()16    def tearDown(self):17        self._frontend_common_teardown()18    def _check_acls(self, host, acl_name_list):19        actual_acl_names = [acl_group.name for acl_group20                            in host.aclgroup_set.all()]21        self.assertEquals(set(actual_acl_names), set(acl_name_list))22    def test_on_host_membership_change(self):23        host1, host2 = self.hosts[1:3]24        everyone_acl = models.AclGroup.objects.get(name='Everyone')25        host1.aclgroup_set.clear()26        self._check_acls(host1, [])27        host2.aclgroup_set.add(everyone_acl)28        self._check_acls(host2, ['Everyone', 'my_acl'])29        models.AclGroup.on_host_membership_change()30        self._check_acls(host1, ['Everyone'])31        self._check_acls(host2, ['my_acl'])32class HostTest(unittest.TestCase,33               test_utils.FrontendTestMixin):34    def setUp(self):35        self._frontend_common_setup()36    def tearDown(self):37        self._frontend_common_teardown()38    def test_add_host_previous_one_time_host(self):39        # ensure that when adding a host which was previously used as a one-time40        # host, the status isn't reset, since this can interfere with the41        # scheduler.42        host = models.Host.create_one_time_host('othost')43        self.assertEquals(host.invalid, True)44        self.assertEquals(host.status, models.Host.Status.READY)45        host.status = models.Host.Status.RUNNING46        host.save()47        host2 = models.Host.add_object(hostname='othost')48        self.assertEquals(host2.id, host.id)49        self.assertEquals(host2.status, models.Host.Status.RUNNING)50class SpecialTaskUnittest(unittest.TestCase,51                          test_utils.FrontendTestMixin):52    def setUp(self):53        self._frontend_common_setup()54    def tearDown(self):55        self._frontend_common_teardown()56    def _create_task(self):57        return models.SpecialTask.objects.create(58            host=self.hosts[0], task=models.SpecialTask.Task.VERIFY,59            requested_by=models.User.current_user())60    def test_execution_path(self):61        task = self._create_task()62        self.assertEquals(task.execution_path(), 'hosts/host1/1-verify')63    def test_status(self):64        task = self._create_task()65        self.assertEquals(task.status, 'Queued')66        task.update_object(is_active=True)67        self.assertEquals(task.status, 'Running')68        task.update_object(is_active=False, is_complete=True, success=True)69        self.assertEquals(task.status, 'Completed')70        task.update_object(success=False)71        self.assertEquals(task.status, 'Failed')72    def test_activate(self):73        task = self._create_task()74        task.activate()75        self.assertTrue(task.is_active)76        self.assertFalse(task.is_complete)77    def test_finish(self):78        task = self._create_task()79        task.activate()80        task.finish(True)81        self.assertFalse(task.is_active)82        self.assertTrue(task.is_complete)83        self.assertTrue(task.success)84    def test_requested_by_from_queue_entry(self):85        job = self._create_job(hosts=[0])86        task = models.SpecialTask.objects.create(87            host=self.hosts[0], task=models.SpecialTask.Task.VERIFY,88            queue_entry=job.hostqueueentry_set.all()[0])89        self.assertEquals(task.requested_by.login, 'autotest_system')90class HostQueueEntryUnittest(unittest.TestCase,91                             test_utils.FrontendTestMixin):92    def setUp(self):93        self._frontend_common_setup()94    def tearDown(self):95        self._frontend_common_teardown()96    def test_execution_path(self):97        entry = self._create_job(hosts=[1]).hostqueueentry_set.all()[0]98        entry.execution_subdir = 'subdir'99        entry.save()100        self.assertEquals(entry.execution_path(), '1-autotest_system/subdir')101class ModelWithInvalidTest(unittest.TestCase,102                           test_utils.FrontendTestMixin):103    def setUp(self):104        self._frontend_common_setup()105    def tearDown(self):106        self._frontend_common_teardown()107    def test_model_with_invalid_delete(self):108        self.assertFalse(self.hosts[0].invalid)109        self.hosts[0].delete()110        self.assertTrue(self.hosts[0].invalid)111        self.assertTrue(models.Host.objects.get(id=self.hosts[0].id))112    def test_model_with_invalid_delete_queryset(self):113        for host in self.hosts:114            self.assertFalse(host.invalid)115        hosts = models.Host.objects.all()116        hosts.delete()117        self.assertEqual(hosts.count(), len(self.hosts))118        for host in hosts:119            self.assertTrue(host.invalid)120    def test_cloned_queryset_delete(self):121        """122        Make sure that a cloned queryset maintains the custom delete()123        """124        to_delete = ('host1', 'host2')125        for host in self.hosts:126            self.assertFalse(host.invalid)127        hosts = models.Host.objects.all().filter(hostname__in=to_delete)128        hosts.delete()129        all_hosts = models.Host.objects.all()130        self.assertEqual(all_hosts.count(), len(self.hosts))131        for host in all_hosts:132            if host.hostname in to_delete:133                self.assertTrue(134                    host.invalid,135                    '%s.invalid expected to be True' % host.hostname)136            else:137                self.assertFalse(138                    host.invalid,139                    '%s.invalid expected to be False' % host.hostname)140    def test_normal_delete(self):141        job = self._create_job(hosts=[1])142        self.assertEqual(1, models.Job.objects.all().count())143        job.delete()144        self.assertEqual(0, models.Job.objects.all().count())145    def test_normal_delete_queryset(self):146        self._create_job(hosts=[1])147        self._create_job(hosts=[2])148        self.assertEqual(2, models.Job.objects.all().count())149        models.Job.objects.all().delete()150        self.assertEqual(0, models.Job.objects.all().count())151class KernelTest(unittest.TestCase, test_utils.FrontendTestMixin):152    def setUp(self):153        self._frontend_common_setup()154    def tearDown(self):155        self._frontend_common_teardown()156    def test_create_kernels_none(self):157        self.assertEqual(None, models.Kernel.create_kernels(None))158    def test_create_kernels(self):159        self.god.stub_function(models.Kernel, '_create')160        num_kernels = 3161        kernel_list = [object() for _ in range(num_kernels)]162        result = [object() for _ in range(num_kernels)]163        for kernel, response in zip(kernel_list, result):164            models.Kernel._create.expect_call(kernel).and_return(response)165        self.assertEqual(result, models.Kernel.create_kernels(kernel_list))166        self.god.check_playback()167    def test_create(self):168        kernel = models.Kernel._create({'version': 'version'})169        self.assertEqual(kernel.version, 'version')170        self.assertEqual(kernel.cmdline, '')171        self.assertEqual(kernel, models.Kernel._create({'version': 'version'}))172class ParameterizedJobTest(unittest.TestCase,173                           test_utils.FrontendTestMixin):174    def setUp(self):175        self._frontend_common_setup()176    def tearDown(self):177        self._frontend_common_teardown()178    def test_job(self):179        settings.override_value('AUTOTEST_WEB', 'parameterized_jobs', 'True')180        test = models.Test.objects.create(181            name='name', author='author', test_class='class',182            test_category='category',183            test_type=model_attributes.TestTypes.SERVER, path='path')184        parameterized_job = models.ParameterizedJob.objects.create(test=test)185        job = self._create_job(hosts=[1], control_file=None,186                               parameterized_job=parameterized_job)187        self.assertEqual(job, parameterized_job.job())188class JobTest(unittest.TestCase, test_utils.FrontendTestMixin):189    def setUp(self):190        self._frontend_common_setup()191    def tearDown(self):192        self._frontend_common_teardown()193    def test_check_parameterized_jobs_no_args(self):194        self.assertRaises(Exception, models.Job.check_parameterized_job,195                          control_file=None, parameterized_job=None)196    def test_check_parameterized_jobs_both_args(self):197        self.assertRaises(Exception, models.Job.check_parameterized_job,198                          control_file=object(), parameterized_job=object())199    def test_check_parameterized_jobs_disabled(self):200        self.assertRaises(Exception, models.Job.check_parameterized_job,201                          control_file=None, parameterized_job=object())202    def test_check_parameterized_jobs_enabled(self):203        settings.override_value('AUTOTEST_WEB', 'parameterized_jobs', 'True')204        self.assertRaises(Exception, models.Job.check_parameterized_job,205                          control_file=object(), parameterized_job=None)206class SoftwareComponentKindTest(unittest.TestCase,207                                test_utils.FrontendTestMixin):208    def setUp(self):209        self._frontend_common_setup()210    def tearDown(self):211        self._frontend_common_teardown()212    def test_create_delete(self):213        models.SoftwareComponentKind.objects.create(name='rpm')214        self.assertEqual(2, models.SoftwareComponentKind.objects.all().count())215        models.SoftwareComponentKind.objects.all().delete()216        self.assertEqual(0, models.SoftwareComponentKind.objects.all().count())217    def test_create_duplicate_distro(self):218        def create_kind():219            models.SoftwareComponentKind.objects.create(name='rpm')220        create_kind()221        self.assertRaises(Exception, create_kind)222        models.SoftwareComponentKind.objects.all().delete()223class SoftwareComponentArchTest(unittest.TestCase,224                                test_utils.FrontendTestMixin):225    def setUp(self):226        self._frontend_common_setup()227    def tearDown(self):228        self._frontend_common_teardown()229    def test_create_delete(self):230        models.SoftwareComponentArch.objects.create(name='x86_64')231        self.assertEqual(2, models.SoftwareComponentArch.objects.all().count())232        models.SoftwareComponentArch.objects.all().delete()233        self.assertEqual(0, models.SoftwareComponentArch.objects.all().count())234    def test_create_duplicate_distro(self):235        def create_arch():236            models.SoftwareComponentArch.objects.create(name='x86_64')237        create_arch()238        self.assertRaises(Exception, create_arch)239        models.SoftwareComponentArch.objects.all().delete()240class SoftwareComponentTest(unittest.TestCase,241                            test_utils.FrontendTestMixin):242    def setUp(self):243        self._frontend_common_setup()244    def tearDown(self):245        self._frontend_common_teardown()246    def test_delete_reference(self):247        '''248        Should not be possible to delete a Software Component Arch that249        is referenced by an existing Software Component250        '''251        kind = models.SoftwareComponentKind.objects.create(name='rpm')252        kind.save()253        kind_id = kind.id254        arch = models.SoftwareComponentArch.objects.create(name='x86_64')255        arch.save()256        arch_id = arch.id257        sc = models.SoftwareComponent.objects.create(name='foo',258                                                     kind=kind,259                                                     arch=arch,260                                                     version='1.0.0')261        sc.save()262        sc_id = sc.id263        sc = models.SoftwareComponent.objects.get(pk=sc_id)264        arch = models.SoftwareComponentArch.objects.get(pk=arch_id)265        def arch_delete():266            arch.delete()267        self.assertRaises(dbmodels.ProtectedError, arch_delete)268        models.SoftwareComponent.objects.all().delete()269        models.SoftwareComponentKind.objects.all().delete()270        models.SoftwareComponentArch.objects.all().delete()271class LinuxDistroTest(unittest.TestCase,272                      test_utils.FrontendTestMixin):273    def setUp(self):274        self._frontend_common_setup()275    def tearDown(self):276        self._frontend_common_teardown()277    def test_create_delete(self):278        # The builtin 'unknown distro' accounts for the first count279        self.assertEqual(1, models.LinuxDistro.objects.all().count())280        models.LinuxDistro.objects.create(name='distro', version='1',281                                          release='0', arch='i386')282        self.assertEqual(2, models.LinuxDistro.objects.all().count())283        models.LinuxDistro.objects.all().delete()284        self.assertEqual(0, models.LinuxDistro.objects.all().count())285    def test_create_query_arch_delete(self):286        models.LinuxDistro.objects.create(name='distro', version='1',287                                          release='0', arch='i386')288        models.LinuxDistro.objects.create(name='distro', version='1',...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!!
