| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 | import osimport pytestfrom blogit.blogit import CONFIG, find_new_posts_and_pages, DataBasefrom blogit.blogit import Entry, Tagfrom tinydb import QueryCONFIG['content_root'] = 'test_root'DB = DataBase(os.path.join(CONFIG['content_root'], 'blogit.db'))Tag.table = DB.tags  # monkey patch Tagtags = ['foo', 'bar', 'baz', 'bug', 'buf']shift = lambda l, n: l[-n:] + l[:-n]post = '''\---title: Blog post {number}author: Famous authorpublished: 2015-01-{number}tags: {tags}public: yeschronological: yeskind: writingsummary: This is a summry of post {number}. Donec id elit non mi porta gravida at eget metus. Fusce dapibus---This is the body of post {number}. 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.This is a snippet in bash```bash$ for i in `seq 1 10`; do   echo $idoneVAR="variable"echo $VAR# This is a very long long long long long long long long long long comment```This is a snippet in python```pythondef yay(top):    for i in range(1, top+1):            yield ifor i in yay:    print(i)```'''try:    os.mkdir(CONFIG['content_root'])except OSError:    passshift_factors = map(lambda x: (x - 1) / 5 +1,   range(1,21))f = open((os.path.join(CONFIG['content_root'],                       'page.md')), 'w')f.write("""\---title: example pagepublic: yeskind: pagetemplate: about.html---# some headingcontent paragraph## heading 2some more content""")f.close()def write_file(i):    f = open((os.path.join(CONFIG['content_root'],                           'post{}.md'.format(i))), 'w')    f.write(post.format(**{'number': i,                           'tags': ','.join(shift(tags, shift_factors[i-1])[:-1])}))[write_file(i) for i in range(1, 21)]def test_find_new_posts_and_pages():    entries = [e for e in find_new_posts_and_pages(DB)]    pages = [e[1] for e in entries if str(e[1]).endswith('page.md')]    assert pages is not None    assert len(DB.posts.all()) == 20def test_tags():    entries = map(Entry.entry_from_db, [e.get('filename') for e in                  DB.posts.all()])    tags = DB.tags.all()    t = entries[0].tags    assert len(t) == 4    assert t[0].name == u'buf'    new_tag = Tag('buggg')    new_tag.posts = [100,100]    with pytest.raises(ValueError):        new_tag.posts = "This should not work"    with pytest.raises(ValueError):        new_tag.posts = 1  # This should not eitherdef test_slug():    t = Tag('foo:bar')    assert t.slug == "foo-bar"    t = Tag('foo:;bar,.,baz')    assert t.slug == "foo-bar-baz"def test_tag_posts():    example = Tag('example')    example.posts = [1,2,3]    assert [1,2,3] == example.posts    Filter = Query()    t = DB.tags.get(Filter.post_ids == [1, 2, 3])    assert t['post_ids'] == [1, 2, 3]os.unlink(DB._db._storage._handle.name)
 |