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

15 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-09-05 17:55 +0000

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

2import uuid 

3from dataclasses import dataclass 

4 

5 

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

7class UniqueId: 

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

9 

10 id: uuid.UUID 

11 

12 @classmethod 

13 def generate(cls): 

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

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

16 

17 @classmethod 

18 def create_from_string(cls, uuid_str: str): 

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

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

21 

22 def __eq__(self, other): 

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

24 return str(self) == str(other) 

25 

26 def __str__(self): 

27 """Return a string representation.""" 

28 return str(self.id)