1
0

test_blogit.py 3.5 KB

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