From 79fb5f1b270c13254bcf151506c954dc19cfd12b Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Mon, 1 Jun 2020 13:21:47 -0400 Subject: [PATCH] Add tests and typing annotation for Credential --- memberPlumbing/hid/Credential.py | 20 +++++++++++++------- memberPlumbing/hid/tests/__init__.py | 0 memberPlumbing/hid/tests/test_Credential.py | 13 +++++++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 memberPlumbing/hid/tests/__init__.py create mode 100644 memberPlumbing/hid/tests/test_Credential.py diff --git a/memberPlumbing/hid/Credential.py b/memberPlumbing/hid/Credential.py index 90ab8f0..950bedd 100644 --- a/memberPlumbing/hid/Credential.py +++ b/memberPlumbing/hid/Credential.py @@ -1,11 +1,14 @@ import bitstring +from typing import Tuple, Optional # Reference for H10301 card format: # https://www.hidglobal.com/sites/default/files/hid-understanding_card_data_formats-wp-en.pdf class Credential: - def __init__(self, code=None, hex=None): + def __init__( + self, code: Optional[Tuple[int, int]] = None, hex: Optional[str] = None + ) -> None: if code is None and hex is None: raise TypeError("Must set either code or hex for a Credential") elif code is not None and hex is not None: @@ -21,21 +24,24 @@ class Credential: elif hex is not None: self.bits = bitstring.Bits(hex=hex) - def __repr__(self): + def __repr__(self) -> str: return f"Credential({self.code})" - def __eq__(self, other): - return self.bits == other.bits + def __eq__(self, other: object) -> bool: + if isinstance(other, Credential): + return self.bits == other.bits + else: + return False - def __hash__(self): + def __hash__(self) -> int: return self.bits.int @property - def code(self): + def code(self) -> Tuple[int, int]: facility = self.bits[7:15].uint code = self.bits[15:31].uint return (facility, code) @property - def hex(self): + def hex(self) -> str: return self.bits.hex.upper() diff --git a/memberPlumbing/hid/tests/__init__.py b/memberPlumbing/hid/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/memberPlumbing/hid/tests/test_Credential.py b/memberPlumbing/hid/tests/test_Credential.py new file mode 100644 index 0000000..8c06bc2 --- /dev/null +++ b/memberPlumbing/hid/tests/test_Credential.py @@ -0,0 +1,13 @@ +import pytest + +from ..Credential import Credential + + +def test_code_to_hex() -> None: + cred = Credential(code=(123, 45678)) + assert cred.hex == "02F764DD" + + +def test_hex_to_code() -> None: + cred = Credential(hex="02F764DD") + assert cred.code == (123, 45678)