Skip to content

Commit

Permalink
Merge pull request #23 from bieniu/fix-uptime
Browse files Browse the repository at this point in the history
Fix tests
  • Loading branch information
bieniu authored Nov 25, 2020
2 parents 385b7fe + 311dd24 commit b93d22d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
10 changes: 5 additions & 5 deletions brother/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Python wrapper for getting data from Brother laser and inkjet printers via SNMP. Uses
the method of parsing data from: https://github.com/saper-2/BRN-Printer-sCounters-Info
"""
import datetime
import logging
import re
from datetime import datetime, timedelta

import pysnmp.hlapi.asyncio as hlapi
from pysnmp.error import PySnmpError
Expand Down Expand Up @@ -115,12 +115,12 @@ async def async_update(
else:
if not self._last_uptime:
data[ATTR_UPTIME] = self._last_uptime = (
datetime.datetime.utcnow() - datetime.timedelta(seconds=uptime)
datetime.utcnow() - timedelta(seconds=uptime)
).replace(microsecond=0)
else:
new_uptime = (
datetime.datetime.utcnow() - datetime.timedelta(seconds=uptime)
).replace(microsecond=0)
new_uptime = (datetime.utcnow() - timedelta(seconds=uptime)).replace(
microsecond=0
)
if abs((new_uptime - self._last_uptime).total_seconds()) > 5:
data[ATTR_UPTIME] = self._last_uptime = new_uptime
else:
Expand Down
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[pytest]
addopts=--cov --cov-report term-missing --disable-pytest-warnings
addopts=--cov --cov-report term-missing --disable-pytest-warnings --error-for-skips
5 changes: 1 addition & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python
from setuptools import setup


setup(
name="brother",
version="0.1.19",
Expand All @@ -21,7 +20,5 @@
"Operating System :: OS Independent",
],
setup_requires=("pytest-runner"),
tests_require=(
"pytest-cov",
),
tests_require=("pytest-cov", "pytest-asyncio", "pytest-error-for-skips"),
)
18 changes: 10 additions & 8 deletions tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
"""Tests for brother package."""
import json
from datetime import datetime

from unittest.mock import patch
from unittest.mock import Mock, patch
from brother import Brother, SnmpError, UnsupportedModel
import pytest

HOST = "localhost"
INVALID_HOST = "foo.local"
TEST_TIME = datetime(2019, 11, 11, 9, 10, 32)


@pytest.mark.asyncio
async def test_hl_l2340dw_model():
"""Test with valid data from HL-L2340DW printer with invalid kind."""
"""Test with valid data from HL-L2340DW printer with invalid kind."""
with open("tests/data/hl-l2340dw.json") as file:
data = json.load(file)

with patch("brother.Brother._get_data", return_value=data):
with patch("brother.Brother._get_data", return_value=data), patch("brother.datetime", utcnow=Mock(return_value=TEST_TIME)):

brother = Brother(HOST, kind="foo")
await brother.async_update()
Expand All @@ -27,7 +29,7 @@ async def test_hl_l2340dw_model():
assert brother.data["status"] == "oczekiwanie"
assert brother.data["black_toner"] == 80
assert brother.data["page_counter"] == 986
assert brother.data["uptime"] == 4136135.15
assert brother.data["uptime"].isoformat() == "2019-09-24T12:14:56"


@pytest.mark.asyncio
Expand Down Expand Up @@ -79,7 +81,7 @@ async def test_mfc_5490cn_model():
with open("tests/data/mfc-5490cn.json") as file:
data = json.load(file)

with patch("brother.Brother._get_data", return_value=data):
with patch("brother.Brother._get_data", return_value=data), patch("brother.datetime", utcnow=Mock(return_value=TEST_TIME)):

brother = Brother(HOST, kind="ink")
await brother.async_update()
Expand All @@ -90,7 +92,7 @@ async def test_mfc_5490cn_model():
assert brother.serial == "serial_number"
assert brother.data["status"] == "sleep mode"
assert brother.data["page_counter"] == 8989
assert brother.data["uptime"] == 725189.56
assert brother.data["uptime"].isoformat() == "2019-11-02T23:44:02"


@pytest.mark.asyncio
Expand Down Expand Up @@ -119,7 +121,7 @@ async def test_dcp_7070dw_model():
with open("tests/data/dcp-7070dw.json") as file:
data = json.load(file)

with patch("brother.Brother._get_data", return_value=data):
with patch("brother.Brother._get_data", return_value=data), patch("brother.datetime", utcnow=Mock(return_value=TEST_TIME)):

brother = Brother(HOST, kind="laser")
await brother.async_update()
Expand All @@ -134,7 +136,7 @@ async def test_dcp_7070dw_model():
assert brother.data["drum_counter"] == 1603
assert brother.data["drum_remaining_life"] == 88
assert brother.data["drum_remaining_pages"] == 10397
assert brother.data["uptime"] == 29878025.61
assert brother.data["uptime"].isoformat() == "2018-11-30T13:43:26"


@pytest.mark.asyncio
Expand Down

0 comments on commit b93d22d

Please sign in to comment.