From c66ced016341b0ac72ac9c8a003afff2090c54b2 Mon Sep 17 00:00:00 2001 From: Akshay B Shetty Date: Tue, 10 Oct 2023 07:17:34 +0000 Subject: [PATCH] test: :white_check_mark: handled input validation for values less than 0 --- physics/boyles_law.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/physics/boyles_law.py b/physics/boyles_law.py index 868da6df2..81d5059e6 100644 --- a/physics/boyles_law.py +++ b/physics/boyles_law.py @@ -58,6 +58,11 @@ def check_validity(values: dict[str, float]) -> None: ... ValueError: Invalid input k is not a valid variable + >>> check_validity({'v1':2,'v2':4,'p1':-6}) + Traceback (most recent call last): + ... + ValueError: Invalid input p1 must be greater than 0 + >>> check_validity({'v1':2,'v2':4,'p1':6}) """ @@ -69,6 +74,9 @@ def check_validity(values: dict[str, float]) -> None: if value not in valid_variables: msg = f"Invalid input {value} is not a valid variable" raise ValueError(msg) + if values[value] <= 0: + msg = f"Invalid input {value} must be greater than 0" + raise ValueError(msg) return @@ -92,6 +100,11 @@ def find_target_variable(values: dict[str, float]) -> str: ... ValueError: Invalid input k is not a valid variable + >>> find_target_variable({'v1':1,'v2':-2,'p2':4}) + Traceback (most recent call last): + ... + ValueError: Invalid input v2 must be greater than 0 + """ check_validity(values) for variable in valid_variables: @@ -123,6 +136,11 @@ def boyles_law(values: dict[str, float]) -> dict[str, str]: ... ValueError: Invalid input k is not a valid variable + >>> boyles_law({'p1':2,'v2':1, 'v1':-6}) + Traceback (most recent call last): + ... + ValueError: Invalid input v1 must be greater than 0 + >>> boyles_law({'p1':100,'v2':150, 'v1':120}) {'p2': '80.0 Pa'}