File: live_client_test.py

package info (click to toggle)
python-gdata 2.0.18%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 8,460 kB
  • ctags: 17,143
  • sloc: python: 70,779; ansic: 150; makefile: 27; sh: 3
file content (160 lines) | stat: -rwxr-xr-x 5,831 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
#
# Copyright (C) 2009 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# This module is used for version 2 of the Google Data APIs.
# These tests attempt to connect to Google servers.


__author__ = 'j.s@google.com (Jeff Scudder)'


import unittest
import gdata.blogger.client
import gdata.blogger.data
import gdata.gauth
import gdata.client
import atom.http_core
import atom.mock_http_core
import atom.core
import gdata.data
import gdata.test_config as conf


conf.options.register_option(conf.BLOG_ID_OPTION)


class BloggerClientTest(unittest.TestCase):

  def setUp(self):
    self.client = None
    if conf.options.get_value('runlive') == 'true':
      self.client = gdata.blogger.client.BloggerClient()
      conf.configure_client(self.client, 'BloggerTest', 'blogger')

  def tearDown(self):
    conf.close_client(self.client)

  def test_create_update_delete(self):
    if not conf.options.get_value('runlive') == 'true':
      return
    # Either load the recording or prepare to make a live request.
    conf.configure_cache(self.client, 'test_create_update_delete')

    # Add a blog post.
    created = self.client.add_post(conf.options.get_value('blogid'),
                                   'test post from BloggerClientTest',
                                   'Hey look, another test!',
                                   labels=['test', 'python'])

    self.assertEqual(created.title.text, 'test post from BloggerClientTest')
    self.assertEqual(created.content.text, 'Hey look, another test!')
    self.assertEqual(len(created.category), 2)
    self.assert_(created.control is None)

    # Change the title of the blog post we just added.
    created.title.text = 'Edited'
    updated = self.client.update(created)

    self.assertEqual(updated.title.text, 'Edited')
    self.assert_(isinstance(updated, gdata.blogger.data.BlogPost))
    self.assertEqual(updated.content.text, created.content.text)

    # Delete the test entry from the blog.
    self.client.delete(updated)

  def test_create_draft_post(self):
    if not conf.options.get_value('runlive') == 'true':
      return
    conf.configure_cache(self.client, 'test_create_draft_post')

    # Add a draft blog post.
    created = self.client.add_post(conf.options.get_value('blogid'),
                                   'draft test post from BloggerClientTest',
                                   'This should only be a draft.',
                                   labels=['test2', 'python'], draft=True)

    self.assertEqual(created.title.text,
                     'draft test post from BloggerClientTest')
    self.assertEqual(created.content.text, 'This should only be a draft.')
    self.assertEqual(len(created.category), 2)
    self.assert_(created.control is not None)
    self.assert_(created.control.draft is not None)
    self.assertEqual(created.control.draft.text, 'yes')
    
    # Publish the blog post. 
    created.control.draft.text = 'no'
    updated = self.client.update(created)

    if updated.control is not None and updated.control.draft is not None:
      self.assertNotEqual(updated.control.draft.text, 'yes')
      
    # Delete the test entry from the blog using the URL instead of the entry.
    self.client.delete(updated.find_edit_link())


  def test_create_draft_page(self):
    if not conf.options.get_value('runlive') == 'true':
      return
    conf.configure_cache(self.client, 'test_create_draft_page')

    # List all pages on the blog.
    pages_before = self.client.get_pages(conf.options.get_value('blogid'))

    # Add a draft page to blog.
    created = self.client.add_page(conf.options.get_value('blogid'),
                                   'draft page from BloggerClientTest',
                                   'draft content',
                                   draft=True)

    self.assertEqual(created.title.text, 'draft page from BloggerClientTest')
    self.assertEqual(created.content.text, 'draft content')
    self.assert_(created.control is not None)
    self.assert_(created.control.draft is not None)
    self.assertEqual(created.control.draft.text, 'yes')
    self.assertEqual(str(int(created.get_page_id())), created.get_page_id())

    # List all pages after adding one.
    pages_after = self.client.get_pages(conf.options.get_value('blogid'))
    self.assertEqual(len(pages_before.entry) + 1, len(pages_after.entry))

    # Publish page.
    created.control.draft.text = 'no'
    updated = self.client.update(created)

    if updated.control is not None and updated.control.draft is not None:
      self.assertNotEqual(updated.control.draft.text, 'yes')

    # Delete test page.
    self.client.delete(updated.find_edit_link())
    pages_after = self.client.get_pages(conf.options.get_value('blogid'))

    self.assertEqual(len(pages_before.entry), len(pages_after.entry))


  def test_retrieve_post_with_categories(self):
    if not conf.options.get_value('runlive') == 'true':
      return
    conf.configure_cache(self.client, 'test_retrieve_post_with_categories')
    query = gdata.blogger.client.Query(categories=["news"], strict=True)
    posts = self.client.get_posts(conf.options.get_value('blogid'), query=query)

def suite():
  return conf.build_suite([BloggerClientTest])


if __name__ == '__main__':
  unittest.TextTestRunner().run(suite())