A fork of hyde, the static site generation. Some patches will be pushed upstream.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

155 lines
5.0 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. 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', 'media']:
  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 hasattr(c, 'plugins')
  98. assert len(c.plugins) == 0
  99. assert c.deploy_root_path == TEST_SITE.child_folder('deploy')
  100. assert c.not_found == '404.html'
  101. def test_conf1(self):
  102. c = Config(sitepath=TEST_SITE, config_dict=yaml.load(self.conf1))
  103. assert c.content_root_path == TEST_SITE.child_folder('stuff')
  104. def test_conf2(self):
  105. c = Config(sitepath=TEST_SITE, config_dict=yaml.load(self.conf2))
  106. assert c.content_root_path == TEST_SITE.child_folder('site/stuff')
  107. assert c.media_root_path == TEST_SITE.child_folder('mmm')
  108. assert c.media_url == TEST_SITE.child_folder('/media')
  109. assert c.deploy_root_path == Folder('~/deploy_site')
  110. def test_read_from_file_by_default(self):
  111. File(TEST_SITE.child('site.yaml')).write(self.conf2)
  112. c = Config(sitepath=TEST_SITE)
  113. assert c.content_root_path == TEST_SITE.child_folder('site/stuff')
  114. assert c.media_root_path == TEST_SITE.child_folder('mmm')
  115. assert c.media_url == TEST_SITE.child_folder('/media')
  116. assert c.deploy_root_path == Folder('~/deploy_site')
  117. def test_read_from_specified_file(self):
  118. File(TEST_SITE.child('another.yaml')).write(self.conf2)
  119. c = Config(sitepath=TEST_SITE, config_file='another.yaml')
  120. assert c.content_root_path == TEST_SITE.child_folder('site/stuff')
  121. assert c.media_root_path == TEST_SITE.child_folder('mmm')
  122. assert c.media_url == TEST_SITE.child_folder('/media')
  123. assert c.deploy_root_path == Folder('~/deploy_site')
  124. def test_extends(self):
  125. another = """
  126. extends: site.yaml
  127. mode: production
  128. media_root: xxx
  129. """
  130. File(TEST_SITE.child('site.yaml')).write(self.conf2)
  131. File(TEST_SITE.child('another.yaml')).write(another)
  132. c = Config(sitepath=TEST_SITE, config_file='another.yaml')
  133. assert c.mode == 'production'
  134. assert c.content_root_path == TEST_SITE.child_folder('site/stuff')
  135. assert c.media_root_path == TEST_SITE.child_folder('xxx')
  136. assert c.media_url == TEST_SITE.child_folder('/media')
  137. assert c.deploy_root_path == Folder('~/deploy_site')