test_blogit2.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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, new_build
  6. from conf import db
  7. post_dummy = """title: Blog post {}
  8. author: Famous author
  9. published: 2015-01-16
  10. tags: [python, git, bash]
  11. public: yes
  12. chronological: yes
  13. kind: writing
  14. summary: |
  15. This is a summry of post {}
  16. ...
  17. This is the body of post {}
  18. """
  19. def insert_single():
  20. Posts = Query()
  21. if not DB['posts'].contains(Posts.filename == 'post4.md'):
  22. DB['posts'].insert({'filename': 'post4.md'})
  23. def create_posts():
  24. os.mkdir('content')
  25. os.chdir('content')
  26. for p in range(1,4):
  27. with open('post'+str(p)+'.md', 'a') as f:
  28. f.write(post_dummy.format(p,p,p))
  29. os.chdir('..')
  30. def clean_posts():
  31. if os.path.exists('content'):
  32. shutil.rmtree('content')
  33. def test_find_new_posts():
  34. db.purge_tables()
  35. insert_single()
  36. clean_posts()
  37. create_posts()
  38. new = list(find_new_posts(DB['posts']))
  39. assert len(DB['posts'].all()) == 4
  40. assert len(new) == 3
  41. def test_tags():
  42. t = Tag('foo')
  43. t.posts = [1]
  44. assert t.posts == [1]
  45. t.posts = [1,3,4,5]
  46. assert t.posts == [1,3,4,5]
  47. db.purge_tables()
  48. def test_new_build():
  49. clean_posts()
  50. create_posts()
  51. new_build()
  52. post_dummy = """title: Blog post {}
  53. author: Famous author
  54. published: 2015-01-16
  55. tags: [python, git, foo]
  56. public: yes
  57. chronological: yes
  58. kind: writing
  59. summary: |
  60. This is a summry of post {}
  61. ...
  62. This is the body of post {}
  63. """
  64. def create_last_post():
  65. os.chdir('content')
  66. with open('post'+str(5)+'.md', 'a') as f:
  67. f.write(post_dummy.format(5,5,5))
  68. os.chdir('..')
  69. def test_new_build2():
  70. create_last_post()
  71. new_build()
  72. # bug: creating a new post with existing tags
  73. # removes older tags ...
  74. #os.unlink('blogit.db')