Skip to content

Commit

Permalink
Merge pull request #43 from klejejs/feat/automated-tests-against-api
Browse files Browse the repository at this point in the history
Add automated test workflow against Thermia API
  • Loading branch information
klejejs authored Nov 29, 2024
2 parents 860fd01 + a49536a commit cf437b4
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
31 changes: 31 additions & 0 deletions .github/workflows/automated-api-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Automated API test that runs against Thermia API and checks for breaking changes

on:
schedule:
# run every six hours
- cron: '0 */6 * * *'
workflow_dispatch:

jobs:
test_api:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/[email protected]
- name: Set up Python
uses: actions/[email protected]
with:
python-version: "3.x"
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
pip install -U -r requirements.txt
pip install -U ThermiaOnlineAPI
- name: Create .env file
run: |
touch .env
echo "USERNAME=${{ secrets.THERMIA_USERNAME }}" >> .env
echo "PASSWORD=${{ secrets.THERMIA_PASSWORD }}" >> .env
- name: Create a debug log file
run: |
python scripts/test_api.py
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ jobs:
python -m pip install --upgrade pip
pip install -U -r requirements.txt -r requirements_testing.txt
- name: Run tests
run: pytest
run: pytest ThermiaOnlineAPI
88 changes: 88 additions & 0 deletions scripts/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import os
from ThermiaOnlineAPI import Thermia

TEST_EXCLUDED_LINE_STRINGS = [
# self.__info related
"lastOnline",
"activeAlarms",
"activeCriticalAlarms",
"unreadErrors",
"unreadInfo",
"unreadWarnings",
# self.__status related
"dcmVersion",
"heatingEffect",
"hotWaterTemperature",
"indoorTemperature",
"isHotWaterActive",
"isOutdoorTempSensorFunctioning",
"outdoorTemperature",
"programVersion",
"reducedHeatingEffect",
# self.__device_data related
"firmwareVersion",
"lastOnline",
# Register group related
"registerValue",
"timeStamp",
"value",
]


def test_excluded_string_in_line(line: str) -> bool:
for excluded_string in TEST_EXCLUDED_LINE_STRINGS:
if excluded_string in line:
return True
return False


USERNAME = None
PASSWORD = None

with open(".env", "r") as env_file:
for line in env_file:
if line.startswith("USERNAME="):
USERNAME = line.split("=")[1].strip()
elif line.startswith("PASSWORD="):
PASSWORD = line.split("=")[1].strip()

if not USERNAME or not PASSWORD:
print("Username and password not present in .env file")
exit(1)

thermia = Thermia(USERNAME, PASSWORD)

print("Connected: " + str(thermia.connected))

heat_pump = thermia.heat_pumps[0]

print("Fetching debug data")

debug_data = heat_pump.debug()

print("Comparing debug data to existing debug file")

absolute_path = os.path.dirname(os.path.abspath(__file__))
existing_data_filename = (
f"{absolute_path}/../ThermiaOnlineAPI/tests/debug_files/diplomat_duo_921.txt"
)

with open("debug.txt", "r") as f:
existing_debug_data = f.read()

for [existing_line, new_line] in zip(
existing_debug_data.split("\n"), debug_data.split("\n")
):
if test_excluded_string_in_line(existing_line) and test_excluded_string_in_line(
new_line
):
continue

if existing_line != new_line:
print("Existing data does not match new data")
print("Existing line: " + existing_line)
print("New line: " + new_line)
print("\n")
exit(1)

print("Debug data matches existing debug file")

0 comments on commit cf437b4

Please sign in to comment.