PageRenderTime 19ms CodeModel.GetById 2ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/hyde/tests/test_model.py

http://github.com/hyde/hyde
Python | 160 lines | 144 code | 10 blank | 6 comment | 0 complexity | eafbc61abe7d3a5dfb021ef226ed86cd MD5 | raw file
  1# -*- coding: utf-8 -*-
  2"""
  3Use nose
  4`$ pip install nose`
  5`$ nosetests`
  6"""
  7from hyde.model import Config, Expando
  8
  9from fswrap import File, Folder
 10
 11def test_expando_one_level():
 12    d = {"a": 123, "b": "abc"}
 13    x = Expando(d)
 14    assert x.a == d['a']
 15    assert x.b == d['b']
 16
 17def test_expando_two_levels():
 18    d = {"a": 123, "b": {"c": 456}}
 19    x = Expando(d)
 20    assert x.a == d['a']
 21    assert x.b.c == d['b']['c']
 22
 23def test_expando_three_levels():
 24    d = {"a": 123, "b": {"c": 456, "d": {"e": "abc"}}}
 25    x = Expando(d)
 26    assert x.a == d['a']
 27    assert x.b.c == d['b']['c']
 28    assert x.b.d.e == d['b']['d']['e']
 29
 30def test_expando_update():
 31    d1 = {"a": 123, "b": "abc"}
 32    x = Expando(d1)
 33    assert x.a == d1['a']
 34    assert x.b == d1['b']
 35    d = {"b": {"c": 456, "d": {"e": "abc"}}, "f": "lmn"}
 36    x.update(d)
 37    assert  x.a == d1['a']
 38    assert x.b.c == d['b']['c']
 39    assert x.b.d.e == d['b']['d']['e']
 40    assert x.f == d["f"]
 41    d2 = {"a": 789, "f": "opq"}
 42    y = Expando(d2)
 43    x.update(y)
 44    assert x.a == 789
 45    assert x.f == "opq"
 46
 47def test_expando_to_dict():
 48    d = {"a": 123, "b": {"c": 456, "d": {"e": "abc"}}}
 49    x = Expando(d)
 50    assert d == x.to_dict()
 51
 52def test_expando_to_dict_with_update():
 53    d1 = {"a": 123, "b": "abc"}
 54    x = Expando(d1)
 55    d = {"b": {"c": 456, "d": {"e": "abc"}}, "f": "lmn"}
 56    x.update(d)
 57    expected = {}
 58    expected.update(d1)
 59    expected.update(d)
 60    assert expected == x.to_dict()
 61    d2 = {"a": 789, "f": "opq"}
 62    y = Expando(d2)
 63    x.update(y)
 64    expected.update(d2)
 65    assert expected == x.to_dict()
 66
 67TEST_SITE = File(__file__).parent.child_folder('_test')
 68
 69import yaml
 70class TestConfig(object):
 71
 72    @classmethod
 73    def setup_class(cls):
 74        cls.conf1 = """
 75        mode: development
 76        content_root: stuff # Relative path from site root
 77        media_root: media # Relative path from site root
 78        media_url: /media
 79        widgets:
 80        plugins:
 81        aggregators:
 82        """
 83
 84        cls.conf2 = """
 85        mode: development
 86        deploy_root: ~/deploy_site
 87        content_root: site/stuff # Relative path from site root
 88        media_root: mmm # Relative path from site root
 89        media_url: /media
 90        widgets:
 91        plugins:
 92        aggregators:
 93        """
 94
 95    def setUp(self):
 96        TEST_SITE.make()
 97        TEST_SITE.parent.child_folder('sites/test_jinja').copy_contents_to(TEST_SITE)
 98
 99    def tearDown(self):
100        TEST_SITE.delete()
101
102    def test_default_configuration(self):
103        c = Config(sitepath=TEST_SITE, config_dict={})
104        for root in ['content', 'layout']:
105            name = root + '_root'
106            path = name + '_path'
107            assert hasattr(c, name)
108            assert getattr(c, name) == root
109            assert hasattr(c, path)
110            assert getattr(c, path) == TEST_SITE.child_folder(root)
111        assert c.media_root_path == c.content_root_path.child_folder('media')
112        assert hasattr(c, 'plugins')
113        assert len(c.plugins) == 0
114        assert hasattr(c, 'ignore')
115        assert c.ignore == ["*~", "*.bak", ".hg", ".git", ".svn"]
116        assert c.deploy_root_path == TEST_SITE.child_folder('deploy')
117        assert c.not_found == '404.html'
118        assert c.meta.nodemeta == 'meta.yaml'
119
120    def test_conf1(self):
121        c = Config(sitepath=TEST_SITE, config_dict=yaml.load(self.conf1))
122        assert c.content_root_path == TEST_SITE.child_folder('stuff')
123
124    def test_conf2(self):
125        c = Config(sitepath=TEST_SITE, config_dict=yaml.load(self.conf2))
126        assert c.content_root_path == TEST_SITE.child_folder('site/stuff')
127        assert c.media_root_path == c.content_root_path.child_folder('mmm')
128        assert c.media_url == TEST_SITE.child_folder('/media')
129        assert c.deploy_root_path == Folder('~/deploy_site')
130
131    def test_read_from_file_by_default(self):
132        File(TEST_SITE.child('site.yaml')).write(self.conf2)
133        c = Config(sitepath=TEST_SITE)
134        assert c.content_root_path == TEST_SITE.child_folder('site/stuff')
135        assert c.media_root_path == c.content_root_path.child_folder('mmm')
136        assert c.media_url == TEST_SITE.child_folder('/media')
137        assert c.deploy_root_path == Folder('~/deploy_site')
138
139    def test_read_from_specified_file(self):
140        File(TEST_SITE.child('another.yaml')).write(self.conf2)
141        c = Config(sitepath=TEST_SITE, config_file='another.yaml')
142        assert c.content_root_path == TEST_SITE.child_folder('site/stuff')
143        assert c.media_root_path == c.content_root_path.child_folder('mmm')
144        assert c.media_url == TEST_SITE.child_folder('/media')
145        assert c.deploy_root_path == Folder('~/deploy_site')
146
147    def test_extends(self):
148        another = """
149        extends: site.yaml
150        mode: production
151        media_root: xxx
152        """
153        File(TEST_SITE.child('site.yaml')).write(self.conf2)
154        File(TEST_SITE.child('another.yaml')).write(another)
155        c = Config(sitepath=TEST_SITE, config_file='another.yaml')
156        assert c.mode == 'production'
157        assert c.content_root_path == TEST_SITE.child_folder('site/stuff')
158        assert c.media_root_path == c.content_root_path.child_folder('xxx')
159        assert c.media_url == TEST_SITE.child_folder('/media')
160        assert c.deploy_root_path == Folder('~/deploy_site')