Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add uuid3 #14

Merged
merged 1 commit into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,3 @@ UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')

- The `getnode` function is not available.
- The `uuid1` and `uuid6` take `node` argument as mandatory.
- The `uuid3` function is not available.
2 changes: 2 additions & 0 deletions python/uuid_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
UUID,
__version__,
uuid1,
uuid3,
uuid4,
uuid5,
uuid6,
Expand All @@ -29,6 +30,7 @@
"UUID",
"__version__",
"uuid1",
"uuid3",
"uuid4",
"uuid5",
"uuid6",
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,13 @@ fn uuid1(node: u128, clock_seq: Option<u64>) -> PyResult<UUID> {
Ok(UUID { uuid })
}

#[pyfunction]
fn uuid3(namespace: UUID, name: &str) -> PyResult<UUID> {
Ok(UUID {
uuid: Uuid::new_v3(&namespace.uuid, name.as_bytes()),
})
}

#[pyfunction]
fn uuid4() -> PyResult<UUID> {
Ok(UUID {
Expand Down Expand Up @@ -328,6 +335,7 @@ fn uuid_utils(_py: Python, m: &PyModule) -> PyResult<()> {
m.add("__version__", env!("CARGO_PKG_VERSION"))?;
m.add_class::<UUID>()?;
m.add_function(wrap_pyfunction!(uuid1, m)?)?;
m.add_function(wrap_pyfunction!(uuid3, m)?)?;
m.add_function(wrap_pyfunction!(uuid4, m)?)?;
m.add_function(wrap_pyfunction!(uuid5, m)?)?;
m.add_function(wrap_pyfunction!(uuid6, m)?)?;
Expand Down
15 changes: 10 additions & 5 deletions tests/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_uuid_constructor() -> None:

def test_uuid_from_hex() -> None:
uuid = uuid_utils.UUID("a8098c1a-f86e-11da-bd1a-00112444be1e")
assert uuid.hex == "a8098c1af86e11dabd1a00112444be1e"
assert str(uuid) == "a8098c1a-f86e-11da-bd1a-00112444be1e"

with pytest.raises(ValueError):
uuid_utils.UUID("0-0-0-0-0")
Expand All @@ -33,7 +33,7 @@ def test_uuid_from_bytes() -> None:
bytes=b"\xa8\t\x8c\x1a\xf8n\x11\xda\xbd\x1a\x00\x11$D\xbe\x1e"
)

assert uuid.bytes == b"\xa8\t\x8c\x1a\xf8n\x11\xda\xbd\x1a\x00\x11$D\xbe\x1e"
assert str(uuid) == "a8098c1a-f86e-11da-bd1a-00112444be1e"

with pytest.raises(ValueError):
uuid_utils.UUID(bytes=b"\xa8\t\x8c\x1a\xf8n\x11")
Expand All @@ -43,14 +43,13 @@ def test_uuid_from_bytes_le() -> None:
uuid = uuid_utils.UUID(
bytes_le=b"\x1a\x8c\t\xa8n\xf8\xda\x11\xbd\x1a\x00\x11$D\xbe\x1e"
)
assert uuid.bytes_le == b"\x1a\x8c\t\xa8n\xf8\xda\x11\xbd\x1a\x00\x11$D\xbe\x1e"
assert str(uuid) == "a8098c1a-f86e-11da-bd1a-00112444be1e"


def test_uuid_from_int() -> None:
uuid = uuid_utils.UUID(int=223359875637754765292326297443183672862)

assert uuid.int == 223359875637754765292326297443183672862
assert uuid.__int__() == 223359875637754765292326297443183672862
assert str(uuid) == "a8098c1a-f86e-11da-bd1a-00112444be1e"


def test_uuid_setattr() -> None:
Expand All @@ -68,6 +67,11 @@ def test_uuid1() -> None:
assert isinstance(uuid, uuid_utils.UUID)


def test_uuid3() -> None:
uuid = uuid_utils.uuid3(namespace=uuid_utils.NAMESPACE_DNS, name="python.org")
assert isinstance(uuid, uuid_utils.UUID)


def test_uuid4() -> None:
uuid = uuid_utils.uuid4()
assert isinstance(uuid, uuid_utils.UUID)
Expand Down Expand Up @@ -144,6 +148,7 @@ def test_uuid_properties() -> None:
assert uuid_1.fields == uuid_2.fields
assert uuid_1.hex == uuid_2.hex
assert uuid_1.int == uuid_2.int
assert uuid_1.__int__() == uuid_1.__int__()
assert uuid_1.node == uuid_2.node
assert uuid_1.time == uuid_2.time
assert uuid_1.time_low == uuid_2.time_low
Expand Down