示例#1
0
class TestBMIInit(unittest.TestCase):
    def setUp(self):
        """Setup function that will apply to all other test functions in the
        class. """
        # Weight that must be valid
        self.control_bmi_weight = 125.0
        # Height that must be valid
        self.control_bmi_height = 63.0
        # The known value for the bmi value given the above weight and height
        self.control_bmi_value = 22.7
        # The known value for the bmi category given the above weight and
        # height
        self.control_bmi_category = BodyMassIndexCategory.NORMAL_WEIGHT

        # BodyMassIndex object that must be valid
        self.control_bmi_object = BodyMassIndex()
        self.control_bmi_object.weight = self.control_bmi_weight
        self.control_bmi_object.height = self.control_bmi_height
        self.control_bmi_object.calculate_body_mass_index()

    def test_bmi_weight_init(self):
        """Tests initializing a BodyMassIndex object with varying weights."""

        # Case: Weight of 0
        # expected: ValueError
        with self.assertRaises(ValueError) as context:
            self.bmi_object = BodyMassIndex(0.0, self.control_bmi_height)
        self.assertEqual(
            str(context.exception),
            "Weight must be greater than 0 and less than or "
            "equal to 900 pounds.")

        # Case: Weight of 900 (boundary)
        # expected: ValueError
        with self.assertRaises(ValueError) as context:
            self.bmi_object = BodyMassIndex(901.0, self.control_bmi_height)
        self.assertEqual(
            str(context.exception),
            "Weight must be greater than 0 and less than or "
            "equal to 900 pounds.")

        # Case: Weight of 1000
        # expected: ValueError
        with self.assertRaises(ValueError) as context:
            self.bmi_object = BodyMassIndex(1000.0, self.control_bmi_height)
        self.assertEqual(
            str(context.exception),
            "Weight must be greater than 0 and less than or "
            "equal to 900 pounds.")

        # Case: Weight of invalid type (str)
        # expected: TypeError
        with self.assertRaises(TypeError) as context:
            self.bmi_object = BodyMassIndex("test", self.control_bmi_height)
        self.assertEqual(str(context.exception),
                         "Weight and height must be floating point integers.")

        # Case: Valid Weight
        # expected: Pass
        self.bmi_object = BodyMassIndex(self.control_bmi_weight,
                                        self.control_bmi_height)

    def test_bmi_init_height(self):
        """Tests initializing a BodyMassIndex object with varying heights."""
        # Case: Height of 0
        # expected: ValueError
        with self.assertRaises(ValueError) as context:
            self.bmi_object = BodyMassIndex(self.control_bmi_weight, 0.0)
        self.assertEqual(
            str(context.exception),
            "Height must be greater than 0 and less than or "
            "equal to 108 inches.")

        # Case: Height of 108 (boundary)
        # expected: ValueError
        with self.assertRaises(ValueError) as context:
            self.bmi_object = BodyMassIndex(self.control_bmi_weight, 109.0)
        self.assertEqual(
            str(context.exception),
            "Height must be greater than 0 and less than or "
            "equal to 108 inches.")

        # Case: Height of 200
        # expected: ValueError
        with self.assertRaises(ValueError) as context:
            self.bmi_object = BodyMassIndex(self.control_bmi_weight, 0.0)
        self.assertEqual(
            str(context.exception),
            "Height must be greater than 0 and less than or "
            "equal to 108 inches.")

        # Case: Height of invalid type (str)
        # expected: TypeError
        with self.assertRaises(TypeError) as context:
            self.bmi_object = BodyMassIndex(self.control_bmi_weight, "test")
        self.assertEqual(str(context.exception),
                         "Weight and height must be floating point integers.")

        # Case: Valid Weight
        # expected: Pass
        self.bmi_object = BodyMassIndex(self.control_bmi_weight,
                                        self.control_bmi_height)

    def test_bmi_init_validity(self):
        """Tests to ensure that a BodyMassIndex object initiated with
        control variables passed as arguments match up with control
        BodyMassIndex object.
        """
        # Case: Initialize object, compare to control case.
        # expected: pass
        self.bmi_object = BodyMassIndex(self.control_bmi_weight,
                                        self.control_bmi_height)

        self.assertEqual(self.bmi_object, self.control_bmi_object)

    def test_bmi_accessors(self):
        """Tests the accessors provided by the BodyMassIndex class."""
        self.assertEqual(self.control_bmi_height,
                         self.control_bmi_object.get_height())
        self.assertEqual(self.control_bmi_weight,
                         self.control_bmi_object.get_weight())
        self.assertAlmostEqual(self.control_bmi_value,
                               self.control_bmi_object.get_bmi_value())
        self.assertEqual(self.control_bmi_category,
                         self.control_bmi_object.get_bmi_category())
示例#2
0
class TestBMICalculations(unittest.TestCase):
    def setUp(self):
        """Setup function that will apply to all other test functions in the
        class. All BMI values are calculated using a calculator with the
        calculation given in http://extoxnet.orst.edu/faqs/dietcancer/web2
        /twohowto.html

        The height values will be kept consistently at 63. Each weight will
        change per case and the expected value and category for each case
        are set up beforehand.

        NOTE: This is essentially a one-dimensional test with the weight
        being the only thing that changes. This was decided upon given the
        characteristics of the equation, where verifying values of one
        dimension correlate to the other dimension also having successful
        results.
        """
        # A height that will be used for all test cases in order to have a
        # uniform test spread.
        self.control_height = 63.0

        # Values representing an obese BMI category.
        self.obese_weight = 190.0
        self.obese_value = 34.5
        self.obese_category = BodyMassIndexCategory.OBESE

        # Values representing the edge case between the overweight and obese
        # categories.
        self.overweight_obese_weight = 165.375
        self.overweight_obese_value = 30
        self.overweight_obese_category = BodyMassIndexCategory.OBESE

        # Values representing an overweight BMI category
        self.overweight_weight = 150.0
        self.overweight_value = 27.2
        self.overweight_category = BodyMassIndexCategory.OVER_WEIGHT

        # Values representing the edge case between the normal and
        # overweight categories.
        self.normal_overweight_weight = 137.81
        self.normal_overweight_value = 25
        self.normal_overweight_category = BodyMassIndexCategory.OVER_WEIGHT

        # Values representing a normal BMI category
        self.normal_weight = 120.0
        self.normal_value = 21.8
        self.normal_category = BodyMassIndexCategory.NORMAL_WEIGHT

        # Values representing the edge case between the underweight and
        # normal categories.
        self.underweight_normal_weight = 101.98
        self.underweight_normal_value = 18.5
        self.underweight_normal_category = BodyMassIndexCategory.NORMAL_WEIGHT

        # Values representing an underweight BMI category
        self.underweight_weight = 90.0
        self.underweight_value = 16.3
        self.underweight_category = BodyMassIndexCategory.UNDERWEIGHT

    def test_bmi_obese(self):
        """Tests the obese values case"""
        self.bmi_object = BodyMassIndex(self.obese_weight, self.control_height)

        self.assertAlmostEqual(self.bmi_object.get_bmi_value(),
                               self.obese_value)
        self.assertEqual(self.bmi_object.get_bmi_category(),
                         self.obese_category)

    def test_bmi_overweight_obese_edge(self):
        """Tests the overweight-obese value edge case"""
        self.bmi_object = BodyMassIndex(self.overweight_obese_weight,
                                        self.control_height)

        self.assertAlmostEqual(self.bmi_object.get_bmi_value(),
                               self.overweight_obese_value)
        self.assertEqual(self.bmi_object.get_bmi_category(),
                         self.overweight_obese_category)

    def test_bmi_overweight(self):
        """Tests the overweight values case"""
        self.bmi_object = BodyMassIndex(self.overweight_weight,
                                        self.control_height)

        self.assertAlmostEqual(self.bmi_object.get_bmi_value(),
                               self.overweight_value)
        self.assertEqual(self.bmi_object.get_bmi_category(),
                         self.overweight_category)

    def test_bmi_normal_overweight_edge(self):
        """Tests the normal-overweight value edge case"""
        self.bmi_object = BodyMassIndex(self.normal_overweight_weight,
                                        self.control_height)

        self.assertAlmostEqual(self.bmi_object.get_bmi_value(),
                               self.normal_overweight_value)
        self.assertEqual(self.bmi_object.get_bmi_category(),
                         self.normal_overweight_category)

    def test_bmi_normal(self):
        """Tests the normal values case"""
        self.bmi_object = BodyMassIndex(self.normal_weight,
                                        self.control_height)

        self.assertAlmostEqual(self.bmi_object.get_bmi_value(),
                               self.normal_value)
        self.assertEqual(self.bmi_object.get_bmi_category(),
                         self.normal_category)

    def test_bmi_underweight_normal_edge(self):
        """Tests the underweight-normal value edge case"""
        self.bmi_object = BodyMassIndex(self.underweight_normal_weight,
                                        self.control_height)

        self.assertAlmostEqual(self.bmi_object.get_bmi_value(),
                               self.underweight_normal_value)
        self.assertEqual(self.bmi_object.get_bmi_category(),
                         self.underweight_normal_category)

    def test_bmi_underweight(self):
        """Tests the underweight values case"""
        self.bmi_object = BodyMassIndex(self.underweight_weight,
                                        self.control_height)

        self.assertAlmostEqual(self.bmi_object.get_bmi_value(),
                               self.underweight_value)
        self.assertEqual(self.bmi_object.get_bmi_category(),
                         self.underweight_category)