-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
385 lines (363 loc) · 14.2 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import unittest
from datetime import date
from unittest import mock
from habitTrackerApp import HabitTrackerApp
from recurrence import Recurrence, RecurrenceType
from habit import Habit, HabitCompletionHistoryPoint
from habitTracker import HabitTracker
import os
class TestHabitTracker(unittest.TestCase):
def setUp(self):
self.habit_tracker = HabitTracker(habits=[
Habit(
"Daily Exercise",
"30 minutes of exercise daily",
date(2024, 7, 5),
Recurrence(RecurrenceType.DAILY),
creation_date=date(2024, 6, 16),
is_broken=False,
completion_history=[
HabitCompletionHistoryPoint(
date(2024, 6, 17), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 18), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 19), False
),
HabitCompletionHistoryPoint(
date(2024, 6, 20), False
),
HabitCompletionHistoryPoint(
date(2024, 6, 21), False
),
HabitCompletionHistoryPoint(
date(2024, 6, 22), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 23), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 24), False
),
HabitCompletionHistoryPoint(
date(2024, 6, 25), False
),
HabitCompletionHistoryPoint(
date(2024, 6, 26), False
),
HabitCompletionHistoryPoint(
date(2024, 6, 27), False
),
HabitCompletionHistoryPoint(
date(2024, 6, 28), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 29), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 30), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 1), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 2), False
),
HabitCompletionHistoryPoint(
date(2024, 7, 3), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 4), True
)
]
),
Habit(
"Weekly Reading",
"Read a book for 1 hour every Week",
date(2024, 7, 6),
Recurrence(RecurrenceType.WEEKLY),
creation_date=date(2024, 4, 27),
is_broken=False,
completion_history=[
HabitCompletionHistoryPoint(
date(2024, 5, 4), True
),
HabitCompletionHistoryPoint(
date(2024, 5, 11), True
),
HabitCompletionHistoryPoint(
date(2024, 5, 18), True
),
HabitCompletionHistoryPoint(
date(2024, 5, 25), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 1), False
),
HabitCompletionHistoryPoint(
date(2024, 6, 8), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 15), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 22), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 29), True
)
]
),
Habit(
"Meditation",
"Practice meditation for 15 minutes daily",
date(2024, 7, 10),
Recurrence(RecurrenceType.DAILY),
creation_date=date(2024, 6, 22),
is_broken=False,
completion_history=[
HabitCompletionHistoryPoint(
date(2024, 6, 23), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 24), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 25), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 26), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 27), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 28), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 29), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 30), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 1), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 2), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 3), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 4), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 5), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 6), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 7), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 8), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 9), True
)
]
),
Habit(
"Healthy Eating",
"Consume a balanced meal with fruits and vegetables daily",
date(2024, 7, 12),
Recurrence(RecurrenceType.DAILY),
creation_date=date(2024, 6, 18),
is_broken=False,
completion_history=[
HabitCompletionHistoryPoint(
date(2024, 6, 19), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 20), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 21), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 22), False
),
HabitCompletionHistoryPoint(
date(2024, 6, 23), False
),
HabitCompletionHistoryPoint(
date(2024, 6, 24), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 25), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 26), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 27), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 28), False
),
HabitCompletionHistoryPoint(
date(2024, 6, 29), False
),
HabitCompletionHistoryPoint(
date(2024, 6, 30), False
),
HabitCompletionHistoryPoint(
date(2024, 7, 1), False
),
HabitCompletionHistoryPoint(
date(2024, 7, 2), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 3), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 4), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 5), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 6), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 7), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 8), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 9), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 10), False
),
HabitCompletionHistoryPoint(
date(2024, 7, 11), True
)
]
),
Habit(
"Language Learning",
"Practice a new language for 30 minutes every day",
date(2024, 7, 14),
Recurrence(RecurrenceType.WEEKLY),
creation_date=date(2024, 5, 26),
is_broken=False,
completion_history=[
HabitCompletionHistoryPoint(
date(2024, 6, 2), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 9), False
),
HabitCompletionHistoryPoint(
date(2024, 6, 16), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 23), True
),
HabitCompletionHistoryPoint(
date(2024, 6, 30), True
),
HabitCompletionHistoryPoint(
date(2024, 7, 7), True
)
]
)
])
def test_add_habit(self):
self.assertEqual(len(self.habit_tracker.habits), 5)
self.habit_tracker.add_habit(Habit("Exercise", "Daily workout", date(2023, 12, 31), Recurrence(RecurrenceType.DAILY)))
self.assertEqual(len(self.habit_tracker.habits), 6)
def test_get_number_of_habits(self):
self.assertEqual(self.habit_tracker.get_number_of_habits(), len(self.habit_tracker.habits))
def test_list_habits_by_recurrence(self):
daily_habits = self.habit_tracker.list_habits_by_recurrence(RecurrenceType.DAILY)
self.assertEqual(len(daily_habits), 3)
def test_get_longest_streak_all_habits(self):
streak = self.habit_tracker.get_longest_streak_all_habits()
self.assertEqual(streak, 17)
def test_save_and_load_habits_to_file(self):
file_path = "test_habits.pickle"
self.habit_tracker.save_habits_to_file(file_path)
self.assertTrue(os.path.exists(file_path))
new_habit_tracker = HabitTracker()
new_habit_tracker.load_habits_from_file(file_path)
self.assertEqual(len(new_habit_tracker.habits), len(self.habit_tracker.habits))
os.remove(file_path)
class TestHabitTrackerApp(unittest.TestCase):
@classmethod
def setUpClass(self):
mocked_inputs = iter([
'10', # Exit the loop immediately
])
with mock.patch('builtins.input', side_effect=lambda *args: next(mocked_inputs)):
self.habit_tracker = HabitTrackerApp()
def test_create_and_add_new_habit(self):
new_habit = Habit('Exercise', 'Daily workout', date(2023, 12, 31), Recurrence(recurrence_type=RecurrenceType.DAILY, interval=1))
with mock.patch.object(self.habit_tracker, 'define_habit', return_value=new_habit):
self.habit_tracker.create_and_add_new_habit()
self.assertEqual(len(self.habit_tracker.habits), 1)
self.habit_tracker.habits.remove(new_habit)
def test_define_habit(self):
inputs = iter([
'Test Habit', # Name
'Description of Test Habit', # Description
'2023-12-25', # First deadline
'1', # Recurrence type (1 for Daily)
'2', # Recurrence interval
])
expected_habit = Habit(
'Test Habit',
'Description of Test Habit',
date(2023, 12, 25),
Recurrence(RecurrenceType.DAILY, interval=2)
)
with mock.patch('builtins.input', side_effect=lambda *args: next(inputs)):
defined_habit = self.habit_tracker.define_habit()
self.assertEqual(defined_habit.name, expected_habit.name)
self.assertEqual(defined_habit.description, expected_habit.description)
self.assertEqual(defined_habit.last_deadline, expected_habit.last_deadline)
self.assertEqual(defined_habit.recurrence.recurrence_type, expected_habit.recurrence.recurrence_type)
self.assertEqual(defined_habit.recurrence.interval, expected_habit.recurrence.interval)
def test_delete_habit(self):
test_habit = Habit('Exercise', 'Daily workout', date(2023, 12, 31), Recurrence(recurrence_type=RecurrenceType.DAILY, interval=1))
self.habit_tracker.habits.append(test_habit)
with mock.patch.object(self.habit_tracker, 'choose_habit', return_value=self.habit_tracker._ChosenHabit(0, test_habit)):
self.habit_tracker.delete_habit()
self.assertEqual(len(self.habit_tracker.habits), 0)
class TestHabit(unittest.TestCase):
def test_habit_completion_first(self):
self.habit1 = Habit("Exercise", "Daily workout", date(2023, 12, 31), Recurrence(RecurrenceType.DAILY))
self.habit1.complete(date(2023, 12, 31))
self.assertEqual(self.habit1.completion_history[0].completion, True)
def test_habit_completion_first_1(self):
self.habit1 = Habit("Exercise", "Daily workout", date(2023, 12, 31), Recurrence(RecurrenceType.DAILY))
self.habit1.complete(date(2023, 12, 31))
self.assertEqual(self.habit1.is_broken, False)
def test_habit_longest_streak(self):
self.habit2 = Habit("Read", "Read a book", date(2023, 12, 25), Recurrence(RecurrenceType.WEEKLY))
self.habit2.complete(date(2023, 12, 25))
self.habit2.complete(date(2024, 1, 1))
self.assertEqual(len(self.habit2.completion_history), 2)
def test_habit_longest_streak_2(self):
self.habit2 = Habit("Read", "Read a book", date(2023, 12, 25), Recurrence(RecurrenceType.WEEKLY))
self.habit2.complete(date(2023, 12, 25))
self.habit2.complete(date(2024, 1, 1))
self.assertEqual(self.habit2.get_longest_streak(), 2)
if __name__ == '__main__':
unittest.main()