예제 #1
0
def test_story_half_play_proxy_class():
    assert test_mod.Stuff(1, 2).mix(3, 4) == (1, 2, 3, 4)

    with Story(test_mod) as story:
        obj = test_mod.Stuff(1, 2)
        obj.mix(3, 4) == (1, 2, 3, 4)

    with story.replay(recurse_lock=True, proxy=True, strict=False) as replay:
        obj = test_mod.Stuff(1, 2)
        assert obj.mix(3, 4) == (1, 2, 3, 4)
        assert obj.meth() is None

        raises(TypeError, obj.meth, 123)

        obj = test_mod.Stuff(0, 1)
        assert obj.mix('a', 'b') == (0, 1, 'a', 'b')
        assert obj.mix(3, 4) == (0, 1, 3, 4)

        raises(TypeError, obj.meth, 123)
    assert replay.unexpected == format_calls(OrderedDict([
        (('stuff_1', 'meth', '', ''), _Returns('None')),
        (('stuff_1', 'meth', '123', ''), _Raises(repr_ex(TypeError(
            'meth() takes exactly 1 argument (2 given)' if PY2 else
            'meth() takes 1 positional argument but 2 were given'
        )))),
        ((None, 'test_pkg1.test_pkg2.test_mod.Stuff', '0, 1', ''), _Binds("stuff_2")),
        (('stuff_2', 'mix', "'a', 'b'", ''), _Returns("(0, 1, 'a', 'b')")),
        (('stuff_2', 'mix',  '3, 4', ''), _Returns('(0, 1, 3, 4)')),
        (('stuff_2', 'meth', '123', ''), _Raises(repr_ex(TypeError(
            'meth() takes exactly 1 argument (2 given)' if PY2 else
            'meth() takes 1 positional argument but 2 were given'
        ))))
    ]))
예제 #2
0
def test_story_half_play_noproxy_class():
    with Story(test_mod) as story:
        obj = test_mod.Stuff(1, 2)

    with story.replay(recurse_lock=True, proxy=False, strict=False):
        obj = test_mod.Stuff(1, 2)
        raises(AssertionError, obj.mix, 3, 4)
예제 #3
0
def test_story_empty_play_proxy_class():
    assert test_mod.Stuff(1, 2).mix(3, 4) == (1, 2, 3, 4)

    with Story(test_mod).replay(recurse_lock=True, proxy=True, strict=False) as replay:
        obj = test_mod.Stuff(1, 2)
        assert obj.mix(3, 4) == (1, 2, 3, 4)
        assert obj.mix('a', 'b') == (1, 2, 'a', 'b')

        raises(TypeError, obj.meth, 123)

        obj = test_mod.Stuff(0, 1)
        assert obj.mix('a', 'b') == (0, 1, 'a', 'b')
        assert obj.mix(3, 4) == (0, 1, 3, 4)

        raises(TypeError, obj.meth, 123)

    assert format_calls(replay._actual) == format_calls(OrderedDict([
        ((None, 'test_pkg1.test_pkg2.test_mod.Stuff', "1, 2", ''), _Binds('stuff_1')),
        (('stuff_1', 'mix', "3, 4", ''), _Returns("(1, 2, 3, 4)")),
        (('stuff_1', 'mix', "'a', 'b'", ''), _Returns("(1, 2, 'a', 'b')")),
        (('stuff_1', 'meth', "123", ''), _Raises(repr_ex(TypeError(
            'meth() takes exactly 1 argument (2 given)' if PY2 else
            'meth() takes 1 positional argument but 2 were given'
        )))),
        ((None, 'test_pkg1.test_pkg2.test_mod.Stuff', "0, 1", ''), _Binds('stuff_2')),
        (('stuff_2', 'mix', "'a', 'b'", ''), _Returns("(0, 1, 'a', 'b')")),
        (('stuff_2', 'mix', "3, 4", ''), _Returns("(0, 1, 3, 4)")),
        (('stuff_2', 'meth', "123", ''), _Raises(repr_ex(TypeError(
            'meth() takes exactly 1 argument (2 given)' if PY2 else
            'meth() takes 1 positional argument but 2 were given'
        ))))
    ]))
예제 #4
0
def test_xxx():
    with Story(test_mod) as story:
        obj = test_mod.Stuff(1, 2)
        test_mod.target(1) == 2
        test_mod.target(2) == 3
        test_mod.target(3) ** ValueError
        other = test_mod.Stuff(2, 2)
        obj.other('a') == other
        obj.meth('a') == 'x'
        obj = test_mod.Stuff(2, 3)
        obj.meth() ** ValueError('crappo')
        obj.meth('c') == 'x'

    with story.replay(recurse_lock=True, strict=False) as replay:
        obj = test_mod.Stuff(1, 2)
        obj.meth('a')
        test_mod.target(1)
        obj.meth()
        test_mod.func(5)

        obj = test_mod.Stuff(4, 4)
        obj.meth()

    for k, v in story._calls.items():
        print(k, "=>", v)
    print("############## UNEXPECTED ##############")
    for k, v in replay._actual.items():
        print(k, "=>", v)
예제 #5
0
def test_story_empty_play_error_on_init():
    with Story(test_mod).replay(strict=False) as replay:
        raises(ValueError, test_mod.Stuff, "error")
        print(replay._actual)
    assert replay._actual == OrderedDict([
        ((None, 'test_pkg1.test_pkg2.test_mod.Stuff', "'error'", ''), _Raises('ValueError()'))
    ])
def test_story_create():
    with Story(test_mod) as story:
        test_mod.target('a', 'b', 'c') == 'abc'
        test_mod.target()**Exception
        test_mod.target(1, 2, 3) == 'foobar'
        obj = test_mod.Stuff('stuff')
        assert isinstance(obj, test_mod.Stuff)
        obj.meth('other', 1, 2) == 123
        obj.mix('other') == 'mixymix'
    #from pprint import pprint as print
    #print (dict(story._calls))
    assert dict(story._calls) == {
        (None, 'test_pkg1.test_pkg2.test_mod.Stuff', "'stuff'", ''):
        _Binds('stuff_1'),
        ('stuff_1', 'meth', "'other', 1, 2", ''):
        _Returns("123"),
        ('stuff_1', 'mix', "'other'", ''):
        _Returns("'mixymix'"),
        (None, 'test_pkg1.test_pkg2.test_mod.target', '', ''):
        _Raises("Exception"),
        (None, 'test_pkg1.test_pkg2.test_mod.target', "1, 2, 3", ''):
        _Returns("'foobar'"),
        (None, 'test_pkg1.test_pkg2.test_mod.target', "'a', 'b', 'c'", ''):
        _Returns("'abc'"),
    }
예제 #7
0
def test_story_full_play_noproxy_dump():
    with Story(test_mod) as story:
        test_mod.target(123) == 'foobar'
        test_mod.target(1234) ** ValueError

    with story.replay(recurse_lock=True, proxy=False, strict=False, dump=True) as replay:
        raises(AssertionError, test_mod.target)
        assert test_mod.target(123) == 'foobar'
        raises(ValueError, test_mod.target, 1234)

    assert replay.unexpected == ""
예제 #8
0
def xtest_story_empty_play_proxy_class_dependencies():
    with Story(test_mod).replay(recurse_lock=True, proxy=True, strict=False) as replay:
        obj = test_mod.Stuff(1, 2)
        other = obj.other('x')
        raises(ValueError, other.raises, 'badarg')
        other.mix(3, 4)
        obj = test_mod.Stuff(0, 1)
        obj.mix(3, 4)
        other = obj.other(2)
        other.mix(3, 4)

    print(repr(replay.diff))

    assert replay.diff == ""
예제 #9
0
def test_story_text_helpers():
    with Story(test_mod) as story:
        obj = test_mod.Stuff(1, 2)
        obj.meth('a') == 'x'
        obj.meth('b') == 'y'
        obj = test_mod.Stuff(2, 3)
        obj.meth('c') == 'z'
        test_mod.target(1) == 2
        test_mod.target(2) == 3

    with story.replay(recurse_lock=True, strict=False) as replay:
        obj = test_mod.Stuff(1, 2)
        obj.meth('a')
        obj.meth()
        obj = test_mod.Stuff(4, 4)
        obj.meth()
        test_mod.func(5)
        test_mod.target(1)

    print (replay.missing)
    assert replay.missing == """stuff_1.meth('b') == 'y'  # returns
stuff_2 = test_pkg1.test_pkg2.test_mod.Stuff(2, 3)
stuff_2.meth('c') == 'z'  # returns
test_pkg1.test_pkg2.test_mod.target(2) == 3  # returns
"""
    print (replay.unexpected)
    assert replay.unexpected == """stuff_1.meth() == None  # returns
stuff_2 = test_pkg1.test_pkg2.test_mod.Stuff(4, 4)
stuff_2.meth() == None  # returns
test_pkg1.test_pkg2.test_mod.func(5) == None  # returns
"""
    print (replay.diff)
    if PY26:
        assert replay.diff == """--- expected """ """
+++ actual """ """
@@ -1,7 +1,7 @@
 stuff_1 = test_pkg1.test_pkg2.test_mod.Stuff(1, 2)
 stuff_1.meth('a') == 'x'  # returns
-stuff_1.meth('b') == 'y'  # returns
-stuff_2 = test_pkg1.test_pkg2.test_mod.Stuff(2, 3)
-stuff_2.meth('c') == 'z'  # returns
+stuff_1.meth() == None  # returns
+stuff_2 = test_pkg1.test_pkg2.test_mod.Stuff(4, 4)
+stuff_2.meth() == None  # returns
+test_pkg1.test_pkg2.test_mod.func(5) == None  # returns
 test_pkg1.test_pkg2.test_mod.target(1) == 2  # returns
-test_pkg1.test_pkg2.test_mod.target(2) == 3  # returns
"""
    else:
        assert replay.diff == """--- expected
예제 #10
0
def test_mysql():
    with Story(
        ['MySQLdb.cursors.BaseCursor',
         'MySQLdb.connections.Connection']) as story:
        pass
    rows = []
    with story.replay(strict=False) as replay:
        import MySQLdb
        con = MySQLdb.connect('localhost', 'root', '')
        con.select_db('mysql')
        cursor = con.cursor()
        cursor.execute('show tables')
        rows.extend(cursor.fetchall())
    assert '== (%s)' % ', '.join(repr(row)
                                 for row in rows) in replay.unexpected
예제 #11
0
def test_story_empty_play_proxy():
    assert test_mod.target() is None
    raises(TypeError, test_mod.target, 123)

    with Story(test_mod).replay(recurse_lock=True, proxy=True, strict=False) as replay:
        assert test_mod.target() is None
        raises(TypeError, test_mod.target, 123)

    assert format_calls(replay._actual) == format_calls(OrderedDict([
        ((None, 'test_pkg1.test_pkg2.test_mod.target', '', ''), _Returns("None")),
        ((None, 'test_pkg1.test_pkg2.test_mod.target', '123', ''), _Raises(repr_ex(TypeError(
            'target() takes no arguments (1 given)' if PY2 else
            'target() takes 0 positional arguments but 1 was given',
        ))))
    ]))
예제 #12
0
def test_story_empty_play_proxy_class_missing_report():
    with Story(test_mod).replay(recurse_lock=True, proxy=True,
                                strict=False) as replay:
        obj = test_mod.Stuff(1, 2)
        obj.mix(3, 4)
        obj.mix('a', 'b')
        raises(ValueError, obj.raises, 123)
        obj = test_mod.Stuff(0, 1)
        obj.mix('a', 'b')
        obj.mix(3, 4)
        test_mod.target()
        raises(ValueError, test_mod.raises, 'badarg')
        raises(ValueError, obj.raises, 123)
        test_mod.ThatLONGStuf(1).mix(2)
        test_mod.ThatLONGStuf(3).mix(4)
        obj = test_mod.ThatLONGStuf(2)
        obj.mix()
        obj.meth()
        obj.mix(10)

    print(repr(replay.diff))

    if PY26:
        assert replay.diff == """--- expected """ """
+++ actual """ """
@@ -1,0 +1,18 @@
+stuff_1 = test_pkg1.test_pkg2.test_mod.Stuff(1, 2)
+stuff_1.mix(3, 4) == (1, 2, 3, 4)  # returns
+stuff_1.mix('a', 'b') == (1, 2, 'a', 'b')  # returns
+stuff_1.raises(123) ** ValueError((123,),)  # raises
+stuff_2 = test_pkg1.test_pkg2.test_mod.Stuff(0, 1)
+stuff_2.mix('a', 'b') == (0, 1, 'a', 'b')  # returns
+stuff_2.mix(3, 4) == (0, 1, 3, 4)  # returns
+test_pkg1.test_pkg2.test_mod.target() == None  # returns
+test_pkg1.test_pkg2.test_mod.raises('badarg') ** ValueError(('badarg',),)  # raises
+stuff_2.raises(123) ** ValueError((123,),)  # raises
+that_long_stuf_1 = test_pkg1.test_pkg2.test_mod.ThatLONGStuf(1)
+that_long_stuf_1.mix(2) == (1, 2)  # returns
+that_long_stuf_2 = test_pkg1.test_pkg2.test_mod.ThatLONGStuf(3)
+that_long_stuf_2.mix(4) == (3, 4)  # returns
+that_long_stuf_3 = test_pkg1.test_pkg2.test_mod.ThatLONGStuf(2)
+that_long_stuf_3.mix() == (2,)  # returns
+that_long_stuf_3.meth() == None  # returns
+that_long_stuf_3.mix(10) == (2, 10)  # returns
"""
    else:
        assert replay.diff == """--- expected
예제 #13
0
def test_story_full_play_proxy():
    with Story(test_mod) as story:
        test_mod.target(123) == 'foobar'
        test_mod.target(1234) ** ValueError

    with story.replay(recurse_lock=True, proxy=True, strict=False) as replay:
        assert test_mod.target() is None
        assert test_mod.target(123) == 'foobar'
        raises(ValueError, test_mod.target, 1234)
        raises(TypeError, test_mod.target, 'asdf')

    assert replay.unexpected == format_calls(OrderedDict([
        ((None, 'test_pkg1.test_pkg2.test_mod.target', '', ''), _Returns("None")),
        ((None, 'test_pkg1.test_pkg2.test_mod.target', "'asdf'", ''), _Raises(repr_ex(TypeError(
            'target() takes no arguments (1 given)'
            if PY2
            else 'target() takes 0 positional arguments but 1 was given',)
        )))
    ]))
예제 #14
0
def test_story_empty_play_noproxy_class():
    with Story(test_mod).replay(recurse_lock=True, proxy=False, strict=False) as replay:
        raises(AssertionError, test_mod.Stuff, 1, 2)

    assert replay._actual == {}