-
Notifications
You must be signed in to change notification settings - Fork 844
/
test_sub_parts.py
138 lines (115 loc) · 4.73 KB
/
test_sub_parts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
from shutil import rmtree
from tempfile import mkdtemp
from pelican.generators import ArticlesGenerator
from pelican.tests.support import unittest, get_settings
import sub_parts
CUR_DIR = os.path.dirname(__file__)
class TestSubParts(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.temp_path = mkdtemp(prefix='pelicantests.')
settings = get_settings(filenames={})
settings['PATH'] = os.path.join(CUR_DIR, 'test_data')
settings['AUTHOR'] = 'Me'
settings['DEFAULT_DATE'] = (1970, 1, 1)
settings['DEFAULT_CATEGORY'] = 'Default'
settings['FILENAME_METADATA'] = '(?P<slug>[^.]+)'
settings['PLUGINS'] = [sub_parts]
settings['CACHE_CONTENT'] = False
context = settings.copy()
context['generated_content'] = dict()
context['static_links'] = set()
cls.generator = ArticlesGenerator(
context=context, settings=settings,
path=settings['PATH'], theme=settings['THEME'], output_path=cls.temp_path)
cls.generator.generate_context()
cls.all_articles = list(cls.generator.articles)
sub_parts.patch_subparts(cls.generator)
@classmethod
def tearDownClass(cls):
rmtree(cls.temp_path)
def test_all_articles(self):
self.assertEqual(
sorted(['noparent', 'parent',
'parent--explicit', 'parent--implicit']),
sorted([a.slug for a in self.all_articles]))
def test_articles(self):
self.assertEqual(
sorted(['noparent', 'parent']),
sorted([a.slug for a in self.generator.articles]))
def test_dates(self):
self.assertEqual(
sorted(['noparent', 'parent']),
sorted([a.slug for a in self.generator.dates]))
def test_categories(self):
self.assertEqual(
sorted(['noparent', 'parent']),
sorted([a.slug for a in self.generator.categories[0][1]]))
def test_tags(self):
self.assertEqual(
sorted([a.slug for a in self.all_articles]),
sorted([a.slug for a in self.generator.tags['atag']]))
def test_authors(self):
self.assertEqual(
sorted([a.slug for a in self.all_articles]),
sorted([a.slug for a in self.generator.authors[0][1]]))
def test_subparts(self):
for a in self.all_articles:
if a.slug == 'parent':
self.assertTrue(hasattr(a, 'subparts'))
self.assertEqual(
sorted(['parent--explicit', 'parent--implicit']),
sorted([a.slug for a in a.subparts]))
else:
self.assertFalse(hasattr(a, 'subparts'))
def test_subpart_of(self):
for a in self.all_articles:
if '--' in a.slug:
self.assertTrue(hasattr(a, 'subpart_of'))
self.assertEqual('parent', a.subpart_of.slug)
else:
self.assertFalse(hasattr(a, 'subpart_of'))
def test_subtitle(self):
for a in self.all_articles:
if '--' in a.slug:
self.assertTrue(hasattr(a, 'subtitle'))
self.assertEqual(a.title,
a.subtitle + ', ' + a.subpart_of.title)
class TestSubPartsPhotos(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.temp_path = mkdtemp(prefix='pelicantests.')
settings = get_settings(filenames={})
settings['PATH'] = os.path.join(CUR_DIR, 'test_data')
settings['AUTHOR'] = 'Me'
settings['DEFAULT_DATE'] = (1970, 1, 1)
settings['DEFAULT_CATEGORY'] = 'Default'
settings['FILENAME_METADATA'] = '(?P<slug>[^.]+)'
settings['PLUGINS'] = [sub_parts]
settings['CACHE_CONTENT'] = False
context = settings.copy()
context['generated_content'] = dict()
context['static_links'] = set()
cls.generator = ArticlesGenerator(
context=context, settings=settings,
path=settings['PATH'], theme=settings['THEME'], output_path=cls.temp_path)
cls.generator.generate_context()
cls.all_articles = list(cls.generator.articles)
for a in cls.all_articles:
a.photo_gallery = [('i.jpg', 'i.jpg', 'it.jpg', '', '')]
sub_parts.patch_subparts(cls.generator)
@classmethod
def tearDownClass(cls):
rmtree(cls.temp_path)
def test_subphotos(self):
for a in self.all_articles:
if a.slug == 'parent':
self.assertTrue(hasattr(a, 'subphotos'))
self.assertEqual(3, a.subphotos)
else:
self.assertFalse(hasattr(a, 'subphotos'))
if __name__ == '__main__':
unittest.main()