File: service_test.py

package info (click to toggle)
python-gdata 2.0.8-1.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 17,816 kB
  • ctags: 29,744
  • sloc: python: 50,599; ansic: 150; makefile: 5
file content (195 lines) | stat: -rwxr-xr-x 8,108 bytes parent folder | download | duplicates (3)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/python
#
# Copyright 2009 Google Inc. All Rights Reserved.
#
# 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.

__author__ = 'api.eric@google.com (Eric Bidelman)'

import getpass
import unittest
from gdata import test_data
import gdata.health
import gdata.health.service

username = ''
password = ''

class HealthQueryProfileListTest(unittest.TestCase):

  def setUp(self):
    self.health = gdata.health.service.HealthService()
    self.health.ClientLogin(username, password, source='Health Client Unit Tests')
    self.profile_list_feed = self.health.GetProfileListFeed()

  def testGetProfileListFeed(self):
    self.assert_(isinstance(self.profile_list_feed,
                            gdata.health.ProfileListFeed))
    self.assertEqual(self.profile_list_feed.id.text,
                     'https://www.google.com/health/feeds/profile/list')
    first_entry = self.profile_list_feed.entry[0]
    self.assert_(isinstance(first_entry, gdata.health.ProfileListEntry))
    self.assert_(first_entry.GetProfileId() is not None)
    self.assert_(first_entry.GetProfileName() is not None)

    query = gdata.health.service.HealthProfileListQuery()
    profile_list = self.health.GetProfileListFeed(query)
    self.assertEqual(first_entry.GetProfileId(),
                     profile_list.entry[0].GetProfileId())
    self.assertEqual(profile_list.id.text,
                     'https://www.google.com/health/feeds/profile/list')


class H9QueryProfileListTest(unittest.TestCase):

  def setUp(self):
    self.h9 = gdata.health.service.HealthService(use_h9_sandbox=True)
    self.h9.ClientLogin(username, password, source='H9 Client Unit Tests')
    self.profile_list_feed = self.h9.GetProfileListFeed()

  def testGetProfileListFeed(self):
    self.assert_(isinstance(self.profile_list_feed,
                            gdata.health.ProfileListFeed))
    self.assertEqual(self.profile_list_feed.id.text,
                     'https://www.google.com/h9/feeds/profile/list')
    first_entry = self.profile_list_feed.entry[0]
    self.assert_(isinstance(first_entry, gdata.health.ProfileListEntry))
    self.assert_(first_entry.GetProfileId() is not None)
    self.assert_(first_entry.GetProfileName() is not None)

    query = gdata.health.service.HealthProfileListQuery()
    profile_list = self.h9.GetProfileListFeed(query)
    self.assertEqual(first_entry.GetProfileId(),
                     profile_list.entry[0].GetProfileId())
    self.assertEqual(profile_list.id.text,
                     'https://www.google.com/h9/feeds/profile/list')


class HealthQueryProfileTest(unittest.TestCase):

  def setUp(self):
    self.health = gdata.health.service.HealthService()
    self.health.ClientLogin(username, password, source='Health Client Unit Tests')
    self.profile_list_feed = self.health.GetProfileListFeed()
    self.profile_id = self.profile_list_feed.entry[0].GetProfileId()

  def testGetProfileFeed(self):
    feed = self.health.GetProfileFeed(profile_id=self.profile_id)
    self.assert_(isinstance(feed, gdata.health.ProfileFeed))
    self.assert_(isinstance(feed.entry[0].ccr, gdata.health.Ccr))

  def testGetProfileFeedByQuery(self):
    query = gdata.health.service.HealthProfileQuery(
        projection='ui', profile_id=self.profile_id)
    feed = self.health.GetProfileFeed(query=query)
    self.assert_(isinstance(feed, gdata.health.ProfileFeed))
    self.assert_(feed.entry[0].ccr is not None)

  def testGetProfileDigestFeed(self):
    query = gdata.health.service.HealthProfileQuery(
        projection='ui', profile_id=self.profile_id,
        params={'digest': 'true'})
    feed = self.health.GetProfileFeed(query=query)
    self.assertEqual(len(feed.entry), 1)

  def testGetMedicationsAndConditions(self):
    query = gdata.health.service.HealthProfileQuery(
        projection='ui', profile_id=self.profile_id,
        params={'digest': 'true'}, categories=['medication|condition'])
    feed = self.health.GetProfileFeed(query=query)
    self.assertEqual(len(feed.entry), 1)
    if feed.entry[0].ccr.GetMedications() is not None:
      self.assert_(feed.entry[0].ccr.GetMedications()[0] is not None)
    self.assert_(feed.entry[0].ccr.GetConditions()[0] is not None)
    self.assert_(feed.entry[0].ccr.GetAllergies() is None)
    self.assert_(feed.entry[0].ccr.GetAlerts() is None)
    self.assert_(feed.entry[0].ccr.GetResults() is None)


class H9QueryProfileTest(unittest.TestCase):

  def setUp(self):
    self.h9 = gdata.health.service.HealthService(use_h9_sandbox=True)
    self.h9.ClientLogin(username, password, source='H9 Client Unit Tests')
    self.profile_list_feed = self.h9.GetProfileListFeed()
    self.profile_id = self.profile_list_feed.entry[0].GetProfileId()

  def testGetProfileFeed(self):
    feed = self.h9.GetProfileFeed(profile_id=self.profile_id)
    self.assert_(isinstance(feed, gdata.health.ProfileFeed))
    self.assert_(feed.entry[0].ccr is not None)

  def testGetProfileFeedByQuery(self):
    query = gdata.health.service.HealthProfileQuery(
        service='h9', projection='ui', profile_id=self.profile_id)
    feed = self.h9.GetProfileFeed(query=query)
    self.assert_(isinstance(feed, gdata.health.ProfileFeed))
    self.assert_(feed.entry[0].ccr is not None)


class HealthNoticeTest(unittest.TestCase):

  def setUp(self):
    self.health = gdata.health.service.HealthService()
    self.health.ClientLogin(username, password, source='Health Client Unit Tests')
    self.profile_list_feed = self.health.GetProfileListFeed()
    self.profile_id = self.profile_list_feed.entry[0].GetProfileId()

  def testSendNotice(self):
    subject_line = 'subject line'
    body = 'Notice <b>body</b>.'
    ccr_xml = test_data.HEALTH_CCR_NOTICE_PAYLOAD
    created_entry = self.health.SendNotice(subject_line,
                                           body,
                                           ccr=ccr_xml,
                                           profile_id=self.profile_id)
    self.assertEqual(created_entry.title.text, subject_line)
    self.assertEqual(created_entry.content.text, body)
    self.assertEqual(created_entry.content.type, 'html')

    problem = created_entry.ccr.GetProblems()[0]
    problem_desc = problem.FindChildren('Description')[0]
    name = problem_desc.FindChildren('Text')[0]
    self.assertEqual(name.text, 'Aortic valve disorders')


class H9NoticeTest(unittest.TestCase):

  def setUp(self):
    self.h9 = gdata.health.service.HealthService(use_h9_sandbox=True)
    self.h9.ClientLogin(username, password, source='H9 Client Unit Tests')
    self.profile_list_feed = self.h9.GetProfileListFeed()
    self.profile_id = self.profile_list_feed.entry[0].GetProfileId()

  def testSendNotice(self):
    subject_line = 'subject line'
    body = 'Notice <b>body</b>.'
    ccr_xml = test_data.HEALTH_CCR_NOTICE_PAYLOAD
    created_entry = self.h9.SendNotice(subject_line, body, ccr=ccr_xml,
                                       profile_id=self.profile_id)
    self.assertEqual(created_entry.title.text, subject_line)
    self.assertEqual(created_entry.content.text, body)
    self.assertEqual(created_entry.content.type, 'html')

    problem = created_entry.ccr.GetProblems()[0]
    problem_desc = problem.FindChildren('Description')[0]
    name = problem_desc.FindChildren('Text')[0]
    self.assertEqual(name.text, 'Aortic valve disorders')


if __name__ == '__main__':
  print ('Health API Tests\nNOTE: Please run these tests only with a test '
         'account. The tests may delete or update your data.')
  username = raw_input('Please enter your username: ')
  password = getpass.getpass()
  unittest.main()