def test_sequence_with_custom_function(self): label = "t %s" Factory.define(Tomato, number=Factory.sequence(lambda x: label % x)) assert_equals(label % 1, Factory(Tomato).number) assert_equals(label % 2, Factory(Tomato).number) assert_equals(label % 3, Factory(Tomato).number)
def test_sequence_with_custom_start(self): start = 100 Factory.define(Tomato, number=Factory.sequence(start=start)) assert_equals(start + 1, Factory(Tomato).number) assert_equals(start + 2, Factory(Tomato).number) assert_equals(start + 3, Factory(Tomato).number)
def test_extend_factory_overwrite_property(self): Factory.define(Tomato, color="green") Factory.extend(Tomato) ("red", color="red") t = Factory(Tomato, "red") assert_true("red", t.color)
def test_extend_factory_add_property(self): Factory.define(Tomato, color="green") Factory.extend(Tomato) ("with more power", power="high") pure_tomato = Factory(Tomato) rich_tomato = Factory(Tomato, "with more power") assert_equals("high", rich_tomato.power)
def test_factory_override(self): Factory.define(Tomato, ripe="yes") t = Factory(Tomato, ripe="no") assert_is_instance(t, Tomato) assert_equals("no", t.ripe) t = Factory(Tomato) assert_is_instance(t, Tomato) assert_equals("yes", t.ripe)
def test_association_one_to_many(self): # note the implicit laziness test via declaration order Factory.define(Tomato, vine=Factory.association(Vine)) Factory.define(Vine) t = Factory(Tomato) assert_is_instance(t.vine, Vine) assert_true(t.vine is t.vine) # make sure we only make one assert_false(t.vine is Factory(Tomato).vine)
def test_items(self): Factory.define( Tomato, color="green", number=3, _hidden='hidden', ) t=Factory(Tomato) items = t.items() assert_equals(len(items), 3) assert_in(('color', 'green'), items) assert_in(('number', 3), items) assert_in(('_hidden', 'hidden'), items)
def test_association_many_to_one(self): Factory.define(Vine, tomatoes=Factory.many( Factory.association(Tomato, color="red"), Factory.association(Tomato, color="green") )) Factory.define(Tomato) v = Factory(Vine) tomato = v.tomatoes[0] assert_is_instance(tomato, Tomato) assert_true(tomato is tomato) assert_equals("red", tomato.color) tomato = v.tomatoes[1] assert_is_instance(tomato, Tomato) assert_true(tomato is tomato) assert_equals("green", tomato.color) assert_false(tomato is Factory(Vine).tomatoes[1])
def test_defined_factory_undefined_specifier(self): Factory.define(Tomato) Factory(Tomato, "with a twist")
def test_extend_factory_with_differing_associations(self): Factory.define(Tomato, color=None) Factory.define(Vine, tomatoes=Factory.many( Factory.association(Tomato), Factory.association(Tomato) )) Factory.extend(Vine) ( "with special tomatoes", tomatoes=Factory.many( Factory.association(Tomato, color="red"), Factory.association(Tomato, color="green") ) ) pure_vine = Factory(Vine) rich_vine = Factory(Vine, "with special tomatoes") assert_equals("green", rich_vine.tomatoes[1].color) assert_equals(None, pure_vine.tomatoes[1].color)
def test_recursive_association(self): Factory.define(Tomato, vine=Factory.association(Vine)) Factory.define(Vine, tomato=Factory.association(Tomato)) t=Factory(Tomato) # will error out if we're eagerly loading
def test_define_factory_different_classes_same_name(self): class Outer(object): class Tomato(object): pass Factory.define(Outer.Tomato, imaginary=1) Factory.define(Tomato, real=1)
def test_factory_case_insensitive_specifier(self): Factory.define(Tomato, "with a twist", green=1) t = Factory(Tomato, "WITH a twist") assert_is_instance(t, Tomato) assert_equals(1, t.green)
def test_redefine_factory(self): Factory.define(Tomato, ripe="no") Factory.define(Tomato, ripe="yes")
def test_extend_nonexistent_factory(self): Factory.extend(Tomato) ("without first writing the base factory", color="oops")
def test_defined_factory_defined_specifier(self): Factory.define(Tomato, "with a twist", green=1) t = Factory(Tomato, "with a twist") assert_is_instance(t, Tomato) assert_equals(1, t.green)
def test_sequence(self): Factory.define(Tomato, number=Factory.sequence()) assert_equals(1, Factory(Tomato).number) assert_equals(2, Factory(Tomato).number) assert_equals(3, Factory(Tomato).number)
def test_defined_factory(self): Factory.define(Tomato, ripe="yes") t = Factory(Tomato) assert_is_instance(t, Tomato) assert_equals("yes", t.ripe)