Fakultas Ilmu Komputer UI

tests.py 22 KB
Newer Older
1
import json
2
import unittest
3
from rest_framework.test import APITestCase
4
from rest_framework import status
5
from .models import DietProfile
6
from django.test import TestCase
7
from constants.quiz_constants import *
8
9
10
11
12
13
from .formulas import (
    body_mass_index,
    nutrition_status,
    ideal_weight_range,
    basic_energy_needs,
    physical_activity_percentage,
14
15
16
17
    daily_energy_needs,
    recommendation_breakfast,
    energy_needed_per_dine,
    physical_activity_recommendation,
18
19
20
21
22
23
24
25
26
27
    program_recommendation,
    calculate_specific_nutrition_needs,
    protein_needs,
    fat_needs,
    carbohydrate_needs,
    fiber_needs,
    get_food_consumed_score,
    get_total_vegetable_and_fruit_score,
    get_vegetable_and_fruit_sufficiency_response,
    get_vegetable_and_fruit_diet_recommendation,
28
)
29

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
30

31
class DietelaQuizTests(APITestCase):
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
32

33
    def setUp(self):
34
        self.dietProfile = DietProfile.objects.create(
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
            name="test",
            email="test@test.com",
            age=20,
            weight=60,
            height=172,
            gender=2,
            special_condition=1,
            body_activity=1,
            vegetables_in_one_day=1,
            fruits_in_one_day=1,
            fried_food_in_one_day=1,
            sweet_snacks_in_one_day=1,
            sweet_drinks_in_one_day=1,
            packaged_food_in_one_day=1,
            large_meal_in_one_day=1,
            snacks_in_one_day=1,
            breakfast_type=1,
            current_condition=1,
            problem_to_solve=1,
54
            health_problem=[2, 3])
55
56

    def test_diet_profile_saved(self):
57
        self.assertEqual(DietProfile.objects.count(), 1)
58
59
60

    def test_get_diet_profile_api(self):
        response = self.client.get('/dietela-quiz/diet-profile/')
61
        self.assertEqual(json.loads(response.content)[0].get('name'), 'test')
62
        self.assertEqual(response.status_code, status.HTTP_200_OK)
63
64
65

    def test_post_diet_profile_api(self):
        data = {
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
            'name': 'test2',
            'email': 'test2@test.com',
            'age': 20,
            'weight': 60,
            'height': 172,
            'gender': 2,
            'special_condition': 1,
            'body_activity': 1,
            'vegetables_in_one_day': 1,
            'fruits_in_one_day': 1,
            'fried_food_in_one_day': 1,
            'sweet_snacks_in_one_day': 1,
            'sweet_drinks_in_one_day': 1,
            'packaged_food_in_one_day': 1,
            'large_meal_in_one_day': 1,
            'snacks_in_one_day': 1,
            'breakfast_type': 1,
            'current_condition': 1,
            'problem_to_solve': 1,
85
            'health_problem': [2, 3]
86
        }
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
87
88
        response = self.client.post(
            '/dietela-quiz/diet-profile/', data, format='json')
89
        self.assertEqual(json.loads(response.content).get('name'), 'test2')
90
        self.assertEqual(DietProfile.objects.count(), 2)
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

    def test_incomplete_post_diet_profile_api(self):
        '''
        Post without food or drink portion in a day,
        and without beakfast type, current condition,
        problem to solve, and health problem fields
        '''

        data = {
            'name': 'test2',
            'email': 'test2@test.com',
            'age': 20,
            'weight': 60,
            'height': 172,
            'gender': 2,
            'special_condition': 1,
            'body_activity': 1
        }
        response = self.client.post(
            '/dietela-quiz/diet-profile/', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
113

114
115
116
117
118
119

class FormulaTest(TestCase):

    def test_body_mass_index(self):
        weight = 61
        height = 172
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
120
121
        self.assertEqual(body_mass_index(weight, height),
                         weight / ((height / 100)**2))
122

123
124
125
126
127
128
129
130
131
132
    def test_invalid_input_body_mass_index(self):
        weight_1 = -1
        height_1 = 172
        weight_2 = 61
        height_2 = -1
        with self.assertRaises(ValueError):
            body_mass_index(weight_1, height_1)
        with self.assertRaises(ValueError):
            body_mass_index(weight_2, height_2)

133
134
135
136
137
138
139
140
141
    def test_nutrition_status(self):
        self.assertEqual(nutrition_status(18), BodyMassConstants.UNDERWEIGHT)
        self.assertEqual(nutrition_status(20.62), BodyMassConstants.NORMAL)
        self.assertEqual(nutrition_status(24), BodyMassConstants.PREOBESITY)
        self.assertEqual(nutrition_status(26), BodyMassConstants.OBESITY1)
        self.assertEqual(nutrition_status(32), BodyMassConstants.OBESITY2)

    def test_ideal_weight_range(self):
        height = 172
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
142
143
144
        self.assertEqual(ideal_weight_range(height),
                         {'min': 19 * ((height / 100)**2),
                          'max': 23 * ((height / 100)**2)})
145

146
147
148
149
150
    def test_invalid_input_ideal_weight_range(self):
        with self.assertRaises(ValueError):
            height = -1
            ideal_weight_range(height)

151
152
153
154
155
156
    def test_basic_energy_needs(self):
        gender_male = 1
        gender_female = 2
        weight = 61
        height = 172
        age = 20
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
157
158
159
160
161
162
163
        self.assertEqual(basic_energy_needs(
            gender_male, weight, height, age), (10 * weight) + (6.25 * height - 5 * age) + 5)
        self.assertEqual(basic_energy_needs(gender_female,
                                            weight,
                                            height,
                                            age),
                         (10 * weight) + (6.25 * height - 5 * age) - 161)
164

165
166
167
168
169
170
171
172
173
174
175
    def test_invalid_input_basic_energy_needs(self):
        gender_male = 0
        gender_female = 3
        weight = -1
        height = -1
        age = -1
        with self.assertRaises(ValueError):
            basic_energy_needs(gender_male, weight, height, age)
        with self.assertRaises(ValueError):
            basic_energy_needs(gender_female, weight, height, age)

176
177
178
179
180
181
182
    def test_physical_activity_percentage(self):
        self.assertEqual(physical_activity_percentage(1), 0.20)
        self.assertEqual(physical_activity_percentage(2), 0.20)
        self.assertEqual(physical_activity_percentage(3), 0.30)
        self.assertEqual(physical_activity_percentage(4), 0.35)
        self.assertEqual(physical_activity_percentage(5), 0.40)

183
184
185
186
187
188
    def test_invalid_input_physical_activity_percentage(self):
        with self.assertRaises(ValueError):
            physical_activity_percentage(0)
        with self.assertRaises(ValueError):
            physical_activity_percentage(6)

189
190
191
192
193
194
195
196
197
198
    def test_daily_energy_needs(self):
        basic_energy_needs = 100
        physical_activity_percentage = 0.2
        no_special_condition = 1
        pregnant_trisemester_1 = 2
        pregnant_trisemester_2 = 3
        pregnant_trisemester_3 = 4
        giving_baby_milk_1 = 5
        giving_baby_milk_2 = 6

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
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
        self.assertEqual(
            daily_energy_needs(
                no_special_condition,
                BodyMassConstants.UNDERWEIGHT,
                basic_energy_needs,
                physical_activity_percentage),
            basic_energy_needs + (
                physical_activity_percentage * basic_energy_needs) + (
                0.1 * basic_energy_needs))
        self.assertEqual(
            daily_energy_needs(
                no_special_condition,
                BodyMassConstants.NORMAL,
                basic_energy_needs,
                physical_activity_percentage),
            basic_energy_needs +
            (
                physical_activity_percentage *
                basic_energy_needs))
        self.assertEqual(
            daily_energy_needs(
                no_special_condition,
                BodyMassConstants.PREOBESITY,
                basic_energy_needs,
                physical_activity_percentage),
            basic_energy_needs +
            (
                physical_activity_percentage *
                basic_energy_needs))

        self.assertEqual(
            daily_energy_needs(
                no_special_condition,
                BodyMassConstants.OBESITY1,
                basic_energy_needs,
                physical_activity_percentage),
            basic_energy_needs)
        self.assertEqual(
            daily_energy_needs(
                no_special_condition,
                BodyMassConstants.OBESITY2,
                basic_energy_needs,
                physical_activity_percentage),
            basic_energy_needs)

        self.assertEqual(
            daily_energy_needs(
                pregnant_trisemester_1,
                BodyMassConstants.NORMAL,
                basic_energy_needs,
                physical_activity_percentage),
            basic_energy_needs +
            (
                physical_activity_percentage *
                basic_energy_needs))
        self.assertEqual(
            daily_energy_needs(
                pregnant_trisemester_2,
                BodyMassConstants.NORMAL,
                basic_energy_needs,
                physical_activity_percentage),
            basic_energy_needs +
            (
                physical_activity_percentage *
                basic_energy_needs) +
            340)
        self.assertEqual(
            daily_energy_needs(
                pregnant_trisemester_3,
                BodyMassConstants.NORMAL,
                basic_energy_needs,
                physical_activity_percentage),
            basic_energy_needs +
            (
                physical_activity_percentage *
                basic_energy_needs) +
            452)
        self.assertEqual(
            daily_energy_needs(
                giving_baby_milk_1,
                BodyMassConstants.NORMAL,
                basic_energy_needs,
                physical_activity_percentage),
            basic_energy_needs +
            (
                physical_activity_percentage *
                basic_energy_needs) +
            330)
        self.assertEqual(
            daily_energy_needs(
                giving_baby_milk_2,
                BodyMassConstants.NORMAL,
                basic_energy_needs,
                physical_activity_percentage),
            basic_energy_needs +
            (
                physical_activity_percentage *
                basic_energy_needs) +
            400)
298
299
300
301
302
303

    def test_recommendation_breakfast(self):
        no_breakfast = 1
        med_breakfast = 2
        hi_breakfast = 3

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
304
305
306
307
308
309
310
311
312
        self.assertEqual(
            recommendation_breakfast(1),
            BreakfastReponse.NO_BREAKFAST)
        self.assertEqual(
            recommendation_breakfast(2),
            BreakfastReponse.MED_BREAKFAST)
        self.assertEqual(
            recommendation_breakfast(3),
            BreakfastReponse.HI_BREAKFAST)
313
314
315
316
317
318
319
320

    def test_physical_activity_recommendation(self):
        level1_activity = 1
        level2_activity = 2
        level3_activity = 3
        level4_activity = 4
        level5_activity = 5

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
        self.assertEqual(
            physical_activity_recommendation(level1_activity),
            PhysicalActivityResponse.LEVEL1_ACTIVITY)
        self.assertEqual(
            physical_activity_recommendation(level2_activity),
            PhysicalActivityResponse.LEVEL2_ACTIVITY)
        self.assertEqual(
            physical_activity_recommendation(level3_activity),
            PhysicalActivityResponse.LEVEL3_ACTIVITY)
        self.assertEqual(
            physical_activity_recommendation(level4_activity),
            PhysicalActivityResponse.LEVEL4_ACTIVITY)
        self.assertEqual(
            physical_activity_recommendation(level5_activity),
            PhysicalActivityResponse.LEVEL5_ACTIVITY)
336

337
338
339
340
341
342
    def test_energy_needed_per_dine(self):
        daily_energy_needs = 450
        never_breakfast = 1
        light_breakfast = 2
        heavy_breakfast = 3

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
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
386
387
388
389
390
391
392
393
394
395
        energy_needed_no_breakfast = energy_needed_per_dine(
            never_breakfast, daily_energy_needs)
        energy_needed_med_breakfast = energy_needed_per_dine(
            light_breakfast, daily_energy_needs)
        energy_needed_hi_breakfast = energy_needed_per_dine(
            heavy_breakfast, daily_energy_needs)
        energy_needed_error = energy_needed_per_dine(0, daily_energy_needs)

        self.assertEqual(
            energy_needed_no_breakfast.get('breakfast'),
            (daily_energy_needs * 0.1))
        self.assertEqual(energy_needed_no_breakfast.get(
            'morning_snack'), (daily_energy_needs * 0.15))
        self.assertEqual(
            energy_needed_no_breakfast.get('lunch'),
            (daily_energy_needs * 0.3))
        self.assertEqual(energy_needed_no_breakfast.get(
            'afternoon_snack'), (daily_energy_needs * 0.15))
        self.assertEqual(
            energy_needed_no_breakfast.get('dinner'),
            (daily_energy_needs * 0.3))

        self.assertEqual(
            energy_needed_no_breakfast.get('breakfast'),
            energy_needed_med_breakfast.get('breakfast'))
        self.assertEqual(
            energy_needed_no_breakfast.get('morning_snack'),
            energy_needed_med_breakfast.get('morning_snack'))
        self.assertEqual(
            energy_needed_no_breakfast.get('lunch'),
            energy_needed_med_breakfast.get('lunch'))
        self.assertEqual(
            energy_needed_no_breakfast.get('afternoon_snack'),
            energy_needed_med_breakfast.get('afternoon_snack'))
        self.assertEqual(
            energy_needed_no_breakfast.get('dinner'),
            energy_needed_med_breakfast.get('dinner'))

        self.assertEqual(
            energy_needed_hi_breakfast.get('breakfast'),
            (daily_energy_needs * 0.2))
        self.assertEqual(energy_needed_hi_breakfast.get(
            'morning_snack'), (daily_energy_needs * 0.1))
        self.assertEqual(
            energy_needed_hi_breakfast.get('lunch'),
            (daily_energy_needs * 0.3))
        self.assertEqual(energy_needed_hi_breakfast.get(
            'afternoon_snack'), (daily_energy_needs * 0.1))
        self.assertEqual(
            energy_needed_hi_breakfast.get('dinner'),
            (daily_energy_needs * 0.3))

        self.assertEqual(energy_needed_error, {})
396

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
    def test_program_recommendation(self):
        client_type_1 = program_recommendation(10, 7, 5)      # Score 22-30
        client_type_2 = program_recommendation(30, 7, 30)     # Score 31-69
        client_type_3 = program_recommendation(10, 50, 15)    # Score 70-79
        client_type_4 = program_recommendation(30, 55, 5)     # Score 80-99
        client_type_5 = program_recommendation(30, 55, 30)    # Score 100-119
        client_type_6 = program_recommendation(30, 115, 5)    # Score 120-150
        client_type_7 = program_recommendation(30, 115, 10)   # Score 151-159
        client_type_8 = program_recommendation(30, 115, 30)   # Score 160-179
        client_type_9 = program_recommendation(200, 115, 30)  # Score 180-345

        self.assertEqual(
            client_type_1.get('priority_1'),
            ProgramRecommendationResponse.BABY_1)
        self.assertEqual(
            client_type_1.get('priority_2'),
            ProgramRecommendationResponse.TRIAL)

        self.assertEqual(
            client_type_2.get('priority_1'),
            ProgramRecommendationResponse.BABY_3)
        self.assertEqual(
            client_type_2.get('priority_2'),
            ProgramRecommendationResponse.BABY_1)

        self.assertEqual(
            client_type_3.get('priority_1'),
            ProgramRecommendationResponse.BABY_1)
        self.assertEqual(
            client_type_3.get('priority_2'),
            ProgramRecommendationResponse.TRIAL)

        self.assertEqual(
            client_type_4.get('priority_1'),
            ProgramRecommendationResponse.GOALS_3)
        self.assertEqual(
            client_type_4.get('priority_2'),
            ProgramRecommendationResponse.GOALS_1)

        self.assertEqual(
            client_type_5.get('priority_1'),
            ProgramRecommendationResponse.GOALS_6)
        self.assertEqual(
            client_type_5.get('priority_2'),
            ProgramRecommendationResponse.GOALS_3)

        self.assertEqual(
            client_type_6.get('priority_1'),
            ProgramRecommendationResponse.BALANCED_1)
        self.assertEqual(
            client_type_6.get('priority_2'),
            ProgramRecommendationResponse.TRIAL)

        self.assertEqual(
            client_type_7.get('priority_1'),
            ProgramRecommendationResponse.BALANCED_3)
        self.assertEqual(
            client_type_7.get('priority_2'),
            ProgramRecommendationResponse.BALANCED_1)

        self.assertEqual(
            client_type_8.get('priority_1'),
            ProgramRecommendationResponse.BALANCED_6)
        self.assertEqual(
            client_type_8.get('priority_2'),
            ProgramRecommendationResponse.BALANCED_3)

        self.assertEqual(
            client_type_9.get('priority_1'),
            ProgramRecommendationResponse.TRIAL)
        self.assertEqual(client_type_9.get('priority_2'), None)
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632

    def test_calculate_specific_nutrition_needs(self):
        percentage = 50
        daily_energy_needs = 500
        denominator = 10

        self.assertEqual(
            calculate_specific_nutrition_needs(
                percentage,
                daily_energy_needs,
                denominator),
            25)

    def test_protein_needs_condition_1(self):
        special_condition_choice = 3
        body_mass_index = 20
        daily_energy_needs = 500

        self.assertEquals(
            protein_needs(
                special_condition_choice,
                body_mass_index,
                daily_energy_needs),
            calculate_specific_nutrition_needs(
                13,
                daily_energy_needs,
                4))

    def test_protein_needs_condition_2(self):
        special_condition_choice = 1
        body_mass_index = 35
        daily_energy_needs = 450

        self.assertEquals(
            protein_needs(
                special_condition_choice,
                body_mass_index,
                daily_energy_needs),
            calculate_specific_nutrition_needs(
                18,
                daily_energy_needs,
                4))

    def test_fat_needs_condition_1(self):
        special_condition_choice = 3
        body_mass_index = 20
        daily_energy_needs = 500

        self.assertEquals(
            fat_needs(
                special_condition_choice,
                body_mass_index,
                daily_energy_needs),
            calculate_specific_nutrition_needs(
                30,
                daily_energy_needs,
                9))

    def test_fat_needs_condition_2(self):
        special_condition_choice = 1
        body_mass_index = 35
        daily_energy_needs = 450

        self.assertEquals(
            fat_needs(
                special_condition_choice,
                body_mass_index,
                daily_energy_needs),
            calculate_specific_nutrition_needs(
                27,
                daily_energy_needs,
                9))

    def test_carbohydrate_needs_condition_1(self):
        special_condition_choice = 3
        body_mass_index = 20
        daily_energy_needs = 500

        self.assertEquals(
            carbohydrate_needs(
                special_condition_choice,
                body_mass_index,
                daily_energy_needs),
            calculate_specific_nutrition_needs(
                57,
                daily_energy_needs,
                4))

    def test_carbohydrate_needs_condition_2(self):
        special_condition_choice = 1
        body_mass_index = 35
        daily_energy_needs = 450

        self.assertEquals(
            carbohydrate_needs(
                special_condition_choice,
                body_mass_index,
                daily_energy_needs),
            calculate_specific_nutrition_needs(
                55,
                daily_energy_needs,
                4))

    def test_fiber_needs(self):
        body_mass_index = 35

        self.assertEqual(fiber_needs(body_mass_index), 25)

    def test_get_food_consumed_score(self):
        choice = 3

        self.assertEqual(get_food_consumed_score(choice), 2)

    def test_get_total_vegetable_and_fruit_score_with_valid_choice(self):
        vegetable_choice = 2
        fruit_choice = 3

        self.assertEqual(
            get_total_vegetable_and_fruit_score(
                vegetable_choice, fruit_choice), 3)

    def test_get_total_vegetable_and_fruit_score_with_invalid_choice(self):
        with self.assertRaises(ValueError):
            vegetable_choice = 5
            fruit_choice = 0

            get_total_vegetable_and_fruit_score(vegetable_choice, fruit_choice)

    def test_get_vegetable_and_fruit_sufficiency_response_with_valid_total_score_condition_1(
            self):
        total_vegetable_and_fruit_score = 4

        self.assertEqual(
            get_vegetable_and_fruit_sufficiency_response(total_vegetable_and_fruit_score),
            VegetableAndFruitSufficiencyResponse.STILL_NOT_ENOUGH)

    def test_get_total_vegetable_and_fruit_sufficiency_response_with_valid_total_score_condition_2(
            self):
        total_vegetable_and_fruit_score = 7

        self.assertEqual(get_vegetable_and_fruit_sufficiency_response(
            total_vegetable_and_fruit_score), VegetableAndFruitSufficiencyResponse.ENOUGH)

    def test_get_total_vegetable_and_fruit_sufficiency_response_with_invalid_total_score(
            self):
        with self.assertRaises(ValueError):
            total_vegetable_and_fruit_score = 10

            get_vegetable_and_fruit_sufficiency_response(
                total_vegetable_and_fruit_score)

    def test_get_vegetable_and_fruit_diet_recommendation_condition_1(
            self):
        vegetable_and_fruit_suffiency_response = VegetableAndFruitSufficiencyResponse.STILL_NOT_ENOUGH

        self.assertEqual(
            get_vegetable_and_fruit_diet_recommendation(vegetable_and_fruit_suffiency_response),
            VegetableAndFruitDietRecommendation.STILL_NOT_ENOUGH)

    def test_get_vegetable_and_fruit_diet_recommendation_condition_2(
            self):
        vegetable_and_fruit_suffiency_response = VegetableAndFruitSufficiencyResponse.ENOUGH

        self.assertEqual(get_vegetable_and_fruit_diet_recommendation(
            vegetable_and_fruit_suffiency_response), VegetableAndFruitDietRecommendation.ENOUGH)