I have following classes:
from abc import ABC, abstractmethod
class VarCharType(str, ABC):
def __init__(self, value):
self._validate_length(value)
def _validate_length(self, value: str):
if not self._min_length <= len(value) <= self._max_length:
raise TypeError(f'Length must be between {self._min_length} and {self._max_length}')
@property
@abstractmethod
def _min_length(self) -> int:
pass
@property
@abstractmethod
def _max_length(self) -> int:
pass
class GivenName(VarCharType):
_MIN_LENGTH = 1
_MAX_LENGTH = 255
@property
def _min_length(self) -> int:
return self._MIN_LENGTH
@property
def _max_length(self) -> int:
return self._MAX_LENGTH
class FamilyName(VarCharType):
...
To test my code I decided to create random values of FamilyName
and GivenName
through a Mother pattern. Something like that:
class GivenNameMother:
def random_max(self) -> GivenName:
return GivenName(''.join(random.choices(string.ascii_uppercase, k=255)))
def random_min(self) -> GivenName:
return GivenName(''.join(random.choices(string.ascii_uppercase, k=1)))
class FamilyNameMother:
def random_max(self) -> FamilyName:
return FamilyName(''.join(random.choices(string.ascii_uppercase, k=255)))
def random_min(self) -> FamilyName:
return FamilyName(''.join(random.choices(string.ascii_uppercase, k=1)))
Previous implementation have several problems:
- If GivenName changes Max or Min length I must change the Mother class. I have some doubts if it is a feature or a problem.
- For each VarCharType children classes I need to create a mother class with more or less the same code. The problem is in some moment I want to test with utf-8 characters I should change all mother classes.
I tried several solutions but are very dirty and I do not like it.
What would be the best solution? It is a requirement avoiding typing with Any from typing package.
Thanks!