1
0

test_all.py 3.7 KB

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