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