Fakultas Ilmu Komputer UI

tests.py 11.6 KB
Newer Older
1
2
import json
from rest_framework.test import APITestCase
3
from .models import DietProfile
4
from django.test import TestCase
5
from constants.quiz_constants import *
6
7
8
9
10
11
from .formulas import (
    body_mass_index,
    nutrition_status,
    ideal_weight_range,
    basic_energy_needs,
    physical_activity_percentage,
12
13
14
15
16
    daily_energy_needs,
    recommendation_breakfast,
    energy_needed_per_dine,
    physical_activity_recommendation,
    program_recommendation
17
)
18

19
class DietelaQuizTests(APITestCase):
20
21
    
    def setUp(self):
22
        self.dietProfile = DietProfile.objects.create(
23
            name='test', email='test@test.com', age=20,
24
25
26
27
28
29
            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
30
        )
31
32

    def test_diet_profile_saved(self):
33
        self.assertEqual(DietProfile.objects.count(), 1)
34
35
36

    def test_get_diet_profile_api(self):
        response = self.client.get('/dietela-quiz/diet-profile/')
37
        self.assertEqual(json.loads(response.content)[0].get('name'), 'test')
38
39
40

    def test_post_diet_profile_api(self):
        data = {
41
42
43
44
45
46
47
            '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, 'health_problem': 1
48
        }
49
50
        response = self.client.post('/dietela-quiz/diet-profile/', data, format='json')
        self.assertEqual(json.loads(response.content).get('name'), 'test2')
51
52
        self.assertEqual(DietProfile.objects.count(), 2)

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

class FormulaTest(TestCase):

    def test_body_mass_index(self):
        weight = 61
        height = 172
        self.assertEqual(body_mass_index(weight, height), weight/((height/100)**2))

    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
        self.assertEqual(ideal_weight_range(height), {'min': 19*((height/100)**2), 'max': 23*((height/100)**2)})

    def test_basic_energy_needs(self):
        gender_male = 1
        gender_female = 2
        weight = 61
        height = 172
        age = 20
        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)

    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)

    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

        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)
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

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

        self.assertEqual(recommendation_breakfast(1),BreakfastReponse.NO_BREAKFAST)
        self.assertEqual(recommendation_breakfast(2),BreakfastReponse.MED_BREAKFAST)
        self.assertEqual(recommendation_breakfast(3),BreakfastReponse.HI_BREAKFAST)

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

        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)

133
134
135
136
137
138
139
140
141
142
143
    def test_energy_needed_per_dine(self):
        daily_energy_needs = 450
        never_breakfast = 1
        light_breakfast = 2
        heavy_breakfast = 3

        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)

144
145
146
147
148
        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))
149

150
151
152
153
154
        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'))
155

156
157
158
159
160
        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))
161
162
163

        self.assertEqual(energy_needed_error,{})
    
164
165
166
167
168
169
170
171
172
173
174
    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

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

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

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

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

187
188
        self.assertEqual(client_type_5.get('priority_1'),ProgramRecommendationResponse.GOALS_6)
        self.assertEqual(client_type_5.get('priority_2'),ProgramRecommendationResponse.GOALS_3)
189
        
190
191
        self.assertEqual(client_type_6.get('priority_1'),ProgramRecommendationResponse.BALANCED_1)
        self.assertEqual(client_type_6.get('priority_2'),ProgramRecommendationResponse.TRIAL)
192

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

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

199
200
        self.assertEqual(client_type_9.get('priority_1'),ProgramRecommendationResponse.TRIAL)
        self.assertEqual(client_type_9.get('priority_2'),None)
201
202
203
204
205