Fakultas Ilmu Komputer UI

tests.py 33.5 KB
Newer Older
1
2
import json
from rest_framework.test import APITestCase
3
from rest_framework import status
4
from django.test import TestCase
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
5
6
7
8
9
10
11
12
13
14
15
16
17
from constants.quiz_constants import (
    BreakfastReponse,
    PhysicalActivityResponse,
    ProgramRecommendationResponse,
    BodyMassConstants,
    VegetableAndFruitSufficiencyResponse,
    VegetableAndFruitDietRecommendation,
    SugarSaltFatProblemResponse,
    SugarSaltFatDietRecommendation,
    LargeMealDietRecommendation,
    SnacksDietRecommendation,
)
from .models import DietProfile, QuizResult
18
from .formulas import (
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    get_body_mass_index,
    get_nutrition_status,
    get_ideal_weight_range,
    get_basic_energy_needs,
    get_physical_activity_percentage,
    get_daily_energy_needs,
    get_breakfast_recommendation,
    get_energy_needed_per_dine,
    get_physical_activity_recommendation,
    get_program_recommendation,
    get_calculate_specific_nutrition_needs,
    get_protein_needs,
    get_fat_needs,
    get_carbohydrate_needs,
    get_fiber_needs,
34
35
36
37
    get_food_consumed_score,
    get_total_vegetable_and_fruit_score,
    get_vegetable_and_fruit_sufficiency_response,
    get_vegetable_and_fruit_diet_recommendation,
38
39
40
41
42
    get_total_sugar_salt_fat_score,
    get_sugar_salt_fat_problem_response,
    get_sugar_salt_fat_diet_recommendation,
    get_large_meal_diet_recommendation,
    get_snacks_diet_recommendation,
43
)
44

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
45

46
class DietelaQuizTests(APITestCase):
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
47

48
    def setUp(self):
49
        self.diet_profile = DietProfile.objects.create(
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
50
51
52
            name="test",
            email="test@test.com",
            age=20,
Kefas Satrio Bangkit Solidedantyo's avatar
Kefas Satrio Bangkit Solidedantyo committed
53
            weight=61,
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
            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,
69
            health_problem=[2, 3])
70
71

    def test_diet_profile_saved(self):
72
        self.assertEqual(DietProfile.objects.count(), 1)
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
73

74
    def test_diet_profile_string(self):
75
        self.assertEqual(str(self.diet_profile), 'test - test@test.com')
76
77
78

    def test_get_diet_profile_api(self):
        response = self.client.get('/dietela-quiz/diet-profile/')
79
        self.assertEqual(json.loads(response.content)[0].get('name'), 'test')
80
        self.assertEqual(response.status_code, status.HTTP_200_OK)
81
82
83

    def test_post_diet_profile_api(self):
        data = {
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
            '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,
103
            'health_problem': [2, 3]
104
        }
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
105
106
        response = self.client.post(
            '/dietela-quiz/diet-profile/', data, format='json')
107
        self.assertEqual(json.loads(response.content).get('name'), 'test2')
108
        self.assertEqual(json.loads(response.content).get('quiz_result').get('age'), 20)
109
        self.assertEqual(DietProfile.objects.count(), 2)
110
111
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    def test_auto_quiz_results_saved(self):
        data = {
            'name': 'test3',
            'email': 'test3@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,
            'health_problem': [1]
        }
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
135
        self.client.post(
136
137
138
            '/dietela-quiz/diet-profile/', data, format='json')
        self.assertEqual(QuizResult.objects.filter(diet_profile__name='test3').count(), 1)

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    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)
159

160
161
162

class FormulaTest(TestCase):

163
    def test_get_body_mass_index(self):
164
165
        weight = 61
        height = 172
166
        self.assertEqual(get_body_mass_index(weight, height),
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
167
                         weight / ((height / 100)**2))
168

169
    def test_invalid_input_get_body_mass_index(self):
170
171
172
173
174
        weight_1 = -1
        height_1 = 172
        weight_2 = 61
        height_2 = -1
        with self.assertRaises(ValueError):
175
            get_body_mass_index(weight_1, height_1)
176
        with self.assertRaises(ValueError):
177
            get_body_mass_index(weight_2, height_2)
178

179
180
181
182
183
184
    def test_get_nutrition_status(self):
        self.assertEqual(get_nutrition_status(18), BodyMassConstants.UNDERWEIGHT)
        self.assertEqual(get_nutrition_status(20.62), BodyMassConstants.NORMAL)
        self.assertEqual(get_nutrition_status(24), BodyMassConstants.PREOBESITY)
        self.assertEqual(get_nutrition_status(26), BodyMassConstants.OBESITY1)
        self.assertEqual(get_nutrition_status(32), BodyMassConstants.OBESITY2)
185

186
    def test_get_ideal_weight_range(self):
187
        height = 172
188
        self.assertEqual(get_ideal_weight_range(height),
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
189
190
                         {'min': 19 * ((height / 100)**2),
                          'max': 23 * ((height / 100)**2)})
191

192
    def test_invalid_input_get_ideal_weight_range(self):
193
194
        with self.assertRaises(ValueError):
            height = -1
195
            get_ideal_weight_range(height)
196

197
    def test_get_basic_energy_needs(self):
198
199
200
201
202
        gender_male = 1
        gender_female = 2
        weight = 61
        height = 172
        age = 20
203
        self.assertEqual(get_basic_energy_needs(
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
204
            gender_male, weight, height, age), (10 * weight) + (6.25 * height - 5 * age) + 5)
205
        self.assertEqual(get_basic_energy_needs(gender_female,
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
206
207
208
209
                                            weight,
                                            height,
                                            age),
                         (10 * weight) + (6.25 * height - 5 * age) - 161)
210

211
    def test_invalid_input_get_basic_energy_needs(self):
212
213
214
215
216
217
        gender_male = 0
        gender_female = 3
        weight = -1
        height = -1
        age = -1
        with self.assertRaises(ValueError):
218
            get_basic_energy_needs(gender_male, weight, height, age)
219
        with self.assertRaises(ValueError):
220
            get_basic_energy_needs(gender_female, weight, height, age)
221

222
223
224
225
226
227
    def test_get_physical_activity_percentage(self):
        self.assertEqual(get_physical_activity_percentage(1), 0.20)
        self.assertEqual(get_physical_activity_percentage(2), 0.20)
        self.assertEqual(get_physical_activity_percentage(3), 0.30)
        self.assertEqual(get_physical_activity_percentage(4), 0.35)
        self.assertEqual(get_physical_activity_percentage(5), 0.40)
228

229
    def test_invalid_input_get_physical_activity_percentage(self):
230
        with self.assertRaises(ValueError):
231
            get_physical_activity_percentage(0)
232
        with self.assertRaises(ValueError):
233
            get_physical_activity_percentage(6)
234

235
    def test_get_daily_energy_needs(self):
236
237
238
239
240
241
242
243
244
        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
245
        self.assertEqual(
246
            get_daily_energy_needs(
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
247
248
249
250
251
252
253
254
                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(
255
            get_daily_energy_needs(
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
256
257
258
259
260
261
262
263
264
                no_special_condition,
                BodyMassConstants.NORMAL,
                basic_energy_needs,
                physical_activity_percentage),
            basic_energy_needs +
            (
                physical_activity_percentage *
                basic_energy_needs))
        self.assertEqual(
265
            get_daily_energy_needs(
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
266
267
268
269
270
271
272
273
274
275
                no_special_condition,
                BodyMassConstants.PREOBESITY,
                basic_energy_needs,
                physical_activity_percentage),
            basic_energy_needs +
            (
                physical_activity_percentage *
                basic_energy_needs))

        self.assertEqual(
276
            get_daily_energy_needs(
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
277
278
279
280
281
282
                no_special_condition,
                BodyMassConstants.OBESITY1,
                basic_energy_needs,
                physical_activity_percentage),
            basic_energy_needs)
        self.assertEqual(
283
            get_daily_energy_needs(
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
284
285
286
287
288
289
290
                no_special_condition,
                BodyMassConstants.OBESITY2,
                basic_energy_needs,
                physical_activity_percentage),
            basic_energy_needs)

        self.assertEqual(
291
            get_daily_energy_needs(
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
292
293
294
295
296
297
298
299
300
                pregnant_trisemester_1,
                BodyMassConstants.NORMAL,
                basic_energy_needs,
                physical_activity_percentage),
            basic_energy_needs +
            (
                physical_activity_percentage *
                basic_energy_needs))
        self.assertEqual(
301
            get_daily_energy_needs(
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
302
303
304
305
306
307
308
309
310
311
                pregnant_trisemester_2,
                BodyMassConstants.NORMAL,
                basic_energy_needs,
                physical_activity_percentage),
            basic_energy_needs +
            (
                physical_activity_percentage *
                basic_energy_needs) +
            340)
        self.assertEqual(
312
            get_daily_energy_needs(
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
313
314
315
316
317
318
319
320
321
322
                pregnant_trisemester_3,
                BodyMassConstants.NORMAL,
                basic_energy_needs,
                physical_activity_percentage),
            basic_energy_needs +
            (
                physical_activity_percentage *
                basic_energy_needs) +
            452)
        self.assertEqual(
323
            get_daily_energy_needs(
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
324
325
326
327
328
329
330
331
332
333
                giving_baby_milk_1,
                BodyMassConstants.NORMAL,
                basic_energy_needs,
                physical_activity_percentage),
            basic_energy_needs +
            (
                physical_activity_percentage *
                basic_energy_needs) +
            330)
        self.assertEqual(
334
            get_daily_energy_needs(
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
335
336
337
338
339
340
341
342
343
                giving_baby_milk_2,
                BodyMassConstants.NORMAL,
                basic_energy_needs,
                physical_activity_percentage),
            basic_energy_needs +
            (
                physical_activity_percentage *
                basic_energy_needs) +
            400)
344

345
    def test_get_breakfast_recommendation(self):
346
347
348
349
        no_breakfast = 1
        med_breakfast = 2
        hi_breakfast = 3

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
350
        self.assertEqual(
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
351
            get_breakfast_recommendation(no_breakfast),
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
352
353
            BreakfastReponse.NO_BREAKFAST)
        self.assertEqual(
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
354
            get_breakfast_recommendation(med_breakfast),
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
355
356
            BreakfastReponse.MED_BREAKFAST)
        self.assertEqual(
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
357
            get_breakfast_recommendation(hi_breakfast),
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
358
            BreakfastReponse.HI_BREAKFAST)
359

360
    def test_get_physical_activity_recommendation(self):
361
362
363
364
365
366
        level1_activity = 1
        level2_activity = 2
        level3_activity = 3
        level4_activity = 4
        level5_activity = 5

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
367
        self.assertEqual(
368
            get_physical_activity_recommendation(level1_activity),
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
369
370
            PhysicalActivityResponse.LEVEL1_ACTIVITY)
        self.assertEqual(
371
            get_physical_activity_recommendation(level2_activity),
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
372
373
            PhysicalActivityResponse.LEVEL2_ACTIVITY)
        self.assertEqual(
374
            get_physical_activity_recommendation(level3_activity),
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
375
376
            PhysicalActivityResponse.LEVEL3_ACTIVITY)
        self.assertEqual(
377
            get_physical_activity_recommendation(level4_activity),
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
378
379
            PhysicalActivityResponse.LEVEL4_ACTIVITY)
        self.assertEqual(
380
            get_physical_activity_recommendation(level5_activity),
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
381
            PhysicalActivityResponse.LEVEL5_ACTIVITY)
382

383
    def test_get_energy_needed_per_dine(self):
384
385
386
387
388
        daily_energy_needs = 450
        never_breakfast = 1
        light_breakfast = 2
        heavy_breakfast = 3

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

        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, {})
442

443
    def test_get_program_recommendation(self):
Muzaki Azami's avatar
Muzaki Azami committed
444
445
446
447
448
449
450
451
452
        client_type_1 = get_program_recommendation(3, 6, [1])      # Score 22-30
        client_type_2 = get_program_recommendation(4, 4, [1])     # Score 31-69
        client_type_3 = get_program_recommendation(3, 1, [1])    # Score 70-79
        client_type_4 = get_program_recommendation(3, 1, [2])     # Score 80-99
        client_type_5 = get_program_recommendation(7, 1, [8])    # Score 100-119
        client_type_6 = get_program_recommendation(3, 7, [1])    # Score 120-150
        client_type_7 = get_program_recommendation(3, 10, [3])   # Score 151-159
        client_type_8 = get_program_recommendation(4, 1, [5,6,7])   # Score 160-179
        client_type_9 = get_program_recommendation(1, 1, [1])  # Score 180-345
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
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

        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)
514

515
    def test_get_calculate_specific_nutrition_needs(self):
516
517
518
519
520
        percentage = 50
        daily_energy_needs = 500
        denominator = 10

        self.assertEqual(
521
            get_calculate_specific_nutrition_needs(
522
523
524
525
526
527
528
529
530
531
                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

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
532
        self.assertEqual(
533
            get_protein_needs(
534
535
536
                special_condition_choice,
                body_mass_index,
                daily_energy_needs),
537
            get_calculate_specific_nutrition_needs(
538
539
540
541
542
543
544
545
546
                13,
                daily_energy_needs,
                4))

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

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
547
        self.assertEqual(
548
            get_protein_needs(
549
550
551
                special_condition_choice,
                body_mass_index,
                daily_energy_needs),
552
            get_calculate_specific_nutrition_needs(
553
554
555
556
557
558
559
560
561
                18,
                daily_energy_needs,
                4))

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

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
562
        self.assertEqual(
563
            get_fat_needs(
564
565
566
                special_condition_choice,
                body_mass_index,
                daily_energy_needs),
567
            get_calculate_specific_nutrition_needs(
568
569
570
571
572
573
574
575
576
                30,
                daily_energy_needs,
                9))

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

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
577
        self.assertEqual(
578
            get_fat_needs(
579
580
581
                special_condition_choice,
                body_mass_index,
                daily_energy_needs),
582
            get_calculate_specific_nutrition_needs(
583
584
585
586
587
588
589
590
591
                27,
                daily_energy_needs,
                9))

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

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
592
        self.assertEqual(
593
            get_carbohydrate_needs(
594
595
596
                special_condition_choice,
                body_mass_index,
                daily_energy_needs),
597
            get_calculate_specific_nutrition_needs(
598
599
600
601
602
603
604
605
606
                57,
                daily_energy_needs,
                4))

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

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
607
        self.assertEqual(
608
            get_carbohydrate_needs(
609
610
611
                special_condition_choice,
                body_mass_index,
                daily_energy_needs),
612
            get_calculate_specific_nutrition_needs(
613
614
615
616
                55,
                daily_energy_needs,
                4))

617
    def test_get_fiber_needs(self):
618

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
619
        self.assertEqual(get_fiber_needs(), 25)
620
621
622
623
624
625
626
627
628
629
630
631
632
633

    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)

634
635
636
637
638
639
640
641
642
643
    def test_get_total_vegetable_and_fruit_score_with_invalid_vegetable_choice(
            self):
        with self.assertRaises(ValueError):
            vegetable_choice = 6
            fruit_choice = 2

            get_total_vegetable_and_fruit_score(vegetable_choice, fruit_choice)

    def test_get_total_vegetable_and_fruit_score_with_invalid_fruit_choice(
            self):
644
        with self.assertRaises(ValueError):
645
            vegetable_choice = 3
646
647
648
649
650
651
652
653
654
655
            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),
656
            VegetableAndFruitSufficiencyResponse.LACKING)
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674

    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):
675
        vegetable_and_fruit_suffiency_response = VegetableAndFruitSufficiencyResponse.LACKING
676
677
678

        self.assertEqual(
            get_vegetable_and_fruit_diet_recommendation(vegetable_and_fruit_suffiency_response),
679
            VegetableAndFruitDietRecommendation.LACKING)
680
681
682
683
684
685
686

    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)
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882

    def test_get_total_sugar_salt_fat_score_with_valid_choice(self):
        fried_food_choice = 1
        sweet_snacks_choice = 3
        sweet_drinks_choice = 1
        packaged_food_choice = 2

        self.assertEqual(
            get_total_sugar_salt_fat_score(
                fried_food_choice,
                sweet_snacks_choice,
                sweet_drinks_choice,
                packaged_food_choice),
            3)

    def test_get_total_sugar_salt_fat_score_with_invalid_fried_food_choice(
            self):
        with self.assertRaises(ValueError):
            fried_food_choice = 0
            sweet_snacks_choice = 3
            sweet_drinks_choice = 1
            packaged_food_choice = 2

            get_total_sugar_salt_fat_score(
                fried_food_choice,
                sweet_snacks_choice,
                sweet_drinks_choice,
                packaged_food_choice)

    def test_get_total_sugar_salt_fat_score_with_invalid_sweet_snacks_choice(
            self):
        with self.assertRaises(ValueError):
            fried_food_choice = 1
            sweet_snacks_choice = 6
            sweet_drinks_choice = 1
            packaged_food_choice = 2

            get_total_sugar_salt_fat_score(
                fried_food_choice,
                sweet_snacks_choice,
                sweet_drinks_choice,
                packaged_food_choice)

    def test_get_total_sugar_salt_fat_score_with_invalid_sweet_drinks_choice(
            self):
        with self.assertRaises(ValueError):
            fried_food_choice = 1
            sweet_snacks_choice = 2
            sweet_drinks_choice = 0
            packaged_food_choice = 2

            get_total_sugar_salt_fat_score(
                fried_food_choice,
                sweet_snacks_choice,
                sweet_drinks_choice,
                packaged_food_choice)

    def test_get_total_sugar_salt_fat_score_with_invalid_packaged_food_choice(
            self):
        with self.assertRaises(ValueError):
            fried_food_choice = 1
            sweet_snacks_choice = 2
            sweet_drinks_choice = 5
            packaged_food_choice = 6

            get_total_sugar_salt_fat_score(
                fried_food_choice,
                sweet_snacks_choice,
                sweet_drinks_choice,
                packaged_food_choice)

    def test_get_sugar_salt_fat_problem_response_with_invalid_total_score(
            self):
        with self.assertRaises(ValueError):
            total_sugar_salt_fat_score = 13

            get_sugar_salt_fat_problem_response(
                total_sugar_salt_fat_score)

    def test_get_sugar_salt_fat_problem_response_with_valid_total_score_condition_1(
            self):
        total_sugar_salt_fat_score = 4

        self.assertEqual(
            get_sugar_salt_fat_problem_response(total_sugar_salt_fat_score),
            SugarSaltFatProblemResponse.CONTROLLED)

    def test_get_sugar_salt_fat_problem_response_with_valid_total_score_condition_2(
            self):
        total_sugar_salt_fat_score = 7

        self.assertEqual(
            get_sugar_salt_fat_problem_response(total_sugar_salt_fat_score),
            SugarSaltFatProblemResponse.EXCESSIVE)

    def test_get_sugar_salt_fat_diet_recommendation_condition_1(
            self):
        sugar_salt_fat_problem_response = SugarSaltFatProblemResponse.CONTROLLED

        self.assertEqual(
            get_sugar_salt_fat_diet_recommendation(sugar_salt_fat_problem_response),
            SugarSaltFatDietRecommendation.CONTROLLED)

    def test_get_sugar_salt_fat_diet_recommendation_condition_2(
            self):
        sugar_salt_fat_problem_response = SugarSaltFatProblemResponse.EXCESSIVE

        self.assertEqual(
            get_sugar_salt_fat_diet_recommendation(sugar_salt_fat_problem_response),
            SugarSaltFatDietRecommendation.EXCESSIVE)

    def test_get_large_meal_diet_recommendation_with_invalid_choice(
            self):
        with self.assertRaises(ValueError):
            large_meal_choice = 5

            get_large_meal_diet_recommendation(large_meal_choice)

    def test_get_large_meal_diet_recommendation_with_valid_choice_condition_1(
            self):
        large_meal_choice = 1

        self.assertEqual(
            get_large_meal_diet_recommendation(large_meal_choice),
            LargeMealDietRecommendation.ONCE_A_DAY)

    def test_get_large_meal_diet_recommendation_with_valid_choice_condition_2(
            self):
        large_meal_choice = 2

        self.assertEqual(
            get_large_meal_diet_recommendation(large_meal_choice),
            LargeMealDietRecommendation.TWICE_A_DAY)

    def test_get_large_meal_diet_recommendation_with_valid_choice_condition_3(
            self):
        large_meal_choice = 3

        self.assertEqual(
            get_large_meal_diet_recommendation(large_meal_choice),
            LargeMealDietRecommendation.THRICE_A_DAY)

    def test_get_large_meal_diet_recommendation_with_valid_choice_condition_4(
            self):
        large_meal_choice = 4

        self.assertEqual(
            get_large_meal_diet_recommendation(large_meal_choice),
            LargeMealDietRecommendation.MORE_THAN_THRICE_A_DAY)

    def test_get_snacks_diet_recommendation_with_invalid_choice(
            self):
        with self.assertRaises(ValueError):
            snacks_choice = 0

            get_snacks_diet_recommendation(snacks_choice)

    def test_get_snacks_diet_recommendation_with_valid_choice_condition_1(
            self):
        snacks_choice = 1

        self.assertEqual(
            get_snacks_diet_recommendation(snacks_choice),
            SnacksDietRecommendation.NO_SNACK)

    def test_get_snacks_diet_recommendation_with_valid_choice_condition_2(
            self):
        snacks_choice = 2

        self.assertEqual(
            get_snacks_diet_recommendation(snacks_choice),
            SnacksDietRecommendation.ONCE_A_DAY)

    def test_get_snacks_diet_recommendation_with_valid_choice_condition_3(
            self):
        snacks_choice = 3

        self.assertEqual(
            get_snacks_diet_recommendation(snacks_choice),
            SnacksDietRecommendation.TWICE_A_DAY)

    def test_get_snacks_diet_recommendation_with_valid_choice_condition_4(
            self):
        snacks_choice = 4

        self.assertEqual(
            get_snacks_diet_recommendation(snacks_choice),
            SnacksDietRecommendation.THRICE_A_DAY)

    def test_get_snacks_diet_recommendation_with_valid_choice_condition_5(
            self):
        snacks_choice = 5

        self.assertEqual(
            get_snacks_diet_recommendation(snacks_choice),
            SnacksDietRecommendation.MORE_THAN_THRICE_A_DAY)
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908


class QuizResultTests(APITestCase):
    def setUp(self):
        self.diet_profile = DietProfile.objects.create(
            name="test2",
            email="test2@test2.com",
            age=21,
            weight=61,
            height=173,
            gender=2,
            special_condition=1,
            body_activity=1,
            vegetables_in_one_day=2,
            fruits_in_one_day=1,
            fried_food_in_one_day=1,
            sweet_snacks_in_one_day=2,
            sweet_drinks_in_one_day=1,
            packaged_food_in_one_day=1,
            large_meal_in_one_day=1,
            snacks_in_one_day=2,
            breakfast_type=1,
            current_condition=1,
            problem_to_solve=1,
            health_problem=[2, 3])

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
909
        self.quiz_result = QuizResult.objects.create(
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
            diet_profile = self.diet_profile,
            age = self.diet_profile.age,
            weight = self.diet_profile.weight,
            height = self.diet_profile.height,
            gender = self.diet_profile.gender,
            body_mass_index = 22.99,
            nutrition_status = BodyMassConstants.NORMAL,
            ideal_weight_range = {'min': 100, 'max': 110},
            daily_energy_needs = 100.99,
            daily_nutrition_needs = {
                'protein_needs': 100.99, 'fat_needs': 100.99,
                'carbohydrate_needs': 100.99, 'fiber_needs': 25,
            },
            vegetable_and_fruit_sufficiency = VegetableAndFruitSufficiencyResponse.ENOUGH,
            vegetable_and_fruit_diet_recommendation = VegetableAndFruitDietRecommendation.ENOUGH,
            sugar_salt_fat_problem = 100.99,
            sugar_salt_fat_diet_recommendation = SugarSaltFatDietRecommendation.EXCESSIVE,
            large_meal_diet_recommendation = LargeMealDietRecommendation.MORE_THAN_THRICE_A_DAY,
            snacks_diet_recommendation = SnacksDietRecommendation.MORE_THAN_THRICE_A_DAY,
            breakfast_recommendation = BreakfastReponse.HI_BREAKFAST,
            energy_needed_per_dine = {
                'breakfast': 100.99, 'morning_snack': 100.99,
                'lunch': 100.99, 'afternoon_snack': 100.99,
                'dinner': 100.99,
            },
            physical_activity_recommendation = PhysicalActivityResponse.LEVEL1_ACTIVITY,
            program_recommendation = {
                'priority_1': ProgramRecommendationResponse.BALANCED_1,
                'priority_2': ProgramRecommendationResponse.TRIAL,
            }
        )
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
941

942
943
    def test_quiz_result_saved(self):
        self.assertEqual(QuizResult.objects.count(), 1)
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
944

945
    def test_quiz_result_string(self):
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
946
        self.assertEqual(str(self.quiz_result), str(self.diet_profile))