mirror of
https://github.com/maoakeEnterprise/amazing.git
synced 2026-04-28 16:04:35 +02:00
adding the unit testing and modify some function for my unit testing its pretty good i learn how to get a good struct for my classes
This commit is contained in:
@@ -18,5 +18,5 @@ lint-strict:
|
|||||||
uv run flake8 .
|
uv run flake8 .
|
||||||
uv run mypy . --strict
|
uv run mypy . --strict
|
||||||
|
|
||||||
run_test:
|
run_test_parsing:
|
||||||
PYTHONPATH=src uv run python3 test/test_parsing.py
|
PYTHONPATH=src uv run pytest tests/test_parsing.py
|
||||||
|
|||||||
+1
-1
@@ -3,4 +3,4 @@ HEIGHT=100
|
|||||||
ENTRY=0,0
|
ENTRY=0,0
|
||||||
EXIT=19,14
|
EXIT=19,14
|
||||||
OUTPUT_FILE=maze.txt
|
OUTPUT_FILE=maze.txt
|
||||||
PERFECT=Truee
|
PERFECT=True
|
||||||
+61
-50
@@ -1,15 +1,12 @@
|
|||||||
class DataMaze:
|
class DataMaze:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_data(name_file: str) -> str:
|
def get_file_data(name_file: str) -> str:
|
||||||
data = ""
|
with open(name_file, "r") as file:
|
||||||
try:
|
data = file.read()
|
||||||
with open(name_file, "r") as file:
|
if data == "":
|
||||||
data = file.read()
|
raise ValueError("The file is empty")
|
||||||
except FileNotFoundError:
|
return data
|
||||||
print("The file do not exist")
|
|
||||||
finally:
|
|
||||||
return data
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def transform_data(data: str) -> dict:
|
def transform_data(data: str) -> dict:
|
||||||
@@ -23,63 +20,77 @@ class DataMaze:
|
|||||||
return data_t
|
return data_t
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def verif_key_data(data: dict) -> dict:
|
def verif_key_data(data: dict) -> None:
|
||||||
key_test = {
|
key_test = {
|
||||||
"WIDTH", "HEIGHT", "ENTRY", "EXIT", "OUTPUT_FILE", "PERFECT"
|
"WIDTH", "HEIGHT", "ENTRY", "EXIT", "OUTPUT_FILE", "PERFECT"
|
||||||
}
|
}
|
||||||
set_key = {
|
set_key = {
|
||||||
key for key in data.keys()
|
key for key in data.keys()
|
||||||
}
|
}
|
||||||
try:
|
if len(set_key) != len(key_test):
|
||||||
res_key = {}
|
raise KeyError("Missing some data the len do not correspond")
|
||||||
if len(set_key) != len(key_test):
|
res_key = {key for key in set_key if key not in key_test}
|
||||||
raise Exception("Missing some data the len do not correspond")
|
if len(res_key) != 0:
|
||||||
res_key = {key for key in set_key if key not in key_test}
|
raise KeyError("Some Key "
|
||||||
if len(res_key) != 0:
|
f"do not correspond the keys: {res_key}")
|
||||||
raise Exception("Some Key "
|
|
||||||
f"do not correspond the keys: {res_key}")
|
|
||||||
return data
|
|
||||||
except Exception as e:
|
|
||||||
print(f"{e}")
|
|
||||||
exit()
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def verif_value_data(data: dict):
|
def convert_values(data: dict):
|
||||||
key_int = {"WIDTH", "HEIGHT"}
|
key_int = {"WIDTH", "HEIGHT"}
|
||||||
key_tuple = {"ENTRY", "EXIT"}
|
key_tuple = {"ENTRY", "EXIT"}
|
||||||
key_bool = {"PERFECT"}
|
key_bool = {"PERFECT"}
|
||||||
|
res: dict = {}
|
||||||
|
for key in key_int:
|
||||||
|
res.update({key: int(data[key])})
|
||||||
|
for key in key_tuple:
|
||||||
|
res.update({key: DataMaze.convert_tuple(data[key])})
|
||||||
|
for key in key_bool:
|
||||||
|
res.update({key: DataMaze.convert_bool(data[key])})
|
||||||
|
return res
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_data_maze(name_file: str) -> dict:
|
||||||
try:
|
try:
|
||||||
res: dict = {}
|
data_str = DataMaze.get_file_data(name_file)
|
||||||
for key in key_int:
|
data_dict = DataMaze.transform_data(data_str)
|
||||||
res.update({key: int(data[key])})
|
DataMaze.verif_key_data(data_dict)
|
||||||
for key in key_tuple:
|
data_maze = DataMaze.convert_values(data_dict)
|
||||||
res.update({key: DataMaze.convert_tuple(data[key])})
|
return data_maze
|
||||||
for key in key_bool:
|
except FileNotFoundError:
|
||||||
res.update({key: DataMaze.convert_bool(data[key])})
|
print("The file do not exist")
|
||||||
return res
|
exit()
|
||||||
|
except PermissionError:
|
||||||
|
print("We dont have the Permission")
|
||||||
|
exit()
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
print("Error on the method verif_value_data"
|
print(f"Error during the convert or the file is empty: {e}")
|
||||||
f" in the class DataMaze: {e}")
|
exit()
|
||||||
|
except KeyError as e:
|
||||||
|
print(f"Error on the key in the file: {e}")
|
||||||
|
exit()
|
||||||
|
except IndexError as e:
|
||||||
|
print("In the function transform Data some data cannot "
|
||||||
|
f"be splited by '=' because '=' was not present: {e}")
|
||||||
|
exit()
|
||||||
|
except AttributeError as e:
|
||||||
|
print("Error on the "
|
||||||
|
f"funciton get_data_maze : {e}")
|
||||||
|
exit()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def convert_tuple(data: str) -> tuple:
|
def convert_tuple(data: str) -> tuple:
|
||||||
try:
|
data_t = data.split(",")
|
||||||
data_t = data.split(",")
|
if len(data_t) != 2:
|
||||||
x, y = data_t
|
raise ValueError("There is too much "
|
||||||
tup = (int(x), int(y))
|
"argument in the coordinate given")
|
||||||
return tup
|
x, y = data_t
|
||||||
except ValueError as e:
|
tup = (int(x), int(y))
|
||||||
print(f"On the convert Tuple: {e}")
|
return tup
|
||||||
exit()
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def convert_bool(data: str) -> bool:
|
def convert_bool(data: str) -> bool:
|
||||||
try:
|
if data != "True" and data != "False":
|
||||||
if data != "True" and data != "False":
|
raise ValueError("This is not True or False")
|
||||||
raise ValueError("This is not True or False")
|
if data == "True":
|
||||||
if data == "True":
|
return True
|
||||||
return True
|
return False
|
||||||
return False
|
|
||||||
except ValueError as e:
|
|
||||||
print(f"Error on the convert_bool in class DataMaze: {e}")
|
|
||||||
exit()
|
|
||||||
|
|||||||
+64
-9
@@ -1,14 +1,69 @@
|
|||||||
from parsing.Parsing import DataMaze
|
from parsing.Parsing import DataMaze
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
class TestParsing:
|
||||||
print("Unit Testing for parsing:")
|
|
||||||
data = DataMaze.get_data("config.txt")
|
|
||||||
data_t = DataMaze.transform_data(data)
|
|
||||||
data_t = DataMaze.verif_key_data(data_t)
|
|
||||||
data_t = DataMaze.verif_value_data(data_t)
|
|
||||||
print(data_t)
|
|
||||||
|
|
||||||
|
def test_get_data_valid(self):
|
||||||
|
data = DataMaze.get_file_data("tests/test_txt/config_1.txt")
|
||||||
|
assert isinstance(data, str) is True
|
||||||
|
|
||||||
if __name__ == "__main__":
|
def test_file_error(self):
|
||||||
main()
|
with pytest.raises(FileNotFoundError):
|
||||||
|
DataMaze.get_file_data("tete")
|
||||||
|
|
||||||
|
def test_permission_error(self):
|
||||||
|
with pytest.raises(PermissionError):
|
||||||
|
DataMaze.get_file_data("tests/test_txt/error_1.txt")
|
||||||
|
|
||||||
|
def test_empty_file_error(self):
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
DataMaze.get_file_data("tests/test_txt/error_6.txt")
|
||||||
|
|
||||||
|
def test_transform_data_valid(self):
|
||||||
|
data = DataMaze.get_file_data("tests/test_txt/config_1.txt")
|
||||||
|
data_2 = DataMaze.transform_data(data)
|
||||||
|
assert isinstance(data_2, dict)
|
||||||
|
|
||||||
|
def test_transform__index_error(self):
|
||||||
|
with pytest.raises(IndexError):
|
||||||
|
DataMaze.transform_data("asdasdasdasdasdasda\nasdasdas=asdasd")
|
||||||
|
|
||||||
|
def test_key_data_error(self):
|
||||||
|
with pytest.raises(KeyError):
|
||||||
|
data = DataMaze.get_file_data("tests/test_txt/error_8.txt")
|
||||||
|
data2 = DataMaze.transform_data(data)
|
||||||
|
DataMaze.verif_key_data(data2)
|
||||||
|
|
||||||
|
def test_key_data_error_2(self):
|
||||||
|
with pytest.raises(KeyError):
|
||||||
|
data = DataMaze.get_file_data("tests/test_txt/error_9.txt")
|
||||||
|
data2 = DataMaze.transform_data(data)
|
||||||
|
DataMaze.verif_key_data(data2)
|
||||||
|
|
||||||
|
def test_convert_int(self):
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
data = DataMaze.get_file_data("tests/test_txt/error_2.txt")
|
||||||
|
data2 = DataMaze.transform_data(data)
|
||||||
|
DataMaze.convert_values(data2)
|
||||||
|
|
||||||
|
def test_tuple_error(self):
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
DataMaze.convert_tuple("0,3,5,5")
|
||||||
|
|
||||||
|
def test_tuple_error1(self):
|
||||||
|
with pytest.raises(AttributeError):
|
||||||
|
DataMaze.convert_tuple(None)
|
||||||
|
|
||||||
|
def test_bool_error(self):
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
DataMaze.convert_bool("Trueeee")
|
||||||
|
|
||||||
|
def test_valid_tuple(self):
|
||||||
|
assert DataMaze.convert_tuple("7534564654, 78") == (7534564654, 78)
|
||||||
|
|
||||||
|
def test_valid_bool(self):
|
||||||
|
assert DataMaze.convert_bool("False") is False
|
||||||
|
|
||||||
|
def test_valid_bool1(self):
|
||||||
|
assert DataMaze.convert_bool("True") is True
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
WIDTH=200
|
||||||
|
HEIGHT=100
|
||||||
|
ENTRY=0,0
|
||||||
|
EXIT=19,14
|
||||||
|
OUTPUT_FILE=maze.txt
|
||||||
|
PERFECT=True
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
WIDTH=200
|
||||||
|
HEIGHT=100
|
||||||
|
ENTRY=0,0
|
||||||
|
EXIT=19,14
|
||||||
|
OUTPUT_FILE=maze.txt
|
||||||
|
PERFECT=True
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
WIDTH=caca
|
||||||
|
HEIGHT=100
|
||||||
|
ENTRY=0,0
|
||||||
|
EXIT=19,14
|
||||||
|
OUTPUT_FILE=maze.txt
|
||||||
|
PERFECT=True
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
WIDTH=200
|
||||||
|
HEIGHT=100
|
||||||
|
ENTRY=0,0
|
||||||
|
EXIT=19,14
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
WIDTH=200
|
||||||
|
HEIGHT=100
|
||||||
|
ENTRY=0,0
|
||||||
|
EXIT=19,14
|
||||||
|
OUTPUT_FILE=maze.txt
|
||||||
|
PERFECT=True
|
||||||
|
PIPI=tut
|
||||||
Reference in New Issue
Block a user