|
| 1 | +import pytest |
| 2 | +from freezegun import freeze_time |
| 3 | +from rest_framework import status |
| 4 | +from rest_framework.test import APIClient |
| 5 | + |
| 6 | +from core import api_examples |
| 7 | +from core import models |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture(autouse=True) |
| 11 | +def frozen_time(): |
| 12 | + with freeze_time("2025-06-25 01:02:03"): |
| 13 | + yield |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture() |
| 17 | +def client(): |
| 18 | + client = APIClient() |
| 19 | + return client |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture() |
| 23 | +def automation(db, pattern_instance) -> models.Automation: |
| 24 | + automation = models.Automation.objects.create( |
| 25 | + automation_type=api_examples.automation_response.value["automation_type"], |
| 26 | + automation_id=api_examples.automation_response.value["automation_id"], |
| 27 | + pattern_instance=pattern_instance, |
| 28 | + primary=api_examples.automation_response.value["primary"], |
| 29 | + ) |
| 30 | + return automation |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture() |
| 34 | +def controller_label(db) -> models.ControllerLabel: |
| 35 | + controller_label = models.ControllerLabel.objects.create( |
| 36 | + label_id=api_examples.controller_label_response.value["label_id"] |
| 37 | + ) |
| 38 | + return controller_label |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture() |
| 42 | +def pattern(db) -> models.Pattern: |
| 43 | + pattern = models.Pattern.objects.create( |
| 44 | + collection_name=api_examples.pattern_post.value["collection_name"], |
| 45 | + collection_version=api_examples.pattern_post.value["collection_version"], |
| 46 | + pattern_name=api_examples.pattern_post.value["pattern_name"], |
| 47 | + ) |
| 48 | + return pattern |
| 49 | + |
| 50 | + |
| 51 | +@pytest.fixture() |
| 52 | +def pattern_instance(db, pattern) -> models.PatternInstance: |
| 53 | + pattern_instance = models.PatternInstance.objects.create( |
| 54 | + credentials=api_examples.pattern_instance_post.value["credentials"], |
| 55 | + executors=api_examples.pattern_instance_post.value["executors"], |
| 56 | + organization_id=1, |
| 57 | + pattern=pattern, |
| 58 | + ) |
| 59 | + return pattern_instance |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture() |
| 63 | +def task(db) -> models.Task: |
| 64 | + task = models.Task.objects.create( |
| 65 | + status=api_examples.task_response.value["status"], |
| 66 | + details=api_examples.task_response.value["details"], |
| 67 | + ) |
| 68 | + return task |
| 69 | + |
| 70 | + |
| 71 | +def test_retrieve_automation_success(client, automation): |
| 72 | + url = f"/api/pattern-service/v1/automations/{automation.pk}/" |
| 73 | + response = client.get(url) |
| 74 | + assert response.status_code == status.HTTP_200_OK |
| 75 | + assert response.json() == api_examples.automation_response.value |
| 76 | + |
| 77 | + |
| 78 | +def test_list_automations_success(client, automation): |
| 79 | + url = "/api/pattern-service/v1/automations/" |
| 80 | + response = client.get(url) |
| 81 | + assert response.status_code == status.HTTP_200_OK |
| 82 | + assert response.json() == [api_examples.automation_response.value] |
| 83 | + |
| 84 | + |
| 85 | +def test_retrieve_controller_label_success(client, controller_label): |
| 86 | + url = f"/api/pattern-service/v1/controller_labels/{controller_label.pk}/" |
| 87 | + response = client.get(url) |
| 88 | + assert response.status_code == status.HTTP_200_OK |
| 89 | + assert response.json() == api_examples.controller_label_response.value |
| 90 | + |
| 91 | + |
| 92 | +def test_list_controller_labels_success(client, controller_label): |
| 93 | + url = "/api/pattern-service/v1/controller_labels/" |
| 94 | + response = client.get(url) |
| 95 | + assert response.status_code == status.HTTP_200_OK |
| 96 | + assert response.json() == [api_examples.controller_label_response.value] |
| 97 | + |
| 98 | + |
| 99 | +def test_create_pattern_success(client, db): |
| 100 | + url = "/api/pattern-service/v1/patterns/" |
| 101 | + data = api_examples.pattern_post.value |
| 102 | + response = client.post(url, data, format="json") |
| 103 | + assert response.status_code == status.HTTP_202_ACCEPTED |
| 104 | + assert response.json() == api_examples.pattern_post_response.value |
| 105 | + |
| 106 | + |
| 107 | +def test_retrieve_pattern_success(client, pattern): |
| 108 | + url = f"/api/pattern-service/v1/patterns/{pattern.pk}/" |
| 109 | + response = client.get(url) |
| 110 | + assert response.status_code == status.HTTP_200_OK |
| 111 | + assert response.json() == api_examples.pattern_response.value |
| 112 | + |
| 113 | + |
| 114 | +def test_list_patterns_success(client, pattern): |
| 115 | + url = "/api/pattern-service/v1/patterns/" |
| 116 | + response = client.get(url) |
| 117 | + assert response.status_code == status.HTTP_200_OK |
| 118 | + assert response.json() == [api_examples.pattern_response.value] |
| 119 | + |
| 120 | + |
| 121 | +def test_create_pattern_instance_success(client, pattern): |
| 122 | + url = "/api/pattern-service/v1/pattern_instances/" |
| 123 | + data = api_examples.pattern_instance_post.value |
| 124 | + data["pattern"] = pattern.pk |
| 125 | + response = client.post(url, data, format="json") |
| 126 | + assert response.status_code == status.HTTP_202_ACCEPTED |
| 127 | + assert response.json() == api_examples.pattern_instance_post_response.value |
| 128 | + |
| 129 | + |
| 130 | +def test_retrieve_pattern_instance_success(client, controller_label, pattern_instance): |
| 131 | + pattern_instance.controller_labels.add(controller_label) |
| 132 | + url = f"/api/pattern-service/v1/pattern_instances/{pattern_instance.pk}/" |
| 133 | + response = client.get(url) |
| 134 | + assert response.status_code == status.HTTP_200_OK |
| 135 | + assert response.json() == api_examples.pattern_instance_response.value |
| 136 | + |
| 137 | + |
| 138 | +def test_list_pattern_instances_success(client, controller_label, pattern_instance): |
| 139 | + pattern_instance.controller_labels.add(controller_label) |
| 140 | + url = "/api/pattern-service/v1/pattern_instances/" |
| 141 | + response = client.get(url) |
| 142 | + assert response.status_code == status.HTTP_200_OK |
| 143 | + assert response.json() == [api_examples.pattern_instance_response.value] |
| 144 | + |
| 145 | + |
| 146 | +def test_retrieve_task_success(client, task): |
| 147 | + url = f"/api/pattern-service/v1/tasks/{task.pk}/" |
| 148 | + response = client.get(url) |
| 149 | + assert response.status_code == status.HTTP_200_OK |
| 150 | + assert response.json() == api_examples.task_response.value |
| 151 | + |
| 152 | + |
| 153 | +def test_list_tasks_success(client, task): |
| 154 | + url = "/api/pattern-service/v1/tasks/" |
| 155 | + response = client.get(url) |
| 156 | + assert response.status_code == status.HTTP_200_OK |
| 157 | + assert response.json() == [api_examples.task_response.value] |
0 commit comments