test_all.py 5.7 KB

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