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


Python manager.SwitchManager类代码示例

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


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

示例1: setUp

    def setUp(self):
        super(NumberConditionSetTests, self).setUp()
        self.gargoyle = SwitchManager(Switch, key='key', value='value', instances=True, auto_create=True)
        self.gargoyle.register(NumberConditionSet())

        Switch.objects.create(key='test', status=SELECTIVE)
        self.switch = self.gargoyle['test']
开发者ID:YPlan,项目名称:gargoyle,代码行数:7,代码来源:test_conditions.py

示例2: MockRequestTest

class MockRequestTest(TestCase):
    def setUp(self):
        self.gargoyle = SwitchManager(Switch, key='key', value='value', instances=True)

    def test_empty_attrs(self):
        req = MockRequest()
        self.assertEquals(req.META['REMOTE_ADDR'], None)
        self.assertEquals(req.user.__class__, AnonymousUser)

    def test_ip(self):
        req = MockRequest(ip_address='127.0.0.1')
        self.assertEquals(req.META['REMOTE_ADDR'], '127.0.0.1')
        self.assertEquals(req.user.__class__, AnonymousUser)

    def test_user(self):
        user = User.objects.create(username='foo', email='[email protected]')
        req = MockRequest(user=user)
        self.assertEquals(req.META['REMOTE_ADDR'], None)
        self.assertEquals(req.user, user)

    def test_as_request(self):
        user = User.objects.create(username='foo', email='[email protected]')

        req = self.gargoyle.as_request(user=user, ip_address='127.0.0.1')

        self.assertEquals(req.META['REMOTE_ADDR'], '127.0.0.1')
        self.assertEquals(req.user, user)
开发者ID:akaihola,项目名称:gargoyle,代码行数:27,代码来源:tests.py

示例3: HostConditionSetTests

class HostConditionSetTests(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"]

        assert not self.gargoyle.is_active("test")

        switch.add_condition(condition_set=condition_set, field_name="hostname", condition=socket.gethostname())

        assert self.gargoyle.is_active("test")
开发者ID:YPlan,项目名称:gargoyle,代码行数:17,代码来源:test_builtins.py

示例4: setUp

    def setUp(self):
        super(IPAddressConditionSetTests, self).setUp()
        self.gargoyle = SwitchManager(Switch, key='key', value='value', instances=True, auto_create=True)
        self.gargoyle.register(IPAddressConditionSet())
        self.request_factory = RequestFactory()

        Switch.objects.create(key='test', status=SELECTIVE)
        self.switch = self.gargoyle['test']
        assert not self.gargoyle.is_active('test')
开发者ID:mixcloud,项目名称:gargoyle,代码行数:9,代码来源:test_builtins.py

示例5: SwitchContextManagerTest

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
        switch.save()

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

        self.assertFalse(test2())
        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
        switch.save()

        with switches(self.gargoyle, test=False):
            self.assertFalse(self.gargoyle.is_active('test'))

        self.assertEquals(self.gargoyle['test'].status, GLOBAL)
开发者ID:akaihola,项目名称:gargoyle,代码行数:41,代码来源:tests.py

示例6: HostConditionSetTest

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:akaihola,项目名称:gargoyle,代码行数:24,代码来源:tests.py

示例7: NumberConditionSetTests

class NumberConditionSetTests(TestCase):

    condition_set = __name__ + '.' + NumberConditionSet.__name__

    def setUp(self):
        super(NumberConditionSetTests, self).setUp()
        self.gargoyle = SwitchManager(Switch, key='key', value='value', instances=True, auto_create=True)
        self.gargoyle.register(NumberConditionSet())

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

    def test_range(self):
        self.switch.add_condition(
            condition_set=self.condition_set,
            field_name='in_range',
            condition='1-3',
        )

        assert not self.gargoyle.is_active('test', 0)
        assert self.gargoyle.is_active('test', 1)
        assert self.gargoyle.is_active('test', 2)
        assert self.gargoyle.is_active('test', 3)
        assert not self.gargoyle.is_active('test', 4)
开发者ID:YPlan,项目名称:gargoyle,代码行数:24,代码来源:test_conditions.py

示例8: setUp

 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))
开发者ID:akaihola,项目名称:gargoyle,代码行数:4,代码来源:tests.py

示例9: TemplateTagTest

class TemplateTagTest(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)
        self.gargoyle.register(UserConditionSet(User))

    def test_simple(self):
        Switch.objects.create(
            key='test',
            status=GLOBAL,
        )

        template = Template("""
            {% load gargoyle_tags %}
            {% ifswitch test %}
            hello world!
            {% endifswitch %}
        """)
        rendered = template.render(Context())

        self.assertTrue('hello world!' in rendered)

    def test_else(self):
        Switch.objects.create(
            key='test',
            status=DISABLED,
        )

        template = Template("""
            {% load gargoyle_tags %}
            {% ifswitch test %}
            hello world!
            {% else %}
            foo bar baz
            {% endifswitch %}
        """)
        rendered = template.render(Context())

        self.assertTrue('foo bar baz' in rendered)
        self.assertFalse('hello world!' in rendered)

    def test_with_request(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',
        )

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

        template = Template("""
            {% load gargoyle_tags %}
            {% ifswitch test %}
            hello world!
            {% else %}
            foo bar baz
            {% endifswitch %}
        """)
        rendered = template.render(Context({'request': request}))

        self.assertFalse('foo bar baz' in rendered)
        self.assertTrue('hello world!' in rendered)

    def test_missing_name(self):
        self.assertRaises(TemplateSyntaxError, Template, """
            {% load gargoyle_tags %}
            {% ifswitch %}
            hello world!
            {% endifswitch %}
        """)

    def test_with_custom_objects(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',
        )

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

#.........这里部分代码省略.........
开发者ID:akaihola,项目名称:gargoyle,代码行数:101,代码来源:tests.py

示例10: APITest

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())
        self.internal_ips = settings.INTERNAL_IPS

    def tearDown(self):
        settings.INTERNAL_IPS = self.internal_ips

    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))

        switch.clear_conditions(condition_set=condition_set)
        switch.add_condition(
            condition_set=condition_set,
            field_name='email',
            condition='[email protected]',
        )

        user = User(pk=8771, email="[email protected]")
        self.assertTrue(self.gargoyle.is_active('test', user))

        user = User(pk=8771, email="[email protected]")
        self.assertFalse(self.gargoyle.is_active('test', user))

        user = User(pk=8771)
#.........这里部分代码省略.........
开发者ID:akaihola,项目名称:gargoyle,代码行数:101,代码来源:tests.py

示例11: APITest

class APITest(TestCase):

    request_factory = RequestFactory()

    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_register_unregister_class(self):
        klass = IPAddressConditionSet
        registered = self.gargoyle.register(IPAddressConditionSet)
        assert registered is klass

        unregistered = self.gargoyle.unregister(klass)
        assert unregistered

        unregistered = self.gargoyle.unregister(klass)
        assert not unregistered

    def test_register_unregister_instance(self):
        condition_set = IPAddressConditionSet()
        registered = self.gargoyle.register(condition_set)
        assert registered is condition_set

        unregistered = self.gargoyle.unregister(condition_set)
        assert unregistered

        unregistered = self.gargoyle.unregister(condition_set)
        assert not unregistered

    def test_builtin_registration(self):
        assert 'gargoyle.builtins.UserConditionSet(auth.user)' in self.gargoyle._registry
        assert 'gargoyle.builtins.IPAddressConditionSet' in self.gargoyle._registry
        assert len(list(self.gargoyle.get_condition_sets())) == 2

    def test_user(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',
        )

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

        user = User(pk=8771)
        assert not 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)
        assert self.gargoyle.is_active('test', user)

        user = User(pk=8771, is_superuser=True)
        assert not 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)
        assert self.gargoyle.is_active('test', user)

        # test with request
        request = self.request_factory.get('/', user=user)
        assert self.gargoyle.is_active('test', request)

        # test date joined condition
        user = User(pk=8771)
        assert not 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, 7, 2))
        assert self.gargoyle.is_active('test', user)

        user = User(pk=8771, date_joined=datetime.datetime(2012, 7, 2))
        assert self.gargoyle.is_active('test', user)

        user = User(pk=8771, date_joined=datetime.datetime(2011, 6, 2))
        assert not self.gargoyle.is_active('test', user)

        user = User(pk=8771, date_joined=datetime.datetime(2011, 7, 1))
#.........这里部分代码省略.........
开发者ID:pombredanne,项目名称:gargoyle,代码行数:101,代码来源:test_api.py

示例12: IPAddressConditionSetTests

class IPAddressConditionSetTests(TestCase):

    condition_set = 'gargoyle.builtins.IPAddressConditionSet'

    def setUp(self):
        super(IPAddressConditionSetTests, self).setUp()
        self.gargoyle = SwitchManager(Switch, key='key', value='value', instances=True, auto_create=True)
        self.gargoyle.register(IPAddressConditionSet())
        self.request_factory = RequestFactory()

        Switch.objects.create(key='test', status=SELECTIVE)
        self.switch = self.gargoyle['test']
        assert not self.gargoyle.is_active('test')

    def test_percent(self):
        self.switch.add_condition(
            condition_set=self.condition_set,
            field_name='percent',
            condition='0-100',
        )

        request = self.request_factory.get('/', REMOTE_ADDR='1.0.0.0')
        assert self.gargoyle.is_active('test', request)

    def test_0_percent(self):
        self.switch.add_condition(
            condition_set=self.condition_set,
            field_name='percent',
            condition='0-0',
        )

        request = self.request_factory.get('/', REMOTE_ADDR='1.0.0.0')
        assert not self.gargoyle.is_active('test', request)

    def test_specific_address(self):
        self.switch.add_condition(
            condition_set=self.condition_set,
            field_name='ip_address',
            condition='1.1.1.1',
        )

        request = self.request_factory.get('/', REMOTE_ADDR='1.0.0.0')
        assert not self.gargoyle.is_active('test', request)

        request = self.request_factory.get('/', REMOTE_ADDR='1.1.1.1')
        assert self.gargoyle.is_active('test', request)

    @override_settings(INTERNAL_IPS=['1.0.0.0'])
    def test_internal_ip(self):
        self.switch.add_condition(
            condition_set=self.condition_set,
            field_name='internal_ip',
            condition='',
        )

        request = self.request_factory.get('/', REMOTE_ADDR='1.0.0.0')
        assert self.gargoyle.is_active('test', request)

        request = self.request_factory.get('/', REMOTE_ADDR='1.1.1.1')
        assert not self.gargoyle.is_active('test', request)

    @override_settings(INTERNAL_IPS=['1.0.0.0'])
    def test_not_internal_ip(self):
        self.switch.add_condition(
            condition_set=self.condition_set,
            field_name='internal_ip',
            condition='',
            exclude=True,
        )

        request = self.request_factory.get('/', REMOTE_ADDR='1.0.0.0')
        assert not self.gargoyle.is_active('test', request)

        request = self.request_factory.get('/', REMOTE_ADDR='1.1.1.1')
        assert self.gargoyle.is_active('test', request)
开发者ID:mixcloud,项目名称:gargoyle,代码行数:75,代码来源:test_builtins.py

示例13: IPAddressConditionSetTests

class IPAddressConditionSetTests(TestCase):

    condition_set = "gargoyle.builtins.IPAddressConditionSet"

    def setUp(self):
        super(IPAddressConditionSetTests, self).setUp()
        self.gargoyle = SwitchManager(Switch, key="key", value="value", instances=True, auto_create=True)
        self.gargoyle.register(IPAddressConditionSet())
        self.request_factory = RequestFactory()

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

    def test_percent(self):
        self.switch.add_condition(condition_set=self.condition_set, field_name="percent", condition="0-100")

        request = self.request_factory.get("/", REMOTE_ADDR="1.0.0.0")
        assert self.gargoyle.is_active("test", request)

    def test_0_percent(self):
        self.switch.add_condition(condition_set=self.condition_set, field_name="percent", condition="0-0")

        request = self.request_factory.get("/", REMOTE_ADDR="1.0.0.0")
        assert not self.gargoyle.is_active("test", request)

    def test_specific_address(self):
        self.switch.add_condition(condition_set=self.condition_set, field_name="ip_address", condition="1.1.1.1")

        request = self.request_factory.get("/", REMOTE_ADDR="1.0.0.0")
        assert not self.gargoyle.is_active("test", request)

        request = self.request_factory.get("/", REMOTE_ADDR="1.1.1.1")
        assert self.gargoyle.is_active("test", request)

    @override_settings(INTERNAL_IPS=["1.0.0.0"])
    def test_internal_ip(self):
        self.switch.add_condition(condition_set=self.condition_set, field_name="internal_ip", condition="")

        request = self.request_factory.get("/", REMOTE_ADDR="1.0.0.0")
        assert self.gargoyle.is_active("test", request)

        request = self.request_factory.get("/", REMOTE_ADDR="1.1.1.1")
        assert not self.gargoyle.is_active("test", request)

    @override_settings(INTERNAL_IPS=["1.0.0.0"])
    def test_not_internal_ip(self):
        self.switch.add_condition(
            condition_set=self.condition_set, field_name="internal_ip", condition="", exclude=True
        )

        request = self.request_factory.get("/", REMOTE_ADDR="1.0.0.0")
        assert not self.gargoyle.is_active("test", request)

        request = self.request_factory.get("/", REMOTE_ADDR="1.1.1.1")
        assert self.gargoyle.is_active("test", request)
开发者ID:YPlan,项目名称:gargoyle,代码行数:56,代码来源:test_builtins.py


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