Skip to content

Commit

Permalink
Merge pull request #21 from hudsonbrendon/feat/alerts
Browse files Browse the repository at this point in the history
feat: add method alerts
  • Loading branch information
hudsonbrendon authored Jan 30, 2025
2 parents 741f535 + 01c9a77 commit 65b3cc2
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 0 deletions.
46 changes: 46 additions & 0 deletions solar_plus_intelbras/solar_plus_intelbras.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,49 @@ def inverters(self, plant_id: int, limit: int = 20, page: int = 1) -> dict:
params=params,
)
return response.json()

def alerts(
self,
plant_id: int,
start_date: str,
end_date: str,
limit: int = 20,
page: int = 1,
) -> dict:
"""Return the alerts.
Args:
plant_id (int): A plant id.
start_date (str): A start date.
end_date (str): An end date.
limit (int, optional): A limit. Defaults to 20.
page (int, optional): A page. Defaults to 1.
Returns:
dict: A dictionary with the alerts.
"""
params = {
"limit": limit,
"page": page,
}

if start_date:
try:
datetime.strptime(start_date, "%Y-%m-%d")
params["start_date"] = start_date
except ValueError:
raise ValueError("start_date must be in the format YYYY-MM-DD.")

if end_date:
try:
datetime.strptime(end_date, "%Y-%m-%d")
params["end_date"] = end_date
except ValueError:
raise ValueError("end_date must be in the format YYYY-MM-DD.")

response = requests.get(
f"{self.base_api_url}{EndpointEnum.PLANTS.value}/{plant_id}/alerts",
headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
params=params,
)
return response.json()
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,3 +913,16 @@ def inverters() -> dict:
}
],
}


@pytest.fixture
def alerts() -> dict:
return {
"limit": 20,
"page": 1,
"sort": "",
"total_rows": 5,
"total_rows_in_this_page": 0,
"total_pages": 0,
"rows": [],
}
167 changes: 167 additions & 0 deletions tests/test_solar_plus_intelbras.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,3 +623,170 @@ def test_shouldnt_return_inverters_float_page(
with pytest.raises(Exception) as exc:
solar_plus_intelbras.inverters(plant_id=1, limit=20, page=1.0)
assert str(exc.value) == "page must be an integer."


class TestSolarPlusIntelbrasAlerts:
def test_should_return_alerts(
self,
requests_mock: Mocker,
solar_plus_intelbras: SolarPlusIntelbras,
alerts: dict,
login_response: dict,
) -> None:
requests_mock.post(
"https://ens-server.intelbras.com.br/api/login",
json=login_response,
status_code=200,
)

requests_mock.get(
"https://ens-server.intelbras.com.br/api/plants/1/alerts?start_date=2025-01-23&end_date=2025-01-23&limit=20&page=1",
json=alerts,
status_code=200,
)
assert (
solar_plus_intelbras.alerts(
plant_id=1,
start_date="2025-01-23",
end_date="2025-01-23",
limit=20,
page=1,
)
== alerts
)

def test_shouldnt_return_alerts_without_plant_id(
self,
solar_plus_intelbras: SolarPlusIntelbras,
) -> None:
with pytest.raises(Exception) as exc:
solar_plus_intelbras.alerts()
assert str(exc.value) == "plant_id must be an integer."

def test_shouldnt_return_alerts_string_plant_id(
self,
solar_plus_intelbras: SolarPlusIntelbras,
) -> None:
with pytest.raises(Exception) as exc:
solar_plus_intelbras.alerts(plant_id="1")
assert str(exc.value) == "plant_id must be an integer."

def test_shouldnt_return_alerts_float_plant_id(
self,
solar_plus_intelbras: SolarPlusIntelbras,
) -> None:
with pytest.raises(Exception) as exc:
solar_plus_intelbras.alerts(plant_id=1.0)
assert str(exc.value) == "plant_id must be an integer."

def test_shouldnt_return_alerts_without_start_date(
self,
solar_plus_intelbras: SolarPlusIntelbras,
) -> None:
with pytest.raises(Exception) as exc:
solar_plus_intelbras.alerts(plant_id=1)
assert str(exc.value) == "start_date must be in the format YYYY-MM-DD."

def test_shouldnt_return_alerts_without_end_date(
self,
solar_plus_intelbras: SolarPlusIntelbras,
) -> None:
with pytest.raises(Exception) as exc:
solar_plus_intelbras.alerts(plant_id=1, start_date="2025-01-23")
assert str(exc.value) == "end_date must be in the format YYYY-MM-DD."

def test_shouldnt_return_alerts_without_limit(
self,
solar_plus_intelbras: SolarPlusIntelbras,
) -> None:
with pytest.raises(Exception) as exc:
solar_plus_intelbras.alerts(
plant_id=1, start_date="2025-01-23", end_date="2025-01-23"
)
assert str(exc.value) == "limit must be an integer."

def test_shouldnt_return_alerts_string_limit(
self,
solar_plus_intelbras: SolarPlusIntelbras,
) -> None:
with pytest.raises(Exception) as exc:
solar_plus_intelbras.alerts(
plant_id=1, start_date="2025-01-23", end_date="2025-01-23", limit="20"
)
assert str(exc.value) == "limit must be an integer."

def test_shouldnt_return_alerts_float_limit(
self,
solar_plus_intelbras: SolarPlusIntelbras,
) -> None:
with pytest.raises(Exception) as exc:
solar_plus_intelbras.alerts(
plant_id=1, start_date="2025-01-23", end_date="2025-01-23", limit=20.0
)
assert str(exc.value) == "limit must be an integer."

def test_shouldnt_return_alerts_without_page(
self,
solar_plus_intelbras: SolarPlusIntelbras,
) -> None:
with pytest.raises(Exception) as exc:
solar_plus_intelbras.alerts(
plant_id=1, start_date="2025-01-23", end_date="2025-01-23", limit=20
)
assert str(exc.value) == "page must be an integer."

def test_shouldnt_return_alerts_string_page(
self,
solar_plus_intelbras: SolarPlusIntelbras,
) -> None:
with pytest.raises(Exception) as exc:
solar_plus_intelbras.alerts(
plant_id=1,
start_date="2025-01-23",
end_date="2025-01-23",
limit=20,
page="1",
)
assert str(exc.value) == "page must be an integer."

def test_shouldnt_return_alerts_float_page(
self,
solar_plus_intelbras: SolarPlusIntelbras,
) -> None:
with pytest.raises(Exception) as exc:
solar_plus_intelbras.alerts(
plant_id=1,
start_date="2025-01-23",
end_date="2025-01-23",
limit=20,
page=1.0,
)
assert str(exc.value) == "page must be an integer."

def test_shouldnt_return_alerts_with_start_date_invalid(
self,
solar_plus_intelbras: SolarPlusIntelbras,
) -> None:
with pytest.raises(Exception) as exc:
solar_plus_intelbras.alerts(
plant_id=1,
start_date="2025/01/23",
end_date="2025-01-23",
limit=20,
page=1,
)
assert str(exc.value) == "start_date must be in the format YYYY-MM-DD."

def test_shouldnt_return_alerts_with_end_date_invalid(
self,
solar_plus_intelbras: SolarPlusIntelbras,
) -> None:
with pytest.raises(Exception) as exc:
solar_plus_intelbras.alerts(
plant_id=1,
start_date="2025-01-23",
end_date="2025/01/23",
limit=20,
page=1,
)
assert str(exc.value) == "end_date must be in the format YYYY-MM-DD."

0 comments on commit 65b3cc2

Please sign in to comment.