PageRenderTime 37ms CodeModel.GetById 15ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/hyde/tests/ext/test_tagger.py

http://github.com/hyde/hyde
Python | 226 lines | 181 code | 39 blank | 6 comment | 27 complexity | f656a249547c047401f19bdef030061c MD5 | raw file
  1# -*- coding: utf-8 -*-
  2"""
  3Use nose
  4`$ pip install nose`
  5`$ nosetests`
  6"""
  7from hyde.generator import Generator
  8from hyde.site import Site
  9
 10from fswrap import File
 11
 12TEST_SITE = File(__file__).parent.parent.child_folder('_test')
 13
 14class TestTagger(object):
 15
 16    def setUp(self):
 17        TEST_SITE.make()
 18        TEST_SITE.parent.child_folder(
 19                  'sites/test_tagger').copy_contents_to(TEST_SITE)
 20        self.s = Site(TEST_SITE)
 21        self.deploy = TEST_SITE.child_folder('deploy')
 22
 23
 24    def tearDown(self):
 25        TEST_SITE.delete()
 26
 27    def test_tagger_walker(self):
 28        gen = Generator(self.s)
 29        gen.load_site_if_needed()
 30        gen.generate_all()
 31
 32        assert hasattr(self.s, 'tagger')
 33        assert hasattr(self.s.tagger, 'tags')
 34        assert self.s.tagger.tags
 35        tags = self.s.tagger.tags.to_dict()
 36
 37        assert len(tags) == 6
 38
 39        for tag in ['sad', 'happy', 'angry', 'thoughts', 'events']:
 40            assert tag in tags
 41
 42        sad_posts = [post.name for post in tags['sad']['resources']]
 43        assert len(sad_posts) == 2
 44        assert "sad-post.html" in sad_posts
 45        assert "another-sad-post.html" in sad_posts
 46        sad_posts == [post.name for post in
 47                        self.s.content.walk_resources_tagged_with('sad')]
 48
 49
 50        happy_posts = [post.name for post in
 51                        self.s.content.walk_resources_tagged_with('happy')]
 52        assert len(happy_posts) == 1
 53        assert "happy-post.html" in happy_posts
 54
 55        angry_posts = [post.name for post in
 56                        self.s.content.walk_resources_tagged_with('angry')]
 57        assert len(angry_posts) == 1
 58        assert "angry-post.html" in angry_posts
 59
 60        sad_thought_posts = [post.name for post in
 61                        self.s.content.walk_resources_tagged_with('sad+thoughts')]
 62        assert len(sad_thought_posts) == 1
 63        assert "sad-post.html" in sad_thought_posts
 64
 65    def test_tagger_archives_generated(self):
 66        gen = Generator(self.s)
 67        gen.load_site_if_needed()
 68        gen.load_template_if_needed()
 69        gen.generate_all()
 70        tags_folder = self.deploy.child_folder('blog/tags')
 71
 72        assert tags_folder.exists
 73        tags = ['sad', 'happy', 'angry', 'thoughts', 'events']
 74
 75        archives = (File(tags_folder.child("%s.html" % tag)) for tag in tags)
 76
 77        for archive in archives:
 78            assert archive.exists
 79
 80        from pyquery import PyQuery
 81
 82        q = PyQuery(File(tags_folder.child('sad.html')).read_all())
 83        assert q
 84
 85        assert q('li').length == 2
 86        assert q('li:nth-child(1) a').attr('href') == '/blog/another-sad-post.html'
 87        assert q('li:nth-child(2) a').attr('href') == '/blog/sad-post.html'
 88
 89        q = PyQuery(File(tags_folder.child('happy.html')).read_all())
 90        assert q
 91
 92        assert q('li').length == 1
 93        assert q('li a:first-child').attr('href') == '/blog/happy-post.html'
 94
 95        q = PyQuery(File(tags_folder.child('angry.html')).read_all())
 96        assert q
 97
 98        assert q('li').length == 1
 99        assert q('li a:first-child').attr('href') == '/blog/angry-post.html'
100
101        q = PyQuery(File(tags_folder.child('thoughts.html')).read_all())
102        assert q
103
104        assert q('li').length == 3
105        assert q('li:nth-child(1) a').attr('href') == '/blog/happy-post.html'
106        assert q('li:nth-child(2) a').attr('href') == '/blog/angry-post.html'
107        assert q('li:nth-child(3) a').attr('href') == '/blog/sad-post.html'
108
109        q = PyQuery(File(tags_folder.child('events.html')).read_all())
110        assert q
111
112        assert q('li').length == 1
113        assert q('li a:first-child').attr('href') == '/blog/another-sad-post.html'
114
115    def test_tagger_metadata(self):
116        conf = {
117            "tagger":{
118                "tags": {
119                    "sad" : {
120                        "emotions": ["Dissappointed", "Lost"]
121                    },
122                    "angry": {
123                        "emotions": ["Irritated", "Annoyed", "Disgusted"]
124                    }
125                }
126            }
127        }
128        s = Site(TEST_SITE)
129        s.config.update(conf)
130        gen = Generator(s)
131        gen.load_site_if_needed()
132        gen.generate_all()
133
134        assert hasattr(s, 'tagger')
135        assert hasattr(s.tagger, 'tags')
136        assert s.tagger.tags
137        tags = s.tagger.tags
138        sad_tag = tags.sad
139        assert hasattr(sad_tag, "emotions")
140
141        assert sad_tag.emotions == s.config.tagger.tags.sad.emotions
142
143        assert hasattr(tags, "angry")
144        angry_tag = tags.angry
145        assert angry_tag
146        assert hasattr(angry_tag, "emotions")
147        assert angry_tag.emotions == s.config.tagger.tags.angry.emotions
148
149        for tagname in ['happy', 'thoughts', 'events']:
150            tag = getattr(tags, tagname)
151            assert tag
152            assert not hasattr(tag, "emotions")
153
154    def test_tagger_sorted(self):
155        conf = {
156           "tagger":{
157               "sorter": "time",
158               "archives": {
159                    "blog": {
160                        "template": "emotions.j2",
161                        "source": "blog",
162                        "target": "blog/tags",
163                        "extension": "html",
164                        "meta": {
165                            "author": "Tagger Plugin"
166                        }
167                    }
168               },
169               "tags": {
170                   "sad" : {
171                       "emotions": ["Dissappointed", "Lost"]
172                   },
173                   "angry": {
174                       "emotions": ["Irritated", "Annoyed", "Disgusted"]
175                   }
176               }
177           }
178        }
179
180        text = """
181<div id="author">{{ resource.meta.author }}</div>
182<h1>Posts tagged: {{ tag }} in {{ node.name|title }}</h1>
183Emotions:
184<ul>
185{% for emotion in tag.emotions %}
186<li class="emotion">
187{{ emotion }}
188</li>
189{% endfor %}
190<ul>
191{% for resource in walker() -%}
192<li>
193<a href="{{ content_url(resource.url) }}">{{ resource.meta.title }}</a>
194</li>
195{%- endfor %}
196</ul>
197"""
198        template = File(TEST_SITE.child('layout/emotions.j2'))
199        template.write(text)
200        s = Site(TEST_SITE)
201        s.config.update(conf)
202        gen = Generator(s)
203        gen.load_site_if_needed()
204        gen.generate_all()
205
206        tags_folder = self.deploy.child_folder('blog/tags')
207        assert tags_folder.exists
208        tags = ['sad', 'happy', 'angry', 'thoughts', 'events']
209        archives = dict((tag, File(tags_folder.child("%s.html" % tag))) for tag in tags)
210
211        for tag, archive in archives.items():
212            assert archive.exists
213
214        from pyquery import PyQuery
215
216        q = PyQuery(archives['sad'].read_all())
217        assert len(q("li.emotion")) == 2
218        assert q("#author").text() == "Tagger Plugin"
219
220        q = PyQuery(archives['angry'].read_all())
221        assert len(q("li.emotion")) == 3
222
223        for tag, archive in archives.items():
224            if tag not in ["sad", "angry"]:
225                q = PyQuery(archives[tag].read_all())
226                assert not len(q("li.emotion"))