This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Decimal quantize to round half up
- Switch the rounding method. - Add unit tests for the rounding.
- Loading branch information
1 parent
1817a4e
commit e5159b1
Showing
4 changed files
with
69 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Device tests.""" | ||
|
||
import pytest | ||
from pymelcloud.ata_device import AtaDevice | ||
from .util import build_device | ||
|
||
|
||
def _build_device(device_conf_name: str, device_state_name: str) -> AtaDevice: | ||
device_conf, client = build_device(device_conf_name, device_state_name) | ||
return AtaDevice(device_conf, client) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_round_temperature(): | ||
device = _build_device("ata_listdevice.json", "ata_get.json") | ||
device._device_conf.get("Device")["TemperatureIncrement"] = 0.5 | ||
|
||
assert device.round_temperature(23.99999) == 24.0 | ||
assert device.round_temperature(24.0) == 24.0 | ||
assert device.round_temperature(24.00001) == 24.0 | ||
assert device.round_temperature(24.24999) == 24.0 | ||
assert device.round_temperature(24.25) == 24.5 | ||
assert device.round_temperature(24.25001) == 24.5 | ||
assert device.round_temperature(24.5) == 24.5 | ||
assert device.round_temperature(24.74999) == 24.5 | ||
assert device.round_temperature(24.75) == 25.0 | ||
assert device.round_temperature(24.75001) == 25.0 | ||
|
||
device._device_conf.get("Device")["TemperatureIncrement"] = 1 | ||
|
||
assert device.round_temperature(23.99999) == 24.0 | ||
assert device.round_temperature(24.0) == 24.0 | ||
assert device.round_temperature(24.00001) == 24.0 | ||
assert device.round_temperature(24.49999) == 24.0 | ||
assert device.round_temperature(24.5) == 25.0 | ||
assert device.round_temperature(24.50001) == 25.0 | ||
assert device.round_temperature(25.0) == 25.0 | ||
assert device.round_temperature(25.00001) == 25.0 | ||
assert device.round_temperature(25.49999) == 25.0 | ||
assert device.round_temperature(25.5) == 26.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import json | ||
import os | ||
|
||
from asynctest import CoroutineMock, Mock, patch | ||
|
||
def build_device(device_conf_name: str, device_state_name: str): | ||
test_dir = os.path.join(os.path.dirname(__file__), "samples") | ||
with open(os.path.join(test_dir, device_conf_name), "r") as json_file: | ||
device_conf = json.load(json_file) | ||
|
||
with open(os.path.join(test_dir, device_state_name), "r") as json_file: | ||
device_state = json.load(json_file) | ||
|
||
with patch("pymelcloud.client.Client") as _client: | ||
_client.update_confs = CoroutineMock() | ||
_client.device_confs.__iter__ = Mock(return_value=[device_conf].__iter__()) | ||
_client.fetch_device_units = CoroutineMock(return_value=[]) | ||
_client.fetch_device_state = CoroutineMock(return_value=device_state) | ||
client = _client | ||
|
||
return device_conf, client |