Skip to content

Commit

Permalink
Add tests for sensors.py
Browse files Browse the repository at this point in the history
  • Loading branch information
DurgNomis-drol committed Oct 12, 2021
1 parent 437267a commit 4da2aaf
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 28 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ repos:
entry: poetry run pylint
language: system
types: [python]
- id: pytest-check
name: pytest-check
entry: pytest
language: system
pass_filenames: false
always_run: true
- id: codespell
name: codespell
entry: poetry run codespell --write-changes --skip="./*"
Expand Down
1 change: 1 addition & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Need for pytest or else it will cause an import error in pytest"""
2 changes: 1 addition & 1 deletion mytoyota/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"""Toyota Connected Services Client"""
"""Toyota Connected Services Client"""
39 changes: 12 additions & 27 deletions mytoyota/sensors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Sensor representation for mytoyota"""
import logging
from typing import Optional

from mytoyota.const import CLOSED, INCAR, LOCKED, OFF, STATE, WARNING

Expand All @@ -9,12 +10,9 @@
class Hood:
"""Representation of the hood of the car"""

warning: bool = False
closed: bool = True

def __init__(self, hood: dict) -> None:
self.warning = hood.get(WARNING, None)
self.closed = hood.get(CLOSED, None)
self.warning: Optional[bool] = hood.get(WARNING, None)
self.closed: Optional[bool] = hood.get(CLOSED, None)

def __str__(self) -> str:
return str(self.as_dict())
Expand All @@ -27,14 +25,10 @@ def as_dict(self) -> dict:
class Door:
"""Representation of a door"""

warning: bool = False
closed: bool = True
locked: bool = True

def __init__(self, door: dict) -> None:
self.warning = door.get(WARNING, None)
self.closed = door.get(CLOSED, None)
self.locked = door.get(LOCKED, None)
self.warning: Optional[bool] = door.get(WARNING, None)
self.closed: Optional[bool] = door.get(CLOSED, None)
self.locked: Optional[bool] = door.get(LOCKED, None)

def __str__(self) -> str:
return str(self.as_dict())
Expand Down Expand Up @@ -85,12 +79,9 @@ def as_dict(self) -> dict:
class Window:
"""Representation of a window"""

warning: bool = False
state: str = None

def __init__(self, window) -> None:
self.warning = window.get(WARNING, None)
self.state = window.get(STATE, None)
self.warning: Optional[bool] = window.get(WARNING, None)
self.state: Optional[str] = window.get(STATE, None)

def __str__(self) -> str:
return str(self.as_dict())
Expand Down Expand Up @@ -138,12 +129,9 @@ def as_dict(self) -> dict:
class Light:
"""Representation of the lights"""

warning: bool = False
off: bool = True

def __init__(self, light: dict) -> None:
self.warning = light.get(WARNING, None)
self.off = light.get(OFF, None)
self.warning: Optional[bool] = light.get(WARNING, None)
self.off: Optional[bool] = light.get(OFF, None)

def __str__(self) -> str:
return str(self.as_dict())
Expand Down Expand Up @@ -187,12 +175,9 @@ def as_dict(self) -> dict:
class Key:
"""Representation of the ignition"""

warning: bool = False
in_car: bool = False

def __init__(self, key: dict) -> None:
self.warning = key.get(WARNING, None)
self.in_car = key.get(INCAR, None)
self.warning: Optional[bool] = key.get(WARNING, None)
self.in_car: Optional[bool] = key.get(INCAR, None)

def __str__(self) -> str:
return str(self.as_dict())
Expand Down
101 changes: 101 additions & 0 deletions tests/test_sensors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"""pytest tests for mytoyota.sensors"""

from mytoyota.sensors import Door, Hood, Key, Light, Window

# pylint: disable=no-self-use


class TestSensors:
"""pytest functions to test Sensors"""

def test_hood(self):
"""Test hood"""
hood = Hood({"warning": False, "closed": True})

assert hood.warning is False
assert hood.closed is True

assert hood.as_dict() == {"warning": False, "closed": True}

def test_hood_no_data(self):
"""Test hood with no initialization data"""
hood = Hood({})

assert hood.warning is None
assert hood.closed is None

assert hood.as_dict() == {"warning": None, "closed": None}

def test_door(self):
"""Test door"""
door = Door({"warning": False, "closed": True, "locked": False})

assert door.warning is False
assert door.closed is True
assert door.locked is False

assert door.as_dict() == {"warning": False, "closed": True, "locked": False}

def test_door_no_data(self):
"""Test door with no initialization data"""
door = Door({})

assert door.warning is None
assert door.closed is None
assert door.locked is None

assert door.as_dict() == {"warning": None, "closed": None, "locked": None}

def test_window(self):
"""Test window"""
window = Window({"warning": False, "state": "close"})

assert window.warning is False
assert window.state == "close"

assert window.as_dict() == {"warning": False, "state": "close"}

def test_window_no_data(self):
"""Test window with no initialization data"""
window = Window({})

assert window.warning is None
assert window.state is None

assert window.as_dict() == {"warning": None, "state": None}

def test_light(self):
"""Test light"""
light = Light({"warning": False, "off": True})

assert light.warning is False
assert light.off is True

assert light.as_dict() == {"warning": False, "off": True}

def test_light_no_data(self):
"""Test light with no initialization data"""
light = Light({})

assert light.warning is None
assert light.off is None

assert light.as_dict() == {"warning": None, "off": None}

def test_key(self):
"""Test key"""
key = Key({"warning": False, "inCar": True})

assert key.warning is False
assert key.in_car is True

assert key.as_dict() == {"warning": False, "in_car": True}

def test_key_no_data(self):
"""Test key with no initialization data"""
key = Key({})

assert key.warning is None
assert key.in_car is None

assert key.as_dict() == {"warning": None, "in_car": None}

0 comments on commit 4da2aaf

Please sign in to comment.