test_all.py 2.9 KB

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