コード例 #1
0
    def setUp(self):
        import copy
        super(ModuleUpdaterTests, self).setUp()
        self.tmp_file = 'tests/test_tmp.py'
        self.tmp_module_name = 'tests.test_tmp'
        shutil.copyfile('tests/a_test_sample.py', self.tmp_file)
        self.ut_module = UTModuleDetails(module_name='test_tmp')
        self.ut_module.add_class(class_name='ChildSampleTests')

        self.anchor_dir = os.getcwd()
コード例 #2
0
class ModuleUpdaterTests(TddSettingsRestoreMixin, MockHelperMixin, TestCase):
    """Auto-gen by DocTag"""
    def setUp(self):
        import copy
        super(ModuleUpdaterTests, self).setUp()
        self.tmp_file = 'tests/test_tmp.py'
        self.tmp_module_name = 'tests.test_tmp'
        shutil.copyfile('tests/a_test_sample.py', self.tmp_file)
        self.ut_module = UTModuleDetails(module_name='test_tmp')
        self.ut_module.add_class(class_name='ChildSampleTests')

        self.anchor_dir = os.getcwd()

    def tearDown(self):
        super(ModuleUpdaterTests, self).tearDown()
        os.remove(self.tmp_file)

    def test_tmp_file(self):
        self.assertTrue(self.tmp_file)
        self.assertTrue(os.path.exists(self.tmp_file))

    def test_create_instance(self):
        updater = ModuleUpdater(ut_module=self.ut_module)
        self.assertEqual(updater.ut_module, self.ut_module)

    def test_get_class_lists(self):
        updater = ModuleUpdater(ut_module=self.ut_module)
        loader = ModuleLoader(anchor_dir=self.anchor_dir)
        mod = loader.load_module(self.tmp_module_name)
        self.assertTrue(mod)

        # --> And test it first with no difference...
        existing_classes, new_classes = updater._get_class_lists(loaded_module=mod)
        self.assertFalse(new_classes)

        # --> Then with a new one
        self.ut_module.add_class(class_name='NewClassTests')
        existing_classes, new_classes = updater._get_class_lists(loaded_module=mod)
        self.assertTrue(new_classes)

    def test_update_verify_path(self):
        with mock.patch('tddtags.core.ModuleUpdater._update_step1', spec=True) as ModCon:
            # --> Prep
            updater = ModuleUpdater(ut_module=self.ut_module)
            updater._update_step1.side_effect = self.return_true
            loader = ModuleLoader(anchor_dir=self.anchor_dir)
            mod = loader.load_module(name=self.tmp_module_name)

            # --> Test
            reply = updater.update(loaded_module=mod)
            parm_info = [('loaded_module', 0), ('module_path', 1)]
            kwargs = self.get_patched_call_parms(parm_info, updater._update_step1, 0)
            self.assertTrue(self.tmp_file in kwargs['module_path'])
            self.assertEqual(kwargs['loaded_module'], mod)

    def test_update_no_save(self):
        with mock.patch('tddtags.core.UTModuleContainer.save_module', spec=True) as ModCon:
            # --> Prep
            updater = ModuleUpdater(ut_module=self.ut_module)
            loader = ModuleLoader(anchor_dir=self.anchor_dir)
            mod = loader.load_module(name=self.tmp_module_name)
            tddtags.core.tddtags_config['save'] = False

            # --> Need something new to do (Remember, this just uses the ut_module setup in setUp())
            ut_class = self.ut_module.class_list['ChildSampleTests']
            ut_class.add_method('eat_peanuts')

            # --> Test
            reply = updater.update(loaded_module=mod)
            self.assertEqual(updater.container.save_module.call_count, 0)

    def test_save_no_container(self):
        updater = ModuleUpdater(ut_module=self.ut_module)
        ret = updater._save(container=None)
        self.assertTrue(ret)

    def test_save_no_changes(self):
        with mock.patch('tddtags.core.UTModuleContainer.save_module', spec=True) as ModCon:
            # --> Prep. This time we need a container, but no changes
            container = UTModuleContainer(module_path=self.tmp_file)
            updater = ModuleUpdater(ut_module=self.ut_module)
            self.assertFalse(container.dirty_flag)
            ret = updater._save(container=container)
            self.assertTrue(ret)

            self.assertEqual(container.save_module.call_count, 0)

    def test_save_with_changes(self):
        with mock.patch('tddtags.core.UTModuleContainer.save_module', spec=True) as ModCon:
            # --> Prep. This time we need a container
            container = UTModuleContainer(module_path=self.tmp_file)
            updater = ModuleUpdater(ut_module=self.ut_module)
            container.dirty_flag = True
            ret = updater._save(container=container)
            self.assertTrue(ret)

            self.assertEqual(container.save_module.call_count, 1)

            # --> Check the filename
            parm_info = [('save_name', 0)]
            kwargs = self.get_patched_call_parms(parm_info, container.save_module, 0)
            abs_file_path = os.path.abspath(self.tmp_file)
            self.assertEqual(kwargs['save_name'], abs_file_path)

    def test_save_different_name(self):

        with mock.patch('tddtags.core.UTModuleContainer.save_module', spec=True) as ModCon:
            # --> Prep. This time we need a container, and a changed filename
            tddtags.core.tddtags_config['save_to_name'] = 'changed.py'
            container = UTModuleContainer(module_path=self.tmp_file)
            updater = ModuleUpdater(ut_module=self.ut_module)
            container.dirty_flag = True
            ret = updater._save(container=container)
            self.assertTrue(ret)

            self.assertEqual(container.save_module.call_count, 1)

            parm_info = [('save_name', 0)]
            kwargs = self.get_patched_call_parms(parm_info, container.save_module, 0)
            self.assertEqual(kwargs['save_name'], 'changed.py')

    def test_add_new_classes(self):
        # updater = ModuleUpdater(ut_module=self.ut_module)
        # loader = ModuleLoader(anchor_dir=self.anchor_dir)
        # mod = loader.load_module(self.tmp_module_name)
        # self.assertTrue(mod)

        container = UTModuleContainer(module_path=self.tmp_file)
        self.assertTrue(container.lines)

        # --> Add our new class
        self.ut_module.add_class(class_name='NewClassTests')

        # --> Test it
        updater = ModuleUpdater(ut_module=self.ut_module)
        updater._add_new_classes(container=container, new_names=['NewClassTests'])

        # --> Verify
        lines = [line for line in container.lines if 'NewClassTests' in line]
        self.assertEqual(len(lines), 2)

    def test_add_new_tests_to_class(self):
        with mock.patch('tddtags.core.UTModuleContainer.add_class_method', spec=True) as ModCon:
            # ModCon.add_class_method.side_effect = False
            container = UTModuleContainer(module_path=self.tmp_file)
            container.add_class_method.side_effect = self.return_true
            updater = ModuleUpdater(ut_module=self.ut_module)
            reply = updater._add_new_tests_to_class(container, 'ChildSampleTests', ['test_something_else'])
            self.assertTrue(reply)

            self.assertEqual(container.add_class_method.call_count, 1)
            parm_info = [('class_name', 0), ('method_name', 1)]
            kwargs = self.get_patched_call_parms(parm_info, container.add_class_method, 0)
            self.assertEqual(kwargs['class_name'], 'ChildSampleTests')
            self.assertEqual(kwargs['method_name'], 'test_something_else')

    def test_add_new_tests_to_class_container_fail(self):
        with mock.patch('tddtags.core.UTModuleContainer.add_class_method', spec=True) as ModCon:
            # ModCon.add_class_method.side_effect = False
            container = UTModuleContainer(module_path=self.tmp_file)
            container.add_class_method.side_effect = self.return_false
            updater = ModuleUpdater(ut_module=self.ut_module)
            reply = updater._add_new_tests_to_class(container, 'ChildSampleTests', ['test_something_else'])
            self.assertFalse(reply)

            self.assertEqual(container.add_class_method.call_count, 1)

    def test_add_new_tests_to_class_none(self):
         with mock.patch('tddtags.core.UTModuleContainer.add_class_method', spec=True):
             container = UTModuleContainer(module_path=self.tmp_file)
             updater = ModuleUpdater(ut_module=self.ut_module)
             reply = updater._add_new_tests_to_class(container, 'class_name', [])
             self.assertTrue(reply)

             self.assertEqual(container.add_class_method.call_count, 0)

    def test_update_new_methods(self):
        with mock.patch('tddtags.core.ModuleUpdater._add_new_tests_to_class', spec=True) as ModUp:
            # --> Prep
            ut_class = self.ut_module.class_list['ChildSampleTests']
            ut_class.add_method('eat_beans')
            container = None  # We'll also verity that it correctly creates the container and returns it
            loader = ModuleLoader(anchor_dir=self.anchor_dir)
            mod = loader.load_module(self.tmp_module_name)
            updater = ModuleUpdater(ut_module=self.ut_module)
            existing_classes, new_names = updater._get_class_lists(loaded_module=mod)

            # --> The test
            container = updater._update_new_methods(container, self.tmp_file, existing_classes)
            self.assertTrue(container)
            self.assertIsInstance(container, UTModuleContainer)

            self.assertEqual(updater._add_new_tests_to_class.call_count, 1)
            parm_info = [('container', 0), ('class_name', 1), ('new_test_names', 2)]
            kwargs = self.get_patched_call_parms(parm_info, updater._add_new_tests_to_class, 0)
            self.assertEqual(kwargs['class_name'], 'ChildSampleTests')
            self.assertEqual(kwargs['new_test_names'], ['test_eat_beans'])

    def test_update_new_methods_none(self):
        with mock.patch('tddtags.core.ModuleUpdater._add_new_tests_to_class', spec=True) as ModUp:
            # --> Prep
            # (Same as test_update_new_methods, but no new test method added)
            container = None  # We'll also verity that it correctly creates the container and returns it
            loader = ModuleLoader(anchor_dir=self.anchor_dir)
            mod = loader.load_module(self.tmp_module_name)
            updater = ModuleUpdater(ut_module=self.ut_module)
            existing_classes, new_names = updater._get_class_lists(loaded_module=mod)

            # --> The test
            container = updater._update_new_methods(container, self.tmp_file, existing_classes)
            self.assertFalse(container)

            self.assertEqual(updater._add_new_tests_to_class.call_count, 0)

    def test_update_new_methods_no_new(self):
        # self.fail('Test not implemented yet')
        pass

    def test_update_with_save_name(self):
        # self.fail('Test not implemented yet')
        pass
コード例 #3
0
 def test_add_class(self):
     ut_module = UTModuleDetails(module_name='my_mod')
     ut_module.add_class(class_name='SomeClass')
     self.assertTrue('SomeClass' in ut_module.class_list)