Esempio n. 1
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))
Esempio n. 2
0
    def test_multi_levels_method(self):
        class Context1(Context):
            def a(self):
                return 1

            def b(self):
                return 2

        class Context2(Context):
            def b(self):
                return 3

            def c(self):
                return 4

        with Context1():
            self.assertEqual(1, Context.current().a())
            self.assertEqual(2, Context.current().b())
            with Context2():
                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):
                self.assertEqual(3, Context.current().c())
Esempio n. 3
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
Esempio n. 4
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))
Esempio n. 5
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)
Esempio n. 6
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)
Esempio n. 7
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)
Esempio n. 8
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)
Esempio n. 9
0
 def test_multi_levels_method(self):
     class Context1(Context):
         def a(self):
             return 1
          
         def b(self):
             return 2
     class Context2(Context):
         def b(self):
             return 3
          
         def c(self):
             return 4
     with Context1():
         self.assertEqual(1, Context.current().a())
         self.assertEqual(2, Context.current().b())
         with Context2():
             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):
             self.assertEqual(3, Context.current().c())
Esempio n. 10
0
 def test_single_level(self):
     ctx = Context(a=1, b=2, _c=3)
     data = ctx.dict()
     self.assertEqual({'a': 1, 'b': 2}, data)
Esempio n. 11
0
 def test_success(self):
     with Context():
         self.assertIsInstance(ctx.get(), Context)
Esempio n. 12
0
 def test_single_level(self):
     with Context(path='/test'):
         self.assertEqual('/test', Context.current().path)
Esempio n. 13
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)
Esempio n. 14
0
 def test_single_level(self):
     ctx = Context(a=1, b=2, _c=3)
     data = ctx.dict()
     self.assertEqual({'a': 1, 'b': 2}, data)
Esempio n. 15
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
Esempio n. 16
0
 def test_single_level(self):
     with Context(path='/test'):
         self.assertEqual('/test', Context.current().path)