test_blogit2.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import os
  2. import shutil
  3. from tinydb import Query
  4. from blogit2 import find_new_posts, DataBase, Entry, Tag
  5. from blogit2 import CONFIG, new_build
  6. post_dummy = """title: Blog post {}
  7. author: Famous author
  8. published: 2015-01-1{}
  9. tags: [python, git, bash, linux]
  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. def insert_single(DB):
  19. Posts = Query()
  20. if not DB.posts.contains(Posts.filename == 'post4.md'):
  21. DB.posts.insert({'filename': 'post4.md'})
  22. def create_posts():
  23. os.mkdir('content')
  24. os.chdir('content')
  25. for p in range(1,4):
  26. with open('post'+str(p)+'.md', 'a') as f:
  27. f.write(post_dummy.format(p,p,p))
  28. os.chdir('..')
  29. def clean_posts():
  30. if os.path.exists('content'):
  31. shutil.rmtree('content')
  32. def test_find_new_posts():
  33. clean_posts()
  34. create_posts()
  35. DB = DataBase(os.path.join(CONFIG['content_root'], 'blogit.db'))
  36. DB._db.purge_tables()
  37. insert_single(DB)
  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('bar')
  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. def test_new_build():
  48. DB = DataBase(os.path.join(CONFIG['content_root'], 'blogit.db'))
  49. DB._db.purge_tables()
  50. clean_posts()
  51. create_posts()
  52. new_build()
  53. post_dummy = """title: Blog post {}
  54. author: Famous author
  55. published: 2015-01-16
  56. tags: [python, git, foo]
  57. public: yes
  58. chronological: yes
  59. kind: writing
  60. summary: |
  61. This is a summry of post {}
  62. ...
  63. This is the body of post {}
  64. """
  65. def create_last_post():
  66. os.chdir('content')
  67. with open('post'+str(5)+'.md', 'a') as f:
  68. f.write(post_dummy.format(5,5,5))
  69. os.chdir('..')
  70. def test_new_build2():
  71. create_last_post()
  72. new_build()
  73. # bug: creating a new post with existing tags
  74. # removes older tags ...
  75. try:
  76. os.unlink('blogit.db')
  77. except OSError:
  78. pass