graham / python_xid

Python Xid Implementation
MIT License
65 stars 25 forks source link

The generated xid string is not always reversible via `Xid.from_string` #5

Open wonderbeyond opened 6 years ago

wonderbeyond commented 6 years ago

I've written a test case to reveal this issue.

Execute py.test to reproduce error like below:

$ py.test
====================================================== test session starts =======================================================
platform linux2 -- Python 2.7.12, pytest-3.2.5, py-1.5.2, pluggy-0.4.0
rootdir: /home/wonder/workspace/python_xid, inifile:
collected 10 items                                                                                                                

xid_test.py .........F

============================================================ FAILURES ============================================================
_______________________________________________ TestXid.test_xid_always_reversible _______________________________________________

self = <xid_test.TestXid testMethod=test_xid_always_reversible>

    def test_xid_always_reversible(self):
        for i in range(1000):
            s = Xid().string()
>           self.assertEqual(Xid.from_string(s).string(), s)

xid_test.py:50: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

cls = <class 'xid.Xid'>, s = 'b8edi0qo1j65kvnoejvg'

    @classmethod
    def from_string(cls, s):
        # type: (str) -> Xid
        val = base32hex.b32decode(s.upper())
        value_check = [0 < x < 255 for x in val]

        if not all(value_check):
>           raise InvalidXid(s)
E           InvalidXid: b8edi0qo1j65kvnoejvg

xid.py:152: InvalidXid
=============================================== 1 failed, 9 passed in 0.06 seconds ===============================================
dwin commented 4 years ago

Also seeing this issue.

============= FAILURES ===================================
______________________ TestXid.test_xid_always_reversible ______________________

self = <xid_test.TestXid testMethod=test_xid_always_reversible>

    def test_xid_always_reversible(self):
        for i in range(1000):
            s = Xid().string()
>           self.assertEqual(Xid.from_string(s).string(), s)

xid_test.py:50: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

cls = <class 'xid.Xid'>, s = 'bnmndnb24smglu325fvg'

    @classmethod
    def from_string(cls, s):
        # type: (str) -> Xid
        val = base32hex.b32decode(s.upper())
        value_check = [0 <= x < 255 for x in val]

        if not all(value_check):
>           raise InvalidXid(s)
E           xid.InvalidXid: bnmndnb24smglu325fvg

xid.py:159: InvalidXid
========================= 1 failed, 9 passed in 0.12s ==========================
##[error]Process completed with exit code 1.