forked from matthewdowney/TogglPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
96 lines (78 loc) · 3.13 KB
/
tests.py
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
import os
import unittest
from TogglPy import Toggl
# these tests assume three things:
#
# first, that you have two environment variables defined
# TOGGL_API_KEY
# WORKPSPACE_ID
#
# second, that you are able to able to reach Toggl's live REST API.
#
# finally, the test_putTimeEntry() will likely fail unless you have 1) a Client "Self" and 2) Project "Self" defined.
# and 3) some Pomodoros completed in your time entries.
# this is because these are acceptance tests that are hitting my personal Toggl repo, where I do all three
# of the above defined
#
class TogglPyTests(unittest.TestCase):
def setUp(self):
self.api_key = os.environ['TOGGL_API_KEY']
if self.api_key is None:
raise Exception("Unable to execute api tests without an api key")
self.workspace_id = os.environ['WORKSPACE_ID']
if self.workspace_id is None:
raise Exception("Unable to execute api tests without a workspace key to query")
self.toggl = Toggl()
self.toggl.setAPIKey(self.api_key)
def test_connect(self):
response = self.toggl.request("https://www.toggl.com/api/v8/clients")
self.assertTrue(response is not None)
def test_putTimeEntry(self):
request_args = {
'workspace_id': self.workspace_id,
}
entries = self.toggl.getDetailedReport(request_args)
#for this tests I'm tagging my Pomodoro Entries
missing_projects = [r for r in entries['data'] if r['project'] is None and 'Pomodoro' in r['description'] ]
me = missing_projects[0]
me_id = me['id'] #remember for later
#I've tagged my pomodoro entries as Self/Self
cp = self.toggl.getClientProject("Self", "Self")
project_id = cp['data']['id']
me['pid'] = project_id
#his is the new stuff
response = self.toggl.putTimeEntry({"id": me_id, "pid":project_id})
self.assertTrue(response is not None)
self.assertTrue('data' in response)
self.assertTrue(response['data']['pid'] == project_id)
def test_getDetailedReportCSV(self):
data = {
'workspace_id': self.workspace_id,
}
csvfile = 'data.csv'
self.toggl.getDetailedReportCSV(data, csvfile)
self.assertTrue(os.path.isfile(csvfile))
os.remove(csvfile)
data = self.toggl.getDetailedReportCSV(data)
self.assertTrue(data is not None)
def test_getDetailedReport(self):
data = {
'workspace_id': self.workspace_id,
}
d = self.toggl.getDetailedReport(data)
self.assertTrue(d is not None)
self.assertTrue(len(d.keys()) > 0 )
fields = ['total_count', 'total_currencies', 'total_billable', 'data']
for f in fields:
self.assertTrue(f in d.keys())
data = d['data']
self.assertTrue(len(data)>0)
dr = data[0]
self.assertTrue('client' in dr)
self.assertTrue('start' in dr)
self.assertTrue('end' in dr)
self.assertTrue('task' in dr)
self.assertTrue('user' in dr)
self.assertTrue('project' in dr)
if __name__ == '__main__':
unittest.main()