Browse Source

fix more bugs, add more testings

Oz N Tiram 9 years ago
parent
commit
8b9a4688c9
2 changed files with 48 additions and 18 deletions
  1. 26 13
      blogit/blogit.py
  2. 22 5
      tests/test_all.py

+ 26 - 13
blogit/blogit.py

@@ -72,6 +72,12 @@ class Tag(object):
         if not tag:
             self.table.insert({'name': self.name, 'post_ids': []})
 
+    def __str__(self):
+        return self.name
+
+    def __repr__(self):
+        return self.name
+
     @property
     def slug(self):
         _slug = self.name.lower()
@@ -109,19 +115,19 @@ class Tag(object):
             post = self.db.posts.get(eid=id)
             if not post:
                  raise ValueError("no post found for eid %s" % id)
-            yield Entry(post['filename'])
+            yield Entry(os.path.join(CONFIG['content_root'], post['filename']))
 
     def render(self):
         """Render html page and atom feed"""
-
         context = GLOBAL_TEMPLATE_CONTEXT.copy()
         context['tag'] = self
         context['entries'] = _sort_entries(self.entries)
 
         # render html page
-        _render(context, 'tag_index.html',
-                os.path.join(CONFIG['output_to'], 'tags', self.slug, render_to,
-                                 'index.html'))
+        render_to = os.path.join(CONFIG['output_to'], 'tags', self.slug)
+        if not os.path.exists(render_to):
+            os.makedirs(render_to)
+        _render(context, 'tag_index.html', os.path.join(render_to, 'index.html'))
         # render atom.xml
         context['entries'] = context['entries'][:10]
         _render(context, 'atom.xml', os.path.join(render_to, 'atom.xml'))
@@ -129,6 +135,7 @@ class Tag(object):
 
 
 class Entry(object):
+
     """This is the base class for creating an HTML page from a Markdown
     based page.
 
@@ -168,6 +175,8 @@ class Entry(object):
         This is the body of post 1. Donec id elit non mi porta gravida
     """
 
+    db = DB
+
     @classmethod
     def entry_from_db(kls, filename):
         f = os.path.join(filename)
@@ -175,7 +184,8 @@ class Entry(object):
 
     def __init__(self, path):
         self._path = path
-        self.path = path.split(CONFIG['content_root'])[-1]
+        self.path = path.split(CONFIG['content_root'])[-1].lstrip('/')
+        self.id = None  # this is set inside prepare()
         self.prepare()
 
     def __str__(self):
@@ -230,7 +240,9 @@ class Entry(object):
     def tags(self):
         """this property is always called after prepare"""
         if 'tags' in self.header:
-            return [Tag(t) for t in self.header['tags']]
+            tags = [Tag(t) for t in self.header['tags']]
+            map(lambda t: setattr(t, 'posts', [self.id]), tags)
+            return tags
         else:
             return []
 
@@ -253,6 +265,12 @@ class Entry(object):
             except AttributeError:
                 pass
 
+        if self.header['kind'] == 'writing':
+            _id = Entry.db.posts.insert({'filename': self.path})
+        elif self.header['kind'] == 'page':
+            _id = Entry.db.pages.insert({'filename': self.path})
+        self.id = _id
+
     def render(self):
         if not self.header['public']:
             return False
@@ -312,12 +330,7 @@ def find_new_posts_and_pages(db):
                 if not db.posts.contains(Q.filename == fullpath) and \
                         not db.pages.contains(Q.filename == fullpath):
                     e = Entry(fullpath)
-                    if e.header['kind'] == 'writing':
-                        post_id = db.posts.insert({'filename': fullpath})
-                        yield post_id, e
-                    if e.header['kind'] == 'page':
-                        page_id = db.pages.insert({'filename': fullpath})
-                        yield page_id, e
+                    yield e, e.id
 
 
 def _get_last_entries():

+ 22 - 5
tests/test_all.py

@@ -1,21 +1,31 @@
 import os
 
 import pytest
+from tinydb import Query
 
 from blogit.blogit import CONFIG, find_new_posts_and_pages, DataBase
 from blogit.blogit import Entry, Tag
-from tinydb import Query
-CONFIG['content_root'] = 'test_root'
 import blogit.blogit as m
+
+
+CONFIG['content_root'] = 'test_root'
 db_name = os.path.join(CONFIG['content_root'], 'blogit.db')
+
+
 if os.path.exists(db_name):
-    os.unlink(db_name)
+    import shutil
+    shutil.rmtree(CONFIG['content_root'])
+
+if not os.path.exists(CONFIG['content_root']):
+    os.mkdir(CONFIG['content_root'])
+
 DB = DataBase(os.path.join(CONFIG['content_root'], 'blogit.db'))
 
 # monkey patch to local DB
 m.DB = DB
 Tag.table = DB.tags
 Tag.db = DB
+Entry.db = DB
 
 tags = ['foo', 'bar', 'baz', 'bug', 'buf']
 
@@ -100,14 +110,14 @@ def write_file(i):
 def test_find_new_posts_and_pages():
     entries = [e for e in find_new_posts_and_pages(DB)]
     assert len(entries)
-    pages = [e[1] for e in entries if str(e[1]).endswith('page.md')]
+    pages = [e[1] for e in entries if str(e[0]).endswith('page.md')]
     assert len(pages)
 
     assert len(DB.posts.all()) == 20
 
 
 def test_tags():
-    entries = map(Entry.entry_from_db, [e.get('filename') for e in
+    entries = map(Entry.entry_from_db, [os.path.join(CONFIG['content_root'], e.get('filename')) for e in
                   DB.posts.all()])
     tags = DB.tags.all()
 
@@ -152,5 +162,12 @@ def test_tag_entries():
     entries = list(tf.entries)
     assert len(entries)
 
+def test_tag_render():
 
+    p = DB.posts.get(eid=1)
+    entry = Entry.entry_from_db(os.path.join(CONFIG['content_root'], p.get('filename')))
+    tags = entry.tags
+    assert map(str, tags) == ['buf', 'foo', 'bar', 'baz']
+    assert tags[0].render()
+    assert len(list(tags[0].entries))