1
0

test_blogit.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import os
  2. import shutil
  3. from tinydb import Query
  4. from blogit.blogit import find_new_posts, DataBase, Entry, Tag
  5. from blogit.blogit 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 {}. Donec id elit non mi porta gravida at eget metus. Fusce dapibus
  15. ...
  16. This is the body of post {}. 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.
  17. This is a snippet in bash
  18. ```bash
  19. $ for i in `seq 1 10`; do
  20. echo $i
  21. done
  22. VAR="variable"
  23. echo
  24. ```
  25. This is a snippet in python
  26. ```python
  27. def yay(top):
  28. for i in range(1, top+1):
  29. yield i
  30. for i in yay:
  31. print(i)
  32. ```
  33. This the last line of the post.
  34. """
  35. def insert_single(DB):
  36. Posts = Query()
  37. if not DB.posts.contains(Posts.filename == 'post4.md'):
  38. DB.posts.insert({'filename': 'post4.md'})
  39. def create_posts():
  40. os.mkdir('content')
  41. os.chdir('content')
  42. for p in range(1,4):
  43. with open('post'+str(p)+'.md', 'a') as f:
  44. f.write(post_dummy.format(p,p,p,p))
  45. os.chdir('..')
  46. def clean_posts():
  47. if os.path.exists('content'):
  48. shutil.rmtree('content')
  49. def test_find_new_posts():
  50. clean_posts()
  51. create_posts()
  52. DB = DataBase(os.path.join(CONFIG['content_root'], 'blogit.db'))
  53. DB._db.purge_tables()
  54. insert_single(DB)
  55. new = list(find_new_posts(DB.posts))
  56. assert len(DB.posts.all()) == 4
  57. assert len(new) == 3
  58. def test_tags():
  59. t = Tag('bar')
  60. t.posts = [1]
  61. assert t.posts == [1]
  62. t.posts = [1,3,4,5]
  63. assert t.posts == [1,3,4,5]
  64. def test_new_build():
  65. DB = DataBase(os.path.join(CONFIG['content_root'], 'blogit.db'))
  66. DB._db.purge_tables()
  67. clean_posts()
  68. create_posts()
  69. new_build()
  70. post_dummy = """title: Blog post {}
  71. author: Famous author
  72. published: 2015-01-1{}
  73. tags: [python, git, bash, linux]
  74. public: yes
  75. chronological: yes
  76. kind: writing
  77. summary: |
  78. This is a summry of post {}. Donec id elit non mi porta gravida at eget metus. Fusce dapibus
  79. ...
  80. This is the body of post {}. 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.
  81. This is a snippet in bash
  82. ```bash
  83. $ for i in `seq 1 10`; do
  84. echo $i
  85. done
  86. VAR="variable"
  87. echo $VAR
  88. ```
  89. This is a snippet in python
  90. ```python
  91. def yay(top):
  92. for i in range(1, top+1):
  93. yield i
  94. for i in yay:
  95. print(i)
  96. ```
  97. """
  98. def create_last_post():
  99. os.chdir('content')
  100. with open('post'+str(5)+'.md', 'a') as f:
  101. f.write(post_dummy.format(5,5,5,5))
  102. os.chdir('..')
  103. def test_new_build2():
  104. create_last_post()
  105. new_build()
  106. try:
  107. os.unlink('blogit.db')
  108. except OSError:
  109. pass