def test_post_detail(self): with mock.patch('views.CACHE_DB_FILE', self.cache_file): conn = sqlite3.connect(self.cache_file) post = Post(title="Hello", slug="hello", content="<h1>Hello</h1>", last_update=datetime.now()) db_initialize(conn) db_save_post(conn, post) response = views.view_post(post.slug) self.assertTrue(post.title in response) self.assertTrue(post.content in response)
def test_db(self): conn = sqlite3.connect(':memory:') db_initialize(conn) results = conn.execute('SELECT name FROM sqlite_master WHERE type = "table"').fetchall() self.assertEqual(len(results), 1) self.assertEqual(results[0][0], 'posts') post = views.Post(id=None, title='Hello', content="<h1>Good<h1>", slug="hello-world", last_update=datetime.now()) db_save_post(conn, post) saved_post = db_get_post(conn, slug='hello-world') self.assertEqual(post.title, 'Hello') self.assertEqual(type(saved_post.last_update), datetime) # Save twice self.assertEqual(len(db_list_post(conn)), 1) self.assertRaises(IntegrityError, lambda: db_save_post(conn, post)) post.slug = db_ensure_unique(conn, 'slug', post.slug) db_save_post(conn, post) self.assertEqual(len(db_list_post(conn)), 2) self.assertEqual(post.slug, "hello-world-2") saved_post.title = "New Title" db_save_post(conn, saved_post) self.assertEqual(len(db_list_post(conn)), 2) self.assertEqual(db_get_post(conn, slug='hello-world').title, "New Title") # test db_list_post with filters self.assertEqual(len(db_list_post(conn, is_published=1)), 0) saved_post.is_published = True db_save_post(conn, saved_post) self.assertEqual(len(db_list_post(conn)), 2) self.assertEqual(len(db_list_post(conn, is_published=1)), 1) # test db_list_post order by rs = db_list_post(conn, order_by="id") self.assertEqual(rs[0].id, 1) rs = db_list_post(conn, order_by="-id") self.assertEqual(rs[0].id, 2)