PageRenderTime 71ms CodeModel.GetById 43ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/hyde/tests/test_site.py

http://github.com/hyde/hyde
Python | 290 lines | 256 code | 28 blank | 6 comment | 16 complexity | a09a8ce2ebbc86f5e1ae1f62f27d89c3 MD5 | raw file
  1# -*- coding: utf-8 -*-
  2"""
  3Use nose
  4`$ pip install nose`
  5`$ nosetests`
  6"""
  7import yaml
  8from urllib import quote
  9
 10from hyde.model import Config
 11from hyde.site import Node, RootNode, Site
 12
 13from fswrap import File, Folder
 14
 15TEST_SITE_ROOT = File(__file__).parent.child_folder('sites/test_jinja')
 16
 17def test_node_site():
 18    s = Site(TEST_SITE_ROOT)
 19    r = RootNode(TEST_SITE_ROOT.child_folder('content'), s)
 20    assert r.site == s
 21    n = Node(r.source_folder.child_folder('blog'), r)
 22    assert n.site == s
 23
 24def test_node_root():
 25    s = Site(TEST_SITE_ROOT)
 26    r = RootNode(TEST_SITE_ROOT.child_folder('content'), s)
 27    assert r.root == r
 28    n = Node(r.source_folder.child_folder('blog'), r)
 29    assert n.root == r
 30
 31def test_node_parent():
 32    s = Site(TEST_SITE_ROOT)
 33    r = RootNode(TEST_SITE_ROOT.child_folder('content'), s)
 34    c = r.add_node(TEST_SITE_ROOT.child_folder('content/blog/2010/december'))
 35    assert c.parent == r.node_from_relative_path('blog/2010')
 36
 37def test_node_module():
 38    s = Site(TEST_SITE_ROOT)
 39    r = RootNode(TEST_SITE_ROOT.child_folder('content'), s)
 40    assert not r.module
 41    n = r.add_node(TEST_SITE_ROOT.child_folder('content/blog'))
 42    assert n.module == n
 43    c = r.add_node(TEST_SITE_ROOT.child_folder('content/blog/2010/december'))
 44    assert c.module == n
 45
 46def test_node_url():
 47    s = Site(TEST_SITE_ROOT)
 48    r = RootNode(TEST_SITE_ROOT.child_folder('content'), s)
 49    assert not r.module
 50    n = r.add_node(TEST_SITE_ROOT.child_folder('content/blog'))
 51    assert n.url == '/' + n.relative_path
 52    assert n.url == '/blog'
 53    c = r.add_node(TEST_SITE_ROOT.child_folder('content/blog/2010/december'))
 54    assert c.url == '/' + c.relative_path
 55    assert c.url == '/blog/2010/december'
 56
 57def test_node_full_url():
 58    s = Site(TEST_SITE_ROOT)
 59    s.config.base_url = 'http://localhost'
 60    r = RootNode(TEST_SITE_ROOT.child_folder('content'), s)
 61    assert not r.module
 62    n = r.add_node(TEST_SITE_ROOT.child_folder('content/blog'))
 63    assert n.full_url == 'http://localhost/blog'
 64    c = r.add_node(TEST_SITE_ROOT.child_folder('content/blog/2010/december'))
 65    assert c.full_url == 'http://localhost/blog/2010/december'
 66
 67def test_node_full_url_quoted():
 68    s = Site(TEST_SITE_ROOT)
 69    s.config.base_url = 'http://localhost'
 70    r = RootNode(TEST_SITE_ROOT.child_folder('content'), s)
 71    assert not r.module
 72    n = r.add_node(TEST_SITE_ROOT.child_folder('content/blo~g'))
 73    assert n.full_url == 'http://localhost/' + quote('blo~g')
 74    c = r.add_node(TEST_SITE_ROOT.child_folder('content/blo~g/2010/december'))
 75    assert c.full_url == 'http://localhost/' + quote('blo~g/2010/december')
 76
 77def test_node_relative_path():
 78    s = Site(TEST_SITE_ROOT)
 79    r = RootNode(TEST_SITE_ROOT.child_folder('content'), s)
 80    assert not r.module
 81    n = r.add_node(TEST_SITE_ROOT.child_folder('content/blog'))
 82    assert n.relative_path == 'blog'
 83    c = r.add_node(TEST_SITE_ROOT.child_folder('content/blog/2010/december'))
 84    assert c.relative_path == 'blog/2010/december'
 85
 86def test_load():
 87    s = Site(TEST_SITE_ROOT)
 88    s.load()
 89    path = 'blog/2010/december'
 90    node = s.content.node_from_relative_path(path)
 91    assert node
 92    assert Folder(node.relative_path) == Folder(path)
 93    path += '/merry-christmas.html'
 94    resource = s.content.resource_from_relative_path(path)
 95    assert resource
 96    assert resource.relative_path == path
 97    assert not s.content.resource_from_relative_path('/happy-festivus.html')
 98
 99def test_walk_resources():
100    s = Site(TEST_SITE_ROOT)
101    s.load()
102    pages = [page.name for page in s.content.walk_resources()]
103    expected = ["404.html",
104                "about.html",
105                "apple-touch-icon.png",
106                "merry-christmas.html",
107                "crossdomain.xml",
108                "favicon.ico",
109                "robots.txt",
110                "site.css"
111                ]
112    pages.sort()
113    expected.sort()
114    assert pages == expected
115
116def test_contains_resource():
117    s = Site(TEST_SITE_ROOT)
118    s.load()
119    path = 'blog/2010/december'
120    node = s.content.node_from_relative_path(path)
121    assert node.contains_resource('merry-christmas.html')
122
123def test_get_resource():
124    s = Site(TEST_SITE_ROOT)
125    s.load()
126    path = 'blog/2010/december'
127    node = s.content.node_from_relative_path(path)
128    resource = node.get_resource('merry-christmas.html')
129    assert resource == s.content.resource_from_relative_path(Folder(path).child('merry-christmas.html'))
130
131def test_resource_slug():
132    s = Site(TEST_SITE_ROOT)
133    s.load()
134    path = 'blog/2010/december'
135    node = s.content.node_from_relative_path(path)
136    resource = node.get_resource('merry-christmas.html')
137    assert resource.slug == 'merry-christmas'
138
139
140def test_get_resource_from_relative_deploy_path():
141    s = Site(TEST_SITE_ROOT)
142    s.load()
143    path = 'blog/2010/december'
144    node = s.content.node_from_relative_path(path)
145    resource = node.get_resource('merry-christmas.html')
146    assert resource == s.content.resource_from_relative_deploy_path(Folder(path).child('merry-christmas.html'))
147    resource.relative_deploy_path = Folder(path).child('merry-christmas.php')
148    assert resource == s.content.resource_from_relative_deploy_path(Folder(path).child('merry-christmas.php'))
149
150def test_is_processable_default_true():
151    s = Site(TEST_SITE_ROOT)
152    s.load()
153    for page in s.content.walk_resources():
154        assert page.is_processable
155
156def test_relative_deploy_path():
157    s = Site(TEST_SITE_ROOT)
158    s.load()
159    for page in s.content.walk_resources():
160        assert page.relative_deploy_path == Folder(page.relative_path)
161        assert page.url == '/' + page.relative_deploy_path
162
163def test_relative_deploy_path_override():
164    s = Site(TEST_SITE_ROOT)
165    s.load()
166    res = s.content.resource_from_relative_path('blog/2010/december/merry-christmas.html')
167    res.relative_deploy_path = 'blog/2010/december/happy-holidays.html'
168    for page in s.content.walk_resources():
169        if res.source_file == page.source_file:
170            assert page.relative_deploy_path == 'blog/2010/december/happy-holidays.html'
171        else:
172            assert page.relative_deploy_path == Folder(page.relative_path)
173
174class TestSiteWithConfig(object):
175
176    @classmethod
177    def setup_class(cls):
178        cls.SITE_PATH =  File(__file__).parent.child_folder('sites/test_jinja_with_config')
179        cls.SITE_PATH.make()
180        TEST_SITE_ROOT.copy_contents_to(cls.SITE_PATH)
181        cls.config_file = File(cls.SITE_PATH.child('alternate.yaml'))
182        with open(cls.config_file.path) as config:
183            cls.config = Config(sitepath=cls.SITE_PATH, config_dict=yaml.load(config))
184        cls.SITE_PATH.child_folder('content').rename_to(cls.config.content_root)
185
186    @classmethod
187    def teardown_class(cls):
188        cls.SITE_PATH.delete()
189
190    def test_load_with_config(self):
191        s = Site(self.SITE_PATH, config=self.config)
192        s.load()
193        path = 'blog/2010/december'
194        node = s.content.node_from_relative_path(path)
195        assert node
196        assert Folder(node.relative_path) == Folder(path)
197        path += '/merry-christmas.html'
198        resource = s.content.resource_from_relative_path(path)
199        assert resource
200        assert resource.relative_path == path
201        assert not s.content.resource_from_relative_path('/happy-festivus.html')
202
203    def test_content_url(self):
204        s = Site(self.SITE_PATH, config=self.config)
205        s.load()
206        path = 'blog/2010/december'
207        assert s.content_url(path) == "/" + path
208
209    def test_content_url_encoding(self):
210        s = Site(self.SITE_PATH, config=self.config)
211        s.load()
212        path = '".jpg'
213        assert s.content_url(path) == "/" + quote(path)
214
215    def test_content_url_encoding_safe(self):
216        s = Site(self.SITE_PATH, config=self.config)
217        s.load()
218        path = '".jpg/abc'
219        print s.content_url(path, "")
220        print "/"  + quote(path, "")
221        assert s.content_url(path, "") == "/" + quote(path, "")
222
223    def test_media_url(self):
224        s = Site(self.SITE_PATH, config=self.config)
225        s.load()
226        path = 'css/site.css'
227        assert s.media_url(path) == "/media/" + path
228
229    def test_is_media(self):
230        s = Site(self.SITE_PATH, config=self.config)
231        s.load()
232        assert s.is_media('media/css/site.css')
233
234        s.config.media_root = 'monkey'
235        assert not s.is_media('media/css/site.css')
236        assert s.is_media('monkey/css/site.css')
237
238    def test_full_url_for_content(self):
239        s = Site(self.SITE_PATH, config=self.config)
240        s.load()
241        path = 'blog/2010/december'
242        assert s.full_url(path) == "/" + path
243
244    def test_full_url_for_media(self):
245        s = Site(self.SITE_PATH, config=self.config)
246        s.load()
247        path = 'media/css/site.css'
248        assert s.is_media(path)
249        full_url = s.full_url(path)
250        assert full_url == "/" + path
251
252    def test_media_url_from_resource(self):
253        s = Site(self.SITE_PATH, config=self.config)
254        s.load()
255        path = 'css/site.css'
256        resource = s.content.resource_from_relative_path(
257                        Folder("media").child(path))
258        assert resource
259        assert resource.full_url == "/media/" + path
260
261    def test_config_ignore(self):
262        c = Config(self.SITE_PATH, config_dict=self.config.to_dict())
263        s = Site(self.SITE_PATH, config=c)
264        s.load()
265        path = 'apple-touch-icon.png'
266        resource = s.content.resource_from_relative_path(path)
267        assert resource
268        assert resource.full_url ==  "/" + path
269        s = Site(self.SITE_PATH, config=c)
270        s.config.ignore.append('*.png')
271        resource = s.content.resource_from_relative_path(path)
272        assert not resource
273
274    def test_config_ignore_nodes(self):
275        c = Config(self.SITE_PATH, config_dict=self.config.to_dict())
276        git = self.SITE_PATH.child_folder('.git')
277        git.make()
278        s = Site(self.SITE_PATH, config=c)
279        s.load()
280        git_node = s.content.node_from_relative_path('.git')
281        assert not git_node
282        blog_node = s.content.node_from_relative_path('blog')
283        assert blog_node
284        assert blog_node.full_url ==  "/blog"
285        s = Site(self.SITE_PATH, config=c)
286        s.config.ignore.append('blog')
287        blog_node = s.content.node_from_relative_path('blog')
288        assert not blog_node
289        git_node = s.content.node_from_relative_path('.git')
290        assert not git_node