Skip to content

Commit

Permalink
Implement tests for common submodule with more explicit error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
NeonDaniel committed Dec 31, 2024
1 parent 9c379b0 commit 52254de
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
12 changes: 10 additions & 2 deletions pyklatchat_utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@
from uuid import uuid4


def generate_uuid(length=10) -> str:
def generate_uuid(length: int = 10) -> str:
"""
Generates UUID string of desired length
:param length: length of the output UUID string
:returns UUID string of the desired length
"""
if length > 32:
raise ValueError("Length cannot be greater than 32")
if length < 1:
raise ValueError("Length must be greater than 0")
return uuid4().hex[:length]


Expand All @@ -54,7 +58,11 @@ def get_hash(input_str: str, encoding="utf-8", algo="sha512") -> str:
:returns hashed string from the provided input
"""
return getattr(hashlib, algo)(input_str.encode(encoding)).hexdigest()
try:
method = getattr(hashlib, algo)
except AttributeError:
raise ValueError(f"{algo} is not a supported algorithm")
return method(input_str.encode(encoding)).hexdigest()


def get_version(from_path: str = None):
Expand Down
53 changes: 41 additions & 12 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,56 @@
from io import BytesIO
from unittest import TestCase


class TestCommon(TestCase):
def test_generate_uuid(self):
from pyklatchat_utils.common import generate_uuid
# TODO

# Default behavior
self.assertIsInstance(generate_uuid(), str)

# Maximum len
long_uuid = generate_uuid(32)
self.assertIsInstance(long_uuid, str)
self.assertEqual(len(long_uuid), 32)

# Minimum len
long_uuid = generate_uuid(1)
self.assertIsInstance(long_uuid, str)
self.assertEqual(len(long_uuid), 1)

# Error minimum len
with self.assertRaises(ValueError):
generate_uuid(0)

# Error maximum len
with self.assertRaises(ValueError):
generate_uuid(33)

def test_get_hash(self):
from pyklatchat_utils.common import get_hash
# TODO
test_string = "test"

def test_get_version(self):
from pyklatchat_utils.common import get_version
# TODO
# Default behavior
self.assertIsInstance(get_hash(test_string), str)

def test_deep_merge(self):
from pyklatchat_utils.common import deep_merge
# TODO
# Valid hash
self.assertIsInstance(get_hash(test_string, algo="sha1"), str)

# Invalid hash
with self.assertRaises(ValueError):
get_hash(test_string, algo="some_invalid_hash")

# TODO: Test encoding

def test_buffer_to_base64(self):
from pyklatchat_utils.common import buffer_to_base64
# TODO

def test_base64_to_buffer(self):
from pyklatchat_utils.common import base64_to_buffer
# TODO

test_bytes = BytesIO(b"test")
encoded = buffer_to_base64(test_bytes)
self.assertIsInstance(encoded, str)
self.assertEqual(base64_to_buffer(encoded).getvalue(),
test_bytes.getvalue())

# TODO: Test edge/error cases

0 comments on commit 52254de

Please sign in to comment.