Beispiel #1
0
 def test_should_return_age_if_goal_is_zero(self):
     assert year_can_retire(30, 1, 1, 0) == 30
Beispiel #2
0
    def run_command(command):
        """
        Runs a given command and returns output as a string
        :param command:
        :return:
        """

        if not isinstance(command, str):
            return "Command must be type string!"

        if command == "":
            return "Need command.."

        args = command.split()
        valid_arguments = ['dist', 'bmi', 'emailval', 'retire', 'exit']

        if args[0] not in valid_arguments:
            return "Bad argument: " + args[0]

        # Email
        if args[0] == 'emailval':

            if len(args) != 2:
                return "emailval takes only 1 arguments"

            if EmailVerifier.is_valid_email(args[1]):
                return 'Valid email'
            else:
                return 'Invalid email'

        # Distance
        if args[0] == 'dist':

            if len(args) != 5:
                return "dist takes 4 arguments"

            for arg in args[1:]:
                try:
                    float(arg)
                except:
                    return "Invalid argument: " + arg

            x1 = float(args[1])
            y1 = float(args[2])
            x2 = float(args[3])
            y2 = float(args[4])

            dist = DistanceFormulaCalculator.compute_distance(x1, y1, x2, y2)

            return "Distance from ({0}, {1}) to ({2}, {3}) = {4}"\
                .format(x1, y1, x2, y2, dist)

        # Retirement
        if args[0] == 'retire':

            if len(args) != 5:
                return "dist takes 4 arguments"

            for arg in args[1:]:
                try:
                    float(arg)
                except:
                    return "Invalid argument: " + arg

            age = float(args[1])
            salary = float(args[2])
            saving = float(args[3])
            goal = float(args[4])

            try:
                retire_at = year_can_retire(age, salary, saving, goal)
                return "You can retire at: {0}".format(retire_at)
            except ValueError as err:
                return "Error running command:\n\t{0}".format(err)

        # BMI
        if args[0] == 'bmi':

            if len(args) != 4:
                return "bmi takes 3 arguments"

            for arg in args[1:]:
                try:
                    float(arg)
                except:
                    return "Invalid argument: " + arg

            feet = float(args[1])
            inches = float(args[2])
            weight = float(args[3])

            bmi = BodyMassIndexCalculator.calculate_bmi((feet * 12) + inches,
                                                        weight)
            category = BodyMassIndexCalculator.determine_bmi_category(bmi)

            return "You have a BMI of {0:.2f} making you {1}"\
                .format(bmi, category)

        if args[0] == 'exit':
            exit()
Beispiel #3
0
 def test_actually_works(self):
     assert year_can_retire(35, 100000, .1, 500000) == 60
Beispiel #4
0
 def test_should_raise_error_when_goal_is_not_a_number(self):
     with pytest.raises(ValueError) as excinfo:
         year_can_retire(0, 1, 1, "1")
     assert "Goal must be a number!" in str(excinfo.value)
Beispiel #5
0
 def test_should_throw_error_with_negative_goal(self):
     with pytest.raises(ValueError) as excinfo:
         year_can_retire(0, 1, 0, -1)
     assert "Can not have a negative goal" in str(excinfo.value)
Beispiel #6
0
 def test_should_raise_error_when_age_is_not_a_number(self):
     with pytest.raises(ValueError) as excinfo:
         year_can_retire("0", 1, 1, 1)
     assert "Age must be a number!" in str(excinfo.value)
Beispiel #7
0
 def test_should_throw_error_with_savings_larger_than_one(self):
     with pytest.raises(ValueError) as excinfo:
         year_can_retire(0, 1, 2, 1)
     assert "Saving must be bound between 0 and 1" in str(excinfo.value)
Beispiel #8
0
 def test_should_throw_error_with_negative_savings(self):
     with pytest.raises(ValueError) as excinfo:
         year_can_retire(10, 1, -1, 0)
     assert "Saving must be bound between 0 and 1" in str(excinfo.value)
Beispiel #9
0
 def test_should_throw_error_with_negative_salary(self):
     with pytest.raises(ValueError) as excinfo:
         year_can_retire(10, -1, 0, 0)
     assert "Salary must be greater than 0" in str(excinfo.value)
Beispiel #10
0
 def test_should_return_hundred_if_goal_is_positive_and_saving_is_0(self):
     assert year_can_retire(40, 1, 0, 1) == 100
Beispiel #11
0
 def test_return_one_hundred_if_too_old(self):
     assert year_can_retire(40, 40000, .1, 500000) == 100