@@ -8,34 +8,38 @@ class ToogleClientApiTests(unittest.TestCase):
8
8
9
9
api = None
10
10
connectLive = False
11
- base_api_url = ''
12
- base_api_report_url = ''
11
+ base_api_url = ""
12
+ base_api_report_url = ""
13
13
http_content_type = "application/json"
14
14
15
15
# Runs before *every* tests
16
16
def setUp (self ):
17
17
self .settings = {
18
- ' workspace_name' : ' A Company' ,
19
- ' token' : ' xxx' ,
20
- ' base_url' : ' https://my.service/api' ,
21
- ' ver_api' : 8 ,
22
- ' base_url_report' : ' https://my.service/reports/api' ,
23
- ' ver_report' : 2
18
+ " workspace_name" : " A Company" ,
19
+ " token" : " xxx" ,
20
+ " base_url" : " https://my.service/api" ,
21
+ " ver_api" : 8 ,
22
+ " base_url_report" : " https://my.service/reports/api" ,
23
+ " ver_report" : 2 ,
24
24
}
25
25
self .api = TogglClientApi (self .settings )
26
26
httpretty .enable () # enable HTTPretty so that it will monkey patch the socket module
27
27
28
- self .base_api_url = self .api .build_api_url (self .settings ['base_url' ], self .settings ['ver_api' ])
29
- self .base_api_report_url = self .api .build_api_url (self .settings ['base_url_report' ], self .settings ['ver_report' ])
28
+ self .base_api_url = self .api .build_api_url (
29
+ self .settings ["base_url" ], self .settings ["ver_api" ]
30
+ )
31
+ self .base_api_report_url = self .api .build_api_url (
32
+ self .settings ["base_url_report" ], self .settings ["ver_report" ]
33
+ )
30
34
31
35
# Runs after *every* tests
32
36
def tearDown (self ):
33
37
del self .api
34
38
httpretty .disable ()
35
39
httpretty .reset ()
36
40
37
- def load_json_file (self , location , base_path = ' tests/json_responses' ):
38
- file_contents = open (base_path + '/' + location + ' .json' )
41
+ def load_json_file (self , location , base_path = " tests/json_responses" ):
42
+ file_contents = open (base_path + "/" + location + " .json" )
39
43
json_data = json .load (file_contents )
40
44
file_contents .close ()
41
45
@@ -45,37 +49,36 @@ def test_api_client_instance_created(self):
45
49
self .assertNotEqual (self .api , None )
46
50
47
51
def test_overriding_default_base_url_and_version_on_instance_creation (self ):
48
- my_base_url = ' http://myownapi.com'
52
+ my_base_url = " http://myownapi.com"
49
53
my_ver_no = 12
50
- credentials = {
51
- 'base_url' : my_base_url ,
52
- 'ver_api' : my_ver_no
53
- }
54
+ credentials = {"base_url" : my_base_url , "ver_api" : my_ver_no }
54
55
self .api = TogglClientApi (credentials )
55
- self .assertEqual (self .api .api_base_url , my_base_url + '/v' + str (my_ver_no ))
56
+ self .assertEqual (self .api .api_base_url , my_base_url + "/v" + str (my_ver_no ))
56
57
57
58
def test_api_token_set (self ):
58
- self .assertNotEqual (self .api .api_token , '' )
59
+ self .assertNotEqual (self .api .api_token , "" )
59
60
60
61
def test_api_toggl_auth_response (self ):
61
- expected_response_json_str = json .dumps (self .load_json_file ('me' ))
62
+ expected_response_json_str = json .dumps (self .load_json_file ("me" ))
62
63
httpretty .register_uri (
63
64
httpretty .GET ,
64
65
self .base_api_url + "/me" ,
65
66
body = expected_response_json_str ,
66
- content_type = self .http_content_type )
67
- response = self .api .query ('/me' )
67
+ content_type = self .http_content_type ,
68
+ )
69
+ response = self .api .query ("/me" )
68
70
69
71
self .assertEqual (response .status_code , 200 )
70
72
self .assertEqual (response .text , expected_response_json_str )
71
73
72
- def setup_test_api_toggl_get_workspace (self , json_file = ' workspaces' ):
74
+ def setup_test_api_toggl_get_workspace (self , json_file = " workspaces" ):
73
75
expected_response_json_str = json .dumps (self .load_json_file (json_file ))
74
76
httpretty .register_uri (
75
77
httpretty .GET ,
76
78
self .base_api_url + "/workspaces" ,
77
79
body = expected_response_json_str ,
78
- content_type = self .http_content_type )
80
+ content_type = self .http_content_type ,
81
+ )
79
82
80
83
return expected_response_json_str
81
84
@@ -86,84 +89,120 @@ def test_api_toggl_get_list_of_workspaces_response(self):
86
89
self .assertEqual (response .status_code , 200 )
87
90
self .assertEqual (response .text , expected_response_json_str )
88
91
89
- def setup_test_api_toggl_get_workspace_by_name (self , expected_found_index = 2 , json_file = 'workspaces' ):
92
+ def setup_test_api_toggl_get_workspace_by_name (
93
+ self , expected_found_index = 2 , json_file = "workspaces"
94
+ ):
90
95
expected_response_json_str = self .setup_test_api_toggl_get_workspace (json_file )
91
96
data = json .loads (expected_response_json_str )
92
97
expected_workspace = data [expected_found_index ]
93
- workspace = self .api .get_workspace_by_name (self .settings [' workspace_name' ])
98
+ workspace = self .api .get_workspace_by_name (self .settings [" workspace_name" ])
94
99
95
100
return workspace , expected_workspace
96
101
97
102
def test_api_toggl_get_workspace_by_name_found (self ):
98
- workspace , expected_workspace = self .setup_test_api_toggl_get_workspace_by_name ()
99
- self .assertEqual (workspace ['id' ], expected_workspace ['id' ])
103
+ workspace , expected_workspace = (
104
+ self .setup_test_api_toggl_get_workspace_by_name ()
105
+ )
106
+ self .assertEqual (workspace ["id" ], expected_workspace ["id" ])
100
107
101
108
def setup_test_api_toggl_get_workspace_members (self ):
102
- workspace , expected_workspace = self .setup_test_api_toggl_get_workspace_by_name ()
109
+ workspace , expected_workspace = (
110
+ self .setup_test_api_toggl_get_workspace_by_name ()
111
+ )
103
112
104
- expected_response_json_str = json .dumps (self .load_json_file ('workspace_members' ))
113
+ expected_response_json_str = json .dumps (
114
+ self .load_json_file ("workspace_members" )
115
+ )
105
116
httpretty .register_uri (
106
117
httpretty .GET ,
107
- self .base_api_url + "/workspaces/" + str (workspace ['id' ]) + "/workspace_users" ,
118
+ self .base_api_url
119
+ + "/workspaces/"
120
+ + str (workspace ["id" ])
121
+ + "/workspace_users" ,
108
122
body = expected_response_json_str ,
109
- content_type = self .http_content_type )
123
+ content_type = self .http_content_type ,
124
+ )
110
125
111
- response = self .api .get_workspace_members (workspace ['id' ])
126
+ response = self .api .get_workspace_members (workspace ["id" ])
112
127
113
128
return response , expected_response_json_str , workspace , expected_workspace
114
129
115
130
def test_api_toggl_get_workspace_members_response_ok (self ):
116
- response , expected_response_json_str , workspace , expected_workspace = self .setup_test_api_toggl_get_workspace_members ()
117
- self .assertEqual (workspace ['id' ], expected_workspace ['id' ])
131
+ response , expected_response_json_str , workspace , expected_workspace = (
132
+ self .setup_test_api_toggl_get_workspace_members ()
133
+ )
134
+ self .assertEqual (workspace ["id" ], expected_workspace ["id" ])
118
135
self .assertEqual (response .status_code , 200 )
119
136
120
137
def test_api_toggl_get_workspace_members_response_count (self ):
121
- response , expected_response_json_str , workspace , expected_workspace = self .setup_test_api_toggl_get_workspace_members ()
138
+ response , expected_response_json_str , workspace , expected_workspace = (
139
+ self .setup_test_api_toggl_get_workspace_members ()
140
+ )
122
141
123
- self .assertEqual (workspace ['id' ], expected_workspace ['id' ])
142
+ self .assertEqual (workspace ["id" ], expected_workspace ["id" ])
124
143
125
144
received_data = response .json ()
126
145
expected_data = json .loads (expected_response_json_str )
127
146
128
147
self .assertEqual (len (received_data ), len (expected_data ))
129
148
130
149
def test_api_toggl_get_member_total_hours_range_response_ok_with_found_data (self ):
131
- self .do_test_api_toggl_get_member_total_hours_range_response_ok ('report_user_project_hours' )
150
+ self .do_test_api_toggl_get_member_total_hours_range_response_ok (
151
+ "report_user_project_hours"
152
+ )
132
153
133
154
def test_api_toggl_get_member_total_hours_range_response_ok_with_empty_data (self ):
134
- self .do_test_api_toggl_get_member_total_hours_range_response_ok ('report_user_project_hours_null' )
155
+ self .do_test_api_toggl_get_member_total_hours_range_response_ok (
156
+ "report_user_project_hours_null"
157
+ )
135
158
136
- def do_test_api_toggl_get_member_total_hours_range_response_ok (self , datafile = 'report_user_project_hours' ):
137
- workspace , expected_workspace = self .setup_test_api_toggl_get_workspace_by_name ()
138
- self .assertEqual (workspace ['id' ], expected_workspace ['id' ])
159
+ def do_test_api_toggl_get_member_total_hours_range_response_ok (
160
+ self , datafile = "report_user_project_hours"
161
+ ):
162
+ workspace , expected_workspace = (
163
+ self .setup_test_api_toggl_get_workspace_by_name ()
164
+ )
165
+ self .assertEqual (workspace ["id" ], expected_workspace ["id" ])
139
166
140
- workspace_id = workspace ['id' ]
167
+ workspace_id = workspace ["id" ]
141
168
user_id = 222222
142
- start_date = '2014-03-03'
143
- end_date = '2014-03-07'
144
- user_agent = 'toggl-python-api-client-nosetests'
145
-
146
- endpoint_url = self .base_api_report_url + "/summary?" + "workspace_id=" + str (workspace ['id' ]) + "&since=" + start_date + "&until=" + end_date + "&user_agent=" + user_agent + "&user_ids=" + str (user_id ) + "&grouping=users" + "&subgrouping=projects"
169
+ start_date = "2014-03-03"
170
+ end_date = "2014-03-07"
171
+ user_agent = "toggl-python-api-client-nosetests"
172
+
173
+ endpoint_url = (
174
+ self .base_api_report_url
175
+ + "/summary?"
176
+ + "workspace_id="
177
+ + str (workspace ["id" ])
178
+ + "&since="
179
+ + start_date
180
+ + "&until="
181
+ + end_date
182
+ + "&user_agent="
183
+ + user_agent
184
+ + "&user_ids="
185
+ + str (user_id )
186
+ + "&grouping=users"
187
+ + "&subgrouping=projects"
188
+ )
147
189
148
190
expected_response_json_str = json .dumps (self .load_json_file (datafile ))
149
191
httpretty .register_uri (
150
192
httpretty .GET ,
151
193
endpoint_url ,
152
194
body = expected_response_json_str ,
153
- content_type = self .http_content_type )
195
+ content_type = self .http_content_type ,
196
+ )
154
197
155
198
expected_response_json = json .loads (expected_response_json_str )
156
- if len (expected_response_json [' data' ]) > 0 :
157
- expected_total = expected_response_json [' data' ][0 ][' time' ]
199
+ if len (expected_response_json [" data" ]) > 0 :
200
+ expected_total = expected_response_json [" data" ][0 ][" time" ]
158
201
else :
159
202
expected_total = 0
160
203
161
204
total = self .api .get_user_hours_range (
162
- user_agent ,
163
- workspace_id ,
164
- user_id ,
165
- start_date ,
166
- end_date
205
+ user_agent , workspace_id , user_id , start_date , end_date
167
206
)
168
207
169
- self .assertEqual (total , expected_total )
208
+ self .assertEqual (total , expected_total )
0 commit comments