alaex777 / freeze-uuid

Python package for mocking uuid.
MIT License
0 stars 0 forks source link

freeze-uuid

Python package for mocking uuid

Usage Example

from freeze_uuid import freeze_uuid

@freeze_uuid('12af6b44-8181-11ee-b890-628ab7cd4d99')
def test_uuid():
    assert str(uuid.uuid1()) == '12af6b44-8181-11ee-b890-628ab7cd4d99'
    assert str(uuid.uuid4()) == '12af6b44-8181-11ee-b890-628ab7cd4d99'

@freeze_uuid()
def test_uuid_default():
    assert str(uuid.uuid1()) == '00000000-0000-0000-0000-000000000000'
    assert str(uuid.uuid4()) == '00000000-0000-0000-0000-000000000000'

@pytest.mark.asyncio
@freeze_uuid('12af6b44-8181-11ee-b890-628ab7cd4d99')
async def test_uuid_async():
    assert str(uuid.uuid1()) == '12af6b44-8181-11ee-b890-628ab7cd4d99'
    assert str(uuid.uuid4()) == '12af6b44-8181-11ee-b890-628ab7cd4d99'

Also you can use list of uuids for multiple usage

@freeze_uuid(['12af6b44-8181-11ee-b890-628ab7cd4d99', '12af6b44-8181-11ee-b890-628ab7cd4d98'])
def test_uuid_list():
    assert str(uuid.uuid1()) == '12af6b44-8181-11ee-b890-628ab7cd4d99'
    assert str(uuid.uuid4()) == '12af6b44-8181-11ee-b890-628ab7cd4d98'
    assert str(uuid.uuid4()) == '12af6b44-8181-11ee-b890-628ab7cd4d98'
    assert str(uuid.uuid4()) == '12af6b44-8181-11ee-b890-628ab7cd4d98'

Since library mocks uuid.UUID class, it can be used with all libraries, that support UUID class, i.e. uuid7

from uuid_extensions import uuid7

@freeze_uuid(TEST_UUID)
def test_uuid_7():
    assert str(uuid7()) == TEST_UUID

With freeze_uuid context manager you can create tests with pytest parametrize

from freeze_uuid import freeze_uuid_manager

@pytest.mark.parametrize(
    ['expected_result', 'freeze_data'],
    [
        pytest.param(
            TEST_UUID_2,
            [TEST_UUID_2],
        ),
        pytest.param(
            TEST_UUID_3,
            [TEST_UUID_3],
        ),
        pytest.param(
            TEST_UUID_4,
            [TEST_UUID_4],
        ),
        pytest.param(
            TEST_UUID_5,
            [TEST_UUID_5],
        ),
    ]
)
def test_parametrize(expected_result, freeze_data):
    with freeze_uuid_manager(freeze_data):
        assert str(uuid.uuid4()) == expected_result