Skip to content

Commit 062c2fb

Browse files
committed
test(main) rm check response content
1 parent 81ffe5c commit 062c2fb

File tree

5 files changed

+73
-120
lines changed

5 files changed

+73
-120
lines changed

app/tests/test_helm_template.py

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
from unittest.mock import patch
33
from fastapi.testclient import TestClient
44
from 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

77
client = TestClient(app)
8-
98
@pytest.fixture
109
def 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
3840
def 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()

app/tests/test_iac_basic.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from app.models import IaCBasicInput
66

77
client = TestClient(app)
8-
mocked_gpt_response = "Mocked GPT response for IaC-basic"
98

109
@pytest.fixture
1110
def valid_iac_basic_data():
@@ -16,19 +15,21 @@ def valid_iac_basic_data():
1615
max_tokens=500
1716
)
1817

19-
@patch('app.main.gpt_service', return_value=mocked_gpt_response)
18+
@patch('app.main.gpt_service')
2019
def test_iac_basic_generation(mock_gpt_service, valid_iac_basic_data):
2120
"""
22-
Test the /IaC-basic/ endpoint with valid input data to ensure correct output.
21+
Test the /IaC-basic/ endpoint with valid input data to ensure it returns a 200 status code.
2322
"""
23+
mock_gpt_service.return_value = "Mocked GPT response for IaC-basic"
24+
2425
response = client.post("/IaC-basic/", json=valid_iac_basic_data.model_dump())
2526
assert response.status_code == 200
26-
assert response.json() == {"output": mocked_gpt_response}
27-
28-
@patch('app.main.gpt_service')
29-
def test_basic_generation(mock_gpt_service):
27+
28+
29+
@patch('app.main.gpt_service')
30+
def test_basic_generation_invalid_service(mock_gpt_service):
3031
"""
31-
Test the /IaC-basic/ endpoint with an invalid service to ensure proper validation.
32+
Test the /IaC-basic/ endpoint with an invalid service to ensure it returns a 422 status code.
3233
"""
3334
invalid_input = {
3435
"input": "Create a basic configuration",
@@ -38,15 +39,4 @@ def test_basic_generation(mock_gpt_service):
3839
}
3940

4041
response = client.post("/IaC-basic/", json=invalid_input)
41-
assert response.status_code == 422, f"Expected status code 422, got {response.status_code}"
42-
assert "detail" in response.json(), "Response JSON does not contain 'detail'"
43-
errors = response.json()["detail"]
44-
expected_error_loc = ["body", "service"]
45-
expected_error_msg = "Service must be one of ['terraform']."
46-
assert any(
47-
error["loc"] == expected_error_loc and error_msg in error["msg"]
48-
for error in errors
49-
for error_msg in [expected_error_msg]
50-
), f"Expected error message '{expected_error_msg}' at location {expected_error_loc}, but not found."
51-
52-
mock_gpt_service.assert_not_called()
42+
assert response.status_code == 422

app/tests/test_iac_bugfix.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from app.models import IaCBugfixInput
66

77
client = TestClient(app)
8-
mocked_gpt_response = "Mocked GPT response for IaC-bugfix"
98

109
@pytest.fixture
1110
def valid_bugfix_data():
@@ -17,35 +16,29 @@ def valid_bugfix_data():
1716
max_tokens=500
1817
)
1918

20-
@patch('app.main.gpt_service', return_value=mocked_gpt_response)
19+
@patch('app.main.gpt_service')
2120
def test_bugfix(mock_gpt_service, valid_bugfix_data):
2221
"""
23-
Test the /IaC-bugfix/ endpoint with valid input data to ensure correct output.
22+
Test the /IaC-bugfix/ endpoint with valid input data to ensure it returns a 200 status code.
2423
"""
24+
mock_gpt_service.return_value = "Mocked GPT response for IaC-bugfix"
25+
2526
response = client.post("/IaC-bugfix/", json=valid_bugfix_data.model_dump())
2627
assert response.status_code == 200
27-
assert response.json() == {"output": mocked_gpt_response}
2828

29-
@patch('app.main.gpt_service')
29+
30+
@patch('app.main.gpt_service')
3031
def test_bugfix_invalid(mock_gpt_service):
3132
"""
32-
Test the /IaC-bugfix/ endpoint with a single invalid input to ensure proper validation.
33+
Test the /IaC-bugfix/ endpoint with invalid input data to ensure it returns a 422 status code.
3334
"""
3435
invalid_input = {
35-
"bug_description": "", # Emptydescription
36+
"bug_description": "", # Empty description
3637
"version": "latest",
3738
"service": "terraform",
3839
"min_tokens": 100,
3940
"max_tokens": 500
4041
}
42+
4143
response = client.post("/IaC-bugfix/", json=invalid_input)
42-
assert response.status_code == 422, f"Expected status code 422, got {response.status_code}"
43-
assert "detail" in response.json(), "Response JSON does not contain 'detail'"
44-
errors = response.json()["detail"]
45-
expected_error_loc = ["body", "bug_description"]
46-
expected_error_msg = "Bug description cannot be empty."
47-
assert any(
48-
error["loc"] == expected_error_loc and expected_error_msg in error["msg"]
49-
for error in errors
50-
), f"Expected error message '{expected_error_msg}' at location {expected_error_loc}, but not found."
51-
mock_gpt_service.assert_not_called()
44+
assert response.status_code == 422

app/tests/test_iac_install.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from app.models import IaCInstallationInput
66

77
client = TestClient(app)
8-
mocked_gpt_response = "Mocked shell script for installing terraform on Ubuntu."
98

109
@pytest.fixture
1110
def valid_installation_data():
@@ -15,37 +14,29 @@ def valid_installation_data():
1514
min_tokens=100,
1615
max_tokens=500
1716
)
18-
@patch('app.main.gpt_service', return_value=mocked_gpt_response)
17+
18+
@patch('app.main.gpt_service')
1919
def test_install(mock_gpt_service, valid_installation_data):
2020
"""
21-
Test the /IaC-install/ endpoint with valid input data to ensure correct output.
21+
Test the /IaC-install/ endpoint with valid input data to ensure it returns a 200 status code.
2222
"""
23+
mock_gpt_service.return_value = "Mocked shell script for installing terraform on Ubuntu."
24+
2325
response = client.post("/IaC-install/", json=valid_installation_data.model_dump())
2426
assert response.status_code == 200
25-
assert response.json() == {"output": mocked_gpt_response}
27+
2628

2729
@patch('app.main.gpt_service')
2830
def test_install_invalid(mock_gpt_service):
2931
"""
30-
Test the /IaC-install/ endpoint with an invalid 'os' value to ensure proper validation.
32+
Test the /IaC-install/ endpoint with an invalid 'os' value to ensure it returns a 422 status code.
3133
"""
3234
invalid_input = {
33-
"os": "Kali",
34-
"service": "terraform"
35+
"os": "Kali", # Unsupported OS
36+
"service": "terraform",
37+
"min_tokens": 100,
38+
"max_tokens": 500
3539
}
3640

3741
response = client.post("/IaC-install/", json=invalid_input)
38-
39-
assert response.status_code == 422, f"Expected status code 422, got {response.status_code}"
40-
assert "detail" in response.json(), "Response JSON does not contain 'detail'"
41-
errors = response.json()["detail"]
42-
43-
expected_error_loc = ["body", "os"]
44-
expected_error_msg = "OS must be one of ['ubuntu', 'centos', 'debian']."
45-
46-
assert any(
47-
error["loc"] == expected_error_loc and error_msg in error["msg"]
48-
for error in errors
49-
for error_msg in [expected_error_msg]
50-
), f"Expected error message '{expected_error_msg}' at location {expected_error_loc}, but not found."
51-
mock_gpt_service.assert_not_called()
42+
assert response.status_code == 422

app/tests/test_iac_template.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,35 @@
99
@pytest.fixture
1010
def sample_iac_template_input():
1111
return IaCTemplateGeneration(
12-
service="terraform",
12+
service="terraform",
1313
CI_integration=True,
14-
base_config="ec2"
14+
base_config="ec2"
1515
)
1616

1717
@patch("app.main.gpt_service")
1818
@patch("app.main.edit_directory_generator")
1919
@patch("app.main.execute_pythonfile")
2020
def test_template(mock_execute_pythonfile, mock_edit_directory_generator, mock_gpt_service, sample_iac_template_input):
21+
"""
22+
Test the /IaC-template/ endpoint with valid input data to ensure it returns a 200 status code.
23+
"""
2124
mock_gpt_service.return_value = "Generated Python Code"
2225

2326
response = client.post("/IaC-template/", json=sample_iac_template_input.model_dump())
24-
2527
assert response.status_code == 200
26-
assert response.json()["output"] == "output"
2728

28-
mock_gpt_service.assert_called_once()
29-
mock_edit_directory_generator.assert_called_once_with("terraform_generator", "Generated Python Code")
30-
mock_execute_pythonfile.assert_called_once_with("MyTerraform", "terraform_generator")
3129

3230
@patch("app.main.gpt_service")
3331
@patch("app.main.edit_directory_generator")
3432
@patch("app.main.execute_pythonfile")
3533
def test_template_invalid(mock_execute_pythonfile, mock_edit_directory_generator, mock_gpt_service):
3634
"""
37-
Test the /IaC-template/ endpoint with an invalid 'base_config' to ensure proper validation.
35+
Test the /IaC-template/ endpoint with an invalid 'base_config' value to ensure it returns a 422 status code.
3836
"""
3937
invalid_input = {
4038
"CI_integration": True,
41-
"base_config": "k8s",
39+
"base_config": "k8s", # Unsupported base_config
4240
"service": "terraform"
4341
}
4442
response = client.post("/IaC-template/", json=invalid_input)
45-
assert response.status_code == 422, f"Expected status code 422, got {response.status_code}"
46-
assert "detail" in response.json(), "Response JSON does not contain 'detail'"
47-
errors = response.json()["detail"]
48-
expected_error_loc = ["body", "base_config"]
49-
expected_error_msg = "Base config must be one of ['ec2', 's3', 'rds']."
50-
assert any(
51-
error["loc"] == expected_error_loc and expected_error_msg in error["msg"]
52-
for error in errors
53-
), f"Expected error message '{expected_error_msg}' at location {expected_error_loc}, but not found."
54-
mock_gpt_service.assert_not_called()
55-
mock_edit_directory_generator.assert_not_called()
56-
mock_execute_pythonfile.assert_not_called()
43+
assert response.status_code == 422

0 commit comments

Comments
 (0)