|
7 | 7 | import os |
8 | 8 | import pytest |
9 | 9 | from flask import Flask |
| 10 | +from unittest.mock import patch |
10 | 11 | from api.api_server import app as flask_app |
11 | 12 |
|
12 | 13 |
|
@@ -165,3 +166,39 @@ def test_global_edgar_with_window_parameter(self, client, auth_headers): |
165 | 166 | trend = data["trend"] |
166 | 167 | assert "pollutant" in trend |
167 | 168 | assert trend["pollutant"] == "NOx" |
| 169 | + |
| 170 | + # --- Test Case Baru untuk Endpoint CAMPD --- |
| 171 | + |
| 172 | + @patch('api.routes.global_data.CAMDClient') |
| 173 | + def test_global_campd_success(self, MockCAMDClient, client, auth_headers): |
| 174 | + """Test /global/campd returns 200 with valid facility_id.""" |
| 175 | + # Siapkan mock untuk mengembalikan data contoh |
| 176 | + mock_instance = MockCAMDClient.return_value |
| 177 | + mock_instance.get_emissions_data.return_value = [{"year": 2023, "co2Mass": 5000}] |
| 178 | + mock_instance.get_compliance_data.return_value = [{"year": 2023, "compliantIndicator": 1}] |
| 179 | + |
| 180 | + resp = client.get("/global/campd?facility_id=123", headers=auth_headers) |
| 181 | + |
| 182 | + assert resp.status_code == 200 |
| 183 | + data = resp.get_json() |
| 184 | + assert data["status"] == "success" |
| 185 | + assert "emissions" in data |
| 186 | + assert "compliance" in data |
| 187 | + assert data["emissions"][0]["co2Mass"] == 5000 |
| 188 | + assert data["compliance"][0]["compliantIndicator"] == 1 |
| 189 | + |
| 190 | + def test_global_campd_missing_param(self, client, auth_headers): |
| 191 | + """Test /global/campd returns 400 if facility_id is missing.""" |
| 192 | + resp = client.get("/global/campd", headers=auth_headers) |
| 193 | + assert resp.status_code == 400 |
| 194 | + data = resp.get_json() |
| 195 | + assert data["status"] == "error" |
| 196 | + assert "facility_id parameter is required" in data["message"] |
| 197 | + |
| 198 | + def test_global_campd_invalid_param(self, client, auth_headers): |
| 199 | + """Test /global/campd returns 400 if facility_id is not a number.""" |
| 200 | + resp = client.get("/global/campd?facility_id=abc", headers=auth_headers) |
| 201 | + assert resp.status_code == 400 |
| 202 | + data = resp.get_json() |
| 203 | + assert data["status"] == "error" |
| 204 | + assert "A valid facility_id parameter is required" in data["message"] |
0 commit comments