class WordPressTest(unittest.TestCase): def setUp(self): self.blog_url = os.environ.get('WORDPRESS_BLOG_URL', 'http://localhost:8888/wordpress') self.http = httplib2.Http() self.wp = WordPress(self.blog_url, cache=None) def check_response(self, result, url): "Check that the parsed result matches the given URL" r, c = self.http.request(url) c = json.loads(c) self.assertEqual(c, result) def test_get_info(self): "Test basic info" result = self.wp.info() self.check_response(result, '%s/?dev=1&json=info' % self.blog_url) def test_get_recent_posts(self): "Test getting recent posts" result = self.wp.get_recent_posts() self.check_response(result, '%s/api/get_recent_posts/?dev=1' % self.blog_url) def test_get_post(self): """ Test getting a single post """ # don't assume there's a post with ID 1 # this assumes get_recent_posts works, will raise KeyError if not results = self.wp.get_recent_posts(count=3) for post in results['posts']: ID = post['id'] post = self.wp.get_post(id=ID) self.check_response( post, '%s/?json=get_post&id=%s&dev=1' % (self.blog_url, ID)) def test_get_categories(self): """ Test getting all active categories """ result = self.wp.get_category_index() self.check_response( result, '%s/?json=get_category_index&dev=1' % self.blog_url) def test_bad_method(self): """ Trying to call something that isn't a real method should raise AttributeError """ # for the record, this is an odd way to test this with self.assertRaises(AttributeError): self.wp.do_something_bad def test_proxy(self): """ WordPress should return the JSON version of any path. """ result = self.wp.proxy('/') self.check_response(result, '%s/?json=1' % self.blog_url)
class WordPressTest(unittest.TestCase): def setUp(self): self.blog_url = os.environ.get('WORDPRESS_BLOG_URL', 'http://localhost:8888/wordpress') self.http = httplib2.Http() self.wp = WordPress(self.blog_url, cache=None) def check_response(self, result, url): "Check that the parsed result matches the given URL" r, c = self.http.request(url) c = json.loads(c) self.assertEqual(c, result) def test_get_info(self): "Test basic info" result = self.wp.info() self.check_response(result, '%s/?dev=1&json=info' % self.blog_url) def test_get_recent_posts(self): "Test getting recent posts" result = self.wp.get_recent_posts() self.check_response(result, '%s/api/get_recent_posts/?dev=1' % self.blog_url) def test_get_post(self): """ Test getting a single post """ # don't assume there's a post with ID 1 # this assumes get_recent_posts works, will raise KeyError if not results = self.wp.get_recent_posts(count=3) for post in results['posts']: ID = post['id'] post = self.wp.get_post(id=ID) self.check_response(post, '%s/?json=get_post&id=%s&dev=1' % (self.blog_url, ID)) def test_get_categories(self): """ Test getting all active categories """ result = self.wp.get_category_index() self.check_response(result, '%s/?json=get_category_index&dev=1' % self.blog_url) def test_bad_method(self): """ Trying to call something that isn't a real method should raise AttributeError """ # for the record, this is an odd way to test this with self.assertRaises(AttributeError): self.wp.do_something_bad def test_proxy(self): """ WordPress should return the JSON version of any path. """ result = self.wp.proxy('/') self.check_response(result, '%s/?json=1' % self.blog_url)
class TestWordPress(unittest.TestCase): def setUp(self): self.wp = WordPress('http://demo.wp-api.org/') def test_get_404(self): with self.assertRaises(Exception): self.wp._get('404') def test_post_404(self): with self.assertRaises(Exception): self.wp._post('404') def test_delete_404(self): with self.assertRaises(Exception): self.wp._delete('404') def test_list_posts(self): posts = self.wp.list_posts() posts.ids() def test_list_posts_after(self): posts = self.wp.list_posts(after=datetime.datetime.now()) self.assertFalse(posts) def test_list_posts_before(self): posts = self.wp.list_posts(before=datetime.datetime.now()) self.assertTrue(posts) def test_list_posts_context(self): with self.assertRaises(ValueError): self.wp.list_posts(context='test') def test_list_posts_order(self): with self.assertRaises(ValueError): self.wp.list_posts(order='test') def test_list_posts_orderby(self): with self.assertRaises(ValueError): self.wp.list_posts(orderby='test') def test_list_posts_status(self): with self.assertRaises(ValueError): self.wp.list_posts(status='test') def test_get_post(self): post = self.wp.get_post(470) post.id self.assertTrue(post) def test_list_categories(self): categories = self.wp.list_categories() self.assertTrue(categories)
class WordPressTest(unittest.TestCase): def setUp(self): self.blog_url = "http://localhost:8888/wordpress" self.wp = WordPress(self.blog_url, cache=None) def check_response(self, result, url): "Check that the parsed result matches the given URL" r, c = http.request(url) c = json.loads(c) self.assertEqual(c, result) def test_get_info(self): "Test basic info" result = self.wp.info() self.check_response(result, 'http://localhost:8888/wordpress/?dev=1&json=info') def test_get_recent_posts(self): "Test getting recent posts" result = self.wp.get_recent_posts() self.check_response(result, 'http://localhost:8888/wordpress/api/get_recent_posts/?dev=1') def test_get_post(self): """Test getting a single post""" result = self.wp.get_post(id=1) self.check_response(result, 'http://localhost:8888/wordpress/?json=get_post&id=1&dev=1') def test_get_categories(self): """Test getting all active categories""" result = self.wp.get_category_index() self.check_response(result, 'http://localhost:8888/wordpress/?json=get_category_index&dev=1') def test_bad_method(self): """Trying to call something that isn't a real method should raise AttributeError""" # for the record, this is an odd way to test this try: self.wp.do_something_bad self.fail() except AttributeError: pass