Пример #1
0
    def t9(self):
        class Foo:
            def bar(self):
                return 'bar'
        class Delegate:
            def action(self):
                f = Foo()
                return f.bar()
        
        rd_bar = redef(Foo, 'bar', lambda s: 'baz')
        ok(rd_bar.not_called()) == True
        captured = stderr_of(Redef.__del__, rd_bar)
        ok(captured.output) == 'redef\'d function \'bar\' was not called and should have been called.\n\tMis-called redefs could be due to test crashes unless explicitly tested using Redef kwargs: must_call\n\ttest_redef.py:152: rd_bar = redef(Foo, \'bar\', lambda s: \'baz\')\n'

        wt_bar = wiretap(Foo, 'bar', must_call=False)
        rd_a = redef(Delegate, 'action', lambda s: 'rainbows!')
        Delegate().action()
        ok(wt_bar.not_called()) == True
        captured = stderr_of(Redef.__del__, wt_bar)
        ok(captured.output) == ''

        wt_bar2 = wiretap(Foo, 'bar', must_call=False)
        Foo().bar()
        captured = stderr_of(Redef.__del__, wt_bar2)
        ok(captured.output) == 'redef\'d function \'bar\' was called and should not have been called.\n\tMis-called redefs could be due to test crashes unless explicitly tested using Redef kwargs: must_call\n\ttest_redef.py:164: wt_bar2 = wiretap(Foo, \'bar\', must_call=False)\n'
Пример #2
0
    def _(self):
        class A:
            """Base class"""

            def a(self, x):
                return x * 2

        class B(A):
            def a(self, x):
                ret = A.a(self, x)
                return "b" + ret

        bee = B()
        ok(bee.a("z")) == "bzz"

        want = "baa baa"
        rd_b = redef(B, "a", lambda s, x: want)
        ok(bee.a("z")) == "baa baa"
        ok(rd_b.called()) == 1
        del rd_b

        want = "atter batter"
        rd_a = redef(A, "a", lambda s, x: want)
        ok(bee.a("z")) == "batter batter"

        ok(rd_a.called()) == 1
Пример #3
0
 def t11(self):
     import string
     with redef(string, 'hexdigits', 'orange'):
         replaced = string.hexdigits
         ok(replaced) == 'orange'
     replaced = string.hexdigits
     ok(replaced) == '0123456789abcdefABCDEF'
Пример #4
0
 def t12(self):
     class Foo:
         def bar(self, x):
             return x * 3
     rd_foo = redef(Foo, 'bar', lambda s,x: x**3 )
     x = Foo()
     ok(x.bar(3)) == 27
     rd_foo.close()
     ok(x.bar(3)) == 9
Пример #5
0
 def t10(self):
     import string
     rd_hexdigits = redef(string, 'hexdigits', 'orange')
     replaced = string.hexdigits
     ok(replaced) == 'orange'
     
     del rd_hexdigits
     replaced = string.hexdigits
     ok(replaced) == '0123456789abcdefABCDEF'
Пример #6
0
    def _(self):
        class Hello:
            @staticmethod
            def world(x):
                return "Hello %s!" % x

        ok(Hello.world("joe")) == "Hello joe!"
        rd_w = redef(Hello, "world", lambda x: "Goodbye %s" % x)

        ok(Hello.world("joe")) == "Goodbye joe"
        ok(rd_w.method_args()) == (Hello, "joe")
Пример #7
0
    def t5(self):
        class Hello:
            @staticmethod
            def world(x):
                return 'Hello %s!' % x
        ok(Hello.world('joe')) == 'Hello joe!'
        rd_w = redef(Hello, 'world', lambda x: 'Goodbye %s' % x)

        ok(Hello.world('joe')) == 'Goodbye joe'
        args = rd_w.method_args()
        ok(args[0][0]) == Hello
        ok(args[0][1]) == 'joe'
Пример #8
0
 def t4(self):
     class Math:
         pi = 3.14
     m = Math()
     ok(m.pi) == 3.14
     rd_p = redef(m, 'pi', 3)
     ok(m.pi) == 3
     ok(rd_p.not_called()) == True
     ok(rd_p.method_args()) == []
     ok(rd_p.named_method_args()) == []
     del rd_p
     ok(m.pi) == 3.14
Пример #9
0
    def _(self):
        class Math:
            pi = 3.14

        m = Math()
        ok(m.pi) == 3.14
        rd_p = redef(m, "pi", 3)
        ok(m.pi) == 3
        ok(rd_p.called()) == 0
        ok(rd_p.method_args()) == None
        ok(rd_p.named_method_args()) == None
        del rd_p
        ok(m.pi) == 3.14
Пример #10
0
    def t6(self):
        class A:
            '''Base class'''
            def a(self, x):
                return x * 2
        class B(A):
            def a(self, x):
                ret = A.a(self, x)
                return 'b' + ret
        bee = B()
        ok(bee.a('z')) == 'bzz'

        want = 'baa baa'
        rd_b = redef(B, 'a', lambda s,x: want)
        ok(bee.a('z')) == 'baa baa'
        ok(rd_b.called()) == 1
        del rd_b

        want = 'atter batter'
        rd_a = redef(A, 'a', lambda s,x: want)
        ok(bee.a('z')) == 'batter batter'

        ok(rd_a.called()) == 1
Пример #11
0
 def _(self):
     somewhere = Somewhere()
     ok(somewhere.show("beyond the sea")) == "Somewhere, beyond the sea"
     rd_show = redef(Somewhere, "show", lambda self, message: "hi")
     ok(somewhere.show("over the rainbow")) == "hi"
     ok(rd_show.method_args()) == (somewhere, "over the rainbow")
     ok(rd_show.named_method_args()) == {}
     ok(rd_show.called()) == 1
     rd_show.reset()
     ok(rd_show.method_args()) == None
     ok(rd_show.named_method_args()) == None
     ok(rd_show.called()) == 0
     ok(somewhere.show("beyond the stars")) == "hi"
     del rd_show
     ok(somewhere.show("beyond the stars")) == "Somewhere, beyond the stars"
Пример #12
0
 def t1(self):
     somewhere = Somewhere()
     ok(somewhere.show('beyond the sea')) == 'Somewhere, beyond the sea'
     rd_show = redef(Somewhere, 'show', lambda self, message: 'hi')
     ok(somewhere.show('over the rainbow')) == 'hi'
     #ok(rd_show.method_args()[0]) == (somewhere, 'over the rainbow') # tye types are different in python 3
     ok(rd_show.named_method_args()[0]) == {}
     ok(rd_show.called()) == 1
     rd_show.reset()
     ok(rd_show.method_args()) == []
     ok(rd_show.named_method_args()) == []
     ok(rd_show.called()) == 0
     ok(somewhere.show('beyond the stars')) == 'hi'
     del rd_show
     ok(somewhere.show('beyond the stars')) == 'Somewhere, beyond the stars'
Пример #13
0
    def _(self):
        class Pirate:
            drunk = 0

            def attack(self):
                print("arrr!")

            def hoard(self, booty):
                print("yarrr, don't touch me %s" % booty)

            def plunder(self, booty):
                self.attack()
                for x in range(1, 10):
                    self.drink()
                self.hoard(booty)
                return "punch"

            def drink(self):
                print("burp!")
                self.drunk = self.drunk + 1

        blackbeard = Pirate()
        rd_d = redef(Pirate, "drink", None)
        captured = stdout_of(blackbeard.plunder, "buried treasure")

        ok(rd_d.called()) == 9

        talk = "arrr!\n" + "burp!\n" * 9 + "yarrr, don't touch me buried treasure\n"

        ok(captured.output) == talk
        ok(captured.returned) == "punch"

        captured = stdout_of(blackbeard.plunder, "parrot")
        ok(rd_d.called()) == 18

        rd_d.reset()
        ok(rd_d.called()) == 0
Пример #14
0
 def _(self):
     somewhere = Somewhere()
     ok(somewhere.show("beyond the stars")) == "Somewhere, beyond the stars"
     rd_show = redef(Somewhere, "show", lambda self, message: "bye")
     ok(somewhere.show("over the rainbow")) == "bye"
Пример #15
0
 def t2(self):
     somewhere = Somewhere()
     ok(somewhere.show('beyond the stars')) == 'Somewhere, beyond the stars'
     rd_show = redef(Somewhere, 'show', lambda self, message: 'bye')
     ok(somewhere.show('over the rainbow')) == 'bye'
Пример #16
0
from redef import redef

# Open the database
client = MongoClient('mongodb://*****:*****@app.route('/register')
def register():
    username = request.args.get('username')
    password = request.args.get('password')

    if (username == None or password == None):
        return "Username or password was not passed in!"

    if (db.users.find({"username": username}).count() == 0):
        salt = bcrypt.gensalt()
        hashed_password = bcrypt.hashpw(password.encode('utf8'), salt)
        db.users.insert({
            "username": username,