Python contextlib 模块,ContextDecorator() 实例源码

我们从Python开源项目中,提取了以下1个代码示例,用于说明如何使用contextlib.ContextDecorator()

项目:taktyk    作者:kosior    | 项目源码 | 文件源码
def test_create(self, mock_get_all_entries, mock_count_tags, mock_db, test_strftime):
        test_strftime.return_value = self.date
        tags = [('testtag1', 123), ('testtag2', 1), ('testtag3', 25)]
        mock_count_tags.return_value = tags
        entry_args = ('444', 'testauthor', 'testdate', '', 'testbody_html', 'testurl', '+1000',
                      'testmedia_url', 'testtags', '')
        sample_entry = Entry(*entry_args)
        mock_get_all_entries.return_value = [sample_entry]

        class MockConnect(ContextDecorator):
            def __enter__(self):
                pass

            def __exit__(self, exc_type, exc_val, exc_tb):
                pass

        DB.Connect = MockConnect
        htmlfile = HtmlFile()
        htmlfile.user_files_path = os.path.join(settings.SAVE_PATH, 'tests')
        htmlfile.create()

        self.assertTrue(os.path.isfile(self.html_path))

        with open(self.html_path, 'r') as file:
            content = file.read()

        for tag, _ in tags:
            self.assertTrue(tag in content)
        for arg in entry_args:
            self.assertTrue(arg in content)