def test_json_dumps_xid(): doc = {"xid": XID("bsqpeinf38q71u3sq6pg")} result = json.dumps(doc) assert result == '{"xid":"bsqpeinf38q71u3sq6pg"}'
from xid import XID def setup(clear: bool = False) -> _InMemDB: if clear is True: global _data _data = {} return _InMemDB() _data = { "users": [ { "id": XID("bsqoofff38q67jp89430"), "created_at": datetime(2020, 5, 9, 16, 20, 40, 560920), "first_name": "Alain", "last_name": "Belanger", "email": "*****@*****.**", }, { "id": XID("bsqpakvf38q6siv586fg"), "created_at": datetime(2020, 5, 9, 16, 21, 1, 326478), "first_name": "Sylvie", "last_name": "Boucher", "email": "*****@*****.**", }, ] }
def test_encode_from_string(): xid = XID(id_="9m4e2mr0ui3e8a215n4g") e = xid.encode() assert e.decode("utf-8") == "9m4e2mr0ui3e8a215n4g"
def test_encode_from_string_invalid(): with pytest.raises(InvalidXID): XID(id_="invalid")
def test_pass_wrong_type(): with pytest.raises(TypeError): XID(id_=[])
def test_string(): xid = XID(id_=bytes([ 0x4D, 0x88, 0xE1, 0x5B, 0x60, 0xF4, 0x86, 0xE4, 0x28, 0x41, 0x2D, 0xC9 ])) assert xid.string() == "9m4e2mr0ui3e8a215n4g"
def test_pass_timestamp(): t = 1234567890 xid = XID(t=1234567890) assert xid.time() == t
def test_repr(): xid = XID(id_=bytes([ 0x4D, 0x88, 0xE1, 0x5B, 0x60, 0xF4, 0x86, 0xE4, 0x28, 0x41, 0x2D, 0xC9 ])) assert repr(xid) == "XID('9m4e2mr0ui3e8a215n4g')"
def test_from_bytes_invariant(): xid1 = XID() xid2 = XID(xid1.bytes()) assert xid1.bytes() == xid2.bytes()
def test_encode_from_bytes(): with pytest.raises(InvalidXID): XID(id_=bytes([0xFF]))
import pytest from xid import XID, InvalidXID XIDs = [ # taken from https://github.com/rs/xid/blob/master/id_test.go { "xid": XID( bytes([ 0x4D, 0x88, 0xE1, 0x5B, 0x60, 0xF4, 0x86, 0xE4, 0x28, 0x41, 0x2D, 0xC9 ])), "ts": 1300816219, "machine": b"`\xf4\x86", "pid": 0xE428, "counter": 4271561, }, { "xid": XID( bytes([ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ])), "ts": 0,