Fakultas Ilmu Komputer UI

Commit 08eb63bf authored by Kefas Satrio Bangkit Solideantyo's avatar Kefas Satrio Bangkit Solideantyo
Browse files

[RED] add tests for dietela quiz formulas

parent 1cdc9ad1
Pipeline #64593 failed with stage
in 31 minutes and 41 seconds
import json
from rest_framework.test import APITestCase
from .models import DietProfile
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
)
# Create your tests here.
class DietelaQuizTests(APITestCase):
def setUp(self):
self.dietProfile = DietProfile.objects.create(
name="test", email="test@test.com", age=20,
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,
......@@ -21,19 +31,76 @@ class DietelaQuizTests(APITestCase):
def test_get_diet_profile_api(self):
response = self.client.get('/dietela-quiz/diet-profile/')
self.assertEqual(json.loads(response.content)[0].get("name"), "test")
self.assertEqual(json.loads(response.content)[0].get('name'), 'test')
def test_post_diet_profile_api(self):
data = {
"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
'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
}
response = self.client.post('/dietela-quiz/diet-profile/', data, format="json")
self.assertEqual(json.loads(response.content).get("name"), "test2")
response = self.client.post('/dietela-quiz/diet-profile/', data, format='json')
self.assertEqual(json.loads(response.content).get('name'), 'test2')
self.assertEqual(DietProfile.objects.count(), 2)
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)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment