def test_lots_of_big_squads(): player = Builder(Player).withA(NumberOf(Squad.units, 5), NumberOf(Player.squads, 3)).build() assert len(player.squads) == 3 for squad in player.squads: assert isinstance(squad.leader, Hero) assert squad.leader.squad == squad assert len(squad.units) == 5
def test_uplink_reset(): class B: a = Uplink() class A: bs = Collection(B) B.a.linksTo(A, A.bs) a = Builder(A).withA(NumberOf(A.bs, 0)).build() b = Builder(B).build() assert a != b.a
def test_collection_from_bottom(number): small = Builder(Small).withA(NumberOf(Big.smalls, number)).build() assert small assert isinstance(small, Small) assert small.big assert isinstance(small.big, Big) assert small.big.smalls assert len(small.big.smalls) == number for s in small.big.smalls: assert s assert s in small.big.smalls assert isinstance(s.big, Big) assert s.big == small.big
def test_Having_instances(): class A: pass class B: a = Collection(A) myA = A() b = Builder(B).withA(HavingIn(B.a, myA)).build() assert myA in b.a b2 = Builder(B).withA(HavingIn(B.a, myA)).withA(NumberOf(B.a, 3)).build() assert myA in b2.a assert len(b2.a) == 3
def test_one_of(): class A: a = 0 b = Uplink() class B: values = Collection(A) A.b.linksTo(B, B.values) b = Builder(B).withA(NumberOf(B.values, 3)).\ withA(OneOf(B.values, InstanceModifier(A).thatSets(a=8))).\ withA(OneOf(B.values, InstanceModifier(A).thatSets(a=5))).\ build() assert len(b.values) == 3 assert len([a for a in b.values if a.a == 8]) == 1 assert len([a for a in b.values if a.a == 5]) == 1
def test_another_modifier(): class A: a = 0 b = Uplink() class B: values = Collection(A) A.b.linksTo(B, B.values) b = Builder(B).withA(NumberOf(B.values, 1)).\ withA(Another(B.values, InstanceModifier(A).thatSets(a=4))).\ withA(Another(B.values, InstanceModifier(A).thatSets(a=28))).build() assert len(b.values) == 3 assert len([a for a in b.values if a.a == 0]) == 1 assert len([a for a in b.values if a.a == 4]) == 1 assert len([a for a in b.values if a.a == 28]) == 1
def test_having(): class A: pass class B: a = Collection(A) def check(b, num): assert b.a assert b.a[0] assert isinstance(b.a[0], A) assert len(b.a) == num b = Builder(B).build() b2 = Builder(B).withA(NumberOf(B.a, 3)).build() b3 = Builder(B).build() check(b, 1) check(b2, 3) check(b3, 1) assert b2.a[0] != b2.a[1] assert b2.a[0] != b2.a[2]
def test_reuse_regression(): u1 = Builder(Up).withA(NumberOf(Up.downs, 3)).build() assert u1.downs[0].up == u1 assert u1.downs[1].up == u1 assert u1.downs[2].up == u1
def test_Collection_with_number_Uplink(): Big.smalls.number = 1 checkBig(Builder(Big).withA(NumberOf(Big.smalls, 2)).build(), range(2))
from builders.construct import Collection, Unique, Maybe class A(ValuesMixin): a = 1 class B: a = Unique(A) ma = Maybe(Unique(A)) aa = Collection(A) def foo(a): pass @pytest.mark.parametrize('modifier', [ InstanceModifier(A).thatDoes(foo), InstanceModifier(A).thatSets(a=2), InstanceModifier(A).thatCarefullySets(a=2), A.values(a=2), NumberOf(B.aa, 2), HavingIn(B.aa, A()), Given(B.a, A()), Enabled(B.ma), OneOf(B.aa, [[]]) ]) def test_modifier(modifier): assert pickle.loads(pickle.dumps(modifier))
def test_big_squads(): player = Builder(Player).withA(NumberOf(Squad.units, 5)).build() assert len(player.squads[0].units) == 5