Example #1
0
def test_sequential_story_steps_set_story_arguments(r, m):
    """There are a few sequential substories with one common parent story.

    One substory should be able to set variable to provide an argument
    to the next sequential story.
    """
    class T(m.ChildWithShrink, m.StringMethod):
        pass

    class E(m.NextParamChildWithString, m.NormalNextMethod):
        pass

    class J(m.SequentialParent, m.NormalParentMethod):
        def __init__(self):
            self.x = T().x
            self.y = E().y

    # Substory DI.

    getter = make_collector()
    r(J().a)()
    assert getter().foo == "1"
    assert getter().bar == ["2"]

    getter = make_collector()
    r(J().a.run)()
    assert getter().foo == "1"
    assert getter().bar == ["2"]
Example #2
0
def test_context_proper_getattr_behavior(r, x):
    expected = """
Branch.show_content
  age_lt_18
  age_gte_18
  load_content (returned: 'allowed')

Context:
  age: 18               # Story argument
  access_allowed: True  # Set by Branch.age_gte_18
    """.strip()
    getter = make_collector()
    r(x.Branch().show_content)(age=18)
    result = repr(getter())
    assert result == expected

    expected = """
Branch.show_content
  age_lt_18
  age_gte_18
  load_content (returned: 'denied')

Context:
  age: 1                 # Story argument
  access_allowed: False  # Set by Branch.age_lt_18
    """.strip()
    getter = make_collector()
    r(x.Branch().show_content)(age=1)
    result = repr(getter())
    assert result == expected
Example #3
0
def test_story_variable_alias_normalization_store_same_object(m):
    """When story step sets a set of variables some of them are aliases of each
    other.

    If the type and the value of alias are equal to the origin value, we
    should preserve the same reference to the value.
    """
    class T(m.ChildAlias, m.AliasMethod):
        pass

    # Simple.

    getter = make_collector()
    T().x()
    assert getter().foo is getter().bar
    assert getter().foo == {"key": "1"}
    assert getter().bar == {"key": "1"}
    assert getter().foo is not getter().baz
    assert getter().bar is not getter().baz
    assert getter().baz == {"key": 1}

    getter = make_collector()
    T().x.run()
    assert getter().foo is getter().bar
    assert getter().foo == {"key": "1"}
    assert getter().bar == {"key": "1"}
    assert getter().foo is not getter().baz
    assert getter().bar is not getter().baz
    assert getter().baz == {"key": 1}
Example #4
0
def test_story_argument_alias_normalization_store_same_object(m):
    """When story has a set of arguments some of them are aliases of each
    other.

    If the type and the value of alias are equal to the origin value, we
    should preserve the same reference to the value.
    """
    class T(m.ParamChildAlias, m.NormalMethod):
        pass

    # Simple.

    value = {"key": "1"}

    getter = make_collector()
    T().x(foo=value, bar=value, baz=value)
    assert getter().foo is getter().bar
    assert getter().foo == {"key": "1"}
    assert getter().bar == {"key": "1"}
    assert getter().foo is not getter().baz
    assert getter().bar is not getter().baz
    assert getter().baz == {"key": 1}

    getter = make_collector()
    T().x.run(foo=value, bar=value, baz=value)
    assert getter().foo is getter().bar
    assert getter().foo == {"key": "1"}
    assert getter().bar == {"key": "1"}
    assert getter().foo is not getter().baz
    assert getter().bar is not getter().baz
    assert getter().baz == {"key": 1}
Example #5
0
def test_context_representation_multiline_variable(r, c):
    class userlist(list):
        def __repr__(self):
            return "\n ".join(super(userlist, self).__repr__().split())

    class T(c.ParamChild, c.NormalMethod):
        foo = userlist(range(3))

    class J(c.ParamParent, c.NormalParentMethod):
        def __init__(self):
            self.x = T().x

    # Simple.

    expected = """
T.x
  one

Context:
  bar: 'baz'  # Story argument
  foo:        # Set by T.one
    [0,
     1,
     2]
    """.strip()

    getter = make_collector()
    r(T().x)(bar="baz")
    assert repr(getter()) == expected

    getter = make_collector()
    r(T().x.run)(bar="baz")
    assert repr(getter()) == expected

    # Substory DI.

    expected = """
J.a
  before
  x (T.x)
    one
  after

Context:
  bar: 'baz'  # Story argument
  foo:        # Set by T.one
    [0,
     1,
     2]
    """.strip()

    getter = make_collector()
    r(J().a)(bar="baz")
    assert repr(getter()) == expected

    getter = make_collector()
    r(J().a.run)(bar="baz")
    assert repr(getter()) == expected
Example #6
0
def test_context_representation_with_context_contract_error(r, m):
    class T(m.ParamChildWithNull, m.StringMethod):
        pass

    class J(m.ParamParentWithNull, m.StringParentMethod):
        def __init__(self):
            self.x = T().x

    # Simple.

    expected = """
T.x
  one (errored: ContextContractError)

Context:
  bar: 2  # Story argument
  foo: 1  # Story argument
    """.strip()

    getter = make_collector()
    with pytest.raises(ContextContractError):
        r(T().x)(foo=1, bar=2)
    assert repr(getter()) == expected

    getter = make_collector()
    with pytest.raises(ContextContractError):
        r(T().x.run)(foo=1, bar=2)
    assert repr(getter()) == expected

    # Substory DI.

    expected = """
J.a
  before
  x (T.x)
    one (errored: ContextContractError)

Context:
  eggs: 2     # Story argument
  ham: 1      # Story argument
  foo: '1'    # Set by J.before
  bar: ['2']  # Set by J.before
    """.strip()

    getter = make_collector()
    with pytest.raises(ContextContractError):
        r(J().a)(ham=1, eggs=2)
    assert repr(getter()) == expected

    getter = make_collector()
    with pytest.raises(ContextContractError):
        r(J().a.run)(ham=1, eggs=2)
    assert repr(getter()) == expected
Example #7
0
def test_context_representation_variable_aliases_ignore(r, c, arg):
    class T(c.ParamChild, c.NormalMethod):
        foo = arg

    class J(c.ParamParent, c.NormalParentMethod):
        def __init__(self):
            self.x = T().x

    # Simple.

    expected = """
T.x
  one

Context:
  bar: %(arg)s  # Story argument
  foo: %(arg)s  # Set by T.one
    """.strip() % {
        "arg": repr(arg)
    }

    getter = make_collector()
    r(T().x)(bar=T.foo)
    assert repr(getter()) == expected

    getter = make_collector()
    r(T().x.run)(bar=T.foo)
    assert repr(getter()) == expected

    # Substory DI.

    expected = """
J.a
  before
  x (T.x)
    one
  after

Context:
  bar: %(arg)s  # Story argument
  foo: %(arg)s  # Set by T.one
    """.strip() % {
        "arg": repr(arg)
    }

    getter = make_collector()
    r(J().a)(bar=T.foo)
    assert repr(getter()) == expected

    getter = make_collector()
    r(J().a.run)(bar=T.foo)
    assert repr(getter()) == expected
Example #8
0
def test_context_representation_long_variable(r, c):
    class T(c.ParamChild, c.NormalMethod):
        foo = list(range(23))

    class J(c.ParamParent, c.NormalParentMethod):
        def __init__(self):
            self.x = T().x

    # Simple.

    expected = """
T.x
  one

Context:
  bar: 'baz'  # Story argument
  foo:        # Set by T.one
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
    """.strip()

    getter = make_collector()
    r(T().x)(bar="baz")
    assert repr(getter()) == expected

    getter = make_collector()
    r(T().x.run)(bar="baz")
    assert repr(getter()) == expected

    # Substory DI.

    expected = """
J.a
  before
  x (T.x)
    one
  after

Context:
  bar: 'baz'  # Story argument
  foo:        # Set by T.one
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
    """.strip()

    getter = make_collector()
    r(J().a)(bar="baz")
    assert repr(getter()) == expected

    getter = make_collector()
    r(J().a.run)(bar="baz")
    assert repr(getter()) == expected
Example #9
0
def test_context_representation_with_failure_reason_list(r, f):
    class T(f.ChildWithList, f.StringMethod):
        pass

    class Q(f.ParentWithList, f.NormalParentMethod, T):
        pass

    class J(f.ParentWithList, f.NormalParentMethod):
        def __init__(self):
            self.x = T().x

    # Simple.

    expected = """
T.x
  one (failed: 'foo')

Context()
    """.strip()

    getter = make_collector()
    with pytest.raises(FailureError):
        r(T().x)()
    assert repr(getter()) == expected

    getter = make_collector()
    r(T().x.run)()
    assert repr(getter()) == expected

    # Substory DI.

    expected = """
J.a
  before
  x (T.x)
    one (failed: 'foo')

Context()
    """.strip()

    getter = make_collector()
    with pytest.raises(FailureError):
        r(J().a)()
    assert repr(getter()) == expected

    getter = make_collector()
    r(J().a.run)()
    assert repr(getter()) == expected
Example #10
0
def test_context_representation_with_missing_variables(r, m):
    class T(m.ParamChildWithNull, m.NormalMethod):
        pass

    class J(m.ParentWithNull, m.NormalParentMethod):
        def __init__(self):
            self.x = T().x

    # Simple.

    expected = """
T.x (errored: ContextContractError)

Context()
    """.strip()

    getter = make_collector()
    with pytest.raises(ContextContractError):
        r(T().x)()
    assert repr(getter()) == expected

    getter = make_collector()
    with pytest.raises(ContextContractError):
        r(T().x.run)()
    assert repr(getter()) == expected

    # Substory DI.

    expected = """
J.a
  before
  x (T.x) (errored: ContextContractError)

Context()
    """.strip()

    getter = make_collector()
    with pytest.raises(ContextContractError):
        r(J().a)()
    assert repr(getter()) == expected

    getter = make_collector()
    with pytest.raises(ContextContractError):
        r(J().a.run)()
    assert repr(getter()) == expected
Example #11
0
def test_story_arguments_normalization_many_levels(r, m):
    """We apply normalization to the story arguments on any levels of story
    composition."""
    class T(m.ParamChild, m.NormalMethod):
        pass

    class J(m.ParamParent, m.NormalParentMethod):
        def __init__(self):
            self.x = T().x

    class F(m.ParamRoot, m.NormalRootMethod):
        def __init__(self):
            self.a = J().a

    # Substory DI.

    getter = make_collector()
    r(J().a)(ham="1", eggs="2", foo="3", bar=["4"])
    assert getter().ham == 1
    assert getter().eggs == 2
    assert getter().foo == 3
    assert getter().bar == [4]

    getter = make_collector()
    r(J().a.run)(ham="1", eggs="2", foo="3", bar=["4"])
    assert getter().ham == 1
    assert getter().eggs == 2
    assert getter().foo == 3
    assert getter().bar == [4]

    getter = make_collector()
    r(F().i)(fizz="0", ham="1", eggs="2", foo="3", bar=["4"])
    assert getter().fizz == 0
    assert getter().ham == 1
    assert getter().eggs == 2
    assert getter().foo == 3
    assert getter().bar == [4]

    getter = make_collector()
    r(F().i.run)(fizz="0", ham="1", eggs="2", foo="3", bar=["4"])
    assert getter().fizz == 0
    assert getter().ham == 1
    assert getter().eggs == 2
    assert getter().foo == 3
    assert getter().bar == [4]
Example #12
0
def test_context_representation_with_error():

    expected = """
StepError.x
  one (errored: Exception)

Context()
    """.strip()

    getter = make_collector()
    with pytest.raises(Exception):
        examples.methods.StepError().x()
    assert repr(getter()) == expected

    getter = make_collector()
    with pytest.raises(Exception):
        examples.methods.StepError().x.run()
    assert repr(getter()) == expected
Example #13
0
def test_context_representation_with_error(r, x):

    expected = """
StepError.x
  one (errored: ExpectedException)

Context()
    """.strip()

    getter = make_collector()
    with pytest.raises(x.ExpectedException):
        r(x.StepError().x)()
    assert repr(getter()) == expected

    getter = make_collector()
    with pytest.raises(x.ExpectedException):
        r(x.StepError().x.run)()
    assert repr(getter()) == expected
Example #14
0
def test_context_representation_with_failure(r, x):

    expected = """
Simple.x
  one
  two (failed)

Context:
  bar: 2  # Story argument
  foo: 3  # Story argument
    """.strip()

    getter = make_collector()
    with pytest.raises(FailureError):
        r(x.Simple().x)(foo=3, bar=2)
    assert repr(getter()) == expected

    getter = make_collector()
    r(x.Simple().x.run)(foo=3, bar=2)
    assert repr(getter()) == expected

    expected = """
SubstoryDI.y
  start
  before
  x (Simple.x)
    one
    two (failed)

Context:
  spam: 3  # Story argument
  foo: 2   # Set by SubstoryDI.start
  bar: 4   # Set by SubstoryDI.before
    """.strip()

    getter = make_collector()
    with pytest.raises(FailureError):
        r(x.SubstoryDI(x.Simple().x).y)(spam=3)
    assert repr(getter()) == expected

    getter = make_collector()
    r(x.SubstoryDI(x.Simple().x).y.run)(spam=3)
    assert repr(getter()) == expected
Example #15
0
def test_context_representation_with_skip(r, x):

    expected = """
Simple.x
  one
  two (skipped)

Context:
  bar: -1  # Story argument
  foo: 1   # Story argument
    """.strip()

    getter = make_collector()
    r(x.Simple().x)(foo=1, bar=-1)
    assert repr(getter()) == expected

    getter = make_collector()
    r(x.Simple().x.run)(foo=1, bar=-1)
    assert repr(getter()) == expected

    expected = """
SubstoryDI.y
  start
  before
  x (Simple.x)
    one
    two (skipped)
  after (returned: -4)

Context:
  spam: -2  # Story argument
  foo: -3   # Set by SubstoryDI.start
  bar: -1   # Set by SubstoryDI.before
    """.strip()

    getter = make_collector()
    r(x.SubstoryDI(x.Simple().x).y)(spam=-2)
    assert repr(getter()) == expected

    getter = make_collector()
    r(x.SubstoryDI(x.Simple().x).y.run)(spam=-2)
    assert repr(getter()) == expected
Example #16
0
def test_parent_steps_set_story_arguments(r, m):
    """Steps of parent stories should be able to set child stories arguments
    with `Success` marker keyword arguments."""
    class T(m.ParamChild, m.NormalMethod):
        pass

    class J(m.Parent, m.StringParentMethod):
        def __init__(self):
            self.x = T().x

    class R(m.Root, m.StringRootMethod, m.Parent, m.NormalParentMethod, T):
        pass

    class F(m.Root, m.StringRootMethod):
        def __init__(self):
            class J(m.Parent, m.NormalParentMethod):
                def __init__(self):
                    self.x = T().x

            self.a = J().a

    # Substory DI.

    getter = make_collector()
    r(J().a)()
    assert getter().foo == 1
    assert getter().bar == [2]

    getter = make_collector()
    r(J().a.run)()
    assert getter().foo == 1
    assert getter().bar == [2]

    getter = make_collector()
    r(F().i)()
    assert getter().foo == 1
    assert getter().bar == [2]

    getter = make_collector()
    r(F().i.run)()
    assert getter().foo == 1
    assert getter().bar == [2]
Example #17
0
def test_context_representation_with_failure_protocol_error(r, f):

    expected = """
T.x
  one (errored: FailureProtocolError)

Context()
    """.strip()

    class T(f.ChildWithList, f.WrongMethod):
        pass

    getter = make_collector()
    with pytest.raises(FailureProtocolError):
        r(T().x)()
    assert repr(getter()) == expected

    getter = make_collector()
    with pytest.raises(FailureProtocolError):
        r(T().x.run)()
    assert repr(getter()) == expected
Example #18
0
def test_context_variables_normalization(r, m):
    """We apply normalization to the context variables, if story defines context
    contract.

    If story step returns a string holding a number, we should store a number in the
    context.

    """
    class T(m.Child, m.StringMethod):
        pass

    class J(m.Parent, m.NormalParentMethod):
        def __init__(self):
            self.x = T().x

    # Simple.

    getter = make_collector()
    r(T().x)()
    assert getter().foo == 1
    assert getter().bar == [2]

    getter = make_collector()
    r(T().x.run)()
    assert getter().foo == 1
    assert getter().bar == [2]

    # Substory DI.

    getter = make_collector()
    r(J().a)()
    assert getter().foo == 1
    assert getter().bar == [2]

    getter = make_collector()
    r(J().a.run)()
    assert getter().foo == 1
    assert getter().bar == [2]
Example #19
0
def test_context_representation_arguments_order(r, x):

    # Simple.

    expected = """
Simple.x
  one
  two
  three (returned: -1)

Context:
  bar: 3  # Story argument
  foo: 1  # Story argument
  baz: 4  # Set by Simple.two
    """.strip()

    getter = make_collector()
    r(x.Simple().x)(bar=3, foo=1)
    assert repr(getter()) == expected

    getter = make_collector()
    r(x.Simple().x.run)(bar=3, foo=1)
    assert repr(getter()) == expected
Example #20
0
def test_story_arguments_normalization(r, m):
    """We apply normalization to the story arguments, if story defines context
    contract.

    If story was called with a string argument holding a number, we
    should store a number in the context.
    """
    class T(m.ParamChild, m.NormalMethod):
        pass

    class J(m.ParamParent, m.StringParentMethod):
        def __init__(self):
            self.x = T().x

    # Simple.

    getter = make_collector()
    r(T().x)(foo="1", bar=["2"])
    assert getter().foo == 1
    assert getter().bar == [2]

    getter = make_collector()
    r(T().x.run)(foo="1", bar=["2"])
    assert getter().foo == 1
    assert getter().bar == [2]

    # Substory DI.

    getter = make_collector()
    r(J().a)(ham="1", eggs="2")
    assert getter().ham == 1
    assert getter().eggs == 2

    getter = make_collector()
    r(J().a.run)(ham="1", eggs="2")
    assert getter().ham == 1
    assert getter().eggs == 2
Example #21
0
def test_context_representation_with_empty():

    expected = """
Empty.x

Context()
    """.strip()

    getter = make_collector()
    examples.methods.Empty().x()
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.Empty().x.run()
    assert repr(getter()) == expected

    expected = """
EmptySubstory.y
  x

Context()
    """.strip()

    getter = make_collector()
    examples.methods.EmptySubstory().y()
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.EmptySubstory().y.run()
    assert repr(getter()) == expected

    expected = """
SubstoryDI.y
  start
  before
  x (Empty.x)
  after (returned: 6)

Context:
  spam: 3  # Story argument
  foo: 2   # Set by SubstoryDI.start
  bar: 4   # Set by SubstoryDI.before
    """.strip()

    getter = make_collector()
    examples.methods.SubstoryDI(examples.methods.Empty().x).y(spam=3)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.SubstoryDI(examples.methods.Empty().x).y.run(spam=3)
    assert repr(getter()) == expected
Example #22
0
def test_context_representation():

    # TODO: Empty.

    expected = """
Empty.x()

Context()
    """.strip()

    # Collector, getter = make_collector(examples.Empty, "two")
    # Collector().x()
    # assert repr(getter()) == expected
    #
    # Collector, getter = make_collector(examples.Empty, "two")
    # Collector().x.run()
    # assert repr(getter()) == expected

    expected = """
EmptySubstory.y:
  x

Context()
        """.strip()

    # Collector, getter = make_collector(examples.EmptySubstory, "x")
    # Collector().y()
    # assert repr(getter()) == expected
    #
    # Collector, getter = make_collector(examples.EmptySubstory, "x")
    # Collector().y.run()
    # assert repr(getter()) == expected

    expected = """
SubstoryDI.y:
  x (Empty.x)

Context()
        """.strip()

    # Collector, getter = make_collector(examples.SubstoryDI, "x")
    # Collector(examples.Empty().x).y(3)
    # assert repr(getter()) == expected
    #
    # Collector, getter = make_collector(examples.SubstoryDI, "x")
    # Collector(examples.Empty().x).y.run(3)
    # assert repr(getter()) == expected

    # Failure.

    expected = """
Simple.x:
  one
  two (failed)

Context:
    foo = 2  # Story argument
    bar = 2  # Story argument
        """.strip()

    Collector, getter = make_collector(examples.Simple, "two")
    with pytest.raises(FailureError):
        Collector().x(2, 2)
    assert repr(getter()) == expected

    Collector, getter = make_collector(examples.Simple, "two")
    Collector().x.run(2, 2)
    assert repr(getter()) == expected

    expected = """
SimpleSubstory.y:
  start
  before
  x
    one
    two (failed)

Context:
    spam = 3  # Story argument
    foo = 2   # Set by SimpleSubstory.start
    bar = 4   # Set by SimpleSubstory.before
        """.strip()

    Collector, getter = make_collector(examples.SimpleSubstory, "two")
    with pytest.raises(FailureError):
        Collector().y(3)
    assert repr(getter()) == expected

    Collector, getter = make_collector(examples.SimpleSubstory, "two")
    Collector().y.run(3)
    assert repr(getter()) == expected

    expected = """
SubstoryDI.y:
  start
  before
  x (Simple.x)
    one
    two (failed)

Context:
    spam = 3  # Story argument
    foo = 2   # Set by SubstoryDI.start
    bar = 4   # Set by SubstoryDI.before
        """.strip()

    Collector, getter = make_collector(examples.Simple, "two")
    with pytest.raises(FailureError):
        examples.SubstoryDI(Collector().x).y(3)
    assert repr(getter()) == expected

    Collector, getter = make_collector(examples.Simple, "two")
    examples.SubstoryDI(Collector().x).y.run(3)
    assert repr(getter()) == expected

    # Failure with reason.

    expected = """
Simple.x:
  one
  two (failed: "'foo' is too big")

Context:
    foo = 3  # Story argument
    bar = 2  # Story argument
        """.strip()

    Collector, getter = make_collector(examples.Simple, "two")
    with pytest.raises(FailureError):
        Collector().x(3, 2)
    assert repr(getter()) == expected

    Collector, getter = make_collector(examples.Simple, "two")
    Collector().x.run(3, 2)
    assert repr(getter()) == expected

    expected = """
SimpleSubstory.y:
  start
  before
  x
    one
    two (failed: "'foo' is too big")

Context:
    spam = 4  # Story argument
    foo = 3   # Set by SimpleSubstory.start
    bar = 5   # Set by SimpleSubstory.before
        """.strip()

    Collector, getter = make_collector(examples.SimpleSubstory, "two")
    with pytest.raises(FailureError):
        Collector().y(4)
    assert repr(getter()) == expected

    Collector, getter = make_collector(examples.SimpleSubstory, "two")
    Collector().y.run(4)
    assert repr(getter()) == expected

    expected = """
SubstoryDI.y:
  start
  before
  x (Simple.x)
    one
    two (failed: "'foo' is too big")

Context:
    spam = 4  # Story argument
    foo = 3   # Set by SubstoryDI.start
    bar = 5   # Set by SubstoryDI.before
        """.strip()

    Collector, getter = make_collector(examples.Simple, "two")
    with pytest.raises(FailureError):
        examples.SubstoryDI(Collector().x).y(4)
    assert repr(getter()) == expected

    Collector, getter = make_collector(examples.Simple, "two")
    examples.SubstoryDI(Collector().x).y.run(4)
    assert repr(getter()) == expected

    # Result.

    expected = """
Simple.x:
  one
  two
  three (returned: -1)

Context:
    foo = 1  # Story argument
    bar = 3  # Story argument
    baz = 4  # Set by Simple.two
        """.strip()

    Collector, getter = make_collector(examples.Simple, "three")
    Collector().x(1, 3)
    assert repr(getter()) == expected

    Collector, getter = make_collector(examples.Simple, "three")
    Collector().x.run(1, 3)
    assert repr(getter()) == expected

    expected = """
SimpleSubstory.y:
  start
  before
  x
    one
    two
    three (returned: -1)

Context:
    spam = 2  # Story argument
    foo = 1   # Set by SimpleSubstory.start
    bar = 3   # Set by SimpleSubstory.before
    baz = 4   # Set by SimpleSubstory.two
        """.strip()

    Collector, getter = make_collector(examples.SimpleSubstory, "three")
    Collector().y(2)
    assert repr(getter()) == expected

    Collector, getter = make_collector(examples.SimpleSubstory, "three")
    Collector().y.run(2)
    assert repr(getter()) == expected

    expected = """
SubstoryDI.y:
  start
  before
  x (Simple.x)
    one
    two
    three (returned: -1)

Context:
    spam = 2  # Story argument
    foo = 1   # Set by SubstoryDI.start
    bar = 3   # Set by SubstoryDI.before
    baz = 4   # Set by Simple.two
    """.strip()

    Collector, getter = make_collector(examples.Simple, "three")
    examples.SubstoryDI(Collector().x).y(2)
    assert repr(getter()) == expected

    Collector, getter = make_collector(examples.Simple, "three")
    examples.SubstoryDI(Collector().x).y.run(2)
    assert repr(getter()) == expected

    expected = """
SubstoryDI.y:
  start
  before
  x (Pipe.x)
    one
    two
    three
  after (returned: 6)

Context:
    spam = 3  # Story argument
    foo = 2   # Set by SubstoryDI.start
    bar = 4   # Set by SubstoryDI.before
        """.strip()

    Collector, getter = make_collector(examples.SubstoryDI, "after")
    Collector(examples.Pipe().x).y(3)
    assert repr(getter()) == expected

    Collector, getter = make_collector(examples.SubstoryDI, "after")
    Collector(examples.Pipe().x).y.run(3)
    assert repr(getter()) == expected

    # Skip.

    expected = """
Simple.x:
  one
  two (skipped)

Context:
    foo = 1   # Story argument
    bar = -1  # Story argument
        """.strip()

    Collector, getter = make_collector(examples.Simple, "two")
    Collector().x(1, -1)
    assert repr(getter()) == expected

    Collector, getter = make_collector(examples.Simple, "two")
    Collector().x.run(1, -1)
    assert repr(getter()) == expected

    expected = """
SimpleSubstory.y:
  start
  before
  x
    one
    two (skipped)
  after (returned: -4)

Context:
    spam = -2  # Story argument
    foo = -3   # Set by SimpleSubstory.start
    bar = -1   # Set by SimpleSubstory.before
        """.strip()

    Collector, getter = make_collector(examples.SimpleSubstory, "after")
    Collector().y(-2)
    assert repr(getter()) == expected

    Collector, getter = make_collector(examples.SimpleSubstory, "after")
    Collector().y.run(-2)
    assert repr(getter()) == expected

    expected = """
SubstoryDI.y:
  start
  before
  x (Simple.x)
    one
    two (skipped)
  after (returned: -4)

Context:
    spam = -2  # Story argument
    foo = -3   # Set by SubstoryDI.start
    bar = -1   # Set by SubstoryDI.before
        """.strip()

    Collector, getter = make_collector(examples.SubstoryDI, "after")
    Collector(examples.Simple().x).y(-2)
    assert repr(getter()) == expected

    Collector, getter = make_collector(examples.SubstoryDI, "after")
    Collector(examples.Simple().x).y.run(-2)
    assert repr(getter()) == expected

    expected = """
SubstoryDI.y:
  start
  before
  x (SimpleSubstory.z)
    first (skipped)
  after (returned: 4)

Context:
    spam = 2  # Story argument
    foo = 1   # Set by SubstoryDI.start
    bar = 3   # Set by SubstoryDI.before
        """.strip()

    Collector, getter = make_collector(examples.SubstoryDI, "after")
    Collector(examples.SimpleSubstory().z).y(2)
    assert repr(getter()) == expected

    Collector, getter = make_collector(examples.SubstoryDI, "after")
    Collector(examples.SimpleSubstory().z).y.run(2)
    assert repr(getter()) == expected

    # Error.

    expected = """
StepError.x:
  one (errored: Exception)

Context()
    """.strip()

    Collector, getter = make_collector(examples.StepError, "one")
    with pytest.raises(Exception):
        Collector().x()
    assert repr(getter()) == expected

    Collector, getter = make_collector(examples.StepError, "one")
    with pytest.raises(Exception):
        Collector().x.run()
    assert repr(getter()) == expected
Example #23
0
def test_context_representation_variable_aliases(c):
    class T(c.ParamChild, c.NormalMethod):
        foo = "baz"

    class Q(c.ParamParent, c.NormalParentMethod, T):
        pass

    class J(c.ParamParent, c.NormalParentMethod):
        def __init__(self):
            self.x = T().x

    # Simple.

    expected = """
T.x
  one

Context:
  bar: 'baz'        # Story argument
  foo: `bar` alias  # Set by T.one
    """.strip()

    getter = make_collector()
    T().x(bar=T.foo)
    assert repr(getter()) == expected

    getter = make_collector()
    T().x.run(bar=T.foo)
    assert repr(getter()) == expected

    # Substory inheritance.

    expected = """
Q.a
  before
  x
    one
  after

Context:
  bar: 'baz'        # Story argument
  foo: `bar` alias  # Set by Q.one
    """.strip()

    getter = make_collector()
    Q().a(bar=T.foo)
    assert repr(getter()) == expected

    getter = make_collector()
    Q().a.run(bar=T.foo)
    assert repr(getter()) == expected

    # Substory DI.

    expected = """
J.a
  before
  x (T.x)
    one
  after

Context:
  bar: 'baz'        # Story argument
  foo: `bar` alias  # Set by T.one
    """.strip()

    getter = make_collector()
    J().a(bar=T.foo)
    assert repr(getter()) == expected

    getter = make_collector()
    J().a.run(bar=T.foo)
    assert repr(getter()) == expected
Example #24
0
def test_context_representation_with_failure_reason_enum(f):
    class T(f.ChildWithEnum, f.EnumMethod):
        pass

    class Q(f.ParentWithEnum, f.NormalParentMethod, T):
        pass

    class J(f.ParentWithEnum, f.NormalParentMethod):
        def __init__(self):
            self.x = T().x

    # Simple.

    expected = """
T.x
  one (failed: <Errors.foo: 1>)

Context()
    """.strip()

    getter = make_collector()
    with pytest.raises(FailureError):
        T().x()
    assert repr(getter()) == expected

    getter = make_collector()
    T().x.run()
    assert repr(getter()) == expected

    # Substory inheritance.

    expected = """
Q.a
  before
  x
    one (failed: <Errors.foo: 1>)

Context()
    """.strip()

    getter = make_collector()
    with pytest.raises(FailureError):
        Q().a()
    assert repr(getter()) == expected

    getter = make_collector()
    Q().a.run()
    assert repr(getter()) == expected

    # Substory DI.

    expected = """
J.a
  before
  x (T.x)
    one (failed: <Errors.foo: 1>)

Context()
    """.strip()

    getter = make_collector()
    with pytest.raises(FailureError):
        J().a()
    assert repr(getter()) == expected

    getter = make_collector()
    J().a.run()
    assert repr(getter()) == expected
Example #25
0
def test_context_representation_with_skip():

    expected = """
Simple.x
  one
  two (skipped)

Context:
  bar: -1  # Story argument
  foo: 1   # Story argument
    """.strip()

    getter = make_collector()
    examples.methods.Simple().x(foo=1, bar=-1)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.Simple().x.run(foo=1, bar=-1)
    assert repr(getter()) == expected

    expected = """
SimpleSubstory.y
  start
  before
  x
    one
    two (skipped)
  after (returned: -4)

Context:
  spam: -2  # Story argument
  foo: -3   # Set by SimpleSubstory.start
  bar: -1   # Set by SimpleSubstory.before
    """.strip()

    getter = make_collector()
    examples.methods.SimpleSubstory().y(spam=-2)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.SimpleSubstory().y.run(spam=-2)
    assert repr(getter()) == expected

    expected = """
SubstoryDI.y
  start
  before
  x (Simple.x)
    one
    two (skipped)
  after (returned: -4)

Context:
  spam: -2  # Story argument
  foo: -3   # Set by SubstoryDI.start
  bar: -1   # Set by SubstoryDI.before
    """.strip()

    getter = make_collector()
    examples.methods.SubstoryDI(examples.methods.Simple().x).y(spam=-2)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.SubstoryDI(examples.methods.Simple().x).y.run(spam=-2)
    assert repr(getter()) == expected

    expected = """
SubstoryDI.y
  start
  before
  x (SimpleSubstory.z)
    first (skipped)
  after (returned: 4)

Context:
  spam: 2  # Story argument
  foo: 1   # Set by SubstoryDI.start
  bar: 3   # Set by SubstoryDI.before
    """.strip()

    getter = make_collector()
    examples.methods.SubstoryDI(examples.methods.SimpleSubstory().z).y(spam=2)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.SubstoryDI(
        examples.methods.SimpleSubstory().z).y.run(spam=2)
    assert repr(getter()) == expected
Example #26
0
def test_context_representation_with_result(r, x):

    expected = """
Simple.x
  one
  two
  three (returned: -1)

Context:
  bar: 3  # Story argument
  foo: 1  # Story argument
  baz: 4  # Set by Simple.two
    """.strip()

    getter = make_collector()
    r(x.Simple().x)(foo=1, bar=3)
    assert repr(getter()) == expected

    getter = make_collector()
    r(x.Simple().x.run)(foo=1, bar=3)
    assert repr(getter()) == expected

    expected = """
SubstoryDI.y
  start
  before
  x (Simple.x)
    one
    two
    three (returned: -1)

Context:
  spam: 2  # Story argument
  foo: 1   # Set by SubstoryDI.start
  bar: 3   # Set by SubstoryDI.before
  baz: 4   # Set by Simple.two
    """.strip()

    getter = make_collector()
    r(x.SubstoryDI(x.Simple().x).y)(spam=2)
    assert repr(getter()) == expected

    getter = make_collector()
    r(x.SubstoryDI(x.Simple().x).y.run)(spam=2)
    assert repr(getter()) == expected

    expected = """
SubstoryDI.y
  start
  before
  x (Pipe.x)
    one
    two
    three
  after (returned: 6)

Context:
  spam: 3  # Story argument
  foo: 2   # Set by SubstoryDI.start
  bar: 4   # Set by SubstoryDI.before
    """.strip()

    getter = make_collector()
    r(x.SubstoryDI(x.Pipe().x).y)(spam=3)
    assert repr(getter()) == expected

    getter = make_collector()
    r(x.SubstoryDI(x.Pipe().x).y.run)(spam=3)
    assert repr(getter()) == expected
Example #27
0
def test_context_representation():

    # Empty.

    expected = """
Empty.x()

Context()
    """.strip()

    getter = make_collector()
    examples.methods.Empty().x()
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.Empty().x.run()
    assert repr(getter()) == expected

    expected = """
EmptySubstory.y()

Context()
        """.strip()

    getter = make_collector()
    examples.methods.EmptySubstory().y()
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.EmptySubstory().y.run()
    assert repr(getter()) == expected

    expected = """
SubstoryDI.y:
  start
  before
  after (returned: 6)

Context:
    spam = 3  # Story argument
    foo = 2   # Set by SubstoryDI.start
    bar = 4   # Set by SubstoryDI.before
        """.strip()

    getter = make_collector()
    examples.methods.SubstoryDI(examples.methods.Empty().x).y(3)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.SubstoryDI(examples.methods.Empty().x).y.run(3)
    assert repr(getter()) == expected

    # Failure.

    expected = """
Simple.x:
  one
  two (failed)

Context:
    foo = 2  # Story argument
    bar = 2  # Story argument
        """.strip()

    getter = make_collector()
    with pytest.raises(FailureError):
        examples.methods.Simple().x(2, 2)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.Simple().x.run(2, 2)
    assert repr(getter()) == expected

    expected = """
SimpleSubstory.y:
  start
  before
  x
    one
    two (failed)

Context:
    spam = 3  # Story argument
    foo = 2   # Set by SimpleSubstory.start
    bar = 4   # Set by SimpleSubstory.before
        """.strip()

    getter = make_collector()
    with pytest.raises(FailureError):
        examples.methods.SimpleSubstory().y(3)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.SimpleSubstory().y.run(3)
    assert repr(getter()) == expected

    expected = """
SubstoryDI.y:
  start
  before
  x (Simple.x)
    one
    two (failed)

Context:
    spam = 3  # Story argument
    foo = 2   # Set by SubstoryDI.start
    bar = 4   # Set by SubstoryDI.before
        """.strip()

    getter = make_collector()
    with pytest.raises(FailureError):
        examples.methods.SubstoryDI(examples.methods.Simple().x).y(3)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.SubstoryDI(examples.methods.Simple().x).y.run(3)
    assert repr(getter()) == expected

    # Failure with reason.

    expected = """
ReasonWithList.x:
  one
  two (failed: 'foo')

Context:
    foo = 3  # Story argument
    bar = 2  # Story argument
    """.strip()

    getter = make_collector()
    with pytest.raises(FailureError):
        examples.methods.ReasonWithList().x(3, 2)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.ReasonWithList().x.run(3, 2)
    assert repr(getter()) == expected

    expected = """
ReasonWithEnum.x:
  one
  two (failed: <Errors.foo: 1>)

Context:
    foo = 3  # Story argument
    bar = 2  # Story argument
    """.strip()

    getter = make_collector()
    with pytest.raises(FailureError):
        examples.methods.ReasonWithEnum().x(3, 2)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.ReasonWithEnum().x.run(3, 2)
    assert repr(getter()) == expected

    expected = """
SubstoryReasonWithList.y:
  start
  before
  x
    one
    two (failed: 'foo')

Context:
    spam = 4  # Story argument
    foo = 3   # Set by SubstoryReasonWithList.start
    bar = 5   # Set by SubstoryReasonWithList.before
    """.strip()

    getter = make_collector()
    with pytest.raises(FailureError):
        examples.methods.SubstoryReasonWithList().y(4)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.SubstoryReasonWithList().y.run(4)
    assert repr(getter()) == expected

    expected = """
SubstoryReasonWithEnum.y:
  start
  before
  x
    one
    two (failed: <Errors.foo: 1>)

Context:
    spam = 4  # Story argument
    foo = 3   # Set by SubstoryReasonWithEnum.start
    bar = 5   # Set by SubstoryReasonWithEnum.before
    """.strip()

    getter = make_collector()
    with pytest.raises(FailureError):
        examples.methods.SubstoryReasonWithEnum().y(4)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.SubstoryReasonWithEnum().y.run(4)
    assert repr(getter()) == expected

    # TODO:
    #
    #     expected = """
    # SubstoryDI.y:
    #   start
    #   before
    #   x (Simple.x)
    #     one
    #     two (failed: "'foo' is too big")
    #
    # Context:
    #     spam = 4  # Story argument
    #     foo = 3   # Set by SubstoryDI.start
    #     bar = 5   # Set by SubstoryDI.before
    #         """.strip()
    #
    #     getter = make_collector()
    #     with pytest.raises(FailureError):
    #         examples.methods.SubstoryDI(examples.methods.Simple().x).y(4)
    #     assert repr(getter()) == expected
    #
    #     getter = make_collector()
    #     examples.methods.SubstoryDI(examples.methods.Simple().x).y.run(4)
    #     assert repr(getter()) == expected

    # Result.

    expected = """
Simple.x:
  one
  two
  three (returned: -1)

Context:
    foo = 1  # Story argument
    bar = 3  # Story argument
    baz = 4  # Set by Simple.two
        """.strip()

    getter = make_collector()
    examples.methods.Simple().x(1, 3)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.Simple().x.run(1, 3)
    assert repr(getter()) == expected

    expected = """
SimpleSubstory.y:
  start
  before
  x
    one
    two
    three (returned: -1)

Context:
    spam = 2  # Story argument
    foo = 1   # Set by SimpleSubstory.start
    bar = 3   # Set by SimpleSubstory.before
    baz = 4   # Set by SimpleSubstory.two
        """.strip()

    getter = make_collector()
    examples.methods.SimpleSubstory().y(2)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.SimpleSubstory().y.run(2)
    assert repr(getter()) == expected

    expected = """
SubstoryDI.y:
  start
  before
  x (Simple.x)
    one
    two
    three (returned: -1)

Context:
    spam = 2  # Story argument
    foo = 1   # Set by SubstoryDI.start
    bar = 3   # Set by SubstoryDI.before
    baz = 4   # Set by Simple.two
    """.strip()

    getter = make_collector()
    examples.methods.SubstoryDI(examples.methods.Simple().x).y(2)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.SubstoryDI(examples.methods.Simple().x).y.run(2)
    assert repr(getter()) == expected

    expected = """
SubstoryDI.y:
  start
  before
  x (Pipe.x)
    one
    two
    three
  after (returned: 6)

Context:
    spam = 3  # Story argument
    foo = 2   # Set by SubstoryDI.start
    bar = 4   # Set by SubstoryDI.before
        """.strip()

    getter = make_collector()
    examples.methods.SubstoryDI(examples.methods.Pipe().x).y(3)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.SubstoryDI(examples.methods.Pipe().x).y.run(3)
    assert repr(getter()) == expected

    # Skip.

    expected = """
Simple.x:
  one
  two (skipped)

Context:
    foo = 1   # Story argument
    bar = -1  # Story argument
        """.strip()

    getter = make_collector()
    examples.methods.Simple().x(1, -1)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.Simple().x.run(1, -1)
    assert repr(getter()) == expected

    expected = """
SimpleSubstory.y:
  start
  before
  x
    one
    two (skipped)
  after (returned: -4)

Context:
    spam = -2  # Story argument
    foo = -3   # Set by SimpleSubstory.start
    bar = -1   # Set by SimpleSubstory.before
        """.strip()

    getter = make_collector()
    examples.methods.SimpleSubstory().y(-2)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.SimpleSubstory().y.run(-2)
    assert repr(getter()) == expected

    expected = """
SubstoryDI.y:
  start
  before
  x (Simple.x)
    one
    two (skipped)
  after (returned: -4)

Context:
    spam = -2  # Story argument
    foo = -3   # Set by SubstoryDI.start
    bar = -1   # Set by SubstoryDI.before
        """.strip()

    getter = make_collector()
    examples.methods.SubstoryDI(examples.methods.Simple().x).y(-2)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.SubstoryDI(examples.methods.Simple().x).y.run(-2)
    assert repr(getter()) == expected

    expected = """
SubstoryDI.y:
  start
  before
  x (SimpleSubstory.z)
    first (skipped)
  after (returned: 4)

Context:
    spam = 2  # Story argument
    foo = 1   # Set by SubstoryDI.start
    bar = 3   # Set by SubstoryDI.before
        """.strip()

    getter = make_collector()
    examples.methods.SubstoryDI(examples.methods.SimpleSubstory().z).y(2)
    assert repr(getter()) == expected

    getter = make_collector()
    examples.methods.SubstoryDI(examples.methods.SimpleSubstory().z).y.run(2)
    assert repr(getter()) == expected

    # Error.

    expected = """
StepError.x:
  one (errored: Exception)

Context()
    """.strip()

    getter = make_collector()
    with pytest.raises(Exception):
        examples.methods.StepError().x()
    assert repr(getter()) == expected

    getter = make_collector()
    with pytest.raises(Exception):
        examples.methods.StepError().x.run()
    assert repr(getter()) == expected