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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#!/usr/bin/env python
"""
Test code for:
WordPress xml-rpc client library
use MovableType API
Copyright (c) 2011 Charles-Axel Dein
Copyright (c) 2005 Michele Ferretti
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
__author__ = "Charles-Axel Dein <ca@d3in.org>"
__version__ = "$Revision: 1.1 $"
__date__ = "$Date: 2011/03/03 $"
__copyright__ = "Copyright (c) 2005 Michele Ferretti, 2011 Charles-Axel Dein"
__license__ = "LGPL"
import unittest
import wordpresslib
import time
class TestWordPressClient(unittest.TestCase):
def setUp(self):
self.wp = wordpresslib.WordPressClient('http://www.site.com/wordpress/xmlrpc.php', 'username', 'password')
self.wp.selectBlog(0)
self.recentPosts = 10
def ifEmpty(self, obj, msg):
self.failUnless( '%s is None' % msg)
self.failIf( obj == '', '%s is empty' % msg)
def testGetUserInfo(self):
user = self.wp.getUserInfo()
self.ifEmpty(user, 'user')
self.ifEmpty(user.id, 'user id')
def testGetUsersBlogs(self):
blogs = self.wp.getUsersBlogs()
self.failUnless( blogs, 'blogs is None')
for blog in blogs:
self.failUnless( blog, 'blog is None')
self.ifEmpty(blog.id, 'blog id')
self.ifEmpty(blog.url, 'blog url')
def testGetRecentPosts(self):
for post in self.wp.getRecentPosts(self.recentPosts):
self.ifEmpty(post, 'post')
self.ifEmpty(post.id, 'post id')
def testGetCategoryList(self):
categories = tuple(self.wp.getCategoryList())
self.failUnless( categories, 'blog categories are None')
for i in categories:
self.ifEmpty(i, 'category item')
self.ifEmpty(i.id, 'category id')
self.ifEmpty(i.name, 'category name')
def testNewPost(self):
categories = tuple(self.wp.getCategoryList())
p = wordpresslib.WordPressPost()
p.title = 'Test'
p.description = u'Questo \xe8 una stringa unicode'
p.categories = (self.wp.getCategoryIdFromName('Progetti'),)
idNewPost = self.wp.newPost(p, True)
self.ifEmpty(idNewPost, 'new post id')
self.failIf( idNewPost < 1 , 'new post id is less than 0')
result = self.wp.deletePost(idNewPost)
self.failUnless( result, 'deletePost %d failed, return=%s' % (idNewPost, str(result)) )
def testGetPost(self):
# gest last post
lastPost = self.wp.getLastPost()
self.failUnless( lastPost, 'last post is None')
for i in range(lastPost.id, 0,-1):
try:
post = self.wp.getPost(i)
except Exception, ex:
self.fail('post id = %d eccezione %s' % (i,ex))
self.ifEmpty( post, 'post')
self.ifEmpty( post.id, 'post id')
self.ifEmpty( post.title, 'post title')
self.ifEmpty( post.permaLink, 'post permaLink')
self.ifEmpty( post.description, 'post description')
self.ifEmpty( post.user, 'post user')
self.ifEmpty( post.date, 'post date')
self.ifEmpty( post.categories, 'post categories')
def testGetNotFoundPost(self):
# gest not found post
try:
post = self.wp.getPost(10000000)
except Exception, ex:
self.failIf(ex.id != 404, 'Post not found response is not 404')
def testGetPostCategories(self):
lastPost = self.wp.getLastPost()
self.failUnless( lastPost, 'last post is None')
i = 0
for cat in self.wp.getPostCategories(lastPost.id):
if i == 0:
self.failIf( cat.isPrimary != 1, 'first post category is not isPrimary')
self.ifEmpty( cat, 'post category')
self.ifEmpty( cat.name, 'post category name')
i += 1
def testDeletePost(self):
lastPost = self.wp.getLastPost()
self.failUnless( lastPost, 'last post is None')
result = self.wp.deletePost(lastPost.id)
self.failUnless( result, 'deletePost %d failed, return=%s' % (lastPost.id, str(result)) )
def testGetLastPost(self):
lastPost = self.wp.getLastPost()
self.failUnless( lastPost, 'last post is None')
self.ifEmpty( lastPost.id, 'post id')
self.ifEmpty( lastPost.title, 'post title')
self.ifEmpty( lastPost.permaLink, 'post permaLink')
self.ifEmpty( lastPost.description, 'post description')
self.ifEmpty( lastPost.user, 'post user')
self.ifEmpty( lastPost.date, 'post date')
self.ifEmpty( lastPost.categories, 'post categories')
def testEditPost(self):
categories = tuple(self.wp.getCategoryList())
lastPost = self.wp.getLastPost()
self.failUnless( lastPost, 'last post is None')
p = wordpresslib.WordPressPost()
p.title = lastPost.title +' (modificato)'
p.date = time.time()
p.description = lastPost.description + u'<br><br><em>Questa \xe8 una stringa unicode modificata</em>'
p.categories = map(self.wp.getCategoryIdFromName, ('Python', 'Progetti'))
self.wp.editPost(lastPost.id, p, True)
def testPublishPost(self):
lastPost = self.wp.getLastPost()
self.failUnless( lastPost, 'last post is None')
result = self.wp.publishPost(lastPost.id)
self.failUnless( result, 'publishPost %d failed, return=%s' % (lastPost.id, str(result)) )
def testNewMediaObject(self):
result = self.wp.newMediaObject('python.jpg')
self.failUnless( result, 'newMediaObject result is None')
if __name__ == '__main__':
unittest.main()
"""
suite = unittest.makeSuite(TestWordPressClient)
unittest.TextTestRunner(verbosity=2).run(suite)
"""
|