import unittest from unittest.mock import patch from crawlers import cmsolar, hyundai, kremc, nrems, sun_wms class FakeResponse: def __init__(self, status_code=500, data=None, headers=None): self.status_code = status_code self._data = data or {} self.headers = headers or {} self.text = "" self.encoding = None def json(self): return self._data class FakeSession: def __init__(self, response): self.response = response def post(self, *_args, **_kwargs): return self.response def get(self, *_args, **_kwargs): return self.response class HistoryFetchContractTest(unittest.TestCase): def test_nrems_http_failure_returns_none(self): plant = { "id": "nrems-03", "name": "3호기", "auth": {"pscode": "x"}, "options": {"is_split": False}, } with patch.object(nrems, "create_session", return_value=FakeSession(FakeResponse())): self.assertIsNone(nrems.fetch_history_daily(plant, "2026-08-05", "2026-08-05")) def test_nrems_successful_empty_response_returns_empty_list(self): plant = { "id": "nrems-03", "name": "3호기", "auth": {"pscode": "x"}, "options": {"is_split": False}, } response = FakeResponse(status_code=200, data={"pdata": []}) with patch.object(nrems, "create_session", return_value=FakeSession(response)): self.assertEqual( nrems.fetch_history_daily(plant, "2026-08-05", "2026-08-05"), [], ) def test_kremc_login_failure_returns_none(self): plant = { "id": "kremc-05", "name": "5호기", "auth": {}, "system": {}, "options": {}, } with patch.object(kremc, "create_session", return_value=FakeSession(FakeResponse())): self.assertIsNone(kremc.fetch_history_daily(plant, "2026-08-05", "2026-08-05")) def test_sun_wms_login_failure_returns_none(self): plant = { "id": "sunwms-06", "name": "6호기", "auth": {}, "system": {}, } # 이 함수는 내부에서 base.create_session을 다시 import한다. with patch("crawlers.base.create_session", return_value=FakeSession(FakeResponse())): self.assertIsNone(sun_wms.fetch_history_daily(plant, "2026-08-05", "2026-08-05")) def test_hyundai_login_failure_returns_none(self): plant = { "id": "hyundai-08", "name": "8호기", "auth": {}, "system": {}, } with patch.object(hyundai, "create_session", return_value=FakeSession(FakeResponse())): self.assertIsNone(hyundai.fetch_history_daily(plant, "2026-08-05", "2026-08-05")) def test_cmsolar_login_failure_returns_none(self): plant = { "id": "cmsolar-10", "name": "10호기", "auth": {}, "system": {}, } with patch.object(cmsolar, "create_session", return_value=FakeSession(FakeResponse())): self.assertIsNone(cmsolar.fetch_history_daily(plant, "2026-08-05", "2026-08-05")) if __name__ == "__main__": unittest.main()