test_blogit2.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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('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.purge_tables()
  49. clean_posts()
  50. create_posts()
  51. import pdb; pdb.set_trace()
  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. import pdb; pdb.set_trace()
  72. create_last_post()
  73. new_build()
  74. # bug: creating a new post with existing tags
  75. # removes older tags ...
  76. #os.unlink('blogit.db')