haps
haps copied to clipboard
Support for multiple profiles in egg
Lets assume such a situation
from abc import ABC, abstractmethod
from haps import base, egg, Container as IoC, PROFILES
from haps.config import Configuration
@base
class IMailService(ABC):
@abstractmethod
def send_mail(self, text: str, to: str, **kwargs):
pass
@egg
class MailService(IMailService):
def send_mail(self, text: str, to: str, **kwargs):
# Send mail here
pass
@egg(profile='mail_disabled')
class DummyMailService(IMailService):
def send_mail(self, text: str, to: str, **kwargs):
pass
profiles = ['mail_disabled']
Configuration().set(PROFILES, profiles)
IoC.autodiscover(['app'])
Case
When it comes to testing I need to configure test profiles like this:
profiles = ['mail_disabled']
Configuration().set(PROFILES, profiles)
IoC.autodiscover(['app'])
Assume I have more than one service like MailService with Dummy implementation, then the configuration will look like this:
profiles = ['mail_disabled', 'serviceX_disabled', 'serviceY_disabled', ...]
Configuration().set(PROFILES, profiles)
IoC.autodiscover(['app'])
It's a lot of profiles and we need to update base tests configuration every time we add a new service...
Solution
We can make it a lot easier with defining multiple profiles on eggs like
...
@egg(profiles=['mail_disabled', 'test'])
class DummyMailService(IMailService):
def send_mail(self, text: str, to: str, **kwargs):
pass
Then our testing configuration will be simply:
profiles = ['test']
Configuration().set(PROFILES, profiles)
IoC.autodiscover(['app'])