Beispiel #1
0
    def test_do_not_use_folder_as_category(self):

        settings = get_settings(filenames={})
        settings['DEFAULT_CATEGORY'] = 'Default'
        settings['DEFAULT_DATE'] = (1970, 1, 1)
        settings['USE_FOLDER_AS_CATEGORY'] = False
        settings['READERS'] = {'asc': None}
        settings['filenames'] = {}
        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        # test for name
        # categories are grouped by slug; if two categories have the same slug
        # but different names they will be grouped together, the first one in
        # terms of process order will define the name for that category
        categories = [cat.name for cat, _ in generator.categories]
        categories_alternatives = (
            sorted(['Default', 'Yeah', 'test', '指導書']),
            sorted(['Default', 'yeah', 'test', '指導書']),
        )
        self.assertIn(sorted(categories), categories_alternatives)
        # test for slug
        categories = [cat.slug for cat, _ in generator.categories]
        categories_expected = ['default', 'yeah', 'test', 'zhi-dao-shu']
        self.assertEqual(sorted(categories), sorted(categories_expected))
Beispiel #2
0
    def test_do_not_use_folder_as_category(self):

        settings = _DEFAULT_CONFIG.copy()
        settings['ARTICLE_DIR'] = 'content'
        settings['DEFAULT_CATEGORY'] = 'Default'
        settings['DEFAULT_DATE'] = (1970, 1, 1)
        settings['USE_FOLDER_AS_CATEGORY'] = False
        settings['filenames'] = {}
        generator = ArticlesGenerator(settings.copy(), settings,
                            CUR_DIR, _DEFAULT_CONFIG['THEME'], None,
                            _DEFAULT_CONFIG['MARKUP'])
        generator.generate_context()
        # test for name
        # categories are grouped by slug; if two categories have the same slug
        # but different names they will be grouped together, the first one in
        # terms of process order will define the name for that category
        categories = [cat.name for cat, _ in generator.categories]
        categories_alternatives = (
            sorted(['Default', 'Yeah', 'test', '指導書']),
            sorted(['Default', 'yeah', 'test', '指導書']),
        )
        self.assertTrue(sorted(categories) in categories_alternatives)
        # test for slug
        categories = [cat.slug for cat, _ in generator.categories]
        categories_expected = ['default', 'yeah', 'test', 'zhi-dao-shu']
        self.assertEqual(sorted(categories), sorted(categories_expected))
Beispiel #3
0
    def test_generate_context(self):

        settings = _DEFAULT_CONFIG.copy()
        settings['ARTICLE_DIR'] = 'content'
        settings['DEFAULT_CATEGORY'] = 'Default'
        generator = ArticlesGenerator(settings.copy(), settings, CUR_DIR,
                                      _DEFAULT_CONFIG['THEME'], None,
                                      _DEFAULT_CONFIG['MARKUP'])
        generator.generate_context()
        for article in generator.articles:
            relfilepath = os.path.relpath(article.filename, CUR_DIR)
            if relfilepath == os.path.join("TestCategory",
                                           "article_with_category.rst"):
                self.assertEquals(article.category.name, 'yeah')
            elif relfilepath == os.path.join("TestCategory",
                                             "article_without_category.rst"):
                self.assertEquals(article.category.name, 'TestCategory')
            elif relfilepath == "article_without_category.rst":
                self.assertEquals(article.category.name, 'Default')

        categories = [cat.name for cat, _ in generator.categories]
        # assert that the categories are ordered as expected
        self.assertEquals(
                categories, ['Default', 'TestCategory', 'Yeah', 'test',
                             'yeah'])
    def test_article_object_caching(self):
        """Test Article objects caching at the generator level"""
        settings = self._get_cache_enabled_settings()
        settings["CONTENT_CACHING_LAYER"] = "generator"
        settings["DEFAULT_DATE"] = (1970, 1, 1)
        settings["READERS"] = {"asc": None}

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings, path=CONTENT_DIR, theme=settings["THEME"], output_path=None
        )
        generator.generate_context()
        self.assertTrue(hasattr(generator, "_cache"))

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings, path=CONTENT_DIR, theme=settings["THEME"], output_path=None
        )
        generator.readers.read_file = MagicMock()
        generator.generate_context()
        """
        3 Files don't get cached because they were not valid
        - article_with_comments.html
        - article_with_null_attributes.html
        - 2012-11-30_md_w_filename_meta#foo-bar.md
        """
        self.assertEqual(generator.readers.read_file.call_count, 3)
Beispiel #5
0
 def test_do_not_use_folder_as_category(self):
     settings = get_settings(filenames={})
     settings['DEFAULT_CATEGORY'] = 'Default'
     settings['DEFAULT_DATE'] = (1970, 1, 1)
     settings['USE_FOLDER_AS_CATEGORY'] = False
     settings['CACHE_PATH'] = self.temp_cache
     settings['READERS'] = {'asc': None}
     settings['filenames'] = {}
     generator = ArticlesGenerator(context=settings.copy(),
                                   settings=settings,
                                   path=CONTENT_DIR,
                                   theme=settings['THEME'],
                                   output_path=None)
     generator.generate_context()
     # test for name
     # categories are grouped by slug; if two categories have the same slug
     # but different names they will be grouped together, the first one in
     # terms of process order will define the name for that category
     categories = [cat.name for cat, _ in generator.categories]
     categories_alternatives = (
         sorted(['Default', 'Yeah', 'test', '指導書']),
         sorted(['Default', 'yeah', 'test', '指導書']),
     )
     self.assertIn(sorted(categories), categories_alternatives)
     # test for slug
     categories = [cat.slug for cat, _ in generator.categories]
     categories_expected = ['default', 'yeah', 'test', 'zhi-dao-shu']
     self.assertEqual(sorted(categories), sorted(categories_expected))
Beispiel #6
0
    def test_generate_ctags(self):
        settings = get_settings(filenames={})
        settings['GENERATE_CTAGS'] = True

        context = settings.copy()
        context['generated_content'] = dict()
        context['static_links'] = set()
        generator = ArticlesGenerator(context=context,
                                      settings=settings,
                                      path=TEST_CONTENT_DIR,
                                      theme=settings['THEME'],
                                      output_path=TEST_CONTENT_DIR)
        generator.generate_context()

        writer = Writer(TEST_CONTENT_DIR, settings=settings)
        generate_ctags(generator, writer)

        output_path = os.path.join(TEST_CONTENT_DIR, 'tags')
        self.assertTrue(os.path.exists(output_path))

        try:
            # output content is correct
            with open(output_path, 'r') as output_file:
                ctags = [l.split('\t')[0] for l in output_file.readlines()]
                self.assertEqual([
                    'bar', 'bar', 'foo', 'foo', 'foobar', 'foobar', 'マック',
                    'パイソン'
                ], ctags)
        finally:
            os.remove(output_path)
Beispiel #7
0
    def test_article_object_caching(self):
        """Test Article objects caching at the generator level"""
        settings = self._get_cache_enabled_settings()
        settings['CONTENT_CACHING_LAYER'] = 'generator'
        settings['DEFAULT_DATE'] = (1970, 1, 1)
        settings['READERS'] = {'asc': None}

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        self.assertTrue(hasattr(generator, '_cache'))

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.readers.read_file = MagicMock()
        generator.generate_context()
        """
        6 files don't get cached because they were not valid
        - article_with_attributes_containing_double_quotes.html
        - article_with_comments.html
        - article_with_null_attributes.html
        - 2012-11-30_md_w_filename_meta#foo-bar.md
        - empty.md
        - empty_with_bom.md
        """
        self.assertEqual(generator.readers.read_file.call_count, 6)
Beispiel #8
0
    def test_article_object_caching(self):
        """Test Article objects caching at the generator level"""
        settings = self._get_cache_enabled_settings()
        settings['CONTENT_CACHING_LAYER'] = 'generator'
        settings['DEFAULT_DATE'] = (1970, 1, 1)
        settings['READERS'] = {'asc': None}

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        self.assertTrue(hasattr(generator, '_cache'))

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.readers.read_file = MagicMock()
        generator.generate_context()
        """
        6 files don't get cached because they were not valid
        - article_with_attributes_containing_double_quotes.html
        - article_with_comments.html
        - article_with_null_attributes.html
        - 2012-11-30_md_w_filename_meta#foo-bar.md
        - empty.md
        - empty_with_bom.md
        """
        self.assertEqual(generator.readers.read_file.call_count, 6)
Beispiel #9
0
    def test_generate_context(self):

        settings = _DEFAULT_CONFIG.copy()
        settings['ARTICLE_DIR'] = 'content'
        settings['DEFAULT_CATEGORY'] = 'Default'
        generator = ArticlesGenerator(settings.copy(), settings, CUR_DIR,
                                      _DEFAULT_CONFIG['THEME'], None,
                                      _DEFAULT_CONFIG['MARKUP'])
        generator.generate_context()
        for article in generator.articles:
            relfilepath = os.path.relpath(article.filename, CUR_DIR)
            if relfilepath == os.path.join("TestCategory",
                                           "article_with_category.rst"):
                self.assertEquals(article.category.name, 'yeah')
            elif relfilepath == os.path.join("TestCategory",
                                             "article_without_category.rst"):
                self.assertEquals(article.category.name, 'TestCategory')
            elif relfilepath == "article_without_category.rst":
                self.assertEquals(article.category.name, 'Default')

        categories = [cat.name for cat, _ in generator.categories]
        # assert that the categories are ordered as expected
        self.assertEquals(
                categories, ['Default', 'TestCategory', 'Yeah', 'test',
                             'yeah'])
Beispiel #10
0
    def test_standard_metadata_in_default_metadata(self):
        settings = get_settings(filenames={})
        settings["CACHE_CONTENT"] = False
        settings["DEFAULT_CATEGORY"] = "Default"
        settings["DEFAULT_DATE"] = (1970, 1, 1)
        settings["DEFAULT_METADATA"] = (
            ("author", "Blogger"),
            # category will be ignored in favor of
            # DEFAULT_CATEGORY
            ("category", "Random"),
            ("tags", "general, untagged"),
        )
        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings, path=CONTENT_DIR, theme=settings["THEME"], output_path=None
        )
        generator.generate_context()

        authors = sorted([author.name for author, _ in generator.authors])
        authors_expected = sorted(
            ["Alexis Métaireau", "Blogger", "Author, First", "Author, Second", "First Author", "Second Author"]
        )
        self.assertEqual(authors, authors_expected)

        categories = sorted([category.name for category, _ in generator.categories])
        categories_expected = [
            sorted(["Default", "TestCategory", "yeah", "test", "指導書"]),
            sorted(["Default", "TestCategory", "Yeah", "test", "指導書"]),
        ]
        self.assertIn(categories, categories_expected)

        tags = sorted([tag.name for tag in generator.tags])
        tags_expected = sorted(["bar", "foo", "foobar", "general", "untagged", "パイソン", "マック"])
        self.assertEqual(tags, tags_expected)
Beispiel #11
0
    def _create_generator(tmp_path):
        series.register()

        settings = get_settings()
        settings["CACHE_CONTENT"] = False
        settings["PLUGINS"] = [series]

        context = get_context(settings)

        base_path = os.path.dirname(os.path.abspath(__file__))
        test_data_path = os.path.join(base_path, data_path)

        generator = ArticlesGenerator(
            context=context,
            settings=settings,
            path=test_data_path,
            theme=settings["THEME"],
            output_path=tmp_path,
        )

        generator.generate_context()

        items = generator.articles

        if sort_key:
            items = sorted(items, key=sort_key)

        return generator, items
Beispiel #12
0
 def test_do_not_use_folder_as_category(self):
     settings = get_settings(filenames={})
     settings["DEFAULT_CATEGORY"] = "Default"
     settings["DEFAULT_DATE"] = (1970, 1, 1)
     settings["USE_FOLDER_AS_CATEGORY"] = False
     settings["CACHE_PATH"] = self.temp_cache
     settings["READERS"] = {"asc": None}
     settings["filenames"] = {}
     generator = ArticlesGenerator(
         context=settings.copy(), settings=settings, path=CONTENT_DIR, theme=settings["THEME"], output_path=None
     )
     generator.generate_context()
     # test for name
     # categories are grouped by slug; if two categories have the same slug
     # but different names they will be grouped together, the first one in
     # terms of process order will define the name for that category
     categories = [cat.name for cat, _ in generator.categories]
     categories_alternatives = (
         sorted(["Default", "Yeah", "test", "指導書"]),
         sorted(["Default", "yeah", "test", "指導書"]),
     )
     self.assertIn(sorted(categories), categories_alternatives)
     # test for slug
     categories = [cat.slug for cat, _ in generator.categories]
     categories_expected = ["default", "yeah", "test", "zhi-dao-shu"]
     self.assertEqual(sorted(categories), sorted(categories_expected))
Beispiel #13
0
    def test_standard_metadata_in_default_metadata(self):
        settings = get_settings()
        settings['CACHE_CONTENT'] = False
        settings['DEFAULT_CATEGORY'] = 'Default'
        settings['DEFAULT_DATE'] = (1970, 1, 1)
        settings['DEFAULT_METADATA'] = (('author', 'Blogger'),
                                        # category will be ignored in favor of
                                        # DEFAULT_CATEGORY
                                        ('category', 'Random'),
                                        ('tags', 'general, untagged'))
        context = get_context(settings)
        generator = ArticlesGenerator(
            context=context, settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()

        authors = sorted([author.name for author, _ in generator.authors])
        authors_expected = sorted(['Alexis Métaireau', 'Blogger',
                                   'Author, First', 'Author, Second',
                                   'First Author', 'Second Author'])
        self.assertEqual(authors, authors_expected)

        categories = sorted([category.name
                             for category, _ in generator.categories])
        categories_expected = [
            sorted(['Default', 'TestCategory', 'yeah', 'test', '指導書']),
            sorted(['Default', 'TestCategory', 'Yeah', 'test', '指導書'])]
        self.assertIn(categories, categories_expected)

        tags = sorted([tag.name for tag in generator.tags])
        tags_expected = sorted(['bar', 'foo', 'foobar', 'general', 'untagged',
                                'パイソン', 'マック'])
        self.assertEqual(tags, tags_expected)
Beispiel #14
0
    def test_article_reader_content_caching(self):
        """Test raw article content caching at the reader level"""
        settings = self._get_cache_enabled_settings()
        settings['READERS'] = {'asc': None}
        context = get_context(settings)

        generator = ArticlesGenerator(context=context.copy(),
                                      settings=settings,
                                      path=CONTENT_DIR,
                                      theme=settings['THEME'],
                                      output_path=None)
        generator.generate_context()
        self.assertTrue(hasattr(generator.readers, '_cache'))

        generator = ArticlesGenerator(context=context.copy(),
                                      settings=settings,
                                      path=CONTENT_DIR,
                                      theme=settings['THEME'],
                                      output_path=None)
        readers = generator.readers.readers
        for reader in readers.values():
            reader.read = MagicMock()
        generator.generate_context()
        for reader in readers.values():
            self.assertEqual(reader.read.call_count, 0)
Beispiel #15
0
    def test_do_not_use_folder_as_category(self):

        settings = _DEFAULT_CONFIG.copy()
        settings['ARTICLE_DIR'] = 'content'
        settings['DEFAULT_CATEGORY'] = 'Default'
        settings['DEFAULT_DATE'] = (1970, 1, 1)
        settings['USE_FOLDER_AS_CATEGORY'] = False
        settings['filenames'] = {}
        generator = ArticlesGenerator(settings.copy(), settings, CUR_DIR,
                                      _DEFAULT_CONFIG['THEME'], None,
                                      _DEFAULT_CONFIG['MARKUP'])
        generator.generate_context()
        # test for name
        # categories are grouped by slug; if two categories have the same slug
        # but different names they will be grouped together, the first one in
        # terms of process order will define the name for that category
        categories = [cat.name for cat, _ in generator.categories]
        categories_alternatives = (
            sorted(['Default', 'Yeah', 'test', '指導書']),
            sorted(['Default', 'yeah', 'test', '指導書']),
        )
        self.assertTrue(sorted(categories) in categories_alternatives)
        # test for slug
        categories = [cat.slug for cat, _ in generator.categories]
        categories_expected = ['default', 'yeah', 'test', 'zhi-dao-shu']
        self.assertEqual(sorted(categories), sorted(categories_expected))
    def test_generate_ctags(self):
        settings = get_settings(filenames={})
        settings["GENERATE_CTAGS"] = True

        context = settings.copy()
        context["generated_content"] = dict()
        context["static_links"] = set()
        generator = ArticlesGenerator(
            context=context,
            settings=settings,
            path=TEST_CONTENT_DIR,
            theme=settings["THEME"],
            output_path=TEST_CONTENT_DIR,
        )
        generator.generate_context()

        writer = Writer(TEST_CONTENT_DIR, settings=settings)
        generate_ctags(generator, writer)

        output_path = os.path.join(TEST_CONTENT_DIR, "tags")
        self.assertTrue(os.path.exists(output_path))

        try:
            # output content is correct
            with open(output_path, "r") as output_file:
                ctags = [l.split("\t")[0] for l in output_file.readlines()]
                self.assertEqual(
                    [
                        "bar", "bar", "foo", "foo", "foobar", "foobar", "マック",
                        "パイソン"
                    ],
                    ctags,
                )
        finally:
            os.remove(output_path)
Beispiel #17
0
    def test_article_ignore_cache(self):
        """Test that all the articles are read again when not loading cache

        used in --ignore-cache or autoreload mode"""
        settings = self._get_cache_enabled_settings()
        settings['READERS'] = {'asc': None}
        context = get_context(settings)

        generator = ArticlesGenerator(
            context=context.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.readers.read_file = MagicMock()
        generator.generate_context()
        self.assertTrue(hasattr(generator, '_cache_open'))
        orig_call_count = generator.readers.read_file.call_count

        settings['LOAD_CONTENT_CACHE'] = False
        generator = ArticlesGenerator(
            context=context.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.readers.read_file = MagicMock()
        generator.generate_context()
        self.assertEqual(
            generator.readers.read_file.call_count,
            orig_call_count)
Beispiel #18
0
    def test_article_ignore_cache(self):
        """Test that all the articles are read again when not loading cache

        used in --ignore-cache or autoreload mode"""
        settings = self._get_cache_enabled_settings()
        settings['READERS'] = {'asc': None}

        generator = ArticlesGenerator(context=settings.copy(),
                                      settings=settings,
                                      path=CONTENT_DIR,
                                      theme=settings['THEME'],
                                      output_path=None)
        generator.readers.read_file = MagicMock()
        generator.generate_context()
        self.assertTrue(hasattr(generator, '_cache_open'))
        orig_call_count = generator.readers.read_file.call_count

        settings['LOAD_CONTENT_CACHE'] = False
        generator = ArticlesGenerator(context=settings.copy(),
                                      settings=settings,
                                      path=CONTENT_DIR,
                                      theme=settings['THEME'],
                                      output_path=None)
        generator.readers.read_file = MagicMock()
        generator.generate_context()
        self.assertEqual(generator.readers.read_file.call_count,
                         orig_call_count)
Beispiel #19
0
    def test_standard_metadata_in_default_metadata(self):
        settings = get_settings(filenames={})
        settings['CACHE_CONTENT'] = False
        settings['DEFAULT_CATEGORY'] = 'Default'
        settings['DEFAULT_DATE'] = (1970, 1, 1)
        settings['DEFAULT_METADATA'] = (('author', 'Blogger'),
                                        # category will be ignored in favor of
                                        # DEFAULT_CATEGORY
                                        ('category', 'Random'),
                                        ('tags', 'general, untagged'))
        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()

        authors = sorted([author.name for author, _ in generator.authors])
        authors_expected = sorted(['Alexis Métaireau', 'Blogger',
                                   'Author, First', 'Author, Second',
                                   'First Author', 'Second Author'])
        self.assertEqual(authors, authors_expected)

        categories = sorted([category.name
                             for category, _ in generator.categories])
        categories_expected = [
            sorted(['Default', 'TestCategory', 'yeah', 'test', '指導書']),
            sorted(['Default', 'TestCategory', 'Yeah', 'test', '指導書'])]
        self.assertIn(categories, categories_expected)

        tags = sorted([tag.name for tag in generator.tags])
        tags_expected = sorted(['bar', 'foo', 'foobar', 'general', 'untagged',
                                'パイソン', 'マック'])
        self.assertEqual(tags, tags_expected)
Beispiel #20
0
    def test_reader_content_caching(self):
        """Test raw content caching at the reader level"""
        settings = get_settings(filenames={})
        settings['CACHE_PATH'] = self.temp_cache
        settings['READERS'] = {'asc': None}

        generator = ArticlesGenerator(context=settings.copy(),
                                      settings=settings,
                                      path=CONTENT_DIR,
                                      theme=settings['THEME'],
                                      output_path=None)
        generator.generate_context()
        self.assertTrue(hasattr(generator.readers, '_cache'))

        generator = ArticlesGenerator(context=settings.copy(),
                                      settings=settings,
                                      path=CONTENT_DIR,
                                      theme=settings['THEME'],
                                      output_path=None)
        readers = generator.readers.readers
        for reader in readers.values():
            reader.read = MagicMock()
        generator.generate_context()
        for reader in readers.values():
            reader.read.assert_called_count == 0
Beispiel #21
0
    def test_article_order_by(self):
        settings = get_settings(filenames={})
        settings["DEFAULT_CATEGORY"] = "Default"
        settings["DEFAULT_DATE"] = (1970, 1, 1)
        settings["ARTICLE_ORDER_BY"] = "title"

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings, path=CONTENT_DIR, theme=settings["THEME"], output_path=None
        )
        generator.generate_context()

        expected = [
            "An Article With Code Block To Test Typogrify Ignore",
            "Article title",
            "Article with Nonconformant HTML meta tags",
            "Article with markdown and summary metadata multi",
            "Article with markdown and summary metadata single",
            "Article with markdown containing footnotes",
            "Article with template",
            "Rst with filename metadata",
            "Test Markdown extensions",
            "Test markdown File",
            "Test md File",
            "Test mdown File",
            "Test metadata duplicates",
            "Test mkd File",
            "This is a super article !",
            "This is a super article !",
            "This is a super article !",
            "This is a super article !",
            "This is a super article !",
            "This is a super article !",
            "This is an article with category !",
            ("This is an article with multiple authors in lastname, " "firstname format!"),
            "This is an article with multiple authors in list format!",
            "This is an article with multiple authors!",
            "This is an article with multiple authors!",
            "This is an article without category !",
            "This is an article without category !",
            "マックOS X 10.8でパイソンとVirtualenvをインストールと設定",
        ]

        articles = [article.title for article in generator.articles]
        self.assertEqual(articles, expected)

        # reversed title
        settings = get_settings(filenames={})
        settings["DEFAULT_CATEGORY"] = "Default"
        settings["DEFAULT_DATE"] = (1970, 1, 1)
        settings["ARTICLE_ORDER_BY"] = "reversed-title"

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings, path=CONTENT_DIR, theme=settings["THEME"], output_path=None
        )
        generator.generate_context()

        articles = [article.title for article in generator.articles]
        self.assertEqual(articles, list(reversed(expected)))
Beispiel #22
0
    def test_article_order_by(self):
        settings = get_settings(filenames={})
        settings['DEFAULT_CATEGORY'] = 'Default'
        settings['DEFAULT_DATE'] = (1970, 1, 1)
        settings['ARTICLE_ORDER_BY'] = 'title'

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()

        expected = [
            'An Article With Code Block To Test Typogrify Ignore',
            'Article title',
            'Article with Nonconformant HTML meta tags',
            'Article with markdown and summary metadata multi',
            'Article with markdown and summary metadata single',
            'Article with markdown containing footnotes',
            'Article with template',
            'Rst with filename metadata',
            'Test Markdown extensions',
            'Test markdown File',
            'Test md File',
            'Test mdown File',
            'Test metadata duplicates',
            'Test mkd File',
            'This is a super article !',
            'This is a super article !',
            'This is a super article !',
            'This is a super article !',
            'This is a super article !',
            'This is a super article !',
            'This is an article with category !',
            ('This is an article with multiple authors in lastname, '
             'firstname format!'),
            'This is an article with multiple authors in list format!',
            'This is an article with multiple authors!',
            'This is an article with multiple authors!',
            'This is an article without category !',
            'This is an article without category !',
            'マックOS X 10.8でパイソンとVirtualenvをインストールと設定']

        articles = [article.title for article in generator.articles]
        self.assertEqual(articles, expected)

        # reversed title
        settings = get_settings(filenames={})
        settings['DEFAULT_CATEGORY'] = 'Default'
        settings['DEFAULT_DATE'] = (1970, 1, 1)
        settings['ARTICLE_ORDER_BY'] = 'reversed-title'

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()

        articles = [article.title for article in generator.articles]
        self.assertEqual(articles, list(reversed(expected)))
Beispiel #23
0
    def test_article_order_by(self):
        settings = get_settings(filenames={})
        settings['DEFAULT_CATEGORY'] = 'Default'
        settings['DEFAULT_DATE'] = (1970, 1, 1)
        settings['CACHE_CONTENT'] = False   # cache not needed for this logic tests
        settings['ARTICLE_ORDER_BY'] = 'title'

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()

        expected = [
            'An Article With Code Block To Test Typogrify Ignore',
            'Article title',
            'Article with Nonconformant HTML meta tags',
            'Article with markdown and summary metadata multi',
            'Article with markdown and summary metadata single',
            'Article with markdown containing footnotes',
            'Article with template',
            'Rst with filename metadata',
            'Test Markdown extensions',
            'Test markdown File',
            'Test md File',
            'Test mdown File',
            'Test mkd File',
            'This is a super article !',
            'This is a super article !',
            'This is a super article !',
            'This is a super article !',
            'This is a super article !',
            'This is a super article !',
            'This is an article with category !',
            'This is an article with multiple authors in lastname, firstname format!',
            'This is an article with multiple authors in list format!',
            'This is an article with multiple authors!',
            'This is an article with multiple authors!',
            'This is an article without category !',
            'This is an article without category !',
            'マックOS X 10.8でパイソンとVirtualenvをインストールと設定']

        articles = [article.title for article in generator.articles]
        self.assertEqual(articles, expected)

        # reversed title
        settings = get_settings(filenames={})
        settings['DEFAULT_CATEGORY'] = 'Default'
        settings['DEFAULT_DATE'] = (1970, 1, 1)
        settings['CACHE_CONTENT'] = False   # cache not needed for this logic tests
        settings['ARTICLE_ORDER_BY'] = 'reversed-title'

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()

        articles = [article.title for article in generator.articles]
        self.assertEqual(articles, list(reversed(expected)))
Beispiel #24
0
def build_article_generator(settings, content_path, output_path=None):
    context = settings.copy()
    context['generated_content'] = dict()
    context['static_links'] = set()
    article_generator = ArticlesGenerator(
        context=context, settings=settings,
        path=content_path, theme=settings['THEME'], output_path=output_path)
    article_generator.generate_context()
    return article_generator
Beispiel #25
0
def _build_article_generator(content_path, output_path):
    settings = get_settings(filenames={})
    settings['PATH'] = content_path
    context = settings.copy()
    context['generated_content'] = dict()
    context['static_links'] = set()
    article_generator = ArticlesGenerator(
        context=context, settings=settings,
        path=settings['PATH'], theme=settings['THEME'], output_path=output_path)
    article_generator.generate_context()
    return article_generator
def _build_article_generator(content_path, tmpdir, site_url='http://localhost/blog/'):
    settings = get_settings(filenames={})
    _setup_cache_dir(settings['CACHE_PATH'])
    settings['SITEURL'] = site_url
    context = settings.copy()
    context['generated_content'] = {}
    context['static_links'] = set()
    article_generator = ArticlesGenerator(
        context=context, settings=settings,
        path=content_path, theme=settings['THEME'], output_path=str(tmpdir))
    article_generator.generate_context()
    return article_generator
Beispiel #27
0
    def test_do_not_use_folder_as_category(self):

        settings = _DEFAULT_CONFIG.copy()
        settings['ARTICLE_DIR'] = 'content'
        settings['DEFAULT_CATEGORY'] = 'Default'
        settings['USE_FOLDER_AS_CATEGORY'] = False
        generator = ArticlesGenerator(settings.copy(), settings,
                            CUR_DIR, _DEFAULT_CONFIG['THEME'], None,
                            _DEFAULT_CONFIG['MARKUP'])
        generator.generate_context()

        categories = [cat.name for cat, _ in generator.categories]
        self.assertEquals(categories, ['Default', 'Yeah', 'test', 'yeah'])
Beispiel #28
0
    def test_do_not_use_folder_as_category(self):

        settings = _DEFAULT_CONFIG.copy()
        settings['ARTICLE_DIR'] = 'content'
        settings['DEFAULT_CATEGORY'] = 'Default'
        settings['USE_FOLDER_AS_CATEGORY'] = False
        generator = ArticlesGenerator(settings.copy(), settings, CUR_DIR,
                                      _DEFAULT_CONFIG['THEME'], None,
                                      _DEFAULT_CONFIG['MARKUP'])
        generator.generate_context()

        categories = [cat.name for cat, _ in generator.categories]
        self.assertEquals(categories, ['Default', 'Yeah', 'test', 'yeah'])
Beispiel #29
0
    def test_do_not_use_folder_as_category(self):

        settings = _DEFAULT_CONFIG.copy()
        settings["ARTICLE_DIR"] = "content"
        settings["DEFAULT_CATEGORY"] = "Default"
        settings["USE_FOLDER_AS_CATEGORY"] = False
        generator = ArticlesGenerator(
            settings.copy(), settings, CUR_DIR, _DEFAULT_CONFIG["THEME"], None, _DEFAULT_CONFIG["MARKUP"]
        )
        generator.generate_context()

        categories = [cat.name for cat, _ in generator.categories]
        self.assertEquals(categories, ["Default", "Yeah", "test", "yeah"])
def _build_article_generator(settings, output_path):
    context = settings.copy()
    context["generated_content"] = dict()
    context["static_links"] = set()
    article_generator = ArticlesGenerator(
        context=context,
        settings=settings,
        path=settings["PATH"],
        theme=settings["THEME"],
        output_path=output_path,
    )
    article_generator.generate_context()
    return article_generator
Beispiel #31
0
    def test_generator_caching(self):
        """Test that cached and uncached content is same in generator level"""
        settings = self._get_cache_enabled_settings()
        settings['CONTENT_CACHING_LAYER'] = 'generator'
        settings['PAGE_PATHS'] = ['TestPages']
        settings['DEFAULT_DATE'] = (1970, 1, 1)
        settings['READERS'] = {'asc': None}
        context = get_context(settings)

        def sorted_titles(items):
            return sorted(item.title for item in items)

        # Articles
        generator = ArticlesGenerator(context=context.copy(),
                                      settings=settings,
                                      path=CONTENT_DIR,
                                      theme=settings['THEME'],
                                      output_path=None)
        generator.generate_context()
        uncached_articles = sorted_titles(generator.articles)
        uncached_drafts = sorted_titles(generator.drafts)

        generator = ArticlesGenerator(context=context.copy(),
                                      settings=settings,
                                      path=CONTENT_DIR,
                                      theme=settings['THEME'],
                                      output_path=None)
        generator.generate_context()
        cached_articles = sorted_titles(generator.articles)
        cached_drafts = sorted_titles(generator.drafts)

        self.assertEqual(uncached_articles, cached_articles)
        self.assertEqual(uncached_drafts, cached_drafts)

        # Pages
        generator = PagesGenerator(context=context.copy(),
                                   settings=settings,
                                   path=CUR_DIR,
                                   theme=settings['THEME'],
                                   output_path=None)
        generator.generate_context()
        uncached_pages = sorted_titles(generator.pages)
        uncached_hidden_pages = sorted_titles(generator.hidden_pages)
        uncached_draft_pages = sorted_titles(generator.draft_pages)

        generator = PagesGenerator(context=context.copy(),
                                   settings=settings,
                                   path=CUR_DIR,
                                   theme=settings['THEME'],
                                   output_path=None)
        generator.generate_context()
        cached_pages = sorted_titles(generator.pages)
        cached_hidden_pages = sorted_titles(generator.hidden_pages)
        cached_draft_pages = sorted_titles(generator.draft_pages)

        self.assertEqual(uncached_pages, cached_pages)
        self.assertEqual(uncached_hidden_pages, cached_hidden_pages)
        self.assertEqual(uncached_draft_pages, cached_draft_pages)
Beispiel #32
0
    def test_content_caching(self):
        """Test that the articles are read only once when caching"""
        settings = get_settings(filenames={})
        settings['CACHE_DIRECTORY'] = self.temp_cache
        settings['READERS'] = {'asc': None}

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        self.assertTrue(hasattr(generator, '_cache'))

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.readers.read_file = MagicMock()
        generator.generate_context()
        generator.readers.read_file.assert_called_count == 0
Beispiel #33
0
    def test_content_caching(self):
        """Test that the articles are read only once when caching"""
        settings = get_settings(filenames={})
        settings['CACHE_DIRECTORY'] = self.temp_cache
        settings['READERS'] = {'asc': None}

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        self.assertTrue(hasattr(generator, '_cache'))

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.readers.read_file = MagicMock()
        generator.generate_context()
        generator.readers.read_file.assert_called_count == 0
Beispiel #34
0
    def test_generate_context(self):

        settings = _DEFAULT_CONFIG
        settings['ARTICLE_DIR'] = 'content'
        settings['DEFAULT_CATEGORY'] = 'Default'
        generator = ArticlesGenerator(settings.copy(), settings, CUR_DIR,
                                      _DEFAULT_CONFIG['THEME'], None,
                                      _DEFAULT_CONFIG['MARKUP'])
        generator.generate_context()
        for article in generator.articles:
            relfilepath = os.path.relpath(article.filename, CUR_DIR)
            if relfilepath == os.path.join("TestCategory",
                                           "article_with_category.rst"):
                self.assertEquals(article.category.name, 'yeah')
            elif relfilepath == os.path.join("TestCategory",
                                             "article_without_category.rst"):
                self.assertEquals(article.category.name, 'TestCategory')
            elif relfilepath == "article_without_category.rst":
                self.assertEquals(article.category.name, 'Default')
Beispiel #35
0
    def test_article_object_caching(self):
        """Test Article objects caching at the generator level"""
        settings = get_settings(filenames={})
        settings['CACHE_DIRECTORY'] = self.temp_cache
        settings['CONTENT_CACHING_LAYER'] = 'generator'
        settings['READERS'] = {'asc': None}

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        self.assertTrue(hasattr(generator, '_cache'))

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.readers.read_file = MagicMock()
        generator.generate_context()
        generator.readers.read_file.assert_called_count == 0
Beispiel #36
0
    def test_article_object_caching(self):
        """Test Article objects caching at the generator level"""
        settings = get_settings(filenames={})
        settings['CACHE_PATH'] = self.temp_cache
        settings['CONTENT_CACHING_LAYER'] = 'generator'
        settings['READERS'] = {'asc': None}

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        self.assertTrue(hasattr(generator, '_cache'))

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.readers.read_file = MagicMock()
        generator.generate_context()
        generator.readers.read_file.assert_called_count == 0
Beispiel #37
0
    def test_article_reader_content_caching(self):
        """Test raw article content caching at the reader level"""
        settings = self._get_cache_enabled_settings()
        settings['READERS'] = {'asc': None}

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        self.assertTrue(hasattr(generator.readers, '_cache'))

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        readers = generator.readers.readers
        for reader in readers.values():
            reader.read = MagicMock()
        generator.generate_context()
        for reader in readers.values():
            self.assertEqual(reader.read.call_count, 0)
Beispiel #38
0
    def test_reader_content_caching(self):
        """Test raw content caching at the reader level"""
        settings = get_settings(filenames={})
        settings['CACHE_PATH'] = self.temp_cache
        settings['READERS'] = {'asc': None}

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        self.assertTrue(hasattr(generator.readers, '_cache'))

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        readers = generator.readers.readers
        for reader in readers.values():
            reader.read = MagicMock()
        generator.generate_context()
        for reader in readers.values():
            reader.read.assert_called_count == 0
Beispiel #39
0
    def test_generator_caching(self):
        """Test that cached and uncached content is same in generator level"""
        settings = self._get_cache_enabled_settings()
        settings['CONTENT_CACHING_LAYER'] = 'generator'
        settings['PAGE_PATHS'] = ['TestPages']
        settings['DEFAULT_DATE'] = (1970, 1, 1)
        settings['READERS'] = {'asc': None}
        context = get_context(settings)

        def sorted_titles(items):
            return sorted(item.title for item in items)

        # Articles
        generator = ArticlesGenerator(
            context=context.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        uncached_articles = sorted_titles(generator.articles)
        uncached_drafts = sorted_titles(generator.drafts)

        generator = ArticlesGenerator(
            context=context.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        cached_articles = sorted_titles(generator.articles)
        cached_drafts = sorted_titles(generator.drafts)

        self.assertEqual(uncached_articles, cached_articles)
        self.assertEqual(uncached_drafts, cached_drafts)

        # Pages
        generator = PagesGenerator(
            context=context.copy(), settings=settings,
            path=CUR_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        uncached_pages = sorted_titles(generator.pages)
        uncached_hidden_pages = sorted_titles(generator.hidden_pages)
        uncached_draft_pages = sorted_titles(generator.draft_pages)

        generator = PagesGenerator(
            context=context.copy(), settings=settings,
            path=CUR_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        cached_pages = sorted_titles(generator.pages)
        cached_hidden_pages = sorted_titles(generator.hidden_pages)
        cached_draft_pages = sorted_titles(generator.draft_pages)

        self.assertEqual(uncached_pages, cached_pages)
        self.assertEqual(uncached_hidden_pages, cached_hidden_pages)
        self.assertEqual(uncached_draft_pages, cached_draft_pages)
Beispiel #40
0
    def test_period_in_timeperiod_archive(self):
        """
        Test that the context of a generated period_archive is passed
        'period' : a tuple of year, month, day according to the time period
        """
        settings = get_settings(filenames={})

        settings['YEAR_ARCHIVE_SAVE_AS'] = 'posts/{date:%Y}/index.html'
        settings['CACHE_DIRECTORY'] = self.temp_cache
        generator = ArticlesGenerator(
            context=settings, settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        write = MagicMock()
        generator.generate_period_archives(write)
        dates = [d for d in generator.dates if d.date.year == 1970]
        self.assertEqual(len(dates), 1)
        #among other things it must have at least been called with this
        settings["period"] = (1970,)
        write.assert_called_with("posts/1970/index.html",
                                 generator.get_template("period_archives"),
                                 settings,
                                 blog=True, dates=dates)

        del settings["period"]
        settings['MONTH_ARCHIVE_SAVE_AS'] = 'posts/{date:%Y}/{date:%b}/index.html'
        generator = ArticlesGenerator(
            context=settings, settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        write = MagicMock()
        generator.generate_period_archives(write)
        dates = [d for d in generator.dates if d.date.year == 1970
                                            and d.date.month == 1]
        self.assertEqual(len(dates), 1)
        settings["period"] = (1970, "January")
        #among other things it must have at least been called with this
        write.assert_called_with("posts/1970/Jan/index.html",
                                 generator.get_template("period_archives"),
                                 settings,
                                 blog=True, dates=dates)

        del settings["period"]
        settings['DAY_ARCHIVE_SAVE_AS'] = 'posts/{date:%Y}/{date:%b}/{date:%d}/index.html'
        generator = ArticlesGenerator(
            context=settings, settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        write = MagicMock()
        generator.generate_period_archives(write)
        dates = [d for d in generator.dates if d.date.year == 1970
                                            and d.date.month == 1
                                            and d.date.day == 1]
        self.assertEqual(len(dates), 1)
        settings["period"] = (1970, "January", 1)
        #among other things it must have at least been called with this
        write.assert_called_with("posts/1970/Jan/01/index.html",
                                 generator.get_template("period_archives"),
                                 settings,
                                 blog=True, dates=dates)
Beispiel #41
0
    def test_period_in_timeperiod_archive(self):
        """
        Test that the context of a generated period_archive is passed
        'period' : a tuple of year, month, day according to the time period
        """
        settings = get_settings(filenames={})

        settings['YEAR_ARCHIVE_SAVE_AS'] = 'posts/{date:%Y}/index.html'
        settings['CACHE_PATH'] = self.temp_cache
        generator = ArticlesGenerator(
            context=settings, settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        write = MagicMock()
        generator.generate_period_archives(write)
        dates = [d for d in generator.dates if d.date.year == 1970]
        self.assertEqual(len(dates), 1)
        #among other things it must have at least been called with this
        settings["period"] = (1970,)
        write.assert_called_with("posts/1970/index.html",
                                 generator.get_template("period_archives"),
                                 settings,
                                 blog=True, dates=dates)

        del settings["period"]
        settings['MONTH_ARCHIVE_SAVE_AS'] = 'posts/{date:%Y}/{date:%b}/index.html'
        generator = ArticlesGenerator(
            context=settings, settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        write = MagicMock()
        generator.generate_period_archives(write)
        dates = [d for d in generator.dates if d.date.year == 1970
                                            and d.date.month == 1]
        self.assertEqual(len(dates), 1)
        settings["period"] = (1970, "January")
        #among other things it must have at least been called with this
        write.assert_called_with("posts/1970/Jan/index.html",
                                 generator.get_template("period_archives"),
                                 settings,
                                 blog=True, dates=dates)

        del settings["period"]
        settings['DAY_ARCHIVE_SAVE_AS'] = 'posts/{date:%Y}/{date:%b}/{date:%d}/index.html'
        generator = ArticlesGenerator(
            context=settings, settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        write = MagicMock()
        generator.generate_period_archives(write)
        dates = [d for d in generator.dates if d.date.year == 1970
                                            and d.date.month == 1
                                            and d.date.day == 1]
        self.assertEqual(len(dates), 1)
        settings["period"] = (1970, "January", 1)
        #among other things it must have at least been called with this
        write.assert_called_with("posts/1970/Jan/01/index.html",
                                 generator.get_template("period_archives"),
                                 settings,
                                 blog=True, dates=dates)
    def test_generate_ctags(self):
        settings = get_settings(filenames={})
        settings['GENERATE_CTAGS'] = True

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=TEST_CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()

        writer = Writer(None, settings=settings)
        generate_ctags(generator, writer)

        output_path = os.path.join(TEST_CONTENT_DIR, 'tags')
        self.assertTrue(os.path.exists(output_path))

        try:
            # output content is correct
            with open(output_path, 'r') as output_file:
                ctags = [l.split('\t')[0] for l in output_file.readlines()]
                self.assertEqual(['bar', 'bar', 'foo', 'foo', 'foobar', 'foobar', 'マック', 'パイソン'], ctags)
        finally:
            os.remove(output_path)
Beispiel #43
0
    def gen_author_and_html_from_name(self, name):
        context = self.settings.copy()
        context['generated_content'] = {}
        context['static_links'] = set()
        context['static_content'] = {}
        context['localsiteurl'] = self.settings['SITEURL']
        generator = ArticlesGenerator(context=context,
                                      settings=self.settings,
                                      path=CONTENT_DIR,
                                      theme=self.settings['THEME'],
                                      output_path=OUTPUT_DIR)
        generator.generate_context()
        generator.generate_authors(self.writer.write_file)
        selectedAuthor = None

        for author, articles in generator.authors:
            if author.name == name:
                selectedAuthor = author

        soup = BeautifulSoup(
            open("./" + self.writer.output_path + '/' +
                 selectedAuthor.save_as), "html.parser")
        return (selectedAuthor, soup)
Beispiel #44
0
    def test_full_rebuild(self):
        """Test that all the articles are read again when not loading cache

        used in --full-rebuild or autoreload mode"""
        settings = get_settings(filenames={})
        settings['CACHE_DIRECTORY'] = self.temp_cache
        settings['READERS'] = {'asc': None}

        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.readers.read_file = MagicMock()
        generator.generate_context()
        self.assertTrue(hasattr(generator, '_cache_open'))
        orig_call_count = generator.readers.read_file.call_count

        settings['LOAD_CONTENT_CACHE'] = False
        generator = ArticlesGenerator(
            context=settings.copy(), settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.readers.read_file = MagicMock()
        generator.generate_context()
        generator.readers.read_file.assert_called_count == orig_call_count
Beispiel #45
0
 def gen_article_and_html_from_rst(self, rstPath):
     content, metadata = self.reader.read(rstPath)
     article = Article(content=content, metadata=metadata)
     context = self.settings.copy()
     context['generated_content'] = {}
     context['static_links'] = set()
     context['static_content'] = {}
     context['localsiteurl'] = self.settings['SITEURL']
     generator = ArticlesGenerator(context=context,
                                   settings=self.settings,
                                   path=CONTENT_DIR,
                                   theme=self.settings['THEME'],
                                   output_path=OUTPUT_DIR)
     generator.generate_context()
     f = lambda a: True if (a.slug == article.slug) else False
     result = list(filter(f, generator.context["articles"]))[0]
     self.writer.write_file(result.save_as,
                            generator.get_template('article'),
                            generator.context,
                            article=result)
     soup = BeautifulSoup(
         open("./" + self.writer.output_path + '/' + result.save_as),
         "html.parser")
     return (result, soup)
Beispiel #46
0
class TestArticlesGenerator(unittest.TestCase):

    def setUp(self):
        super(TestArticlesGenerator, self).setUp()
        self.generator = None

    def get_populated_generator(self):
        """
        We only need to pull all the test articles once, but read from it
         for each test.
        """
        if self.generator is None:
            settings = _DEFAULT_CONFIG.copy()
            settings['ARTICLE_DIR'] = 'content'
            settings['DEFAULT_CATEGORY'] = 'Default'
            self.generator = ArticlesGenerator(settings.copy(), settings,
                                CUR_DIR, _DEFAULT_CONFIG['THEME'], None,
                                _DEFAULT_CONFIG['MARKUP'])
            self.generator.generate_context()
        return self.generator

    def distill_articles(self, articles):
        distilled = []
        for page in articles:
           distilled.append([
                    page.title,
                    page.status,
                    page.category.name,
                    page.template
                ]
           )
        return distilled

    def test_generate_feeds(self):

        generator = ArticlesGenerator(None, {'FEED_ATOM': _DEFAULT_CONFIG['FEED_ATOM']},
                                      None, _DEFAULT_CONFIG['THEME'], None,
                                      _DEFAULT_CONFIG['MARKUP'])
        writer = MagicMock()
        generator.generate_feeds(writer)
        writer.write_feed.assert_called_with([], None, 'feeds/all.atom.xml')

        generator = ArticlesGenerator(None, {'FEED_ATOM': None}, None,
                                      _DEFAULT_CONFIG['THEME'], None, None)
        writer = MagicMock()
        generator.generate_feeds(writer)
        self.assertFalse(writer.write_feed.called)

    def test_generate_context(self):

        settings = _DEFAULT_CONFIG.copy()
        settings['ARTICLE_DIR'] = 'content'
        settings['DEFAULT_CATEGORY'] = 'Default'
        generator = ArticlesGenerator(settings.copy(), settings, CUR_DIR,
                                      _DEFAULT_CONFIG['THEME'], None,
                                      _DEFAULT_CONFIG['MARKUP'])
        generator.generate_context()
        for article in generator.articles:
            relfilepath = os.path.relpath(article.filename, CUR_DIR)
            if relfilepath == os.path.join("TestCategory",
                                           "article_with_category.rst"):
                self.assertEquals(article.category.name, 'yeah')
            elif relfilepath == os.path.join("TestCategory",
                                             "article_without_category.rst"):
                self.assertEquals(article.category.name, 'TestCategory')
            elif relfilepath == "article_without_category.rst":
                self.assertEquals(article.category.name, 'Default')

        categories = [cat.name for cat, _ in generator.categories]
        # assert that the categories are ordered as expected
        self.assertEquals(
                categories, ['Default', 'TestCategory', 'Yeah', 'test',
                             'yeah'])

    def test_direct_templates_save_as_default(self):

        settings = _DEFAULT_CONFIG.copy()
        settings['DIRECT_TEMPLATES'] = ['archives']
        generator = ArticlesGenerator(settings.copy(), settings, None,
                                      _DEFAULT_CONFIG['THEME'], None,
                                      _DEFAULT_CONFIG['MARKUP'])
        write = MagicMock()
        generator.generate_direct_templates(write)
        write.assert_called_with("archives.html",
            generator.get_template("archives"), settings,
            blog=True, paginated={}, page_name='archives')

    def test_direct_templates_save_as_modified(self):

        settings = _DEFAULT_CONFIG.copy()
        settings['DIRECT_TEMPLATES'] = ['archives']
        settings['ARCHIVES_SAVE_AS'] = 'archives/index.html'
        generator = ArticlesGenerator(settings, settings, None,
                                      _DEFAULT_CONFIG['THEME'], None,
                                      _DEFAULT_CONFIG['MARKUP'])
        write = MagicMock()
        generator.generate_direct_templates(write)
        write.assert_called_with("archives/index.html",
            generator.get_template("archives"), settings,
            blog=True, paginated={}, page_name='archives')

    def test_direct_templates_save_as_false(self):

        settings = _DEFAULT_CONFIG.copy()
        settings['DIRECT_TEMPLATES'] = ['archives']
        settings['ARCHIVES_SAVE_AS'] = 'archives/index.html'
        generator = ArticlesGenerator(settings, settings, None,
                                      _DEFAULT_CONFIG['THEME'], None,
                                      _DEFAULT_CONFIG['MARKUP'])
        write = MagicMock()
        generator.generate_direct_templates(write)
        write.assert_called_count == 0

    def test_per_article_template(self):
        """
        Custom template articles get the field but standard/unset are None
        """
        generator = self.get_populated_generator()
        articles = self.distill_articles(generator.articles)
        custom_template = ['Article with template', 'published', 'Default', 'custom']
        standard_template = ['This is a super article !', 'published', 'Yeah', 'article']
        self.assertIn(custom_template, articles)
        self.assertIn(standard_template, articles)
Beispiel #47
0
class TestArticlesGenerator(unittest.TestCase):

    def setUp(self):
        super(TestArticlesGenerator, self).setUp()
        self.generator = None

    def get_populated_generator(self):
        """
        We only need to pull all the test articles once, but read from it
        for each test.
        """
        if self.generator is None:
            settings = get_settings()
            settings['ARTICLE_DIR'] = 'content'
            settings['DEFAULT_CATEGORY'] = 'Default'
            settings['DEFAULT_DATE'] = (1970, 1, 1)
            self.generator = ArticlesGenerator(settings.copy(), settings,
                                CUR_DIR, settings['THEME'], None,
                                settings['MARKUP'])
            self.generator.generate_context()
        return self.generator

    def distill_articles(self, articles):
        distilled = []
        for page in articles:
            distilled.append([
                    page.title,
                    page.status,
                    page.category.name,
                    page.template
                ]
           )
        return distilled

    def test_generate_feeds(self):
        settings = get_settings()
        generator = ArticlesGenerator(settings,
                {'FEED_ALL_ATOM': settings['FEED_ALL_ATOM']}, None,
                settings['THEME'], None, settings['MARKUP'])
        writer = MagicMock()
        generator.generate_feeds(writer)
        writer.write_feed.assert_called_with([], settings,
                                             'feeds/all.atom.xml')

        generator = ArticlesGenerator(settings, {'FEED_ALL_ATOM': None}, None,
                                      settings['THEME'], None, None)
        writer = MagicMock()
        generator.generate_feeds(writer)
        self.assertFalse(writer.write_feed.called)

    def test_generate_context(self):

        generator = self.get_populated_generator()
        articles = self.distill_articles(generator.articles)
        articles_expected = [
            ['Article title', 'published', 'Default', 'article'],
            ['Article with markdown and summary metadata single', 'published',
             'Default', 'article'],
            ['Article with markdown and summary metadata multi', 'published',
             'Default', 'article'],
            ['Article with template', 'published', 'Default', 'custom'],
            ['Test md File', 'published', 'test', 'article'],
            ['Rst with filename metadata', 'published', 'yeah', 'article'],
            ['Test Markdown extensions', 'published', 'Default', 'article'],
            ['This is a super article !', 'published', 'Yeah', 'article'],
            ['This is an article with category !', 'published', 'yeah',
              'article'],
            ['This is an article without category !', 'published', 'Default',
             'article'],
            ['This is an article without category !', 'published',
             'TestCategory', 'article'],
            ['This is a super article !', 'published', 'yeah', 'article'],
            ['マックOS X 10.8でパイソンとVirtualenvをインストールと設定',
             'published', '指導書', 'article'],
            ['Article with markdown containing footnotes', 'published',
             'Default', 'article']
        ]
        self.assertEqual(sorted(articles_expected), sorted(articles))

    def test_generate_categories(self):

        generator = self.get_populated_generator()
        # test for name
        # categories are grouped by slug; if two categories have the same slug
        # but different names they will be grouped together, the first one in
        # terms of process order will define the name for that category
        categories = [cat.name for cat, _ in generator.categories]
        categories_alternatives = (
            sorted(['Default', 'TestCategory', 'Yeah', 'test', '指導書']),
            sorted(['Default', 'TestCategory', 'yeah', 'test', '指導書']),
        )
        self.assertTrue(sorted(categories) in categories_alternatives)
        # test for slug
        categories = [cat.slug for cat, _ in generator.categories]
        categories_expected = ['default', 'testcategory', 'yeah', 'test',
                               'zhi-dao-shu']
        self.assertEqual(sorted(categories), sorted(categories_expected))

    def test_do_not_use_folder_as_category(self):

        settings = _DEFAULT_CONFIG.copy()
        settings['ARTICLE_DIR'] = 'content'
        settings['DEFAULT_CATEGORY'] = 'Default'
        settings['DEFAULT_DATE'] = (1970, 1, 1)
        settings['USE_FOLDER_AS_CATEGORY'] = False
        settings['filenames'] = {}
        generator = ArticlesGenerator(settings.copy(), settings,
                            CUR_DIR, _DEFAULT_CONFIG['THEME'], None,
                            _DEFAULT_CONFIG['MARKUP'])
        generator.generate_context()
        # test for name
        # categories are grouped by slug; if two categories have the same slug
        # but different names they will be grouped together, the first one in
        # terms of process order will define the name for that category
        categories = [cat.name for cat, _ in generator.categories]
        categories_alternatives = (
            sorted(['Default', 'Yeah', 'test', '指導書']),
            sorted(['Default', 'yeah', 'test', '指導書']),
        )
        self.assertTrue(sorted(categories) in categories_alternatives)
        # test for slug
        categories = [cat.slug for cat, _ in generator.categories]
        categories_expected = ['default', 'yeah', 'test', 'zhi-dao-shu']
        self.assertEqual(sorted(categories), sorted(categories_expected))

    def test_direct_templates_save_as_default(self):

        settings = get_settings()
        generator = ArticlesGenerator(settings, settings, None,
                                      settings['THEME'], None,
                                      settings['MARKUP'])
        write = MagicMock()
        generator.generate_direct_templates(write)
        write.assert_called_with("archives.html",
            generator.get_template("archives"), settings,
            blog=True, paginated={}, page_name='archives')

    def test_direct_templates_save_as_modified(self):

        settings = get_settings()
        settings['DIRECT_TEMPLATES'] = ['archives']
        settings['ARCHIVES_SAVE_AS'] = 'archives/index.html'
        generator = ArticlesGenerator(settings, settings, None,
                                      settings['THEME'], None,
                                      settings['MARKUP'])
        write = MagicMock()
        generator.generate_direct_templates(write)
        write.assert_called_with("archives/index.html",
            generator.get_template("archives"), settings,
            blog=True, paginated={}, page_name='archives/index')

    def test_direct_templates_save_as_false(self):

        settings = get_settings()
        settings['DIRECT_TEMPLATES'] = ['archives']
        settings['ARCHIVES_SAVE_AS'] = 'archives/index.html'
        generator = ArticlesGenerator(settings, settings, None,
                                      settings['THEME'], None,
                                      settings['MARKUP'])
        write = MagicMock()
        generator.generate_direct_templates(write)
        write.assert_called_count == 0

    def test_per_article_template(self):
        """
        Custom template articles get the field but standard/unset are None
        """
        generator = self.get_populated_generator()
        articles = self.distill_articles(generator.articles)
        custom_template = ['Article with template', 'published', 'Default',
                           'custom']
        standard_template = ['This is a super article !', 'published', 'Yeah',
                             'article']
        self.assertIn(custom_template, articles)
        self.assertIn(standard_template, articles)
Beispiel #48
0
class TestArticlesGenerator(unittest.TestCase):
    def setUp(self):
        super(TestArticlesGenerator, self).setUp()
        self.generator = None

    def get_populated_generator(self):
        """
        We only need to pull all the test articles once, but read from it
        for each test.
        """
        if self.generator is None:
            settings = get_settings(filenames={})
            settings['ARTICLE_DIR'] = 'content'
            settings['DEFAULT_CATEGORY'] = 'Default'
            settings['DEFAULT_DATE'] = (1970, 1, 1)
            self.generator = ArticlesGenerator(settings.copy(), settings,
                                               CUR_DIR, settings['THEME'],
                                               None, settings['MARKUP'])
            self.generator.generate_context()
        return self.generator

    def distill_articles(self, articles):
        distilled = []
        for page in articles:
            distilled.append(
                [page.title, page.status, page.category.name, page.template])
        return distilled

    def test_generate_feeds(self):
        settings = get_settings()
        generator = ArticlesGenerator(settings, settings, None,
                                      settings['THEME'], None,
                                      settings['MARKUP'])
        writer = MagicMock()
        generator.generate_feeds(writer)
        writer.write_feed.assert_called_with([], settings,
                                             'feeds/all.atom.xml')

        generator = ArticlesGenerator(settings,
                                      get_settings(FEED_ALL_ATOM=None), None,
                                      settings['THEME'], None, None)
        writer = MagicMock()
        generator.generate_feeds(writer)
        self.assertFalse(writer.write_feed.called)

    def test_generate_context(self):

        generator = self.get_populated_generator()
        articles = self.distill_articles(generator.articles)
        articles_expected = [
            ['Article title', 'published', 'Default', 'article'],
            [
                'Article with markdown and summary metadata single',
                'published', 'Default', 'article'
            ],
            [
                'Article with markdown and summary metadata multi',
                'published', 'Default', 'article'
            ], ['Article with template', 'published', 'Default', 'custom'],
            ['Test md File', 'published', 'test', 'article'],
            ['Rst with filename metadata', 'published', 'yeah', 'article'],
            ['Test Markdown extensions', 'published', 'Default', 'article'],
            ['This is a super article !', 'published', 'Yeah', 'article'],
            [
                'This is an article with category !', 'published', 'yeah',
                'article'
            ],
            [
                'This is an article without category !', 'published',
                'Default', 'article'
            ],
            [
                'This is an article without category !', 'published',
                'TestCategory', 'article'
            ], ['This is a super article !', 'published', 'yeah', 'article'],
            [
                'マックOS X 10.8でパイソンとVirtualenvをインストールと設定', 'published', '指導書',
                'article'
            ],
            [
                'Article with markdown containing footnotes', 'published',
                'Default', 'article'
            ]
        ]
        self.assertEqual(sorted(articles_expected), sorted(articles))

    def test_generate_categories(self):

        generator = self.get_populated_generator()
        # test for name
        # categories are grouped by slug; if two categories have the same slug
        # but different names they will be grouped together, the first one in
        # terms of process order will define the name for that category
        categories = [cat.name for cat, _ in generator.categories]
        categories_alternatives = (
            sorted(['Default', 'TestCategory', 'Yeah', 'test', '指導書']),
            sorted(['Default', 'TestCategory', 'yeah', 'test', '指導書']),
        )
        self.assertTrue(sorted(categories) in categories_alternatives)
        # test for slug
        categories = [cat.slug for cat, _ in generator.categories]
        categories_expected = [
            'default', 'testcategory', 'yeah', 'test', 'zhi-dao-shu'
        ]
        self.assertEqual(sorted(categories), sorted(categories_expected))

    def test_do_not_use_folder_as_category(self):

        settings = DEFAULT_CONFIG.copy()
        settings['ARTICLE_DIR'] = 'content'
        settings['DEFAULT_CATEGORY'] = 'Default'
        settings['DEFAULT_DATE'] = (1970, 1, 1)
        settings['USE_FOLDER_AS_CATEGORY'] = False
        settings['filenames'] = {}
        generator = ArticlesGenerator(settings.copy(), settings, CUR_DIR,
                                      DEFAULT_CONFIG['THEME'], None,
                                      DEFAULT_CONFIG['MARKUP'])
        generator.generate_context()
        # test for name
        # categories are grouped by slug; if two categories have the same slug
        # but different names they will be grouped together, the first one in
        # terms of process order will define the name for that category
        categories = [cat.name for cat, _ in generator.categories]
        categories_alternatives = (
            sorted(['Default', 'Yeah', 'test', '指導書']),
            sorted(['Default', 'yeah', 'test', '指導書']),
        )
        self.assertTrue(sorted(categories) in categories_alternatives)
        # test for slug
        categories = [cat.slug for cat, _ in generator.categories]
        categories_expected = ['default', 'yeah', 'test', 'zhi-dao-shu']
        self.assertEqual(sorted(categories), sorted(categories_expected))

    def test_direct_templates_save_as_default(self):

        settings = get_settings(filenames={})
        generator = ArticlesGenerator(settings, settings, None,
                                      settings['THEME'], None,
                                      settings['MARKUP'])
        write = MagicMock()
        generator.generate_direct_templates(write)
        write.assert_called_with("archives.html",
                                 generator.get_template("archives"),
                                 settings,
                                 blog=True,
                                 paginated={},
                                 page_name='archives')

    def test_direct_templates_save_as_modified(self):

        settings = get_settings()
        settings['DIRECT_TEMPLATES'] = ['archives']
        settings['ARCHIVES_SAVE_AS'] = 'archives/index.html'
        generator = ArticlesGenerator(settings, settings, None,
                                      settings['THEME'], None,
                                      settings['MARKUP'])
        write = MagicMock()
        generator.generate_direct_templates(write)
        write.assert_called_with("archives/index.html",
                                 generator.get_template("archives"),
                                 settings,
                                 blog=True,
                                 paginated={},
                                 page_name='archives/index')

    def test_direct_templates_save_as_false(self):

        settings = get_settings()
        settings['DIRECT_TEMPLATES'] = ['archives']
        settings['ARCHIVES_SAVE_AS'] = 'archives/index.html'
        generator = ArticlesGenerator(settings, settings, None,
                                      settings['THEME'], None,
                                      settings['MARKUP'])
        write = MagicMock()
        generator.generate_direct_templates(write)
        write.assert_called_count == 0

    def test_per_article_template(self):
        """
        Custom template articles get the field but standard/unset are None
        """
        generator = self.get_populated_generator()
        articles = self.distill_articles(generator.articles)
        custom_template = [
            'Article with template', 'published', 'Default', 'custom'
        ]
        standard_template = [
            'This is a super article !', 'published', 'Yeah', 'article'
        ]
        self.assertIn(custom_template, articles)
        self.assertIn(standard_template, articles)
Beispiel #49
0
class TestArticlesGenerator(unittest.TestCase):
    def setUp(self):
        super(TestArticlesGenerator, self).setUp()
        self.generator = None

    def get_populated_generator(self):
        """
        We only need to pull all the test articles once, but read from it
        for each test.
        """
        if self.generator is None:
            settings = get_settings()
            settings["ARTICLE_DIR"] = "content"
            settings["DEFAULT_CATEGORY"] = "Default"
            settings["DEFAULT_DATE"] = (1970, 1, 1)
            self.generator = ArticlesGenerator(
                settings.copy(), settings, CUR_DIR, settings["THEME"], None, settings["MARKUP"]
            )
            self.generator.generate_context()
        return self.generator

    def distill_articles(self, articles):
        distilled = []
        for page in articles:
            distilled.append([page.title, page.status, page.category.name, page.template])
        return distilled

    def test_generate_feeds(self):
        settings = get_settings()
        generator = ArticlesGenerator(
            settings, {"FEED_ALL_ATOM": settings["FEED_ALL_ATOM"]}, None, settings["THEME"], None, settings["MARKUP"]
        )
        writer = MagicMock()
        generator.generate_feeds(writer)
        writer.write_feed.assert_called_with([], settings, "feeds/all.atom.xml")

        generator = ArticlesGenerator(settings, {"FEED_ALL_ATOM": None}, None, settings["THEME"], None, None)
        writer = MagicMock()
        generator.generate_feeds(writer)
        self.assertFalse(writer.write_feed.called)

    def test_generate_context(self):

        generator = self.get_populated_generator()
        articles = self.distill_articles(generator.articles)
        articles_expected = [
            ["Article title", "published", "Default", "article"],
            ["Article with markdown and summary metadata single", "published", "Default", "article"],
            ["Article with markdown and summary metadata multi", "published", "Default", "article"],
            ["Article with template", "published", "Default", "custom"],
            ["Test md File", "published", "test", "article"],
            ["Rst with filename metadata", "published", "yeah", "article"],
            ["Test Markdown extensions", "published", "Default", "article"],
            ["This is a super article !", "published", "Yeah", "article"],
            ["This is an article with category !", "published", "yeah", "article"],
            ["This is an article without category !", "published", "Default", "article"],
            ["This is an article without category !", "published", "TestCategory", "article"],
            ["This is a super article !", "published", "yeah", "article"],
            ["マックOS X 10.8でパイソンとVirtualenvをインストールと設定", "published", "指導書", "article"],
        ]
        self.assertItemsEqual(articles_expected, articles)

    def test_generate_categories(self):

        generator = self.get_populated_generator()
        categories = [cat.name for cat, _ in generator.categories]
        categories_expected = ["Default", "TestCategory", "Yeah", "test", "yeah", "指導書"]
        self.assertEquals(categories, categories_expected)

    def test_do_not_use_folder_as_category(self):

        settings = _DEFAULT_CONFIG.copy()
        settings["ARTICLE_DIR"] = "content"
        settings["DEFAULT_CATEGORY"] = "Default"
        settings["DEFAULT_DATE"] = (1970, 1, 1)
        settings["USE_FOLDER_AS_CATEGORY"] = False
        settings["filenames"] = {}
        generator = ArticlesGenerator(
            settings.copy(), settings, CUR_DIR, _DEFAULT_CONFIG["THEME"], None, _DEFAULT_CONFIG["MARKUP"]
        )
        generator.generate_context()

        categories = [cat.name for cat, _ in generator.categories]
        self.assertEquals(categories, ["Default", "Yeah", "test", "yeah", "指導書"])

    def test_direct_templates_save_as_default(self):

        settings = get_settings()
        generator = ArticlesGenerator(settings, settings, None, settings["THEME"], None, settings["MARKUP"])
        write = MagicMock()
        generator.generate_direct_templates(write)
        write.assert_called_with(
            "archives.html", generator.get_template("archives"), settings, blog=True, paginated={}, page_name="archives"
        )

    def test_direct_templates_save_as_modified(self):

        settings = get_settings()
        settings["DIRECT_TEMPLATES"] = ["archives"]
        settings["ARCHIVES_SAVE_AS"] = "archives/index.html"
        generator = ArticlesGenerator(settings, settings, None, settings["THEME"], None, settings["MARKUP"])
        write = MagicMock()
        generator.generate_direct_templates(write)
        write.assert_called_with(
            "archives/index.html",
            generator.get_template("archives"),
            settings,
            blog=True,
            paginated={},
            page_name="archives/index",
        )

    def test_direct_templates_save_as_false(self):

        settings = get_settings()
        settings["DIRECT_TEMPLATES"] = ["archives"]
        settings["ARCHIVES_SAVE_AS"] = "archives/index.html"
        generator = ArticlesGenerator(settings, settings, None, settings["THEME"], None, settings["MARKUP"])
        write = MagicMock()
        generator.generate_direct_templates(write)
        write.assert_called_count == 0

    def test_per_article_template(self):
        """
        Custom template articles get the field but standard/unset are None
        """
        generator = self.get_populated_generator()
        articles = self.distill_articles(generator.articles)
        custom_template = ["Article with template", "published", "Default", "custom"]
        standard_template = ["This is a super article !", "published", "Yeah", "article"]
        self.assertIn(custom_template, articles)
        self.assertIn(standard_template, articles)
Beispiel #50
0
    def test_period_in_timeperiod_archive(self):
        """
        Test that the context of a generated period_archive is passed
        'period' : a tuple of year, month, day according to the time period
        """
        old_locale = locale.setlocale(locale.LC_ALL)
        locale.setlocale(locale.LC_ALL, str('C'))
        settings = get_settings()

        settings['YEAR_ARCHIVE_SAVE_AS'] = 'posts/{date:%Y}/index.html'
        settings['YEAR_ARCHIVE_URL'] = 'posts/{date:%Y}/'
        settings['CACHE_PATH'] = self.temp_cache
        context = get_context(settings)
        generator = ArticlesGenerator(
            context=context, settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        write = MagicMock()
        generator.generate_period_archives(write)
        dates = [d for d in generator.dates if d.date.year == 1970]
        articles = [d for d in generator.articles if d.date.year == 1970]
        self.assertEqual(len(dates), 1)
        # among other things it must have at least been called with this
        context["period"] = (1970,)
        write.assert_called_with("posts/1970/index.html",
                                 generator.get_template("period_archives"),
                                 context, blog=True, articles=articles,
                                 dates=dates, template_name='period_archives',
                                 url="posts/1970/",
                                 all_articles=generator.articles)

        settings['MONTH_ARCHIVE_SAVE_AS'] = \
            'posts/{date:%Y}/{date:%b}/index.html'
        settings['MONTH_ARCHIVE_URL'] = \
            'posts/{date:%Y}/{date:%b}/'
        context = get_context(settings)
        generator = ArticlesGenerator(
            context=context, settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        write = MagicMock()
        generator.generate_period_archives(write)
        dates = [d for d in generator.dates
                 if d.date.year == 1970 and d.date.month == 1]
        articles = [d for d in generator.articles
                    if d.date.year == 1970 and d.date.month == 1]
        self.assertEqual(len(dates), 1)
        context["period"] = (1970, "January")
        # among other things it must have at least been called with this
        write.assert_called_with("posts/1970/Jan/index.html",
                                 generator.get_template("period_archives"),
                                 context, blog=True, articles=articles,
                                 dates=dates, template_name='period_archives',
                                 url="posts/1970/Jan/",
                                 all_articles=generator.articles)

        settings['DAY_ARCHIVE_SAVE_AS'] = \
            'posts/{date:%Y}/{date:%b}/{date:%d}/index.html'
        settings['DAY_ARCHIVE_URL'] = \
            'posts/{date:%Y}/{date:%b}/{date:%d}/'
        context = get_context(settings)
        generator = ArticlesGenerator(
            context=context, settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        write = MagicMock()
        generator.generate_period_archives(write)
        dates = [
            d for d in generator.dates if
            d.date.year == 1970 and
            d.date.month == 1 and
            d.date.day == 1
        ]
        articles = [
            d for d in generator.articles if
            d.date.year == 1970 and
            d.date.month == 1 and
            d.date.day == 1
        ]
        self.assertEqual(len(dates), 1)
        context["period"] = (1970, "January", 1)
        # among other things it must have at least been called with this
        write.assert_called_with("posts/1970/Jan/01/index.html",
                                 generator.get_template("period_archives"),
                                 context, blog=True, articles=articles,
                                 dates=dates, template_name='period_archives',
                                 url="posts/1970/Jan/01/",
                                 all_articles=generator.articles)
        locale.setlocale(locale.LC_ALL, old_locale)
Beispiel #51
0
class TestArticlesGenerator(unittest.TestCase):

    def setUp(self):
        super(TestArticlesGenerator, self).setUp()
        self.generator = None

    def get_populated_generator(self):
        """
        We only need to pull all the test articles once, but read from it
         for each test.
        """
        if self.generator is None:
            settings = _DEFAULT_CONFIG.copy()
            settings['ARTICLE_DIR'] = 'content'
            settings['DEFAULT_CATEGORY'] = 'Default'
            settings['DEFAULT_DATE'] = (1970, 01, 01)
            self.generator = ArticlesGenerator(settings.copy(), settings,
                                CUR_DIR, _DEFAULT_CONFIG['THEME'], None,
                                _DEFAULT_CONFIG['MARKUP'])
            self.generator.generate_context()
        return self.generator

    def distill_articles(self, articles):
        distilled = []
        for page in articles:
           distilled.append([
                    page.title,
                    page.status,
                    page.category.name,
                    page.template
                ]
           )
        return distilled

    def test_generate_feeds(self):

        generator = ArticlesGenerator(None, {'FEED_ALL_ATOM': _DEFAULT_CONFIG['FEED_ALL_ATOM']},
                                      None, _DEFAULT_CONFIG['THEME'], None,
                                      _DEFAULT_CONFIG['MARKUP'])
        writer = MagicMock()
        generator.generate_feeds(writer)
        writer.write_feed.assert_called_with([], None, 'feeds/all.atom.xml')

        generator = ArticlesGenerator(None, {'FEED_ALL_ATOM': None}, None,
                                      _DEFAULT_CONFIG['THEME'], None, None)
        writer = MagicMock()
        generator.generate_feeds(writer)
        self.assertFalse(writer.write_feed.called)

    def test_generate_context(self):

        generator = self.get_populated_generator()
        articles = self.distill_articles(generator.articles)
        articles_expected = [
            [u'Article title', 'published', 'Default', 'article'],
            [u'Article with template', 'published', 'Default', 'custom'],
            [u'Test md File', 'published', 'test', 'article'],
            [u'Test Markdown extensions', 'published', u'Default', 'article'],
            [u'This is a super article !', 'published', 'Yeah', 'article'],
            [u'This is an article with category !', 'published', 'yeah', 'article'],
            [u'This is an article without category !', 'published', 'Default', 'article'],
            [u'This is an article without category !', 'published', 'TestCategory', 'article'],
            [u'This is a super article !', 'published', 'yeah', 'article']
        ]
        self.assertItemsEqual(articles_expected, articles)

    def test_generate_categories(self):

        generator = self.get_populated_generator()
        categories = [cat.name for cat, _ in generator.categories]
        categories_expected = ['Default', 'TestCategory', 'Yeah', 'test', 'yeah']
        self.assertEquals(categories, categories_expected)

    def test_do_not_use_folder_as_category(self):

        settings = _DEFAULT_CONFIG.copy()
        settings['ARTICLE_DIR'] = 'content'
        settings['DEFAULT_CATEGORY'] = 'Default'
        settings['DEFAULT_DATE'] = (1970, 01, 01)
        settings['USE_FOLDER_AS_CATEGORY'] = False
        generator = ArticlesGenerator(settings.copy(), settings,
                            CUR_DIR, _DEFAULT_CONFIG['THEME'], None,
                            _DEFAULT_CONFIG['MARKUP'])
        generator.generate_context()

        categories = [cat.name for cat, _ in generator.categories]
        self.assertEquals(categories, ['Default', 'Yeah', 'test', 'yeah'])

    def test_direct_templates_save_as_default(self):

        settings = _DEFAULT_CONFIG.copy()
        settings['DIRECT_TEMPLATES'] = ['archives']
        generator = ArticlesGenerator(settings.copy(), settings, None,
                                      _DEFAULT_CONFIG['THEME'], None,
                                      _DEFAULT_CONFIG['MARKUP'])
        write = MagicMock()
        generator.generate_direct_templates(write)
        write.assert_called_with("archives.html",
            generator.get_template("archives"), settings,
            blog=True, paginated={}, page_name='archives')

    def test_direct_templates_save_as_modified(self):

        settings = _DEFAULT_CONFIG.copy()
        settings['DIRECT_TEMPLATES'] = ['archives']
        settings['ARCHIVES_SAVE_AS'] = 'archives/index.html'
        generator = ArticlesGenerator(settings, settings, None,
                                      _DEFAULT_CONFIG['THEME'], None,
                                      _DEFAULT_CONFIG['MARKUP'])
        write = MagicMock()
        generator.generate_direct_templates(write)
        write.assert_called_with("archives/index.html",
            generator.get_template("archives"), settings,
            blog=True, paginated={}, page_name='archives')

    def test_direct_templates_save_as_false(self):

        settings = _DEFAULT_CONFIG.copy()
        settings['DIRECT_TEMPLATES'] = ['archives']
        settings['ARCHIVES_SAVE_AS'] = 'archives/index.html'
        generator = ArticlesGenerator(settings, settings, None,
                                      _DEFAULT_CONFIG['THEME'], None,
                                      _DEFAULT_CONFIG['MARKUP'])
        write = MagicMock()
        generator.generate_direct_templates(write)
        write.assert_called_count == 0

    def test_per_article_template(self):
        """
        Custom template articles get the field but standard/unset are None
        """
        generator = self.get_populated_generator()
        articles = self.distill_articles(generator.articles)
        custom_template = ['Article with template', 'published', 'Default', 'custom']
        standard_template = ['This is a super article !', 'published', 'Yeah', 'article']
        self.assertIn(custom_template, articles)
        self.assertIn(standard_template, articles)
    def test_period_in_timeperiod_archive(self):
        """
        Test that the context of a generated period_archive is passed
        'period' : a tuple of year, month, day according to the time period
        """
        old_locale = locale.setlocale(locale.LC_ALL)
        locale.setlocale(locale.LC_ALL, str('C'))
        settings = get_settings()

        settings['YEAR_ARCHIVE_SAVE_AS'] = 'posts/{date:%Y}/index.html'
        settings['YEAR_ARCHIVE_URL'] = 'posts/{date:%Y}/'
        settings['CACHE_PATH'] = self.temp_cache
        context = get_context(settings)
        generator = ArticlesGenerator(
            context=context, settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        write = MagicMock()
        generator.generate_period_archives(write)
        dates = [d for d in generator.dates if d.date.year == 1970]
        articles = [d for d in generator.articles if d.date.year == 1970]
        self.assertEqual(len(dates), 1)
        # among other things it must have at least been called with this
        context["period"] = (1970,)
        write.assert_called_with("posts/1970/index.html",
                                 generator.get_template("period_archives"),
                                 context, blog=True, articles=articles,
                                 dates=dates, template_name='period_archives',
                                 url="posts/1970/")

        settings['MONTH_ARCHIVE_SAVE_AS'] = \
            'posts/{date:%Y}/{date:%b}/index.html'
        settings['MONTH_ARCHIVE_URL'] = \
            'posts/{date:%Y}/{date:%b}/'
        context = get_context(settings)
        generator = ArticlesGenerator(
            context=context, settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        write = MagicMock()
        generator.generate_period_archives(write)
        dates = [d for d in generator.dates
                 if d.date.year == 1970 and d.date.month == 1]
        articles = [d for d in generator.articles
                    if d.date.year == 1970 and d.date.month == 1]
        self.assertEqual(len(dates), 1)
        context["period"] = (1970, "January")
        # among other things it must have at least been called with this
        write.assert_called_with("posts/1970/Jan/index.html",
                                 generator.get_template("period_archives"),
                                 context, blog=True, articles=articles,
                                 dates=dates, template_name='period_archives',
                                 url="posts/1970/Jan/")

        settings['DAY_ARCHIVE_SAVE_AS'] = \
            'posts/{date:%Y}/{date:%b}/{date:%d}/index.html'
        settings['DAY_ARCHIVE_URL'] = \
            'posts/{date:%Y}/{date:%b}/{date:%d}/'
        context = get_context(settings)
        generator = ArticlesGenerator(
            context=context, settings=settings,
            path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
        generator.generate_context()
        write = MagicMock()
        generator.generate_period_archives(write)
        dates = [
            d for d in generator.dates if
            d.date.year == 1970 and
            d.date.month == 1 and
            d.date.day == 1
        ]
        articles = [
            d for d in generator.articles if
            d.date.year == 1970 and
            d.date.month == 1 and
            d.date.day == 1
        ]
        self.assertEqual(len(dates), 1)
        context["period"] = (1970, "January", 1)
        # among other things it must have at least been called with this
        write.assert_called_with("posts/1970/Jan/01/index.html",
                                 generator.get_template("period_archives"),
                                 context, blog=True, articles=articles,
                                 dates=dates, template_name='period_archives',
                                 url="posts/1970/Jan/01/")
        locale.setlocale(locale.LC_ALL, old_locale)
Beispiel #53
0
class TestArticlesGenerator(unittest.TestCase):
    def setUp(self):
        super(TestArticlesGenerator, self).setUp()
        self.generator = None

    def get_populated_generator(self):
        """
        We only need to pull all the test articles once, but read from it
         for each test.
        """
        if self.generator is None:
            settings = get_settings()
            settings['ARTICLE_DIR'] = 'content'
            settings['DEFAULT_CATEGORY'] = 'Default'
            settings['DEFAULT_DATE'] = (1970, 1, 1)
            self.generator = ArticlesGenerator(settings.copy(), settings,
                                               CUR_DIR, settings['THEME'],
                                               None, settings['MARKUP'])
            self.generator.generate_context()
        return self.generator

    def distill_articles(self, articles):
        distilled = []
        for page in articles:
            distilled.append(
                [page.title, page.status, page.category.name, page.template])
        return distilled

    def test_generate_feeds(self):
        settings = get_settings()
        generator = ArticlesGenerator(
            settings, {'FEED_ALL_ATOM': settings['FEED_ALL_ATOM']}, None,
            settings['THEME'], None, settings['MARKUP'])
        writer = MagicMock()
        generator.generate_feeds(writer)
        writer.write_feed.assert_called_with([], settings,
                                             'feeds/all.atom.xml')

        generator = ArticlesGenerator(settings, {'FEED_ALL_ATOM': None}, None,
                                      settings['THEME'], None, None)
        writer = MagicMock()
        generator.generate_feeds(writer)
        self.assertFalse(writer.write_feed.called)

    def test_generate_context(self):

        generator = self.get_populated_generator()
        articles = self.distill_articles(generator.articles)
        articles_expected = [
            ['Article title', 'published', 'Default', 'article'],
            [
                'Article with markdown and summary metadata single',
                'published', 'Default', 'article'
            ],
            [
                'Article with markdown and summary metadata multi',
                'published', 'Default', 'article'
            ], ['Article with template', 'published', 'Default', 'custom'],
            ['Test md File', 'published', 'test', 'article'],
            ['Rst with filename metadata', 'published', 'yeah', 'article'],
            ['Test Markdown extensions', 'published', 'Default', 'article'],
            ['This is a super article !', 'published', 'Yeah', 'article'],
            [
                'This is an article with category !', 'published', 'yeah',
                'article'
            ],
            [
                'This is an article without category !', 'published',
                'Default', 'article'
            ],
            [
                'This is an article without category !', 'published',
                'TestCategory', 'article'
            ], ['This is a super article !', 'published', 'yeah', 'article']
        ]
        self.assertItemsEqual(articles_expected, articles)

    def test_generate_categories(self):

        generator = self.get_populated_generator()
        categories = [cat.name for cat, _ in generator.categories]
        categories_expected = [
            'Default', 'TestCategory', 'Yeah', 'test', 'yeah'
        ]
        self.assertEquals(categories, categories_expected)

    def test_do_not_use_folder_as_category(self):

        settings = _DEFAULT_CONFIG.copy()
        settings['ARTICLE_DIR'] = 'content'
        settings['DEFAULT_CATEGORY'] = 'Default'
        settings['DEFAULT_DATE'] = (1970, 1, 1)
        settings['USE_FOLDER_AS_CATEGORY'] = False
        settings['filenames'] = {}
        generator = ArticlesGenerator(settings.copy(), settings, CUR_DIR,
                                      _DEFAULT_CONFIG['THEME'], None,
                                      _DEFAULT_CONFIG['MARKUP'])
        generator.generate_context()

        categories = [cat.name for cat, _ in generator.categories]
        self.assertEquals(categories, ['Default', 'Yeah', 'test', 'yeah'])

    def test_direct_templates_save_as_default(self):

        settings = get_settings()
        generator = ArticlesGenerator(settings, settings, None,
                                      settings['THEME'], None,
                                      settings['MARKUP'])
        write = MagicMock()
        generator.generate_direct_templates(write)
        write.assert_called_with("archives.html",
                                 generator.get_template("archives"),
                                 settings,
                                 blog=True,
                                 paginated={},
                                 page_name='archives')

    def test_direct_templates_save_as_modified(self):

        settings = get_settings()
        settings['DIRECT_TEMPLATES'] = ['archives']
        settings['ARCHIVES_SAVE_AS'] = 'archives/index.html'
        generator = ArticlesGenerator(settings, settings, None,
                                      settings['THEME'], None,
                                      settings['MARKUP'])
        write = MagicMock()
        generator.generate_direct_templates(write)
        write.assert_called_with("archives/index.html",
                                 generator.get_template("archives"),
                                 settings,
                                 blog=True,
                                 paginated={},
                                 page_name='archives')

    def test_direct_templates_save_as_false(self):

        settings = get_settings()
        settings['DIRECT_TEMPLATES'] = ['archives']
        settings['ARCHIVES_SAVE_AS'] = 'archives/index.html'
        generator = ArticlesGenerator(settings, settings, None,
                                      settings['THEME'], None,
                                      settings['MARKUP'])
        write = MagicMock()
        generator.generate_direct_templates(write)
        write.assert_called_count == 0

    def test_per_article_template(self):
        """
        Custom template articles get the field but standard/unset are None
        """
        generator = self.get_populated_generator()
        articles = self.distill_articles(generator.articles)
        custom_template = [
            'Article with template', 'published', 'Default', 'custom'
        ]
        standard_template = [
            'This is a super article !', 'published', 'Yeah', 'article'
        ]
        self.assertIn(custom_template, articles)
        self.assertIn(standard_template, articles)