Skip to content

Commit b0d92a2

Browse files
committed
Add mock for openai and builtin open functions
1 parent ce8a660 commit b0d92a2

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

app/tests/conftest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
@pytest.fixture
1313
def client():
14-
with TestClient(app) as client:
15-
yield client
14+
# with TestClient(app) as client:
15+
# yield client
16+
return TestClient(app)
1617

1718

1819
@pytest.fixture

app/tests/test_iac_template.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
from unittest.mock import patch
1+
from unittest.mock import MagicMock, patch, mock_open
22

33

4-
class TestTerraformTemplates:
4+
class TestIaCTemplates:
55
def setup_method(self):
6-
self.mock_execute_pythonfile = patch('app.main.execute_pythonfile').start()
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+
)
10+
11+
self.mock_execute_python_file = patch('app.main.execute_pythonfile').start()
712
self.mock_edit_directory_generator = patch('app.main.edit_directory_generator').start()
8-
self.mock_gpt_service = patch('app.main.gpt_service').start()
9-
self.mock_gpt_service.return_value = 'Generated Python Code'
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()
1016

1117
self.iac_template_docker_url = '/IaC-template/docker'
1218
self.iac_template_ec2_url = '/IaC-template/aws/ec2'
@@ -21,7 +27,6 @@ def teardown_method(self):
2127

2228
def test_iac_template_docker(self, client, iac_template_docker_sample_input):
2329
response = client.post(self.iac_template_docker_url, json=iac_template_docker_sample_input)
24-
print(response.json())
2530
assert response.status_code == 200
2631

2732
def test_iac_template_ec2(self, client, iac_template_ec2_sample_input):

0 commit comments

Comments
 (0)