Fakultas Ilmu Komputer UI

tests.py 5.95 KB
Newer Older
1
2
import json
from rest_framework.test import APITestCase
3
from .models import DietProfile
4
5
6
7
8
9
10
11
12
13
from django.test import TestCase
from constants.body_mass import BodyMassConstants
from .formulas import (
    body_mass_index,
    nutrition_status,
    ideal_weight_range,
    basic_energy_needs,
    physical_activity_percentage,
    daily_energy_needs
)
14
15

# Create your tests here.
16
class DietelaQuizTests(APITestCase):
17
18
    
    def setUp(self):
19
        self.dietProfile = DietProfile.objects.create(
20
            name='test', email='test@test.com', age=20,
21
22
23
24
25
26
            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
27
        )
28
29

    def test_diet_profile_saved(self):
30
        self.assertEqual(DietProfile.objects.count(), 1)
31
32
33

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

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

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

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)