test_all.py 2.3 KB

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