Skip to content

Commit

Permalink
Fix base64 constructor and generate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ronweikamp committed Feb 3, 2025
1 parent 5594eb6 commit 194ab79
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions epcpy/epc_schemes/base_scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from enum import Enum
from typing import Dict, Optional, Type, TypeVar

from epcpy.utils.common import ConvertException, hex_to_base64, hex_to_binary
from epcpy.utils.common import ConvertException, base64_to_hex, hex_to_base64, hex_to_binary
from epcpy.utils.regex import TAG_URI

T_EPCScheme = TypeVar("T_EPCScheme", bound="EPCScheme")
Expand Down Expand Up @@ -164,7 +164,7 @@ def from_base64(
Returns:
TagEncodable: Instance of TagEncodable class
"""
return cls.from_binary(hex_to_base64(tag_base64_string))
return cls.from_hex(base64_to_hex(tag_base64_string))

@classmethod
def from_tag_uri(
Expand Down
20 changes: 20 additions & 0 deletions tests/epc_schemes/test_base_scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from epcpy import ConvertException
from epcpy.epc_schemes.base_scheme import EPCScheme, GS1Element, GS1Keyed, TagEncodable
from epcpy.utils.common import hex_to_base64


class TestEPCSchemeInitMeta(type):
Expand Down Expand Up @@ -207,6 +208,20 @@ def test(self: unittest.TestCase):

return test

def generate_valid_from_base64_test(
scheme: TagEncodable, epc_uri: str, hex_string: str
):
def test(self: unittest.TestCase):
try:
s: EPCScheme = scheme.from_base64(hex_to_base64(hex_string))
self.assertEqual(s.epc_uri, epc_uri)
except ConvertException:
self.fail(
f"{scheme} from hex unexpectedly raised ConvertException for URI {epc_uri}"
)

return test

def generate_invalid_tag_encodable_tests(
scheme: EPCScheme, epc_uri: str, **kwargs
):
Expand Down Expand Up @@ -241,6 +256,11 @@ def test(self: unittest.TestCase):
entry["uri"],
entry["hex"],
)
attrs[f"{name}_from_base64"] = generate_valid_from_base64_test(
scheme,
entry["uri"],
entry["hex"],
)

for entry in invalid_data:
attrs[entry["name"]] = generate_invalid_tag_encodable_tests(
Expand Down

0 comments on commit 194ab79

Please sign in to comment.