# Path: feature_toggle/constants.py
# class Attribute(Container):
# class Environment(Container):
#     def __init__(self):
#     def __init__(self):
# 
# Path: feature_toggle/exceptions.py
# class FeatureToggleDoesNotExist(Exception):
#     """
#     This exception is to aid consumers in providing an common exception to catch instead of relying on
#     model.DoesNotExist.
#     """
#     pass
# 
# class FeatureToggleAlreadyExists(Exception):
#     """
#     This exception is raised when trying to create a toggle that already exists.
#     """
#     pass
# 
# Path: feature_toggle/models.py
# class FeatureToggle(TimeStampedModel):
#     uid = models.CharField(max_length=20)
#     name = models.CharField(max_length=20)
#     environment = models.CharField(max_length=50, choices=ENV_CHOICES)
#     is_active = models.BooleanField(default=True)
#     start_date_time = models.DateTimeField(auto_now_add=True)
#     end_date_time = models.DateTimeField(auto_now_add=True)  # todo: add 3 months from now by default
#     time_bomb = models.BooleanField(default=False)
# 
#     class Meta:
#         unique_together = (('name', 'environment'), ('uid',))
#         managed = True
# 
#     def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
#         if not self.uid:
#             self.uid = make_meanigful_id(self.name, length=5)
#         super().save(force_insert, force_update, using, update_fields)
# 
#     def set_attribute(self, key, value=None, update_if_existing=True):
#         attrib, created = self.attributes.get_or_create(key=key)
#         if not created and not update_if_existing:
#             raise FeatureToggleAttributeAlreadyExists(**dict(key=key))
# 
#         attrib.value = value
#         attrib.save()
# 
#     def __str__(self):
#         return "{e}: {n}({u})".format(e=self.environment, n=self.name, u=self.uid)
# 
#     def __unicode__(self):
#         return self.__str__()
# 
# Path: feature_toggle/toggle.py
# class Toggle(BaseToggle):
# 
#     def __init__(self, uid, name, environment, is_active,
#                  start_date_time, end_date_time,
#                  attributes=None, **kwargs):
#         assert isinstance(start_date_time, datetime.datetime)  # should be a time stamp
#         assert start_date_time.tzinfo is not None  # should have a time zone. otherwise causes confusions
#         self.start_date_time = start_date_time
# 
#         assert isinstance(end_date_time, datetime.datetime)  # should be a time stamp
#         assert end_date_time.tzinfo is not None  # should have a time zone. otherwise causes confusions
#         self.end_date_time = end_date_time
#         super().__init__(uid, name, environment, is_active, attributes, **kwargs)
# 
#     def __bool__(self):
#         active = super().__bool__()
# 
#         if self.end_date_time and self.end_date_time < utc_now():
#             # this check because toggles can quickly get overwhelming
#             warnings.warn('{t} has expired. Get rid of me and help me attain salvation'.format(t=str(self)))
# 
#         return active


import datetime
from django.test import TestCase
from constants import Environments
from feature_toggle import constants
from feature_toggle.exceptions import FeatureToggleDoesNotExist, FeatureToggleAlreadyExists
from feature_toggle.models import FeatureToggle
from feature_toggle.toggle import Toggle
from services import FeatureToggleService
        tgl = Toggle(environment=constants.FeatureToggle.Environment.LOCAL, code=code, raise_does_not_exist=False)
        self.assertEqual(tgl.code, code)

    def test_is_active_with_active_toggle(self):
        ft_tgl = self.active_ft_tgl
        tgl = Toggle(environment=ft_tgl.environment, name=ft_tgl.name, code=ft_tgl.code)
        self.assertTrue(tgl.is_active())

    def test_is_active_with_inactive_toggle(self):
        ft_tgl = self.inactive_ft_tgl
        tgl = Toggle(environment=ft_tgl.environment, name=ft_tgl.name, code=ft_tgl.code)
        self.assertFalse(tgl.is_active())

    def test_is_enabled_with_start_date_less_than_now(self):
        yesterday = datetime.date.today() - datetime.timedelta(1)
        ft_tgl = self.active_ft_tgl
        ft_tgl.set_attribute(constants.FeatureToggle.Attributes.START_DATE, yesterday)
        tgl = Toggle(environment=ft_tgl.environment, name=ft_tgl.name, code=ft_tgl.code)
        self.assertTrue(tgl.is_enabled())

    def test_is_enabled_with_start_date_greater_than_now(self):
        tomorrow = datetime.date.today() + datetime.timedelta(1)
        ft_tgl = self.active_ft_tgl
        ft_tgl.set_attribute(constants.FeatureToggle.Attributes.START_DATE, tomorrow)
        tgl = Toggle(environment=ft_tgl.environment, name=ft_tgl.name, code=ft_tgl.code)
        self.assertFalse(tgl.is_enabled())

    def test_is_enabled_with_end_date_less_than_now(self):
        yesterday = datetime.date.today() - datetime.timedelta(1)
        ft_tgl = self.active_ft_tgl

# Path: feature_toggle/constants.py
# class Attribute(Container):
# class Environment(Container):
#     def __init__(self):
#     def __init__(self):
# 
# Path: feature_toggle/exceptions.py
# class FeatureToggleDoesNotExist(Exception):
#     """
#     This exception is to aid consumers in providing an common exception to catch instead of relying on
#     model.DoesNotExist.
#     """
#     pass
# 
# class FeatureToggleAlreadyExists(Exception):
#     """
#     This exception is raised when trying to create a toggle that already exists.
#     """
#     pass
# 
# Path: feature_toggle/models.py
# class FeatureToggle(TimeStampedModel):
#     uid = models.CharField(max_length=20)
#     name = models.CharField(max_length=20)
#     environment = models.CharField(max_length=50, choices=ENV_CHOICES)
#     is_active = models.BooleanField(default=True)
#     start_date_time = models.DateTimeField(auto_now_add=True)
#     end_date_time = models.DateTimeField(auto_now_add=True)  # todo: add 3 months from now by default
#     time_bomb = models.BooleanField(default=False)
# 
#     class Meta:
#         unique_together = (('name', 'environment'), ('uid',))
#         managed = True
# 
#     def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
#         if not self.uid:
#             self.uid = make_meanigful_id(self.name, length=5)
#         super().save(force_insert, force_update, using, update_fields)
# 
#     def set_attribute(self, key, value=None, update_if_existing=True):
#         attrib, created = self.attributes.get_or_create(key=key)
#         if not created and not update_if_existing:
#             raise FeatureToggleAttributeAlreadyExists(**dict(key=key))
# 
#         attrib.value = value
#         attrib.save()
# 
#     def __str__(self):
#         return "{e}: {n}({u})".format(e=self.environment, n=self.name, u=self.uid)
# 
#     def __unicode__(self):
#         return self.__str__()
# 
# Path: feature_toggle/toggle.py
# class Toggle(BaseToggle):
# 
#     def __init__(self, uid, name, environment, is_active,
#                  start_date_time, end_date_time,
#                  attributes=None, **kwargs):
#         assert isinstance(start_date_time, datetime.datetime)  # should be a time stamp
#         assert start_date_time.tzinfo is not None  # should have a time zone. otherwise causes confusions
#         self.start_date_time = start_date_time
# 
#         assert isinstance(end_date_time, datetime.datetime)  # should be a time stamp
#         assert end_date_time.tzinfo is not None  # should have a time zone. otherwise causes confusions
#         self.end_date_time = end_date_time
#         super().__init__(uid, name, environment, is_active, attributes, **kwargs)
# 
#     def __bool__(self):
#         active = super().__bool__()
# 
#         if self.end_date_time and self.end_date_time < utc_now():
#             # this check because toggles can quickly get overwhelming
#             warnings.warn('{t} has expired. Get rid of me and help me attain salvation'.format(t=str(self)))
# 
#         return active


# Path: feature_toggle/tests/test_toggle_service.py
import datetime
from django.test import TestCase
from constants import Environments
from feature_toggle import constants
from feature_toggle.exceptions import FeatureToggleDoesNotExist, FeatureToggleAlreadyExists
from feature_toggle.models import FeatureToggle
from feature_toggle.toggle import Toggle
from services import FeatureToggleService

        tgl = Toggle(environment=constants.FeatureToggle.Environment.LOCAL, code=code, raise_does_not_exist=False)
        self.assertEqual(tgl.code, code)

    def test_is_active_with_active_toggle(self):
        ft_tgl = self.active_ft_tgl
        tgl = Toggle(environment=ft_tgl.environment, name=ft_tgl.name, code=ft_tgl.code)
        self.assertTrue(tgl.is_active())

    def test_is_active_with_inactive_toggle(self):
        ft_tgl = self.inactive_ft_tgl
        tgl = Toggle(environment=ft_tgl.environment, name=ft_tgl.name, code=ft_tgl.code)
        self.assertFalse(tgl.is_active())

    def test_is_enabled_with_start_date_less_than_now(self):
        yesterday = datetime.date.today() - datetime.timedelta(1)
        ft_tgl = self.active_ft_tgl
        ft_tgl.set_attribute(constants.FeatureToggle.Attributes.START_DATE, yesterday)
        tgl = Toggle(environment=ft_tgl.environment, name=ft_tgl.name, code=ft_tgl.code)
        self.assertTrue(tgl.is_enabled())

    def test_is_enabled_with_start_date_greater_than_now(self):
        tomorrow = datetime.date.today() + datetime.timedelta(1)
        ft_tgl = self.active_ft_tgl
        ft_tgl.set_attribute(constants.FeatureToggle.Attributes.START_DATE, tomorrow)
        tgl = Toggle(environment=ft_tgl.environment, name=ft_tgl.name, code=ft_tgl.code)
        self.assertFalse(tgl.is_enabled())

    def test_is_enabled_with_end_date_less_than_now(self):
        yesterday = datetime.date.today() - datetime.timedelta(1)
        ft_tgl = self.active_ft_tgl

        ft_tgl.set_attribute(constants.FeatureToggle.Attributes.END_DATE, yesterday)