예제 #1
0
 def test_multi_levels(self):
     with Context(a=1, b=2):
         self.assertEqual(1, Context.current().a)
         self.assertEqual(2, Context.current().b)
         with Context(b=3, c=4):
             self.assertEqual(1, Context.current().a)
             self.assertEqual(3, Context.current().b)
             self.assertEqual(4, Context.current().c)
         self.assertEqual(1, Context.current().a)
         self.assertEqual(2, Context.current().b)
         with self.assertRaises(AttributeError):
             Context.current().c
예제 #2
0
    def test_multi_levels(self):
        @Once
        def foo(a, b):
            return a + b

        with Context():
            with OnceSession():
                with Context():
                    self.assertEqual(3, foo(1, 2))
            with OnceSession():
                with Context():
                    self.assertEqual(7, foo(3, 4))
예제 #3
0
    def test_context_is_not_session(self):
        @Once
        def foo(a, b):
            return a + b

        with Context():
            self.assertEqual(3, foo(1, 2))
            self.assertEqual(3, foo(3, 4))
예제 #4
0
 def test_set(self):
     with Context(a=1):
         self.assertEquals(1, ctx.a)
         ctx.a = 2
         self.assertEquals(2, ctx.a)
         ctx.b = 3
         self.assertEquals(3, ctx.b)
         d = ctx.dict()
         self.assertEquals({'a': 2, 'b': 3}, d)
예제 #5
0
    def test_error(self):
        self.calls = []

        def _action1():
            raise Exception

        def _action2():
            self.calls.append('action2')

        with Context() as ctx:
            ctx.defer(_action1)
            ctx.defer(_action2)
        self.assertEqual(['action2'], self.calls)
예제 #6
0
    def test_normal(self):
        self.calls = []

        def _action1():
            self.calls.append('action1')

        def _action2():
            self.calls.append('action2')

        with Context() as ctx:
            ctx.defer(_action1)
            ctx.defer(_action2)
        self.assertEqual(['action1', 'action2'], self.calls)
예제 #7
0
    def test_extra_vars(self):
        # set up
        @self.cache.cache('/{a}/{ctx.b}', vars={'a': 1, 'ctx': ctx})
        def foo():
            pass

        @self.cache.uncache('/{a}/{ctx.b}', vars={'a': 1, 'ctx': ctx})
        def unfoo():
            pass

        with Context(b=2):
            foo()
            self.assertIn('/1/2', self.cache._data)
            unfoo()
            self.assertNotIn('/1/2', self.cache._data)
예제 #8
0
 def test_success(self):
     with Context():
         self.assertIsInstance(ctx.get(), Context)
예제 #9
0
 def test_single_level(self):
     with Context(path='/test'):
         self.assertEqual('/test', Context.current().path)
예제 #10
0
 def test_multi_levels(self):
     with Context(a=1, b=2, _c=3):
         with Context(b=3, _d=4) as ctx:
             data = ctx.dict()
             self.assertEqual({'a': 1, 'b': 3}, data)
예제 #11
0
 def test_single_level(self):
     ctx = Context(a=1, b=2, _c=3)
     data = ctx.dict()
     self.assertEqual({'a': 1, 'b': 2}, data)