test_all.py 5.0 KB

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