예제 #1
0
파일: tests.py 프로젝트: Javex/miniblog
    def test_Entry_init(self):
        from miniblog.models import Entry, Category
        from datetime import datetime

        # Ensure that title and text are enforced
        self.assertRaises(TypeError, Entry)
        self.assertRaises(TypeError, Entry, '')
        self.assertRaises(TypeError, Entry, title='')
        self.assertRaises(TypeError, Entry, text='')

        self.assertIsInstance(Entry('', ''), Entry)

        # Now check that category assignment works
        cat = Category('Test')
        DBSession.add(cat)
        entry = Entry('Test Title', 'Text', 'Test')
        DBSession.add(entry)
        DBSession.flush()
        self.assertEqual(entry.category, cat)

        # Test that entry time is set (test on equality would of course fail)
        self.assertLess((datetime.now() - entry.entry_time).seconds, 1)


        # Check title and text are correctly set on arguments
        entry = Entry('Title', 'Text')
        self.assertEqual(entry.title, 'Title')
        self.assertEqual(entry._text, 'Text')

        entry = Entry(title='Title', text='Text')
        self.assertEqual(entry.title, 'Title')
        self.assertEqual(entry._text, 'Text')
예제 #2
0
파일: views.py 프로젝트: Javex/miniblog
 def manage_categories(self):
     form = CategoryForm(self.request.POST)
     if self.request.method == 'POST':
         category = Category(form.data['name'])
         DBSession.add(category)
         get_categories.invalidate()
         return HTTPFound(location=self.request.route_url('manage_categories'))
     categories = DBSession.query(Category).all()
     return {'form': form, 'categories': categories}
예제 #3
0
파일: views.py 프로젝트: Javex/miniblog
    def add_entry(self):
        entry_id = self.request.matchdict.get("id_", 0)
        if entry_id:
            entry = DBSession.query(Entry).filter(Entry.id == entry_id).one()
            form_data = MultiDict({'title': entry.title, 'text': entry._text,
                                   'category': entry.category_name})
            form_data.update(self.request.session.get("edit_entry_%i_form" % entry.id, {}))
        else:
            entry = None
            form_data = MultiDict(self.request.session.get("add_entry_form", {}))
        form_data.update(self.request.POST)
        form = EntryForm(form_data)
        form.category.choices = [('', ' - None - ')] + [(name[0], name[0]) for name in get_categories()]
        preview = None
        if self.request.method == 'POST':
            if not form.validate():
                for field, errors in form.errors.items():
                    for error in errors:
                        self.request.session.flash('Field "%s" has the following error: "%s"' % (field, error))
                if entry:
                    self.request.session["edit_entry_%i_form" % entry.id] = form.data
                else:
                    self.request.session["add_entry_form"] = form.data
                return HTTPFound(location=self.request.route_url('add_entry'))

            if form.submit.data:
                if "category" in form.data and form.data["category"]:
                    category = DBSession.query(Category)\
                        .filter(Category.name == form.data["category"]).one()
                else:
                    category = None
                if not entry:
                    entry = Entry(form.data["title"], form.data["text"])
                    entry.category = category
                    if "add_entry_form" in self.request.session:
                        del self.request.session["add_entry_form"]
                    DBSession.add(entry)
                else:
                    if "edit_entry_%i_form" \
                            % entry.id in self.request.session:
                        del self.request.session["edit_entry_%i_form"
                                                 % entry.id]
                    entry._text = form.data["text"]
                    entry.title = form.data["title"]
                    entry.category = category
                DBSession.flush()
                get_categories.invalidate()
                get_recent_posts.invalidate()
                return HTTPFound(location=self.request.route_url('view_entry', id_=entry.id))
            if form.preview.data:
                preview = entry
        return {'form': form, 'preview': preview}
예제 #4
0
파일: tests.py 프로젝트: Javex/miniblog
 def setUp(self):
     self.config = testing.setUp()
     from sqlalchemy import create_engine
     engine = create_engine('sqlite://')
     from miniblog.models import Category, Base, DBSession, cache
     from tempfile import mktemp
     try:
         cache.configure_from_config({'cache.backend': 'dogpile.cache.dbm',
                      'cache.arguments.filename': mktemp()},
                     'cache.')
     except Exception:
         pass
     DBSession.configure(bind=engine)
     Base.metadata.create_all(engine)
     with transaction.manager:
         for i in range(3):
             category = Category('Category {0}'.format(i))
             DBSession.add(category)
예제 #5
0
파일: tests.py 프로젝트: Javex/miniblog
    def test_Category_entries(self):
        from miniblog.models import Category, Entry
        cat = Category('Test')
        entry1 = Entry('Test 1', '...')
        entry2 = Entry('Test 2', '...')

        cat.entries.append(entry1)
        cat.entries.append(entry2)
        DBSession.add(cat)
        DBSession.flush()

        inserted_entry = DBSession.\
            query(Entry).\
            filter(Entry.title == 'Test 1').\
            one()

        self.assertEqual(inserted_entry, entry1)
        self.assertEqual(inserted_entry.category, cat)
예제 #6
0
파일: tests.py 프로젝트: Javex/miniblog
    def test_get_recent_posts(self):
        from miniblog.models import get_recent_posts, Entry
        from time import sleep
        # test with not enough entries
        DBSession.add(Entry('Title 1', 'Text 1'))
        self.assertEqual(len(get_recent_posts()), 1)

        # make sure caching is still in place
        # sleep(1)  # activate this when order test fails
        DBSession.add(Entry('Title 2', 'Text 2'))
        self.assertEqual(len(get_recent_posts()), 1)

        # Invalidate and recount
        get_recent_posts.invalidate()
        self.assertEqual(len(get_recent_posts()), 2)

        # Check order is correct
        recent = get_recent_posts()
        first = recent[0].entry_time
        second = recent[1].entry_time
        self.assertGreater(first, second)

        # Check count param:
        self.assertEqual(len(get_recent_posts(1)), 1)

        for i in range(3, 9):
            DBSession.add(Entry('Title {0}'.format(i),
                                'Text {0}'.format(i)))
        get_recent_posts.invalidate()
        self.assertEqual(len(get_recent_posts()), 7)