PageRenderTime 30ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/gdata_tests/blogger/live_client_test.py

https://gitlab.com/karambir/gdata
Python | 160 lines | 92 code | 39 blank | 29 comment | 10 complexity | 16f858d7e93a87240a69f283068d5f1d MD5 | raw file
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2009 Google Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. # This module is used for version 2 of the Google Data APIs.
  17. # These tests attempt to connect to Google servers.
  18. __author__ = 'j.s@google.com (Jeff Scudder)'
  19. import unittest
  20. import gdata.blogger.client
  21. import gdata.blogger.data
  22. import gdata.gauth
  23. import gdata.client
  24. import atom.http_core
  25. import atom.mock_http_core
  26. import atom.core
  27. import gdata.data
  28. import gdata.test_config as conf
  29. conf.options.register_option(conf.BLOG_ID_OPTION)
  30. class BloggerClientTest(unittest.TestCase):
  31. def setUp(self):
  32. self.client = None
  33. if conf.options.get_value('runlive') == 'true':
  34. self.client = gdata.blogger.client.BloggerClient()
  35. conf.configure_client(self.client, 'BloggerTest', 'blogger')
  36. def tearDown(self):
  37. conf.close_client(self.client)
  38. def test_create_update_delete(self):
  39. if not conf.options.get_value('runlive') == 'true':
  40. return
  41. # Either load the recording or prepare to make a live request.
  42. conf.configure_cache(self.client, 'test_create_update_delete')
  43. # Add a blog post.
  44. created = self.client.add_post(conf.options.get_value('blogid'),
  45. 'test post from BloggerClientTest',
  46. 'Hey look, another test!',
  47. labels=['test', 'python'])
  48. self.assertEqual(created.title.text, 'test post from BloggerClientTest')
  49. self.assertEqual(created.content.text, 'Hey look, another test!')
  50. self.assertEqual(len(created.category), 2)
  51. self.assert_(created.control is None)
  52. # Change the title of the blog post we just added.
  53. created.title.text = 'Edited'
  54. updated = self.client.update(created)
  55. self.assertEqual(updated.title.text, 'Edited')
  56. self.assert_(isinstance(updated, gdata.blogger.data.BlogPost))
  57. self.assertEqual(updated.content.text, created.content.text)
  58. # Delete the test entry from the blog.
  59. self.client.delete(updated)
  60. def test_create_draft_post(self):
  61. if not conf.options.get_value('runlive') == 'true':
  62. return
  63. conf.configure_cache(self.client, 'test_create_draft_post')
  64. # Add a draft blog post.
  65. created = self.client.add_post(conf.options.get_value('blogid'),
  66. 'draft test post from BloggerClientTest',
  67. 'This should only be a draft.',
  68. labels=['test2', 'python'], draft=True)
  69. self.assertEqual(created.title.text,
  70. 'draft test post from BloggerClientTest')
  71. self.assertEqual(created.content.text, 'This should only be a draft.')
  72. self.assertEqual(len(created.category), 2)
  73. self.assert_(created.control is not None)
  74. self.assert_(created.control.draft is not None)
  75. self.assertEqual(created.control.draft.text, 'yes')
  76. # Publish the blog post.
  77. created.control.draft.text = 'no'
  78. updated = self.client.update(created)
  79. if updated.control is not None and updated.control.draft is not None:
  80. self.assertNotEqual(updated.control.draft.text, 'yes')
  81. # Delete the test entry from the blog using the URL instead of the entry.
  82. self.client.delete(updated.find_edit_link())
  83. def test_create_draft_page(self):
  84. if not conf.options.get_value('runlive') == 'true':
  85. return
  86. conf.configure_cache(self.client, 'test_create_draft_page')
  87. # List all pages on the blog.
  88. pages_before = self.client.get_pages(conf.options.get_value('blogid'))
  89. # Add a draft page to blog.
  90. created = self.client.add_page(conf.options.get_value('blogid'),
  91. 'draft page from BloggerClientTest',
  92. 'draft content',
  93. draft=True)
  94. self.assertEqual(created.title.text, 'draft page from BloggerClientTest')
  95. self.assertEqual(created.content.text, 'draft content')
  96. self.assert_(created.control is not None)
  97. self.assert_(created.control.draft is not None)
  98. self.assertEqual(created.control.draft.text, 'yes')
  99. self.assertEqual(str(int(created.get_page_id())), created.get_page_id())
  100. # List all pages after adding one.
  101. pages_after = self.client.get_pages(conf.options.get_value('blogid'))
  102. self.assertEqual(len(pages_before.entry) + 1, len(pages_after.entry))
  103. # Publish page.
  104. created.control.draft.text = 'no'
  105. updated = self.client.update(created)
  106. if updated.control is not None and updated.control.draft is not None:
  107. self.assertNotEqual(updated.control.draft.text, 'yes')
  108. # Delete test page.
  109. self.client.delete(updated.find_edit_link())
  110. pages_after = self.client.get_pages(conf.options.get_value('blogid'))
  111. self.assertEqual(len(pages_before.entry), len(pages_after.entry))
  112. def test_retrieve_post_with_categories(self):
  113. if not conf.options.get_value('runlive') == 'true':
  114. return
  115. conf.configure_cache(self.client, 'test_retrieve_post_with_categories')
  116. query = gdata.blogger.client.Query(categories=["news"], strict=True)
  117. posts = self.client.get_posts(conf.options.get_value('blogid'), query=query)
  118. def suite():
  119. return conf.build_suite([BloggerClientTest])
  120. if __name__ == '__main__':
  121. unittest.TextTestRunner().run(suite())