test_all.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import os
  2. from blogit.blogit import CONFIG, find_new_posts_and_pages, DataBase
  3. from blogit.blogit import Entry
  4. CONFIG['content_root'] = 'test_root'
  5. DB = DataBase(os.path.join(CONFIG['content_root'], 'blogit.db'))
  6. tags = ['foo', 'bar', 'baz', 'bug', 'buf']
  7. shift = lambda l, n: l[-n:] + l[:-n]
  8. post = '''\
  9. ---
  10. title: Blog post {number}
  11. author: Famous author
  12. published: 2015-01-{number}
  13. tags: {tags}
  14. public: yes
  15. chronological: yes
  16. kind: writing
  17. summary: This is a summry of post {number}. Donec id elit non mi porta gravida at eget metus. Fusce dapibus
  18. ---
  19. 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.
  20. This is a snippet in bash
  21. ```bash
  22. $ for i in `seq 1 10`; do
  23. echo $i
  24. done
  25. VAR="variable"
  26. echo $VAR
  27. # This is a very long long long long long long long long long long comment
  28. ```
  29. This is a snippet in python
  30. ```python
  31. def yay(top):
  32. for i in range(1, top+1):
  33. yield i
  34. for i in yay:
  35. print(i)
  36. ```
  37. '''
  38. try:
  39. os.mkdir(CONFIG['content_root'])
  40. except OSError:
  41. pass
  42. shift_factors = map(lambda x: (x - 1) / 5 +1, range(1,21))
  43. f = open((os.path.join(CONFIG['content_root'],
  44. 'page.md')), 'w')
  45. f.write("""\
  46. ---
  47. title: example page
  48. public: yes
  49. kind: page
  50. template: about.html
  51. ---
  52. # some heading
  53. content paragraph
  54. ## heading 2
  55. some more content
  56. """)
  57. f.close()
  58. def write_file(i):
  59. f = open((os.path.join(CONFIG['content_root'],
  60. 'post{}.md'.format(i))), 'w')
  61. f.write(post.format(**{'number': i,
  62. 'tags': shift(tags, shift_factors[i-1])[:-1]}))
  63. [write_file(i) for i in range(1, 21)]
  64. def test_find_new_posts_and_pages():
  65. entries = [e for e in find_new_posts_and_pages(DB)]
  66. pages = [e[1] for e in entries if str(e[1]).endswith('page.md')]
  67. assert pages is not None
  68. assert len(DB.posts.all()) == 20
  69. def test_tags():
  70. entries = map(Entry.entry_from_db, [e.get('filename') for e in DB.posts.all()])
  71. #os.unlink(DB._db._storage._handle.name)