mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-10-12 18:38:03 +00:00
Follow main package structure for tests (#1317)
* Transport tests under tests/transports/ * Protocol tests under tests/protocols/ * IOT tests under iot/ * Plus some minor cleanups, most code changes are related to splitting up smart & iot tests
This commit is contained in:
86
tests/iot/modules/test_usage.py
Normal file
86
tests/iot/modules/test_usage.py
Normal file
@@ -0,0 +1,86 @@
|
||||
import datetime
|
||||
from unittest.mock import Mock
|
||||
|
||||
from kasa.iot.modules import Usage
|
||||
|
||||
|
||||
def test_usage_convert_stat_data():
|
||||
usage = Usage(None, module="usage")
|
||||
|
||||
test_data = []
|
||||
assert usage._convert_stat_data(test_data, "day") == {}
|
||||
|
||||
test_data = [
|
||||
{"year": 2016, "month": 5, "day": 2, "time": 20},
|
||||
{"year": 2016, "month": 5, "day": 4, "time": 30},
|
||||
]
|
||||
d = usage._convert_stat_data(test_data, "day")
|
||||
assert len(d) == len(test_data)
|
||||
assert isinstance(d, dict)
|
||||
k, v = d.popitem()
|
||||
assert isinstance(k, int)
|
||||
assert isinstance(v, int)
|
||||
assert k == 4
|
||||
assert v == 30
|
||||
|
||||
|
||||
def test_usage_today():
|
||||
"""Test fetching the usage for today.
|
||||
|
||||
This test uses inline data since the fixtures
|
||||
will not have data for the current day.
|
||||
"""
|
||||
emeter_data = {
|
||||
"get_daystat": {
|
||||
"day_list": [],
|
||||
"err_code": 0,
|
||||
}
|
||||
}
|
||||
|
||||
class MockUsage(Usage):
|
||||
@property
|
||||
def data(self):
|
||||
return emeter_data
|
||||
|
||||
usage = MockUsage(Mock(), "usage")
|
||||
assert usage.usage_today is None
|
||||
now = datetime.datetime.now()
|
||||
emeter_data["get_daystat"]["day_list"].extend(
|
||||
[
|
||||
{"day": now.day - 1, "time": 200, "month": now.month - 1, "year": now.year},
|
||||
{"day": now.day, "time": 500, "month": now.month, "year": now.year},
|
||||
{"day": now.day + 1, "time": 100, "month": now.month + 1, "year": now.year},
|
||||
]
|
||||
)
|
||||
assert usage.usage_today == 500
|
||||
|
||||
|
||||
def test_usage_this_month():
|
||||
"""Test fetching the usage for this month.
|
||||
|
||||
This test uses inline data since the fixtures
|
||||
will not have data for the current month.
|
||||
"""
|
||||
emeter_data = {
|
||||
"get_monthstat": {
|
||||
"month_list": [],
|
||||
"err_code": 0,
|
||||
}
|
||||
}
|
||||
|
||||
class MockUsage(Usage):
|
||||
@property
|
||||
def data(self):
|
||||
return emeter_data
|
||||
|
||||
usage = MockUsage(Mock(), "usage")
|
||||
assert usage.usage_this_month is None
|
||||
now = datetime.datetime.now()
|
||||
emeter_data["get_monthstat"]["month_list"].extend(
|
||||
[
|
||||
{"time": 200, "month": now.month - 1, "year": now.year},
|
||||
{"time": 500, "month": now.month, "year": now.year},
|
||||
{"time": 100, "month": now.month + 1, "year": now.year},
|
||||
]
|
||||
)
|
||||
assert usage.usage_this_month == 500
|
Reference in New Issue
Block a user