22from unittest .mock import patch
33from fastapi .testclient import TestClient
44from app .main import app
5- from app .models import HelmTemplateGeneration , Pod , Persistance , Ingress , Environment , Output
5+ from app .models import HelmTemplateGeneration , Pod , Persistance , Ingress , Environment
66
77client = TestClient (app )
8-
98@pytest .fixture
109def sample_helm_template_input ():
10+ """
11+ Provides a valid HelmTemplateGeneration object as input for tests.
12+ """
1113 return HelmTemplateGeneration (
1214 api_version = 3 ,
1315 pods = [
@@ -36,18 +38,21 @@ def sample_helm_template_input():
3638
3739@pytest .fixture
3840def invalid_input ():
41+ """
42+ Provides an invalid input dictionary for tests, containing various validation errors.
43+ """
3944 return {
40- "api_version" : 0 , # Invalid api_version
45+ "api_version" : 0 ,
4146 "pods" : [
4247 {
43- "name" : "" , # Invalid: empty name
48+ "name" : "" ,
4449 "image" : "nginx:latest" ,
45- "target_port" : 70000 , # Invalid: exceeds 65535
46- "replicas" : 0 , # Invalid: less than 1
47- "persistance" : {"size" : "invalid_size" , "accessModes" : "InvalidMode" }, # Invalid size and accessModes
48- "environment" : [{"name" : "" , "value" : "" }], # Invalid: empty name and value
50+ "target_port" : 70000 ,
51+ "replicas" : 0 ,
52+ "persistance" : {"size" : "invalid_size" , "accessModes" : "InvalidMode" },
53+ "environment" : [{"name" : "" , "value" : "" }],
4954 "stateless" : True ,
50- "ingress" : {"enabled" : True , "host" : "" } # Invalid: empty host
55+ "ingress" : {"enabled" : True , "host" : "" }
5156 }
5257 ]
5358 }
@@ -61,39 +66,29 @@ def test_helm_template_generation(
6166 mock_execute_pythonfile ,
6267 sample_helm_template_input
6368):
69+ """
70+ Tests the /Helm-template/ endpoint with valid input.
71+ """
72+
6473 mock_gpt_service .return_value = "Generated Python Code"
65-
6674 response = client .post ("/Helm-template/" , json = sample_helm_template_input .model_dump ())
67-
6875 assert response .status_code == 200
69- assert response .json ()["output" ] == "output"
70-
7176 mock_gpt_service .assert_called_once ()
7277 mock_edit_directory_generator .assert_called_once_with ("helm_generator" , "Generated Python Code" )
7378 mock_execute_pythonfile .assert_called_once_with ("MyHelm" , "helm_generator" )
7479
75- @patch ("app.main.execute_pythonfile" )
76- @patch ("app.main.edit_directory_generator" )
77- @patch ("app.main.gpt_service" )
78- def test_helm_invalid_api (
79- mock_gpt_service ,
80- mock_edit_directory_generator ,
81- mock_execute_pythonfile ,
82- invalid_input
83- ):
80+ def test_helm_invalid_api (invalid_input ):
81+ """
82+ Tests the /Helm-template/ endpoint with an invalid API version.
83+ """
8484 invalid_input = invalid_input .copy ()
8585 invalid_input ["api_version" ] = 0
86+
87+
8688 response = client .post ("/Helm-template/" , json = invalid_input )
87- assert response .status_code == 422
88- assert "detail" in response .json ()
89- errors = response .json ()["detail" ]
90- assert any (
91- error ["loc" ] == ["body" , "api_version" ] and "API version must be a positive integer." in error ["msg" ]
92- for error in errors
93- )
94- mock_gpt_service .assert_not_called ()
95- mock_edit_directory_generator .assert_not_called ()
96- mock_execute_pythonfile .assert_not_called ()
89+
90+ assert response .status_code == 422
91+
9792
9893@patch ("app.main.execute_pythonfile" )
9994@patch ("app.main.edit_directory_generator" )
@@ -102,18 +97,15 @@ def test_helm_invalid_port(
10297 mock_gpt_service ,
10398 mock_edit_directory_generator ,
10499 mock_execute_pythonfile ,
105- invalid_input ):
106-
100+ invalid_input
101+ ):
102+ """
103+ Tests the /Helm-template/ endpoint with an invalid target port.
104+ """
107105 invalid_input = invalid_input .copy ()
108- invalid_input ["pods" ][0 ]["target_port" ] = 70000 # Invalid target_port
106+ invalid_input ["pods" ][0 ]["target_port" ] = 70000
109107 response = client .post ("/Helm-template/" , json = invalid_input )
110108 assert response .status_code == 422
111- assert "detail" in response .json ()
112- errors = response .json ()["detail" ]
113- assert any (
114- error ["loc" ] == ["body" , "pods" , 0 , "target_port" ] and "Target port must be between 1 and 65535." in error ["msg" ]
115- for error in errors
116- )
117109 mock_gpt_service .assert_not_called ()
118110 mock_edit_directory_generator .assert_not_called ()
119111 mock_execute_pythonfile .assert_not_called ()
0 commit comments