Some checks are pending
CI / Crawler (Python ${{ matrix.python-version }}) (3.10) (push) Waiting to run
CI / Crawler (Python ${{ matrix.python-version }}) (3.11) (push) Waiting to run
CI / API (Python 3.11) (push) Waiting to run
CI / Database migration (push) Waiting to run
CI / App web build (Node 20) (push) Waiting to run
156 lines
5.2 KiB
Python
156 lines
5.2 KiB
Python
import json
|
|
from pathlib import Path
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from crawlers import cmsolar, hyundai, kremc, nrems, sun_wms
|
|
|
|
|
|
FIXTURES = Path(__file__).resolve().parent / "fixtures"
|
|
|
|
|
|
def load_json(name):
|
|
with (FIXTURES / name).open(encoding="utf-8") as fixture_file:
|
|
return json.load(fixture_file)
|
|
|
|
|
|
def load_text(name):
|
|
return (FIXTURES / name).read_text(encoding="utf-8")
|
|
|
|
|
|
class FakeResponse:
|
|
def __init__(self, *, status_code=200, data=None, text="", headers=None):
|
|
self.status_code = status_code
|
|
self._data = data or {}
|
|
self.text = text
|
|
self.headers = headers or {}
|
|
self.encoding = None
|
|
|
|
def json(self):
|
|
return self._data
|
|
|
|
|
|
class SequenceSession:
|
|
def __init__(self, *, post_responses=None, get_responses=None):
|
|
self.post_responses = list(post_responses or [])
|
|
self.get_responses = list(get_responses or [])
|
|
|
|
def post(self, *_args, **_kwargs):
|
|
if not self.post_responses:
|
|
raise AssertionError("unexpected POST request")
|
|
return self.post_responses.pop(0)
|
|
|
|
def get(self, *_args, **_kwargs):
|
|
if not self.get_responses:
|
|
raise AssertionError("unexpected GET request")
|
|
return self.get_responses.pop(0)
|
|
|
|
|
|
class HistoryResponseFixtureTest(unittest.TestCase):
|
|
def test_nrems_daily_json_fixture(self):
|
|
session = SequenceSession(post_responses=[
|
|
FakeResponse(data=load_json("nrems_daily.json")),
|
|
])
|
|
plant = {
|
|
"id": "nrems-03",
|
|
"name": "3호기",
|
|
"auth": {"pscode": "fixture"},
|
|
"options": {"is_split": False},
|
|
}
|
|
|
|
with patch.object(nrems, "create_session", return_value=session):
|
|
result = nrems.fetch_history_daily(plant, "2026-08-05", "2026-08-05")
|
|
|
|
self.assertEqual(result, [{
|
|
"plant_id": "nrems-03",
|
|
"date": "2026-08-05",
|
|
"generation_kwh": 121.5,
|
|
}])
|
|
|
|
def test_kremc_daily_json_fixture(self):
|
|
session = SequenceSession(
|
|
post_responses=[FakeResponse(data=load_json("kremc_login.json"))],
|
|
get_responses=[FakeResponse(data=load_json("kremc_daily.json"))],
|
|
)
|
|
plant = {
|
|
"id": "kremc-05",
|
|
"name": "5호기",
|
|
"auth": {"user_id": "fixture", "password": "fixture"},
|
|
"system": {"login_url": "https://fixture/login", "api_base": "https://fixture"},
|
|
"options": {},
|
|
}
|
|
|
|
with patch.object(kremc, "create_session", return_value=session):
|
|
result = kremc.fetch_history_daily(plant, "2026-08-05", "2026-08-05")
|
|
|
|
self.assertEqual(result[0]["date"], "2026-08-05")
|
|
self.assertEqual(result[0]["generation_kwh"], 53.25)
|
|
|
|
def test_hyundai_daily_json_fixture(self):
|
|
session = SequenceSession(
|
|
post_responses=[FakeResponse(headers={"x-auth-token": "fixture-token"})],
|
|
get_responses=[FakeResponse(data=load_json("hyundai_daily.json"))],
|
|
)
|
|
plant = {
|
|
"id": "hyundai-08",
|
|
"name": "8호기",
|
|
"auth": {"user_id": "fixture", "password": "fixture", "site_id": "fixture"},
|
|
"system": {"base_url": "https://fixture", "login_path": "/login"},
|
|
}
|
|
|
|
with patch.object(hyundai, "create_session", return_value=session):
|
|
result = hyundai.fetch_history_daily(plant, "2026-08-05", "2026-08-05")
|
|
|
|
self.assertEqual(result, [{
|
|
"plant_id": "hyundai-08",
|
|
"date": "2026-08-05",
|
|
"generation_kwh": 133.6,
|
|
}])
|
|
|
|
def test_sun_wms_daily_html_fixture(self):
|
|
session = SequenceSession(
|
|
post_responses=[FakeResponse()],
|
|
get_responses=[FakeResponse(text=load_text("sun_wms_daily.html"))],
|
|
)
|
|
plant = {
|
|
"id": "sunwms-06",
|
|
"name": "6호기",
|
|
"auth": {"payload_id": "fixture", "payload_pw": "fixture"},
|
|
"system": {
|
|
"login_url": "https://fixture/login",
|
|
"base_url": "https://fixture",
|
|
"statics_url": "https://fixture/statics",
|
|
},
|
|
}
|
|
|
|
with patch("crawlers.base.create_session", return_value=session):
|
|
result = sun_wms.fetch_history_daily(plant, "2026-08-05", "2026-08-05")
|
|
|
|
self.assertEqual(result[0]["date"], "2026-08-05")
|
|
self.assertEqual(result[0]["generation_kwh"], 52.59)
|
|
|
|
def test_cmsolar_daily_html_fixture(self):
|
|
session = SequenceSession(
|
|
post_responses=[FakeResponse()],
|
|
get_responses=[
|
|
FakeResponse(),
|
|
FakeResponse(text=load_text("cmsolar_daily.html")),
|
|
],
|
|
)
|
|
plant = {
|
|
"id": "cmsolar-10",
|
|
"name": "10호기",
|
|
"auth": {"login_id": "fixture", "login_pw": "fixture", "site_no": "fixture"},
|
|
"system": {"login_url": "https://fixture/login", "api_url": "https://fixture"},
|
|
}
|
|
|
|
with patch.object(cmsolar, "create_session", return_value=session):
|
|
result = cmsolar.fetch_history_daily(plant, "2026-08-05", "2026-08-05")
|
|
|
|
self.assertEqual(result[0]["date"], "2026-08-05")
|
|
self.assertEqual(result[0]["generation_kwh"], 1051.25)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|