test_all.py 2.6 KB

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