test_all.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import os
  2. import pytest
  3. from bs4 import BeautifulSoup
  4. from tinydb import Query, where
  5. from blogit.blogit import (CONFIG, find_new_posts_and_pages, DataBase,
  6. Entry, Tag, _sort_entries, _get_last_entries,
  7. render_archive, update_index, build)
  8. import blogit.blogit as m
  9. CONFIG['content_root'] = 'test_root'
  10. ARCHIVE_SIZE = 10
  11. db_name = os.path.join(CONFIG['content_root'], 'blogit.db')
  12. if os.path.exists(db_name):
  13. import shutil
  14. shutil.rmtree(CONFIG['content_root'])
  15. if not os.path.exists(CONFIG['content_root']):
  16. os.mkdir(CONFIG['content_root'])
  17. DB = DataBase(os.path.join(CONFIG['content_root'], 'blogit.db'))
  18. # monkey patch to local DB
  19. m.DB = DB
  20. Tag.table = DB.tags
  21. Tag.db = DB
  22. Entry.db = DB
  23. tags = ['foo', 'bar', 'baz', 'bug', 'buf']
  24. shift = lambda l, n: l[-n:] + l[:-n]
  25. post = '''\
  26. ---
  27. title: Blog post {number}
  28. author: Famous author
  29. published: 2015-01-{number}
  30. tags: {tags}
  31. public: yes
  32. chronological: yes
  33. kind: writing
  34. summary: This is a summry of post {number}. Donec id elit non mi porta gravida at eget metus. Fusce dapibus
  35. ---
  36. This is the body of post {number}. Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui.
  37. This is a snippet in bash
  38. ```bash
  39. $ for i in `seq 1 10`; do
  40. echo $i
  41. done
  42. VAR="variable"
  43. echo $VAR
  44. # This is a very long long long long long long long long long long comment
  45. ```
  46. This is a snippet in python
  47. ```python
  48. def yay(top):
  49. for i in range(1, top+1):
  50. yield i
  51. for i in yay:
  52. print(i)
  53. ```
  54. '''
  55. try:
  56. os.mkdir(CONFIG['content_root'])
  57. except OSError:
  58. pass
  59. shift_factors = map(lambda x: (x - 1) / 5 +1, range(1,21))
  60. f = open((os.path.join(CONFIG['content_root'],
  61. 'page.md')), 'w')
  62. f.write("""\
  63. ---
  64. title: example page
  65. public: yes
  66. kind: page
  67. template: about.html
  68. ---
  69. # some heading
  70. content paragraph
  71. ## heading 2
  72. some more content
  73. """)
  74. f.close()
  75. def write_file(i):
  76. f = open((os.path.join(CONFIG['content_root'],
  77. 'post{}.md'.format(i))), 'w')
  78. f.write(post.format(**{'number': i,
  79. 'tags':
  80. ','.join(shift(tags, shift_factors[i-1])[:-1])}))
  81. [write_file(i) for i in range(1, 21)]
  82. def test_find_new_posts_and_pages():
  83. entries = [e for e in find_new_posts_and_pages(DB)]
  84. assert len(entries)
  85. pages = [e[1] for e in entries if str(e[0]).endswith('page.md')]
  86. assert len(pages)
  87. assert len(DB.posts.all()) == 20
  88. entries = [e for e in find_new_posts_and_pages(DB)]
  89. # no new posts sould be found
  90. assert len(DB.posts.all()) == 20
  91. [e[0].tags for e in entries]
  92. foo = DB.tags.search(where('name')=='foo')
  93. assert foo[0]['post_ids'] == range(1, 16)
  94. def test_tags():
  95. entries = map(Entry.entry_from_db,
  96. [os.path.join(CONFIG['content_root'], e.get('filename'))
  97. for e in DB.posts.all()])
  98. tags = DB.tags.all()
  99. t = entries[0].tags
  100. assert len(t) == 4
  101. assert t[0].name == u'buf'
  102. new_tag = Tag('buggg')
  103. new_tag.posts = [100,100]
  104. with pytest.raises(ValueError):
  105. new_tag.posts = "This should not work"
  106. with pytest.raises(ValueError):
  107. new_tag.posts = 1 # This should not either
  108. def test_slug():
  109. t = Tag('foo:bar')
  110. assert t.slug == "foo-bar"
  111. t = Tag('foo:;bar,.,baz')
  112. assert t.slug == "foo-bar-baz"
  113. def test_tag_posts():
  114. example = Tag('example')
  115. example.posts = [1,2,3]
  116. assert [1,2,3] == example.posts
  117. Filter = Query()
  118. t = DB.tags.get(Filter.post_ids == [1, 2, 3])
  119. assert t['post_ids'] == [1, 2, 3]
  120. example = Tag('example')
  121. example.posts = [4,5,6]
  122. rv = DB.tags.search(where('name') == 'example')
  123. assert rv[0]['post_ids'] == range(1, 7)
  124. def test_tag_entries():
  125. t = Tag('breaks')
  126. t.posts = [10000]
  127. with pytest.raises(ValueError):
  128. list(t.entries)
  129. tf = Tag(u'example')
  130. entries = list(tf.entries)
  131. assert len(entries)
  132. def test_tag_post_ids():
  133. m ="""\
  134. ---
  135. title: Blog post {}
  136. author: Famous author
  137. published: 2015-01-{}
  138. tags: tag1, tag2
  139. public: yes
  140. chronological: yes
  141. kind: writing
  142. summary: This is a summry
  143. ---
  144. """
  145. with open(os.path.join(CONFIG['content_root'], 'e.md'), 'w') as f:
  146. f.write(m.format(25, 25))
  147. with open(os.path.join(CONFIG['content_root'], 'f.md'), 'w') as f:
  148. f.write(m.format(27, 27))
  149. e1 = Entry(os.path.join(CONFIG['content_root'], 'e.md'))
  150. e1.tags
  151. e2 = Entry(os.path.join(CONFIG['content_root'], 'f.md'))
  152. e2.tags
  153. assert e1.tags[0].posts == e2.tags[0].posts
  154. e1.render()
  155. [t.render() for t in e1.tags]
  156. l = _sort_entries([e2, e1])
  157. assert l == [e2, e1]
  158. def test_tag_render():
  159. p = DB.posts.get(eid=1)
  160. entry = Entry.entry_from_db(
  161. os.path.join(CONFIG['content_root'], p.get('filename')))
  162. #entry = Entry(os.path.join(CONFIG['content_root'], 'post1.md'))
  163. tags = entry.tags
  164. assert map(str, tags) == ['buf', 'foo', 'bar', 'baz']
  165. # the entries are wrongly sorted, need to look at that
  166. assert tags[0].render()
  167. assert len(list(tags[0].entries))
  168. def test_get_last_entries():
  169. le = _get_last_entries(DB)
  170. assert [e.id for e in le] == range(22, 12, -1)
  171. def test_render_archive():
  172. entries = [Entry.entry_from_db(
  173. os.path.join(CONFIG['content_root'], e.get('filename'))) for e in
  174. DB.posts.all()]
  175. render_archive(_sort_entries(entries, reversed=True)[ARCHIVE_SIZE:])
  176. # pages should not be in the archive
  177. with open(os.path.join(CONFIG['output_to'], 'archive', 'index.html')) as html_index:
  178. soup = BeautifulSoup(html_index.read(), 'html.parser')
  179. assert len(soup.find_all(class_='post')) == 12
  180. def test_render_index():
  181. update_index(_get_last_entries(DB))
  182. with open(os.path.join(CONFIG['output_to'], 'index.html')) as html_index:
  183. soup = BeautifulSoup(html_index.read(), 'html.parser')
  184. assert len(soup.find_all(class_='clearfix entry')) == 10
  185. def test_build():
  186. build()
  187. # check that the index really contains the last 10 entries
  188. with open(os.path.join(CONFIG['output_to'], 'index.html')) as html_index:
  189. soup = BeautifulSoup(html_index.read(), 'html.parser')
  190. assert len(soup.find_all(class_='clearfix entry')) == 10
  191. # pages should not be in the archive, but archive size here is different
  192. with open(os.path.join(CONFIG['output_to'], 'archive', 'index.html')) as html_index:
  193. soup = BeautifulSoup(html_index.read(), 'html.parser')
  194. assert len(soup.find_all(class_='post')) == 22