Пример #1
0
 def test_create(self):
     # assert default post added
     self.assertEqual(1, len(Post.query.all()))
     # assert correct post values
     q = Post.query.get(self.post.id)
     self.assertEqual(self.title, q.title)
     self.assertEqual(self.body, q.body)
     slug = slugify(datetime.utcnow(), self.title)
     self.assertEqual(slug, q.slug)
     self.assertEqual(self.author_id, q.author_id)
     # assert unique for same date
     db.session.add(Post(self.title, self.body, self.author_id))
     self.assertRaises(IntegrityError, db_commit)
     # assert not unique for diffrent date
     now = datetime.utcnow()
     latest_post = Post(self.title, self.body, self.author_id)
     latest_post.published = datetime(now.year - 1, now.month, now.day)
     latest_post.edit(self.title, self.body)
     db.session.add(latest_post)
     db.session.commit()
Пример #2
0
def filldb():
    print('Filling database...'),
    from app.models import User, Post

    admin = User(u'admin', u'admin')
    db.session.add(admin)
    db.session.commit()

    post = Post(
        title=u'Hello, world!',
        body=open('fill/p1.txt', 'r').read(),
        author_id=admin.id
    )
    db.session.add(post)
    db.session.commit()
    post.published = datetime(2011, 06, 13)
    post.edit(post.title, post.body)
    db.session.commit()
    post = Post(
        title=u'Random Words 1',
        body=open('fill/p5.txt', 'r').read(),
        author_id=admin.id
    )
    db.session.add(post)
    db.session.commit()
    post.published = datetime(2012, 8, 15)
    post.edit(post.title, post.body)
    db.session.commit()
    post = Post(
        title=u'Random Words 2',
        body=open('fill/p2.txt', 'r').read(),
        author_id=admin.id
    )
    db.session.add(post)
    db.session.commit()
    post.published = datetime(2012, 12, 24)
    post.edit(post.title, post.body)
    db.session.commit()
    post = Post(
        title=u'Picture of a cat!',
        body=open('fill/p3.txt', 'r').read(),
        author_id=admin.id
    )
    db.session.add(post)
    db.session.commit()
    post = Post(
        title=u'Some code',
        body=open('fill/p4.txt', 'r').read(),
        author_id=admin.id
    )
    db.session.add(post)
    db.session.commit()
    post = Post(
        title=u'Random Words 3',
        body=open('fill/p5.txt', 'r').read(),
        author_id=admin.id
    )
    db.session.add(post)
    db.session.commit()
    print('done!')