test_blogit2.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import os
  2. import shutil
  3. from tinydb import Query
  4. from blogit2 import find_new_posts, DB, Entry, Tag
  5. from blogit2 import CONFIG
  6. post_dummy = """title: Blog post {}
  7. author: Famous author
  8. published: 2015-01-16
  9. tags: [python, git, bash]
  10. public: yes
  11. chronological: yes
  12. kind: writing
  13. summary: |
  14. This is a summry of post {}
  15. ...
  16. This is the body of post {}
  17. """
  18. Posts = Query()
  19. if not DB['posts'].contains(Posts.filename == 'post4.md'):
  20. DB['posts'].insert({'filename': 'post4.md'})
  21. def create_posts():
  22. os.mkdir('content')
  23. os.chdir('content')
  24. for p in range(1,4):
  25. with open('post'+str(p)+'.md', 'a') as f:
  26. f.write(post_dummy.format(p,p,p))
  27. os.chdir('..')
  28. def clean_posts():
  29. if os.path.exists('content'):
  30. shutil.rmtree('content')
  31. def test_find_new_posts():
  32. clean_posts()
  33. create_posts()
  34. new = list(find_new_posts(DB['posts']))
  35. assert len(DB['posts']) == 4
  36. assert len(new) == 3
  37. def test_tags():
  38. t = Tag('foo')
  39. t.posts = [1]
  40. assert t.posts == [1]
  41. t.posts = [1,3,4,5]
  42. assert t.posts == [1,3,4,5]
  43. os.unlink('blogit.db')