Fakultas Ilmu Komputer UI

Commit 7386e000 authored by Glenda Emanuella Sutanto's avatar Glenda Emanuella Sutanto Committed by Kefas Satrio Bangkit Solidedantyo
Browse files

Implement formulas for Dietela quiz item 9-15

parent 6726f768
......@@ -35,10 +35,35 @@ class BodyMassConstants:
class VegetableAndFruitSufficiencyResponse:
STILL_NOT_ENOUGH = "Kurang makan sayur dan buah"
LACKING = "Kurang makan sayur dan buah"
ENOUGH = "Asupan sayur dan buah sudah cukup"
class VegetableAndFruitDietRecommendation:
STILL_NOT_ENOUGH = "Yah.. asupan buah & sayur kamu masih kurang nih. Yuk, cari solusi agar kamu bisa konsumsi setidaknya 2 porsi sayur & 3 porsi buah atau sebaliknya per hari. Pokoknya kalau di total kamu perlu 5 porsi buah & sayur per hari."
LACKING = "Yah.. asupan buah & sayur kamu masih kurang nih. Yuk, cari solusi agar kamu bisa konsumsi setidaknya 2 porsi sayur & 3 porsi buah atau sebaliknya per hari. Pokoknya kalau di total kamu perlu 5 porsi buah & sayur per hari."
ENOUGH = "Selamat! Kamu sudah memenuhi salah satu langkah menuju hidup sehat. Pertahankan untuk konsumsi setidaknya 2 porsi sayur & 3 porsi buah atau sebaliknya per hari ya. Pokoknya kalau di total kamu perlu 5 porsi buah dan sayur per sehari."
class SugarSaltFatProblemResponse:
CONTROLLED = "Asupan gula, garam, dan lemak terkontrol"
EXCESSIVE = "Kelebihan makan gula, garam, dan lemak"
class SugarSaltFatDietRecommendation:
CONTROLLED = "Selamat! Kamu sudah memenuhi salah satu langkah untuk menuju hidup sehat. Pertahankan pemilihan jenis makanan yang seperti ini ya. Upayakan agar jumlah gorengan, cemilan manis, makan manis, makanan kemasan, dan makanan cepat saji yang kamu makan bisa terkontrol jumlahnya."
EXCESSIVE = "Duh, asupan gula, garam, dan lemak kamu kebanyakan nih. Asupan gula, garam, dan lemak juga ada batasannya lho. Kamu harus hati-hati dalam memilih jenis makanan. Kalau konsumsi kamu berlebih berisiko menimbulkan masalah kesehatan juga Lho."
class LargeMealDietRecommendation:
ONCE_A_DAY = "Hmmm.... Kenapa cuma makan besar 1 kali sehari? Makan besar cuma 1 kali dalam sehari berisiko membuat kamu makan berlebihan dan kekurangan zat gizi tertentu Lho! Yuk coba mulai diatur pola makannya, dengan merutinkan waktu makan agar metabolisme kamu lebih baik."
TWICE_A_DAY = "Makan besar kamu sudah 2 kali, tapi masih kurang nih. Sebaiknya makan besar itu 3 kali yaah, karena ketika skip 1 jam makan, bisa menyebabkan berbagai risiko, seperti makan berlebihan di jam makan berikutnya, kekurangan zat gizi dan sampai ke masalah kesehatan."
THRICE_A_DAY = "Keren! Apresiasi buat kamu yang sudah rutin makan besar 3 kali sehari. Tapi pastikan agar sesuai dengan kebutuhan kamu yah, jangan berlebih atau kekurangan."
MORE_THAN_THRICE_A_DAY = "Eiiits, porsi makan besar kamu sulit untuk bisa seimbang kalau kamu makan besar lebih dari 3 kali dalam sehari. Cek porsi yang tepat untukmu yah kalau kamu memang sering makan besar."
class SnacksDietRecommendation:
NO_SNACK = "Yah kok gak ngemil? Makan cemilan itu penting untuk menjaga kadar gula darah kamu seharian, dengan ngemil gula darah seharian kamu bisa terkontrol sehingga mencegah lemas dan berkurangnya konsentrasi."
ONCE_A_DAY = "Tambah waktu nyemil ga dosa loh asal porsinya tetap sesuai dengan kebutuhan kamu. Sebaiknya sehari bisa 2 kali makan cemilan agar kamu tidak kekurangan energi di sela-sela aktivitas dan tidak berlebihan makan pada 1 jam makan tertentu. Atur waktu makan kamu dengan lebih baik yah."
TWICE_A_DAY = "Baguuuus! Pertahankan ya makan cemilan 2x sehari. Perhatikan porsi dan kandungan zat gizi cemilanmu ya. Utamakan yang padat gizi sehingga kualitasnya baik untuk tubuh kamu."
THRICE_A_DAY = "Boleh sih makan cemilan 3 kali sehari, artinya porsi dan jenis cemilan yang kamu konsumsi harus diatur sedemikian rupa agar tidak melebihi kebutuhan energi dan zat gizi dalam sehari."
MORE_THAN_THRICE_A_DAY = "Hmmmm.... kalau makan cemilan lebih dari 3 kali dalam sehari, bisa-bisa asupan kalori kamu berlebih nih dalam sehari atau malah kurang karena seharian cuma makan cemilan. Yuk atur jam makan kamu agar lebih baik. Porsi dan jenis makanan yang kamu konsumsi juga menjadi kunci agar asupan energi dan zat gizi kamu sehat dan seimbang."
from django.contrib import admin
from .models import DietProfile
# Register your models here.
admin.site.register(DietProfile)
......@@ -204,9 +204,11 @@ def get_food_consumed_score(choice):
def get_total_vegetable_and_fruit_score(vegetable_choice, fruit_choice):
if (vegetable_choice < 1) or (vegetable_choice > 5) or (
fruit_choice < 1) or (fruit_choice > 5):
raise ValueError("Invalid choice number!")
if (vegetable_choice < 1) or (vegetable_choice > 5):
raise ValueError("Invalid vegetable choice number!")
if (fruit_choice < 1) or (fruit_choice > 5):
raise ValueError("Invalid fruit choice number!")
vegetable_score = get_food_consumed_score(vegetable_choice)
fruit_score = get_food_consumed_score(fruit_choice)
......@@ -218,16 +220,86 @@ def get_vegetable_and_fruit_sufficiency_response(
total_vegetable_and_fruit_score):
if (total_vegetable_and_fruit_score < 0) or (
total_vegetable_and_fruit_score > 8):
raise ValueError("Invalid total score!")
raise ValueError("Invalid total vegetable and fruit score!")
if (0 <= total_vegetable_and_fruit_score <= 4):
return VegetableAndFruitSufficiencyResponse.STILL_NOT_ENOUGH
return VegetableAndFruitSufficiencyResponse.LACKING
return VegetableAndFruitSufficiencyResponse.ENOUGH
def get_vegetable_and_fruit_diet_recommendation(
vegetable_and_fruit_suffiency_response):
if (vegetable_and_fruit_suffiency_response ==
VegetableAndFruitSufficiencyResponse.STILL_NOT_ENOUGH):
return VegetableAndFruitDietRecommendation.STILL_NOT_ENOUGH
VegetableAndFruitSufficiencyResponse.LACKING):
return VegetableAndFruitDietRecommendation.LACKING
return VegetableAndFruitDietRecommendation.ENOUGH
def get_total_sugar_salt_fat_score(
fried_food_choice,
sweet_snacks_choice,
sweet_drinks_choice,
packaged_food_choice):
if (fried_food_choice < 1) or (fried_food_choice > 5):
raise ValueError("Invalid fried food choice number!")
if (sweet_snacks_choice < 1) or (sweet_snacks_choice > 5):
raise ValueError("Invalid sweet snacks choice number!")
if (sweet_drinks_choice < 1) or (sweet_drinks_choice > 5):
raise ValueError("Invalid sweet drinks choice number!")
if (packaged_food_choice < 1) or (packaged_food_choice > 5):
raise ValueError("Invalid packaged food choice number!")
fried_food_score = get_food_consumed_score(fried_food_choice)
sweet_snacks_score = get_food_consumed_score(sweet_snacks_choice)
sweet_drinks_score = get_food_consumed_score(sweet_drinks_choice)
packaged_food_score = get_food_consumed_score(packaged_food_choice)
return fried_food_score + sweet_snacks_score + \
sweet_drinks_score + packaged_food_score
def get_sugar_salt_fat_problem_response(total_sugar_salt_fat_score):
if (total_sugar_salt_fat_score < 0) or (total_sugar_salt_fat_score > 12):
raise ValueError("Invalid total sugar, salt, fat score!")
if (0 <= total_sugar_salt_fat_score <= 5):
return SugarSaltFatProblemResponse.CONTROLLED
return SugarSaltFatProblemResponse.EXCESSIVE
def get_sugar_salt_fat_diet_recommendation(sugar_salt_fat_problem_response):
if (sugar_salt_fat_problem_response ==
SugarSaltFatProblemResponse.CONTROLLED):
return SugarSaltFatDietRecommendation.CONTROLLED
return SugarSaltFatDietRecommendation.EXCESSIVE
def get_large_meal_diet_recommendation(large_meal_choice):
if (large_meal_choice < 1) or (large_meal_choice > 4):
raise ValueError("Invalid large meal choice number!")
if (large_meal_choice == 1):
return LargeMealDietRecommendation.ONCE_A_DAY
if (large_meal_choice == 2):
return LargeMealDietRecommendation.TWICE_A_DAY
if (large_meal_choice == 3):
return LargeMealDietRecommendation.THRICE_A_DAY
return LargeMealDietRecommendation.MORE_THAN_THRICE_A_DAY
def get_snacks_diet_recommendation(snacks_choice):
if (snacks_choice < 1) or (snacks_choice > 5):
raise ValueError("Invalid snacks choice number!")
if (snacks_choice == 1):
return SnacksDietRecommendation.NO_SNACK
if (snacks_choice == 2):
return SnacksDietRecommendation.ONCE_A_DAY
if (snacks_choice == 3):
return SnacksDietRecommendation.TWICE_A_DAY
if (snacks_choice == 4):
return SnacksDietRecommendation.THRICE_A_DAY
return SnacksDietRecommendation.MORE_THAN_THRICE_A_DAY
......@@ -51,3 +51,6 @@ class DietProfile(models.Model):
choices=ProblemToSolve.choices)
health_problem = MultiSelectField(
choices=HealthProblem.choices)
def __str__(self):
return f"{self.name} - {self.email}"
......@@ -25,6 +25,11 @@ from .formulas import (
get_total_vegetable_and_fruit_score,
get_vegetable_and_fruit_sufficiency_response,
get_vegetable_and_fruit_diet_recommendation,
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,
)
......@@ -55,6 +60,9 @@ class DietelaQuizTests(APITestCase):
def test_diet_profile_saved(self):
self.assertEqual(DietProfile.objects.count(), 1)
def test_diet_profile_string(self):
self.assertEqual(str(self.dietProfile), 'test - test@test.com')
def test_get_diet_profile_api(self):
response = self.client.get('/dietela-quiz/diet-profile/')
......@@ -586,9 +594,18 @@ class FormulaTest(TestCase):
get_total_vegetable_and_fruit_score(
vegetable_choice, fruit_choice), 3)
def test_get_total_vegetable_and_fruit_score_with_invalid_choice(self):
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):
with self.assertRaises(ValueError):
vegetable_choice = 5
vegetable_choice = 3
fruit_choice = 0
get_total_vegetable_and_fruit_score(vegetable_choice, fruit_choice)
......@@ -599,7 +616,7 @@ class FormulaTest(TestCase):
self.assertEqual(
get_vegetable_and_fruit_sufficiency_response(total_vegetable_and_fruit_score),
VegetableAndFruitSufficiencyResponse.STILL_NOT_ENOUGH)
VegetableAndFruitSufficiencyResponse.LACKING)
def test_get_total_vegetable_and_fruit_sufficiency_response_with_valid_total_score_condition_2(
self):
......@@ -618,11 +635,11 @@ class FormulaTest(TestCase):
def test_get_vegetable_and_fruit_diet_recommendation_condition_1(
self):
vegetable_and_fruit_suffiency_response = VegetableAndFruitSufficiencyResponse.STILL_NOT_ENOUGH
vegetable_and_fruit_suffiency_response = VegetableAndFruitSufficiencyResponse.LACKING
self.assertEqual(
get_vegetable_and_fruit_diet_recommendation(vegetable_and_fruit_suffiency_response),
VegetableAndFruitDietRecommendation.STILL_NOT_ENOUGH)
VegetableAndFruitDietRecommendation.LACKING)
def test_get_vegetable_and_fruit_diet_recommendation_condition_2(
self):
......@@ -630,3 +647,199 @@ class FormulaTest(TestCase):
self.assertEqual(get_vegetable_and_fruit_diet_recommendation(
vegetable_and_fruit_suffiency_response), VegetableAndFruitDietRecommendation.ENOUGH)
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)
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