/hyde/tests/ext/test_grouper.py
Python | 322 lines | 310 code | 6 blank | 6 comment | 0 complexity | af61a4688ebcc81223415132ddbd41f9 MD5 | raw file
1# -*- coding: utf-8 -*- 2""" 3Use nose 4`$ pip install nose` 5`$ nosetests` 6""" 7from hyde.ext.plugins.meta import GrouperPlugin, MetaPlugin, SorterPlugin 8from hyde.generator import Generator 9from hyde.site import Site 10from hyde.model import Config, Expando 11 12from fswrap import File 13from hyde.tests.util import assert_html_equals 14import yaml 15 16TEST_SITE = File(__file__).parent.parent.child_folder('_test') 17 18 19class TestGrouperSingleLevel(object): 20 21 def setUp(self): 22 TEST_SITE.make() 23 TEST_SITE.parent.child_folder( 24 'sites/test_grouper').copy_contents_to(TEST_SITE) 25 26 self.s = Site(TEST_SITE) 27 cfg = """ 28 nodemeta: meta.yaml 29 plugins: 30 - hyde.ext.plugins.meta.MetaPlugin 31 - hyde.ext.plugins.meta.SorterPlugin 32 - hyde.ext.plugins.meta.GrouperPlugin 33 sorter: 34 kind: 35 attr: 36 - source_file.kind 37 filters: 38 is_processable: True 39 grouper: 40 section: 41 description: Sections in the site 42 sorter: kind 43 groups: 44 - 45 name: start 46 description: Getting Started 47 - 48 name: plugins 49 description: Plugins 50 51 """ 52 self.s.config = Config(TEST_SITE, config_dict=yaml.load(cfg)) 53 self.s.load() 54 MetaPlugin(self.s).begin_site() 55 SorterPlugin(self.s).begin_site() 56 GrouperPlugin(self.s).begin_site() 57 58 self.all = ['installation.html', 'overview.html', 'templating.html', 'plugins.html', 'tags.html'] 59 self.start = ['installation.html', 'overview.html', 'templating.html'] 60 self.plugins = ['plugins.html', 'tags.html'] 61 self.section = self.all 62 63 def tearDown(self): 64 TEST_SITE.delete() 65 66 def test_site_grouper_groups(self): 67 68 groups = dict([(g.name, g) for g in self.s.grouper['section'].groups]) 69 assert len(groups) == 2 70 assert 'start' in groups 71 assert 'plugins' in groups 72 73 def test_site_grouper_walk_groups(self): 74 75 groups = dict([(g.name, g) for g in self.s.grouper['section'].walk_groups()]) 76 assert len(groups) == 3 77 assert 'section' in groups 78 assert 'start' in groups 79 assert 'plugins' in groups 80 81 def test_walk_section_groups(self): 82 83 assert hasattr(self.s.content, 'walk_section_groups') 84 groups = dict([(grouper.group.name, grouper) for grouper in self.s.content.walk_section_groups()]) 85 assert len(groups) == 3 86 assert 'section' in groups 87 assert 'start' in groups 88 assert 'plugins' in groups 89 for name in ['start', 'plugins']: 90 res = [resource.name for resource in groups[name].resources] 91 assert res == getattr(self, name) 92 93 def test_walk_start_groups(self): 94 95 assert hasattr(self.s.content, 'walk_start_groups') 96 groups = dict([(g.name, g) for g, resources in self.s.content.walk_start_groups()]) 97 assert len(groups) == 1 98 assert 'start' in groups 99 100 101 def test_walk_plugins_groups(self): 102 103 assert hasattr(self.s.content, 'walk_plugins_groups') 104 groups = dict([(g.name, g) for g, resources in self.s.content.walk_plugins_groups()]) 105 assert len(groups) == 1 106 assert 'plugins' in groups 107 108 def test_walk_section_resources(self): 109 110 assert hasattr(self.s.content, 'walk_resources_grouped_by_section') 111 112 resources = [resource.name for resource in self.s.content.walk_resources_grouped_by_section()] 113 assert resources == self.all 114 115 116 def test_walk_start_resources(self): 117 118 assert hasattr(self.s.content, 'walk_resources_grouped_by_start') 119 120 start_resources = [resource.name for resource in self.s.content.walk_resources_grouped_by_start()] 121 assert start_resources == self.start 122 123 def test_walk_plugins_resources(self): 124 125 assert hasattr(self.s.content, 'walk_resources_grouped_by_plugins') 126 127 plugin_resources = [resource.name for resource in self.s.content.walk_resources_grouped_by_plugins()] 128 assert plugin_resources == self.plugins 129 130 def test_resource_group(self): 131 132 groups = dict([(g.name, g) for g in self.s.grouper['section'].groups]) 133 134 for name, group in groups.items(): 135 pages = getattr(self, name) 136 for page in pages: 137 res = self.s.content.resource_from_relative_path('blog/' + page) 138 assert hasattr(res, 'section_group') 139 res_group = getattr(res, 'section_group') 140 assert res_group == group 141 142 def test_resource_belongs_to(self): 143 144 groups = dict([(g.name, g) for g in self.s.grouper['section'].groups]) 145 146 for name, group in groups.items(): 147 pages = getattr(self, name) 148 for page in pages: 149 res = self.s.content.resource_from_relative_path('blog/' + page) 150 res_groups = getattr(res, 'walk_%s_groups' % name)() 151 assert group in res_groups 152 153 def test_prev_next(self): 154 155 resources = [] 156 for page in self.all: 157 resources.append(self.s.content.resource_from_relative_path('blog/' + page)) 158 159 index = 0 160 for res in resources: 161 if index < 4: 162 assert res.next_in_section.name == self.all[index + 1] 163 else: 164 assert not res.next_in_section 165 index += 1 166 167 index = 0 168 for res in resources: 169 if index: 170 assert res.prev_in_section.name == self.all[index - 1] 171 else: 172 assert not res.prev_in_section 173 index += 1 174 175 def test_nav_with_grouper(self): 176 177 text =""" 178{% for group, resources in site.content.walk_section_groups() %} 179<ul> 180 <li> 181 <h2>{{ group.name|title }}</h2> 182 <h3>{{ group.description }}</h3> 183 <ul class="links"> 184 {% for resource in resources %} 185 <li>{{resource.name}}</li> 186 {% endfor %} 187 </ul> 188 </li> 189</ul> 190{% endfor %} 191 192""" 193 expected = """ 194<ul> 195 <li> 196 <h2>Section</h2> 197 <h3>Sections in the site</h3> 198 <ul class="links"></ul> 199 </li> 200</ul> 201<ul> 202 <li> 203 <h2>Start</h2> 204 <h3>Getting Started</h3> 205 <ul class="links"> 206 <li>installation.html</li> 207 <li>overview.html</li> 208 <li>templating.html</li> 209 </ul> 210 </li> 211</ul> 212<ul> 213 <li> 214 <h2>Plugins</h2> 215 <h3>Plugins</h3> 216 <ul class="links"> 217 <li>plugins.html</li> 218 <li>tags.html</li> 219 </ul> 220 </li> 221</ul> 222 223""" 224 225 gen = Generator(self.s) 226 gen.load_site_if_needed() 227 gen.load_template_if_needed() 228 out = gen.template.render(text, {'site':self.s}) 229 assert_html_equals(out, expected) 230 231 def test_nav_with_grouper_sorted(self): 232 233 cfg = """ 234 nodemeta: meta.yaml 235 plugins: 236 - hyde.ext.plugins.meta.MetaPlugin 237 - hyde.ext.plugins.meta.SorterPlugin 238 - hyde.ext.plugins.meta.GrouperPlugin 239 sorter: 240 kind: 241 attr: 242 - source_file.kind 243 filters: 244 is_processable: True 245 grouper: 246 section: 247 description: Sections in the site 248 sorter: kind 249 groups: 250 - 251 name: start 252 description: Getting Started 253 - 254 name: awesome 255 description: Awesome 256 - 257 name: plugins 258 description: Plugins 259 260 """ 261 self.s.config = Config(TEST_SITE, config_dict=yaml.load(cfg)) 262 self.s.load() 263 MetaPlugin(self.s).begin_site() 264 SorterPlugin(self.s).begin_site() 265 GrouperPlugin(self.s).begin_site() 266 267 text =""" 268{% set sorted = site.grouper['section'].groups|sort(attribute='name') %} 269{% for group in sorted %} 270<ul> 271 <li> 272 <h2>{{ group.name|title }}</h2> 273 <h3>{{ group.description }}</h3> 274 <ul class="links"> 275 {% for resource in group.walk_resources_in_node(site.content) %} 276 <li>{{resource.name}}</li> 277 {% endfor %} 278 </ul> 279 </li> 280</ul> 281{% endfor %} 282 283""" 284 expected = """ 285<ul> 286 <li> 287 <h2>Awesome</h2> 288 <h3>Awesome</h3> 289 <ul class="links"> 290 </ul> 291 </li> 292</ul> 293<ul> 294 <li> 295 <h2>Plugins</h2> 296 <h3>Plugins</h3> 297 <ul class="links"> 298 <li>plugins.html</li> 299 <li>tags.html</li> 300 </ul> 301 </li> 302</ul> 303<ul> 304 <li> 305 <h2>Start</h2> 306 <h3>Getting Started</h3> 307 <ul class="links"> 308 <li>installation.html</li> 309 <li>overview.html</li> 310 <li>templating.html</li> 311 </ul> 312 </li> 313</ul> 314 315 316""" 317 self.s.config.grouper.section.groups.append(Expando({"name": "awesome", "description": "Aweesoome"})); 318 gen = Generator(self.s) 319 gen.load_site_if_needed() 320 gen.load_template_if_needed() 321 out = gen.template.render(text, {'site':self.s}) 322 assert_html_equals(out, expected)