1
0

test_all.py 3.3 KB

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