forked from devopshobbies/devops-gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request devopshobbies#91 from arasheghdam84/test/update-mo…
…dels Update terraform model tests. Define fixtures in conftest.
- Loading branch information
Showing
2 changed files
with
99 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import pytest | ||
|
||
from fastapi.testclient import TestClient | ||
|
||
from app.main import app | ||
from app.models import ( | ||
IaCTemplateGenerationDocker, IaCTemplateGenerationEC2, IaCTemplateGenerationS3, IaCTemplateGenerationIAM, | ||
IaCTemplateGenerationArgoCD, IaCTemplateGenerationELB, IaCTemplateGenerationEFS, SyncPolicy, ArgoApplication | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def client(): | ||
with TestClient(app) as client: | ||
yield client | ||
|
||
|
||
@pytest.fixture | ||
def iac_template_docker_sample_input(): | ||
return IaCTemplateGenerationDocker().model_dump() | ||
|
||
|
||
@pytest.fixture | ||
def iac_template_ec2_sample_input(): | ||
return IaCTemplateGenerationEC2().model_dump() | ||
|
||
|
||
@pytest.fixture | ||
def iac_template_s3_sample_input(): | ||
return IaCTemplateGenerationS3().model_dump() | ||
|
||
|
||
@pytest.fixture | ||
def iac_template_iam_sample_input(): | ||
return IaCTemplateGenerationIAM().model_dump() | ||
|
||
|
||
@pytest.fixture | ||
def iac_template_argocd_sample_input(): | ||
sync_policy = SyncPolicy() | ||
argocd_application = ArgoApplication(sync_policy=sync_policy) | ||
return IaCTemplateGenerationArgoCD(argocd_application=argocd_application).model_dump() | ||
|
||
|
||
@pytest.fixture | ||
def iac_template_elb_sample_input(): | ||
return IaCTemplateGenerationELB().model_dump() | ||
|
||
|
||
@pytest.fixture | ||
def iac_template_efs_sample_input(): | ||
return IaCTemplateGenerationEFS().model_dump() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,48 @@ | ||
import pytest | ||
from unittest.mock import patch | ||
from fastapi.testclient import TestClient | ||
from app.main import app | ||
from app.models import IaCTemplateGeneration | ||
|
||
client = TestClient(app) | ||
|
||
@pytest.fixture | ||
def sample_iac_template_input(): | ||
return IaCTemplateGeneration( | ||
service="terraform", | ||
CI_integration=True, | ||
base_config="ec2" | ||
) | ||
|
||
@patch("app.main.gpt_service") | ||
@patch("app.main.edit_directory_generator") | ||
@patch("app.main.execute_pythonfile") | ||
def test_template(mock_execute_pythonfile, mock_edit_directory_generator, mock_gpt_service, sample_iac_template_input): | ||
""" | ||
Test the /IaC-template/ endpoint with valid input data to ensure it returns a 200 status code. | ||
""" | ||
mock_gpt_service.return_value = "Generated Python Code" | ||
|
||
response = client.post("/IaC-template/", json=sample_iac_template_input.model_dump()) | ||
assert response.status_code == 200 | ||
|
||
|
||
@patch("app.main.gpt_service") | ||
@patch("app.main.edit_directory_generator") | ||
@patch("app.main.execute_pythonfile") | ||
def test_template_invalid(mock_execute_pythonfile, mock_edit_directory_generator, mock_gpt_service): | ||
""" | ||
Test the /IaC-template/ endpoint with an invalid 'base_config' value to ensure it returns a 422 status code. | ||
""" | ||
invalid_input = { | ||
"CI_integration": True, | ||
"base_config": "k8s", # Unsupported base_config | ||
"service": "terraform" | ||
} | ||
response = client.post("/IaC-template/", json=invalid_input) | ||
assert response.status_code == 422 | ||
|
||
|
||
class TestTerraformTemplates: | ||
def setup_method(self): | ||
self.mock_execute_pythonfile = patch('app.main.execute_pythonfile').start() | ||
self.mock_edit_directory_generator = patch('app.main.edit_directory_generator').start() | ||
self.mock_gpt_service = patch('app.main.gpt_service').start() | ||
self.mock_gpt_service.return_value = 'Generated Python Code' | ||
|
||
self.iac_template_docker_url = '/IaC-template/docker' | ||
self.iac_template_ec2_url = '/IaC-template/aws/ec2' | ||
self.iac_template_s3_url = '/IaC-template/aws/s3' | ||
self.iac_template_iam_url = '/IaC-template/aws/iam' | ||
self.iac_template_argocd_url = '/IaC-template/argocd' | ||
self.iac_template_elb_url = '/IaC-template/aws/elb' | ||
self.iac_template_efs_url = '/IaC-template/aws/efs' | ||
|
||
def teardown_method(self): | ||
patch.stopall() | ||
|
||
def test_iac_template_docker(self, client, iac_template_docker_sample_input): | ||
response = client.post(self.iac_template_docker_url, json=iac_template_docker_sample_input) | ||
assert response.status_code == 200 | ||
|
||
def test_iac_template_ec2(self, client, iac_template_ec2_sample_input): | ||
response = client.post(self.iac_template_ec2_url, json=iac_template_ec2_sample_input) | ||
assert response.status_code == 200 | ||
|
||
def test_iac_template_s3(self, client, iac_template_s3_sample_input): | ||
response = client.post(self.iac_template_s3_url, json=iac_template_s3_sample_input) | ||
assert response.status_code == 200 | ||
|
||
def test_iac_template_iam(self, client, iac_template_iam_sample_input): | ||
response = client.post(self.iac_template_iam_url, json=iac_template_iam_sample_input) | ||
assert response.status_code == 200 | ||
|
||
def test_iac_template_argocd(self, client, iac_template_argocd_sample_input): | ||
response = client.post(self.iac_template_argocd_url, json=iac_template_argocd_sample_input) | ||
assert response.status_code == 200 | ||
|
||
def test_iac_template_elb(self, client, iac_template_elb_sample_input): | ||
response = client.post(self.iac_template_elb_url, json=iac_template_elb_sample_input) | ||
assert response.status_code == 200 | ||
|
||
def test_iac_template_efs(self, client, iac_template_efs_sample_input): | ||
response = client.post(self.iac_template_efs_url, json=iac_template_efs_sample_input) | ||
assert response.status_code == 200 |