Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 117 lines (79 sloc) 3.176 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
# -*- coding: utf-8 -*-

try:
    import unittest2 as unittest
except ImportError:
    import unittest

import tempfile

from datetime import datetime

from acrylamid import log, errors
from acrylamid.errors import AcrylamidException

from acrylamid.readers import Entry
from acrylamid.helpers import escape
from acrylamid.defaults import conf

log.init('acrylamid', level=40)

def create(path, **kwargs):

    with open(path, 'w') as fp:
        fp.write('---\n')
        for k, v in kwargs.iteritems():
            if isinstance(v, basestring):
                v = escape(v)
            fp.write('%s: %s\n' % (k, v))
        fp.write('---\n')


class TestEntry(unittest.TestCase):

    @classmethod
    def setup_class(self):
        fd, path = tempfile.mkstemp(suffix='.txt')
        self.path = path

    def test_dates(self):

        create(self.path, date='13.02.2011, 15:36', title='bla')
        date = Entry(self.path, conf).date

        assert date.year == 2011
        assert date.month == 2
        assert date.day == 13
        assert date == datetime(year=2011, month=2, day=13, hour=15, minute=36)

    def test_alternate_dates(self):

        create(self.path, date='1.2.2034', title='bla')
        date = Entry(self.path, conf).date

        assert date.year == 2034
        assert date.month == 2
        assert date.day == 1
        assert date == datetime(year=2034, month=2, day=1)

    def test_invalid_dates(self):

        create(self.path, date='unparsable', title='bla')
        with self.assertRaises(AcrylamidException):
            Entry(self.path, conf).date

    def test_permalink(self):

        create(self.path, title='foo')
        entry = Entry(self.path, conf)

        assert entry.permalink == '/2012/foo/'

        create(self.path, title='foo', permalink='/hello/world/')
        entry = Entry(self.path, conf)

        assert entry.permalink == '/hello/world/'

        create(self.path, title='foo', permalink_format='/:year/:slug/index.html')
        entry = Entry(self.path, conf)

        assert entry.permalink == '/2012/foo/'

    def test_tags(self):

        create(self.path, title='foo', tags='Foo')
        assert Entry(self.path, conf).tags == ['Foo']

        create(self.path, title='foo', tags='[Foo, Bar]')
        assert Entry(self.path, conf).tags == ['Foo', 'Bar']

    def test_deprecated_keys(self):

        create(self.path, title='foo', tag=None, filter=None)
        entry = Entry(self.path, conf)

        assert 'tags' in entry
        assert 'filters' in entry

    def test_custom_values(self):

        create(self.path, title='foo', image='/img/test.png')
        entry = Entry(self.path, conf)

        assert 'image' in entry
        assert entry.image == '/img/test.png'

    def test_fallbacks(self):

        create(self.path, title='Bla')
        entry = Entry(self.path, conf)

        assert entry.draft == False
        assert entry.email == 'info@example.com'
        assert entry.author == 'Anonymous'
        assert entry.extension == 'txt'
        assert entry.year == datetime.now().year
        assert entry.month == datetime.now().month
        assert entry.day == datetime.now().day
Something went wrong with that request. Please try again.