当前位置: 首页>>代码示例>>Python>>正文


Python SwitchManager.is_active方法代码示例

本文整理汇总了Python中gargoyle.models.SwitchManager.is_active方法的典型用法代码示例。如果您正苦于以下问题:Python SwitchManager.is_active方法的具体用法?Python SwitchManager.is_active怎么用?Python SwitchManager.is_active使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在gargoyle.models.SwitchManager的用法示例。


在下文中一共展示了SwitchManager.is_active方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: SwitchContextManagerTest

# 需要导入模块: from gargoyle.models import SwitchManager [as 别名]
# 或者: from gargoyle.models.SwitchManager import is_active [as 别名]
class SwitchContextManagerTest(TestCase):
    def setUp(self):
        self.gargoyle = SwitchManager(Switch, key='key', value='value', instances=True, auto_create=True)

    def test_as_decorator(self):
        switch = self.gargoyle['test']
        switch.status = DISABLED
        
        @switches(self.gargoyle, test=True)
        def test():
            return self.gargoyle.is_active('test')
        
        self.assertTrue(test())
        self.assertEquals(self.gargoyle['test'].status, DISABLED)

        switch.status = GLOBAL
        
        @switches(self.gargoyle, test=False)
        def test():
            return self.gargoyle.is_active('test')

        self.assertFalse(test())
        self.assertEquals(self.gargoyle['test'].status, GLOBAL)

    def test_context_manager(self):
        switch = self.gargoyle['test']
        switch.status = DISABLED
        
        with switches(self.gargoyle, test=True):
            self.assertTrue(self.gargoyle.is_active('test'))
        
        self.assertEquals(self.gargoyle['test'].status, DISABLED)

        switch.status = GLOBAL
        
        with switches(self.gargoyle, test=False):
            self.assertFalse(self.gargoyle.is_active('test'))
        
        self.assertEquals(self.gargoyle['test'].status, GLOBAL)
开发者ID:laszlopandy,项目名称:gargoyle,代码行数:41,代码来源:tests.py

示例2: HostConditionSetTest

# 需要导入模块: from gargoyle.models import SwitchManager [as 别名]
# 或者: from gargoyle.models.SwitchManager import is_active [as 别名]
class HostConditionSetTest(TestCase):
    def setUp(self):
        self.gargoyle = SwitchManager(Switch, key='key', value='value', instances=True, auto_create=True)
        self.gargoyle.register(HostConditionSet())

    def test_simple(self):
        condition_set = 'gargoyle.builtins.HostConditionSet'
        
        # we need a better API for this (model dict isnt cutting it)
        switch = Switch.objects.create(
            key='test',
            status=SELECTIVE,
        )
        switch = self.gargoyle['test']

        self.assertFalse(self.gargoyle.is_active('test'))
        
        switch.add_condition(
            condition_set=condition_set,
            field_name='hostname',
            condition=socket.gethostname(),
        )

        self.assertTrue(self.gargoyle.is_active('test'))
开发者ID:szilveszter,项目名称:gargoyle,代码行数:26,代码来源:tests.py

示例3: APITest

# 需要导入模块: from gargoyle.models import SwitchManager [as 别名]
# 或者: from gargoyle.models.SwitchManager import is_active [as 别名]
class APITest(TestCase):
    urls = 'tests.urls'
    
    def setUp(self):
        self.user = User.objects.create(username='foo', email='[email protected]')
        self.gargoyle = SwitchManager(Switch, key='key', value='value', instances=True, auto_create=True)
        self.gargoyle.register(UserConditionSet(User))
        self.gargoyle.register(IPAddressConditionSet())

    def test_builtin_registration(self):
        self.assertTrue('gargoyle.builtins.UserConditionSet(auth.user)' in self.gargoyle._registry)
        self.assertTrue('gargoyle.builtins.IPAddressConditionSet' in self.gargoyle._registry)
        self.assertEquals(len(list(self.gargoyle.get_condition_sets())), 2, self.gargoyle)
        
    def test_user(self):
        condition_set = 'gargoyle.builtins.UserConditionSet(auth.user)'
        
        # we need a better API for this (model dict isnt cutting it)
        switch = Switch.objects.create(
            key='test',
            status=SELECTIVE,
        )
        switch = self.gargoyle['test']
        
        switch.add_condition(
            condition_set=condition_set,
            field_name='percent',
            condition='0-50',
        )

        user = User(pk=5)
        self.assertTrue(self.gargoyle.is_active('test', user))

        user = User(pk=8771)
        self.assertFalse(self.gargoyle.is_active('test', user))
        
        switch.add_condition(
            condition_set=condition_set,
            field_name='is_staff',
            condition='1',
        )

        user = User(pk=8771, is_staff=True)
        self.assertTrue(self.gargoyle.is_active('test', user))

        user = User(pk=8771, is_superuser=True)
        self.assertFalse(self.gargoyle.is_active('test', user))

        switch.add_condition(
            condition_set=condition_set,
            field_name='is_superuser',
            condition='1',
        )
        
        user = User(pk=8771, is_superuser=True)
        self.assertTrue(self.gargoyle.is_active('test', user))

        # test with mock request
        self.assertTrue(self.gargoyle.is_active('test', self.gargoyle.as_request(user=user)))

        # test date joined condition
        user = User(pk=8771)
        self.assertFalse(self.gargoyle.is_active('test', user))

        switch.add_condition(
            condition_set=condition_set,
            field_name='date_joined',
            condition='2011-07-01',
        )

        user = User(pk=8771, date_joined=datetime.datetime(2011, 07, 02))
        self.assertTrue(self.gargoyle.is_active('test', user))

        user = User(pk=8771, date_joined=datetime.datetime(2012, 07, 02))
        self.assertTrue(self.gargoyle.is_active('test', user))

        user = User(pk=8771, date_joined=datetime.datetime(2011, 06, 02))
        self.assertFalse(self.gargoyle.is_active('test', user))

        user = User(pk=8771, date_joined=datetime.datetime(2011, 07, 01))
        self.assertTrue(self.gargoyle.is_active('test', user))

    def test_exclusions(self):
        condition_set = 'gargoyle.builtins.UserConditionSet(auth.user)'
        
        switch = Switch.objects.create(
            key='test',
            status=SELECTIVE,
        )
        switch = self.gargoyle['test']
        
        switch.add_condition(
            condition_set=condition_set,
            field_name='percent',
            condition='0-50',
            exclude=True,
        )
        switch.add_condition(
            condition_set=condition_set,
            field_name='username',
#.........这里部分代码省略.........
开发者ID:szilveszter,项目名称:gargoyle,代码行数:103,代码来源:tests.py

示例4: GargoyleTest

# 需要导入模块: from gargoyle.models import SwitchManager [as 别名]
# 或者: from gargoyle.models.SwitchManager import is_active [as 别名]
class GargoyleTest(TestCase):
    urls = "gargoyle.tests.urls"

    def setUp(self):
        self.user = User.objects.create(username="foo", email="[email protected]")
        self.gargoyle = SwitchManager(Switch, key="key", value="value", instances=True)
        self.gargoyle.register(UserConditionSet(User))
        self.gargoyle.register(IPAddressConditionSet())

    def test_builtin_registration(self):
        self.assertTrue("gargoyle.builtins.UserConditionSet(auth.user)" in self.gargoyle._registry)
        self.assertTrue("gargoyle.builtins.IPAddressConditionSet" in self.gargoyle._registry)
        self.assertEquals(len(list(self.gargoyle.get_condition_sets())), 2, self.gargoyle)

    def test_user(self):
        condition_set = "gargoyle.builtins.UserConditionSet(auth.user)"

        # we need a better API for this (model dict isnt cutting it)
        switch = Switch.objects.create(key="test", status=SELECTIVE)
        switch = self.gargoyle["test"]

        switch.add_condition(condition_set=condition_set, field_name="percent", condition="0-50")

        user = User(pk=5)
        self.assertTrue(self.gargoyle.is_active("test", user))

        user = User(pk=8771)
        self.assertFalse(self.gargoyle.is_active("test", user))

        switch.add_condition(condition_set=condition_set, field_name="is_staff", condition="1")

        user = User(pk=8771, is_staff=True)
        self.assertTrue(self.gargoyle.is_active("test", user))

        user = User(pk=8771, is_superuser=True)
        self.assertFalse(self.gargoyle.is_active("test", user))

        switch.add_condition(condition_set=condition_set, field_name="is_superuser", condition="1")

        user = User(pk=8771, is_superuser=True)
        self.assertTrue(self.gargoyle.is_active("test", user))

    def test_exclusions(self):
        condition_set = "gargoyle.builtins.UserConditionSet(auth.user)"

        switch = Switch.objects.create(key="test", status=SELECTIVE)
        switch = self.gargoyle["test"]

        switch.add_condition(condition_set=condition_set, field_name="percent", condition="0-50", exclude=True)
        switch.add_condition(condition_set=condition_set, field_name="username", condition="foo")

        user = User(pk=5, username="foo")
        self.assertFalse(self.gargoyle.is_active("test", user))

        user = User(pk=8771, username="foo")
        self.assertTrue(self.gargoyle.is_active("test", user))

    def test_decorator_for_user(self):
        condition_set = "gargoyle.builtins.UserConditionSet(auth.user)"

        switch = Switch.objects.create(key="test", status=DISABLED)
        switch = self.gargoyle["test"]

        @switch_is_active("test")
        def test(request):
            return True

        request = HttpRequest()
        request.user = self.user

        self.assertRaises(Http404, test, request)

        switch.status = SELECTIVE
        switch.save()

        self.assertTrue(test(request))

        switch.add_condition(condition_set=condition_set, field_name="username", condition="foo")

        self.assertTrue(test(request))

    def test_decorator_for_ip_address(self):
        condition_set = "gargoyle.builtins.IPAddressConditionSet"

        switch = Switch.objects.create(key="test", status=DISABLED)
        switch = self.gargoyle["test"]

        @switch_is_active("test")
        def test(request):
            return True

        request = HttpRequest()
        request.META["REMOTE_ADDR"] = "192.168.1.1"

        self.assertRaises(Http404, test, request)

        switch.status = SELECTIVE
        switch.save()

        switch.add_condition(condition_set=condition_set, field_name="ip_address", condition="192.168.1.1")
#.........这里部分代码省略.........
开发者ID:slyons,项目名称:gargoyle,代码行数:103,代码来源:tests.py


注:本文中的gargoyle.models.SwitchManager.is_active方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。