예제 #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,