|
1 | | -import pytest |
2 | | -from unittest.mock import patch |
3 | | -from fastapi.testclient import TestClient |
4 | | -from app.main import app |
5 | | -from app.models import HelmTemplateGeneration, Pod, Persistance, Ingress, Environment |
| 1 | +from unittest.mock import MagicMock, patch, mock_open |
6 | 2 |
|
7 | | -client = TestClient(app) |
8 | | -@pytest.fixture |
9 | | -def sample_helm_template_input(): |
10 | | - """ |
11 | | - Provides a valid HelmTemplateGeneration object as input for tests. |
12 | | - """ |
13 | | - return HelmTemplateGeneration( |
14 | | - api_version=3, |
15 | | - pods=[ |
16 | | - Pod( |
17 | | - name="nginx", |
18 | | - image="nginx:latest", |
19 | | - target_port=80, |
20 | | - replicas=2, |
21 | | - persistance=Persistance(enabled=False), |
22 | | - environment=[Environment(name="DEBUG", value="true")], |
23 | | - stateless=True, |
24 | | - ingress=Ingress(enabled=True) |
25 | | - ), |
26 | | - Pod( |
27 | | - name="redis", |
28 | | - image="redis:latest", |
29 | | - target_port=6379, |
30 | | - replicas=1, |
31 | | - persistance=Persistance(enabled=True), |
32 | | - environment=[], |
33 | | - stateless=False, |
34 | | - ingress=Ingress(enabled=False) |
35 | | - ) |
36 | | - ] |
37 | | - ) |
38 | 3 |
|
39 | | -@pytest.fixture |
40 | | -def invalid_input(): |
41 | | - """ |
42 | | - Provides an invalid input dictionary for tests, containing various validation errors. |
43 | | - """ |
44 | | - return { |
45 | | - "api_version": 0, |
46 | | - "pods": [ |
47 | | - { |
48 | | - "name": "", |
49 | | - "image": "nginx:latest", |
50 | | - "target_port": 70000, |
51 | | - "replicas": 0, |
52 | | - "persistance": {"size": "invalid_size", "accessModes": "InvalidMode"}, |
53 | | - "environment": [{"name": "", "value": ""}], |
54 | | - "stateless": True, |
55 | | - "ingress": {"enabled": True, "host": ""} |
56 | | - } |
57 | | - ] |
58 | | - } |
| 4 | +class TestHelmTemplate: |
| 5 | + def setup_method(self): |
| 6 | + mock_client_instance = MagicMock() |
| 7 | + mock_client_instance.chat.completions.create.return_value = MagicMock( |
| 8 | + choices=[MagicMock(message=MagicMock(content='Mocked OpenAI Response'))] |
| 9 | + ) |
59 | 10 |
|
60 | | -@patch("app.main.execute_pythonfile") |
61 | | -@patch("app.main.edit_directory_generator") |
62 | | -@patch("app.main.gpt_service") |
63 | | -def test_helm_template_generation( |
64 | | - mock_gpt_service, |
65 | | - mock_edit_directory_generator, |
66 | | - mock_execute_pythonfile, |
67 | | - sample_helm_template_input |
68 | | -): |
69 | | - """ |
70 | | - Tests the /Helm-template/ endpoint with valid input. |
71 | | - """ |
72 | | - |
73 | | - mock_gpt_service.return_value = "Generated Python Code" |
74 | | - response = client.post("/Helm-template/", json=sample_helm_template_input.model_dump()) |
75 | | - assert response.status_code == 200 |
76 | | - mock_gpt_service.assert_called_once() |
77 | | - mock_edit_directory_generator.assert_called_once_with("helm_generator", "Generated Python Code") |
78 | | - mock_execute_pythonfile.assert_called_once_with("MyHelm", "helm_generator") |
| 11 | + self.mock_execute_python_file = patch('app.main.execute_pythonfile').start() |
| 12 | + self.mock_edit_directory_generator = patch('app.main.edit_directory_generator').start() |
| 13 | + self.mock_gpt_service = patch('app.main.gpt_service', return_value='Mocked GPT Response').start() |
| 14 | + self.mock_openai = patch('app.gpt_services.OpenAI', return_value=mock_client_instance).start() |
| 15 | + self.mock_builtin_open = patch('builtins.open', mock_open()).start() |
| 16 | + self.mock_shutil_rm = patch('shutil.rmtree').start() |
79 | 17 |
|
80 | | -def test_helm_invalid_api(invalid_input): |
81 | | - """ |
82 | | - Tests the /Helm-template/ endpoint with an invalid API version. |
83 | | - """ |
84 | | - invalid_input = invalid_input.copy() |
85 | | - invalid_input["api_version"] = 0 |
| 18 | + self.url = '/Helm-template/' |
86 | 19 |
|
| 20 | + def teardown_method(self): |
| 21 | + patch.stopall() |
87 | 22 |
|
88 | | - response = client.post("/Helm-template/", json=invalid_input) |
| 23 | + def test_helm_template_generation(self, client, helm_template_sample_input): |
| 24 | + response = client.post(self.url, json=helm_template_sample_input) |
| 25 | + assert response.status_code == 200 |
89 | 26 |
|
90 | | - assert response.status_code == 422 |
| 27 | + def test_helm_invalid_api(self, client, helm_template_invalid_sample_input): |
| 28 | + resource = client.post(self.url, json=helm_template_invalid_sample_input) |
| 29 | + assert resource.status_code == 422 |
91 | 30 |
|
92 | | - |
93 | | -@patch("app.main.execute_pythonfile") |
94 | | -@patch("app.main.edit_directory_generator") |
95 | | -@patch("app.main.gpt_service") |
96 | | -def test_helm_invalid_port( |
97 | | - mock_gpt_service, |
98 | | - mock_edit_directory_generator, |
99 | | - mock_execute_pythonfile, |
100 | | - invalid_input |
101 | | -): |
102 | | - """ |
103 | | - Tests the /Helm-template/ endpoint with an invalid target port. |
104 | | - """ |
105 | | - invalid_input = invalid_input.copy() |
106 | | - invalid_input["pods"][0]["target_port"] = 70000 |
107 | | - response = client.post("/Helm-template/", json=invalid_input) |
108 | | - assert response.status_code == 422 |
109 | | - mock_gpt_service.assert_not_called() |
110 | | - mock_edit_directory_generator.assert_not_called() |
111 | | - mock_execute_pythonfile.assert_not_called() |
| 31 | + def test_helm_invalid_port(self, client, helm_template_invalid_sample_input): |
| 32 | + helm_template_invalid_sample_input['pods'][0]['target_port'] = 70000 |
| 33 | + resource = client.post(self.url, json=helm_template_invalid_sample_input) |
| 34 | + assert resource.status_code == 422 |
0 commit comments