A fork of hyde, the static site generation. Some patches will be pushed upstream.
 
 
 

135 lines
4.4 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. Use nose
  4. `$ pip install nose`
  5. `$ nosetests`
  6. """
  7. from hyde.model import Config, Expando
  8. from hyde.fs import *
  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. TEST_SITE = File(__file__).parent.child_folder('_test')
  42. import yaml
  43. class TestConfig(object):
  44. @classmethod
  45. def setup_class(cls):
  46. cls.conf1 = """
  47. mode: development
  48. content_root: stuff # Relative path from site root
  49. media_root: media # Relative path from site root
  50. media_url: /media
  51. widgets:
  52. plugins:
  53. aggregators:
  54. """
  55. cls.conf2 = """
  56. mode: development
  57. deploy_root: ~/deploy_site
  58. content_root: site/stuff # Relative path from site root
  59. media_root: mmm # Relative path from site root
  60. media_url: /media
  61. widgets:
  62. plugins:
  63. aggregators:
  64. """
  65. def setUp(self):
  66. TEST_SITE.make()
  67. TEST_SITE.parent.child_folder('sites/test_jinja').copy_contents_to(TEST_SITE)
  68. def tearDown(self):
  69. TEST_SITE.delete()
  70. def test_default_configuration(self):
  71. c = Config(sitepath=TEST_SITE, config_dict={})
  72. for root in ['content', 'layout', 'media']:
  73. name = root + '_root'
  74. path = name + '_path'
  75. assert hasattr(c, name)
  76. assert getattr(c, name) == root
  77. assert hasattr(c, path)
  78. assert getattr(c, path) == TEST_SITE.child_folder(root)
  79. assert hasattr(c, 'plugins')
  80. assert len(c.plugins) == 0
  81. assert c.deploy_root_path == TEST_SITE.child_folder('deploy')
  82. assert c.not_found == '404.html'
  83. def test_conf1(self):
  84. c = Config(sitepath=TEST_SITE, config_dict=yaml.load(self.conf1))
  85. assert c.content_root_path == TEST_SITE.child_folder('stuff')
  86. def test_conf2(self):
  87. c = Config(sitepath=TEST_SITE, config_dict=yaml.load(self.conf2))
  88. assert c.content_root_path == TEST_SITE.child_folder('site/stuff')
  89. assert c.media_root_path == TEST_SITE.child_folder('mmm')
  90. assert c.media_url == TEST_SITE.child_folder('/media')
  91. assert c.deploy_root_path == Folder('~/deploy_site')
  92. def test_read_from_file_by_default(self):
  93. File(TEST_SITE.child('site.yaml')).write(self.conf2)
  94. c = Config(sitepath=TEST_SITE)
  95. assert c.content_root_path == TEST_SITE.child_folder('site/stuff')
  96. assert c.media_root_path == TEST_SITE.child_folder('mmm')
  97. assert c.media_url == TEST_SITE.child_folder('/media')
  98. assert c.deploy_root_path == Folder('~/deploy_site')
  99. def test_read_from_specified_file(self):
  100. File(TEST_SITE.child('another.yaml')).write(self.conf2)
  101. c = Config(sitepath=TEST_SITE, config_file='another.yaml')
  102. assert c.content_root_path == TEST_SITE.child_folder('site/stuff')
  103. assert c.media_root_path == TEST_SITE.child_folder('mmm')
  104. assert c.media_url == TEST_SITE.child_folder('/media')
  105. assert c.deploy_root_path == Folder('~/deploy_site')
  106. def test_extends(self):
  107. another = """
  108. extends: site.yaml
  109. mode: production
  110. media_root: xxx
  111. """
  112. File(TEST_SITE.child('site.yaml')).write(self.conf2)
  113. File(TEST_SITE.child('another.yaml')).write(another)
  114. c = Config(sitepath=TEST_SITE, config_file='another.yaml')
  115. assert c.mode == 'production'
  116. assert c.content_root_path == TEST_SITE.child_folder('site/stuff')
  117. assert c.media_root_path == TEST_SITE.child_folder('xxx')
  118. assert c.media_url == TEST_SITE.child_folder('/media')
  119. assert c.deploy_root_path == Folder('~/deploy_site')