-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
211 lines (166 loc) · 7.74 KB
/
test.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import datetime
import unittest
from company_jobs import CompanyJobs
from invoice import Invoice
from time_entry import TimeEntry
_dt = datetime.datetime
class BaseClass(unittest.TestCase):
def setUp(self):
comp1 = 'Company1'
comp2 = 'Company2'
job1 = 'job1'
job2 = 'job2'
job3 = 'job3'
job4 = 'job4'
_te = TimeEntry
self.entries = [
_te(1.0, _dt(2010,1,1), '1st entry', comp1, job1),
_te(2.0, _dt(2010,1,2), '2nd entry', comp1, job2),
_te(2.5, _dt(2019,1,3), '3rd entry', comp2, job3, billable=False),
_te(4.9, _dt(2019,2,4), '4th entry', comp2, job4)
]
self.company1_jobs_dict = {job1: 'Job One', job2: 'Job Two'}
self.company1_jobs = CompanyJobs(comp1, self.company1_jobs_dict, 20)
self.company2_jobs_dict = {job3: 'Job Three', job4: 'Job Four'}
self.company2_jobs = CompanyJobs(comp2, self.company2_jobs_dict, 20)
class TestTimeEntries(BaseClass):
def testCreateTimeEntry(self):
entry = TimeEntry(1, _dt(2010,1,1), '1st entry', 'Company1', 'job1')
self.assertEqual(entry.hours, 1.0)
self.assertEqual(entry.dt, _dt(2010,1,1))
self.assertEqual(entry.message, '1st entry')
self.assertEqual(entry.company, 'Company1')
self.assertEqual(entry.job, 'job1')
def testSumEntryHours(self):
entry1 = TimeEntry(1, None, None, None, None)
entry2 = TimeEntry(2, None, None, None, None)
entry3 = TimeEntry(2.5, None, None, None, None)
entry4 = TimeEntry(3.0, None, None, None, None)
_total = TimeEntry.get_hours_total
self.assertEqual(_total([entry1, entry2]), 3.0)
self.assertEqual(_total([entry1, entry2, entry3]), 5.5)
self.assertEqual(_total([entry1, entry2, entry3, entry4]), 8.5)
def testFilterEntries(self):
comp1_entries = [
TimeEntry(None, None, None, 'Company1', None) for i in range(100)
]
comp2_entries = [
TimeEntry(None, None, None, 'Company2', None) for i in range(100)
]
comp3_entries = [
TimeEntry(None, None, None, 'Company3', None) for i in range(100)
]
all_entries = comp1_entries + comp2_entries + comp3_entries
for entry in TimeEntry.query(all_entries, 'Company1'):
self.assertEqual(entry.company, 'Company1')
for entry in TimeEntry.query(all_entries, 'Company2'):
self.assertEqual(entry.company, 'Company2')
for entry in TimeEntry.query(all_entries, 'Company3'):
self.assertEqual(entry.company, 'Company3')
import random
random.shuffle(all_entries)
for entry in TimeEntry.query(all_entries, 'Company1'):
self.assertEqual(entry.company, 'Company1')
for entry in TimeEntry.query(all_entries, 'Company2'):
self.assertEqual(entry.company, 'Company2')
for entry in TimeEntry.query(all_entries, 'Company3'):
self.assertEqual(entry.company, 'Company3')
def testFilterEntriesByDate(self):
e1 = TimeEntry(1, _dt(2010,1,1), '1st entry', 'Company1', 'job1')
e2 = TimeEntry(1, _dt(2010,1,2), '2nd entry', 'Company1', 'job1')
e3 = TimeEntry(1, _dt(2010,1,3), '3rd entry', 'Company1', 'job1')
self.assertEqual(
TimeEntry.filter_by_date([e1], _dt(2010,1,1), _dt(2010,1,1)),
[e1]
)
self.assertEqual(
TimeEntry.filter_by_date([e1, e2, e3], _dt(2010,1,1), _dt(2010,1,1)),
[e1]
)
# With date range in February, all January entries are filtered out
self.assertEqual(
TimeEntry.filter_by_date([e1, e2, e3], _dt(2010,2,1), _dt(2010,2,3)),
[]
)
class TestInvoicing(BaseClass):
def testCreateInvoice(self):
filtered_entries = TimeEntry.query(self.entries, 'Company1')
invoice = Invoice(filtered_entries, None, (None, None), self.company1_jobs)
self.assertEqual(invoice.hours_total, 3)
filtered_entries = TimeEntry.query(self.entries, 'Company2')
invoice = Invoice(filtered_entries, None, (None, None), self.company2_jobs)
self.assertEqual(invoice.hours_total, 4.9)
filtered_entry_ids = [e.id for e in filtered_entries]
self.assertEqual(sorted(invoice.entry_ids), sorted(filtered_entry_ids))
def testInvoicePayperiod(self):
invoice = Invoice([],
_dt(2019,2,18),
(_dt(2019,2,4), _dt(2019,2,17)), self.company1_jobs)
self.assertEqual(invoice.payperiod_start, _dt(2019,2,4))
self.assertEqual(invoice.payperiod_end, _dt(2019,2,17))
invoice = Invoice([],
None,
(_dt(2019,2,1), _dt(2019,2,28)), self.company1_jobs)
self.assertEqual(invoice.payperiod_start, _dt(2019,2,1))
self.assertEqual(invoice.payperiod_end, _dt(2019,2,28))
def testSendInvoice(self):
"""Asserting the invoiced_dt value is weird, like what am I
trying to prove? So far, it's only use is being printed in
text; of interest for future record keeping? I don't
really have a personal need for send(), so it's just this
kinda-academic thing I thought an invoice should have. And
I think that uncertainty shows in this lame test.
"""
filtered_entries = TimeEntry.query(self.entries, 'Company1')
invoice = Invoice(filtered_entries, None, (None, None), self.company1_jobs)
send_start = _dt.now()
invoice.send()
send_end = _dt.now()
self.assertLess(send_start, invoice.invoiced_dt)
self.assertGreater(send_end, invoice.invoiced_dt)
for entry in invoice.entries:
self.assertFalse(entry.can_be_invoiced())
self.assertEqual(entry.invoiced_dt, invoice.invoiced_dt)
self.assertTrue(invoice.sent)
def testIdCollisions(self):
"""Using something other than self(id), right now uuid1().
I was using self.id = id(self) for ID, which would make for
a collison in about 10 iterations of the following loop.
Noticed this problem as an intermittent
already-exists-error in:
test_io:TestGenInvoice.testRealRun()
"""
ids = []
for i in range(100):
invoice = Invoice(
[
TimeEntry(1, _dt(2010,1,1), '1st entry', 'Company1', 'job1'),
TimeEntry(2.5, _dt(2010,1,2), '2nd entry', 'Company1', 'job1')
],
_dt(2010,1,14),
(_dt(2010,1,1), _dt(2010,1,13)),
CompanyJobs('Company1', {'job1': 'Job One'}, 20)
)
self.assertNotIn(invoice.id, ids)
ids.append(invoice.id)
class TestCompanyJobs(unittest.TestCase):
def testCompanyJobs(self):
company1_jobs_dict = {
'job1': 'Job One',
'job2': 'Job Two'
}
jobs = CompanyJobs('Company1', company1_jobs_dict, 20)
self.assertEqual(jobs.wage, 20)
# Test simple id <--> name
self.assertEqual(jobs.get_id_by_name('Job One'), 'job1')
self.assertEqual(jobs.get_name_by_id('job1'), 'Job One')
self.assertEqual(jobs.get_id_by_name('Job Two'), 'job2')
self.assertEqual(jobs.get_name_by_id('job2'), 'Job Two')
# Test normalization
self.assertEqual(jobs.get_id_by_name('job one'), 'job1')
self.assertEqual(jobs.get_id_by_name('JoB One'), 'job1')
self.assertEqual(jobs.get_id_by_name('JOB ONE'), 'job1')
# id is not normalized
self.assertIsNone(jobs.get_name_by_id('JOB1'))
# Check for bogus name
self.assertIsNone(jobs.get_id_by_name('Job 1'))