Coverage for src/kwai/core/domain/value_objects/unique_id.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2024-01-01 00:00 +0000

1"""Module that defines a value object for a unique id.""" 

2 

3import uuid 

4 

5from dataclasses import dataclass 

6 

7 

8@dataclass(frozen=True, slots=True) 

9class UniqueId: 

10 """A value object for a unique id.""" 

11 

12 id: uuid.UUID 

13 

14 @classmethod 

15 def generate(cls): 

16 """Create a new unique id (UUID4).""" 

17 return UniqueId(id=uuid.uuid4()) 

18 

19 @classmethod 

20 def create_from_string(cls, uuid_str: str): 

21 """Create a unique id from te string.""" 

22 return UniqueId(id=uuid.UUID(uuid_str)) 

23 

24 def __eq__(self, other): 

25 """Check if a unique id equals the given id.""" 

26 return str(self) == str(other) 

27 

28 def __str__(self): 

29 """Return a string representation.""" 

30 return str(self.id) 

31 

32 def __hash__(self): 

33 """Return a hash value for the unique id.""" 

34 return hash(self.id)