本文整理汇总了Python中model_mommy.recipe.Recipe.prepare方法的典型用法代码示例。如果您正苦于以下问题:Python Recipe.prepare方法的具体用法?Python Recipe.prepare怎么用?Python Recipe.prepare使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类model_mommy.recipe.Recipe
的用法示例。
在下文中一共展示了Recipe.prepare方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: LongAnswerResponseTests
# 需要导入模块: from model_mommy.recipe import Recipe [as 别名]
# 或者: from model_mommy.recipe.Recipe import prepare [as 别名]
class LongAnswerResponseTests(TestCase):
def setUp(self):
self.recipe = Recipe(
FormFieldResponse,
form_field__kind='long-answer',
form_field__details={'required': True}
)
def test_should_pass_when_required_and_answer_not_blank(self):
field_response = self.recipe.prepare(details={'answer': 'ok'})
self.assertEqual(field_response.clean(), None)
def test_should_not_pass_when_required_and_answer_not_provided(self):
field_response = self.recipe.prepare(details={})
self.assertRaises(ValidationError, field_response.clean)
def test_should_not_pass_when_required_and_answer_blank(self):
field_response = self.recipe.prepare(details={'answer': ''})
self.assertRaises(ValidationError, field_response.clean)
def test_should_pass_when_not_required_and_answer_not_provided(self):
field_response = self.recipe.prepare(details={})
field_response.form_field.details['required'] = False
self.assertEqual(field_response.clean(), None)
def test_should_pass_when_not_required_and_answer_blank(self):
field_response = self.recipe.prepare(details={'answer': ''})
field_response.form_field.details['required'] = False
self.assertEqual(field_response.clean(), None)
示例2: AddressResponseTests
# 需要导入模块: from model_mommy.recipe import Recipe [as 别名]
# 或者: from model_mommy.recipe.Recipe import prepare [as 别名]
class AddressResponseTests(TestCase):
def setUp(self):
self.recipe = Recipe(
FormFieldResponse,
form_field__kind='address',
form_field__details={'required': True}
)
def test_should_pass_when_required_and_only_addr2_blank(self):
field_response = self.recipe.prepare(details={
'addressLine1': 'x', 'city': 'x', 'state': 'x', 'zip': 'x'
})
self.assertEqual(field_response.clean(), None)
def test_should_not_pass_when_required_and_any_field_but_addr2_blank(self):
field_response = self.recipe.prepare(details={
'addressLine1': '', 'city': 'x', 'state': 'x', 'zip': 'x'
})
self.assertRaises(ValidationError, field_response.clean)
def test_should_pass_when_not_required_and_all_fields_blank(self):
field_response = self.recipe.prepare()
field_response.form_field.details['required'] = False
self.assertEqual(field_response.clean(), None)
示例3: InfoResponseTests
# 需要导入模块: from model_mommy.recipe import Recipe [as 别名]
# 或者: from model_mommy.recipe.Recipe import prepare [as 别名]
class InfoResponseTests(TestCase):
def setUp(self):
self.recipe = Recipe(FormFieldResponse, form_field__kind='info')
def test_should_never_pass(self):
for details in [{}, {'answer': 'no good'}]:
field_response = self.recipe.prepare(details=details)
self.assertRaises(ValidationError, field_response.clean)
示例4: test_prepare_recipe_with_foreign_key
# 需要导入模块: from model_mommy.recipe import Recipe [as 别名]
# 或者: from model_mommy.recipe.Recipe import prepare [as 别名]
def test_prepare_recipe_with_foreign_key(self):
person_recipe = Recipe(Person, name='John Doe')
dog_recipe = Recipe(Dog,
owner=foreign_key(person_recipe),
)
dog = dog_recipe.prepare()
self.assertIsNone(dog.id)
self.assertIsNone(dog.owner.id)
示例5: test_prepare_recipe_without_all_model_needed_data
# 需要导入模块: from model_mommy.recipe import Recipe [as 别名]
# 或者: from model_mommy.recipe.Recipe import prepare [as 别名]
def test_prepare_recipe_without_all_model_needed_data(self):
person_recipe = Recipe(Person, name='John Doe')
person = person_recipe.prepare()
self.assertEqual('John Doe', person.name)
self.assertTrue(person.nickname)
self.assertTrue(person.age)
self.assertTrue(person.bio)
self.assertTrue(person.birthday)
self.assertTrue(person.appointment)
self.assertTrue(person.blog)
self.assertTrue(person.wanted_games_qtd)
self.assertFalse(person.id)
示例6: test_do_query_lookup_empty_recipes
# 需要导入模块: from model_mommy.recipe import Recipe [as 别名]
# 或者: from model_mommy.recipe.Recipe import prepare [as 别名]
def test_do_query_lookup_empty_recipes(self):
"""
It should not attempt to create other object when
using query lookup syntax
"""
dog_recipe = Recipe(Dog)
dog = dog_recipe.make(owner__name='James')
self.assertEqual(Person.objects.count(), 1)
self.assertEqual(dog.owner.name, 'James')
dog = dog_recipe.prepare(owner__name='Zezin')
self.assertEqual(Person.objects.count(), 1)
self.assertEqual(dog.owner.name, 'Zezin')
示例7: SingleChoiceResponseTests
# 需要导入模块: from model_mommy.recipe import Recipe [as 别名]
# 或者: from model_mommy.recipe.Recipe import prepare [as 别名]
class SingleChoiceResponseTests(TestCase):
def setUp(self):
self.recipe = Recipe(
FormFieldResponse,
form_field__kind='single-choice',
form_field__details={
'choices': [{'label': 'A'}, {'label': 'B'}, {'label': 'C'}],
'required': True
}
)
def test_should_pass_when_required_and_answer_valid_choice(self):
field_response = self.recipe.prepare(details={'answer': 'B'})
self.assertEqual(field_response.clean(), None)
def test_should_not_pass_when_required_and_answer_non_choice(self):
field_response = self.recipe.prepare(details={'answer': 'X'})
self.assertRaises(ValidationError, field_response.clean)
def test_should_not_pass_when_required_and_answer_not_provided(self):
field_response = self.recipe.prepare(details={})
self.assertRaises(ValidationError, field_response.clean)
def test_should_not_pass_when_required_and_answer_blank(self):
field_response = self.recipe.prepare(details={'answer': ''})
self.assertRaises(ValidationError, field_response.clean)
def test_should_not_pass_when_not_required_and_answer_non_choice(self):
field_response = self.recipe.prepare(details={'answer': 'X'})
field_response.form_field.details['required'] = False
self.assertRaises(ValidationError, field_response.clean)
def test_should_pass_when_not_required_and_answer_not_provided(self):
field_response = self.recipe.prepare(details={})
field_response.form_field.details['required'] = False
self.assertEqual(field_response.clean(), None)
def test_should_pass_when_not_required_and_answer_blank(self):
field_response = self.recipe.prepare(details={'answer': ''})
field_response.form_field.details['required'] = False
self.assertEqual(field_response.clean(), None)
示例8: ImovelTest
# 需要导入模块: from model_mommy.recipe import Recipe [as 别名]
# 或者: from model_mommy.recipe.Recipe import prepare [as 别名]
class ImovelTest(TestCase):
def setUp(self):
self.endereco_base = 'Rua Baronesa, 175'
self.cidade_base = 'Rio de Janeiro'
self.lat_base = -22.8950148
self.lng_base = -43.3542673
self.imovel = mommy.make(Imovel)
self.basic_imovel_recipe = Recipe(Imovel, latitude=None, longitude=None)
imoveis_recipe = Recipe(Imovel,
endereco=self.endereco_base,
cidade=self.cidade_base,
latitude=self.lat_base,
longitude=self.lng_base,
disponivel=cycle([False, True])
)
# Cria 9 imóveis alterando disponíveis e indisponíveis
imoveis_recipe.make(_quantity=9)
def test_get_disponiveis_qtd(self):
"""Garante que tem 5 imóveis disponíveis apenas"""
imoveis = Imovel.get_disponiveis()
self.assertEqual(5, len(imoveis))
def test_get_disponiveis_value(self):
"""Garante que todos os imóveis retornados estão com disponível True"""
imoveis = Imovel.get_disponiveis()
self.assertEqual([True] * len(imoveis), [i.disponivel for i in imoveis])
def test_get_proximos_a_vazio(self):
imoveis = Imovel.get_proximos_a(latitude=0, longitude=0)
self.assertEqual(0, len(imoveis))
def test_get_proximos(self):
coordenadas = get_coordenates("Rua Baronesa, 300, Rio de Janeiro")
imoveis = Imovel.get_proximos_a(latitude=coordenadas[0], longitude=coordenadas[1])
self.assertGreater(len(imoveis), 0)
def test_get_proximos_mais_longe(self):
coordenadas = get_coordenates("Rua Nelson Cardoso, 300, Rio de Janeiro")
imoveis = Imovel.get_proximos_a(latitude=coordenadas[0], longitude=coordenadas[1])
self.assertEquals(len(imoveis), 0)
def test_get_proximos_mais_longe_haversine(self):
""" Esse endereço fica a 1.2km do endereço que possui imóveis
Porém ele fica dentro das coordenadas mínimas e máximas.
A validação adicional através da fórmula garante que nenhum resultado será encontrado
"""
coordenadas = get_coordenates("Rua Luiz Beltrão, 646, Rio de Janeiro")
# Garante que o endereço pesquisado está dentro do "quadrado" inicial de filtragem
bounds = get_min_max_coordenates(self.lat_base, self.lng_base)
self.assertGreaterEqual(coordenadas[0], bounds[0])
self.assertLessEqual(coordenadas[0], bounds[1])
self.assertGreater(coordenadas[1], bounds[2])
self.assertLessEqual(coordenadas[1], bounds[3])
# Procura imóveis na região do endereço, mas graças a fórmula ninguém é encontrado
imoveis = Imovel.get_proximos_a(latitude=coordenadas[0], longitude=coordenadas[1])
self.assertEquals(len(imoveis), 0)
def test_custom_save_erro(self):
imovel = self.basic_imovel_recipe.prepare()
with self.assertRaises(IntegrityError):
imovel.save()
def test_custom_save(self):
imovel = self.basic_imovel_recipe.prepare(endereco=self.endereco_base, cidade=self.cidade_base)
imovel.save()
self.assertIsNotNone(imovel.latitude)
self.assertIsNotNone(imovel.longitude)
self.assertIsNotNone(imovel.endereco_formatado)
def test_remover_anuncio(self):
self.imovel.remover_anuncio()
self.assertFalse(self.imovel.disponivel)
def test_str(self):
self.assertTrue('Imóvel em %s' % self.imovel.endereco, str(self.imovel))
示例9: TestDefiningRecipes
# 需要导入模块: from model_mommy.recipe import Recipe [as 别名]
# 或者: from model_mommy.recipe.Recipe import prepare [as 别名]
class TestDefiningRecipes(TestCase):
def setUp(self):
self.recipe_attrs = {
'name': 'John Doe',
'nickname': 'joe',
'age': 18,
'bio': 'Someone in the crowd',
'birthday': now().date(),
'appointment': now(),
'blog': 'http://joe.blogspot.com',
'wanted_games_qtd': 4,
'birth_time': now()
}
self.person_recipe = Recipe(
Person,
**self.recipe_attrs
)
def test_flat_model_make_recipe_with_the_correct_attributes(self):
"""
A 'flat model' means a model without associations, like
foreign keys, many to many and one to one
"""
person = self.person_recipe.make()
self.assertEqual(person.name, self.recipe_attrs['name'])
self.assertEqual(person.nickname, self.recipe_attrs['nickname'])
self.assertEqual(person.age, self.recipe_attrs['age'])
self.assertEqual(person.bio, self.recipe_attrs['bio'])
self.assertEqual(person.birthday, self.recipe_attrs['birthday'])
self.assertEqual(person.appointment, self.recipe_attrs['appointment'])
self.assertEqual(person.blog, self.recipe_attrs['blog'])
self.assertEqual(person.wanted_games_qtd, self.recipe_attrs['wanted_games_qtd'])
self.assertNotEqual(person.id, None)
def test_flat_model_prepare_recipe_with_the_correct_attributes(self):
person = self.person_recipe.prepare()
self.assertEqual(person.name, self.recipe_attrs['name'])
self.assertEqual(person.nickname, self.recipe_attrs['nickname'])
self.assertEqual(person.age, self.recipe_attrs['age'])
self.assertEqual(person.bio, self.recipe_attrs['bio'])
self.assertEqual(person.birthday, self.recipe_attrs['birthday'])
self.assertEqual(person.appointment, self.recipe_attrs['appointment'])
self.assertEqual(person.blog, self.recipe_attrs['blog'])
self.assertEqual(person.wanted_games_qtd, self.recipe_attrs['wanted_games_qtd'])
self.assertEqual(person.id, None)
def test_accepts_callable(self):
r = Recipe(DummyBlankFieldsModel,
blank_char_field = lambda: 'callable!!'
)
value = r.make().blank_char_field
self.assertEqual(value, 'callable!!')
def test_always_calls_when_creating(self):
with patch('test.generic.tests.test_recipes.choice') as choice_mock:
l = ['foo', 'bar', 'spam', 'eggs']
r = Recipe(DummyBlankFieldsModel,
blank_char_field = lambda: choice(l)
)
r.make().blank_char_field
r.make().blank_char_field
self.assertEqual(choice_mock.call_count, 2)
def test_always_calls_with_quantity(self):
with patch('test.generic.tests.test_recipes.choice') as choice_mock:
l = ['foo', 'bar', 'spam', 'eggs']
r = Recipe(DummyBlankFieldsModel,
blank_char_field = lambda: choice(l)
)
r.make(_quantity=3)
self.assertEqual(choice_mock.call_count, 3)
def test_make_recipes_with_args(self):
"""
Overriding some fields values at recipe execution
"""
person = self.person_recipe.make(name='Guido', age=56)
self.assertNotEqual(person.name, self.recipe_attrs['name'])
self.assertEqual(person.name, 'Guido')
self.assertNotEqual(person.age, self.recipe_attrs['age'])
self.assertEqual(person.age, 56)
self.assertEqual(person.nickname, self.recipe_attrs['nickname'])
self.assertEqual(person.bio, self.recipe_attrs['bio'])
self.assertEqual(person.birthday, self.recipe_attrs['birthday'])
self.assertEqual(person.appointment, self.recipe_attrs['appointment'])
self.assertEqual(person.blog, self.recipe_attrs['blog'])
self.assertEqual(person.wanted_games_qtd, self.recipe_attrs['wanted_games_qtd'])
self.assertNotEqual(person.id, None)
def test_prepare_recipes_with_args(self):
"""
Overriding some fields values at recipe execution
"""
person = self.person_recipe.prepare(name='Guido', age=56)
self.assertNotEqual(person.name, self.recipe_attrs['name'])
self.assertEqual(person.name, 'Guido')
self.assertNotEqual(person.age, self.recipe_attrs['age'])
#.........这里部分代码省略.........
示例10: TestDefiningRecipes
# 需要导入模块: from model_mommy.recipe import Recipe [as 别名]
# 或者: from model_mommy.recipe.Recipe import prepare [as 别名]
class TestDefiningRecipes(TestCase):
def setUp(self):
self.recipe_attrs = {
'name': 'John Doe',
'nickname': 'joe',
'age': 18,
'bio': 'Someone in the crowd',
'birthday': now().date(),
'appointment': now(),
'blog': 'http://joe.blogspot.com',
'wanted_games_qtd': 4,
'birth_time': now()
}
self.person_recipe = Recipe(
Person,
**self.recipe_attrs
)
def test_flat_model_make_recipe_with_the_correct_attributes(self):
"""
A 'flat model' means a model without associations, like
foreign keys, many to many and one to one
"""
person = self.person_recipe.make()
self.assertEqual(person.name, self.recipe_attrs['name'])
self.assertEqual(person.nickname, self.recipe_attrs['nickname'])
self.assertEqual(person.age, self.recipe_attrs['age'])
self.assertEqual(person.bio, self.recipe_attrs['bio'])
self.assertEqual(person.birthday, self.recipe_attrs['birthday'])
self.assertEqual(person.appointment, self.recipe_attrs['appointment'])
self.assertEqual(person.blog, self.recipe_attrs['blog'])
self.assertEqual(person.wanted_games_qtd, self.recipe_attrs['wanted_games_qtd'])
self.assertNotEqual(person.id, None)
def test_flat_model_prepare_recipe_with_the_correct_attributes(self):
person = self.person_recipe.prepare()
self.assertEqual(person.name, self.recipe_attrs['name'])
self.assertEqual(person.nickname, self.recipe_attrs['nickname'])
self.assertEqual(person.age, self.recipe_attrs['age'])
self.assertEqual(person.bio, self.recipe_attrs['bio'])
self.assertEqual(person.birthday, self.recipe_attrs['birthday'])
self.assertEqual(person.appointment, self.recipe_attrs['appointment'])
self.assertEqual(person.blog, self.recipe_attrs['blog'])
self.assertEqual(person.wanted_games_qtd, self.recipe_attrs['wanted_games_qtd'])
self.assertEqual(person.id, None)
def test_accepts_callable(self):
r = Recipe(DummyBlankFieldsModel,
blank_char_field = lambda: 'callable!!'
)
value = r.make().blank_char_field
self.assertEqual(value, 'callable!!')
def test_make_recipes_with_args(self):
"""
Overriding some fields values at recipe execution
"""
person = self.person_recipe.make(name='Guido', age=56)
self.assertNotEqual(person.name, self.recipe_attrs['name'])
self.assertEqual(person.name, 'Guido')
self.assertNotEqual(person.age, self.recipe_attrs['age'])
self.assertEqual(person.age, 56)
self.assertEqual(person.nickname, self.recipe_attrs['nickname'])
self.assertEqual(person.bio, self.recipe_attrs['bio'])
self.assertEqual(person.birthday, self.recipe_attrs['birthday'])
self.assertEqual(person.appointment, self.recipe_attrs['appointment'])
self.assertEqual(person.blog, self.recipe_attrs['blog'])
self.assertEqual(person.wanted_games_qtd, self.recipe_attrs['wanted_games_qtd'])
self.assertNotEqual(person.id, None)
def test_prepare_recipes_with_args(self):
"""
Overriding some fields values at recipe execution
"""
person = self.person_recipe.prepare(name='Guido', age=56)
self.assertNotEqual(person.name, self.recipe_attrs['name'])
self.assertEqual(person.name, 'Guido')
self.assertNotEqual(person.age, self.recipe_attrs['age'])
self.assertEqual(person.age, 56)
self.assertEqual(person.nickname, self.recipe_attrs['nickname'])
self.assertEqual(person.bio, self.recipe_attrs['bio'])
self.assertEqual(person.birthday, self.recipe_attrs['birthday'])
self.assertEqual(person.appointment, self.recipe_attrs['appointment'])
self.assertEqual(person.blog, self.recipe_attrs['blog'])
self.assertEqual(person.wanted_games_qtd, self.recipe_attrs['wanted_games_qtd'])
self.assertEqual(person.id, None)
示例11: TestDefiningRecipes
# 需要导入模块: from model_mommy.recipe import Recipe [as 别名]
# 或者: from model_mommy.recipe.Recipe import prepare [as 别名]
class TestDefiningRecipes(TestCase):
def setUp(self):
self.recipe_attrs = {
"name": "John Doe",
"nickname": "joe",
"age": 18,
"bio": "Someone in the crowd",
"birthday": now().date(),
"appointment": now(),
"blog": "http://joe.blogspot.com",
"wanted_games_qtd": 4,
"birth_time": now(),
}
self.person_recipe = Recipe(Person, **self.recipe_attrs)
def test_flat_model_make_recipe_with_the_correct_attributes(self):
"""
A 'flat model' means a model without associations, like
foreign keys, many to many and one to one
"""
person = self.person_recipe.make()
self.assertEqual(person.name, self.recipe_attrs["name"])
self.assertEqual(person.nickname, self.recipe_attrs["nickname"])
self.assertEqual(person.age, self.recipe_attrs["age"])
self.assertEqual(person.bio, self.recipe_attrs["bio"])
self.assertEqual(person.birthday, self.recipe_attrs["birthday"])
self.assertEqual(person.appointment, self.recipe_attrs["appointment"])
self.assertEqual(person.blog, self.recipe_attrs["blog"])
self.assertEqual(person.wanted_games_qtd, self.recipe_attrs["wanted_games_qtd"])
self.assertNotEqual(person.id, None)
def test_flat_model_prepare_recipe_with_the_correct_attributes(self):
person = self.person_recipe.prepare()
self.assertEqual(person.name, self.recipe_attrs["name"])
self.assertEqual(person.nickname, self.recipe_attrs["nickname"])
self.assertEqual(person.age, self.recipe_attrs["age"])
self.assertEqual(person.bio, self.recipe_attrs["bio"])
self.assertEqual(person.birthday, self.recipe_attrs["birthday"])
self.assertEqual(person.appointment, self.recipe_attrs["appointment"])
self.assertEqual(person.blog, self.recipe_attrs["blog"])
self.assertEqual(person.wanted_games_qtd, self.recipe_attrs["wanted_games_qtd"])
self.assertEqual(person.id, None)
def test_accepts_callable(self):
r = Recipe(DummyBlankFieldsModel, blank_char_field=lambda: "callable!!")
value = r.make().blank_char_field
self.assertEqual(value, "callable!!")
def test_always_calls_when_creating(self):
with patch("test.generic.tests.test_recipes.choice") as choice_mock:
choice.return_value = "foo"
l = ["foo", "bar", "spam", "eggs"]
r = Recipe(DummyBlankFieldsModel, blank_char_field=lambda: choice(l))
r.make().blank_char_field
r.make().blank_char_field
self.assertEqual(choice_mock.call_count, 2)
def test_always_calls_with_quantity(self):
with patch("test.generic.tests.test_recipes.choice") as choice_mock:
choice.return_value = "foo"
l = ["foo", "bar", "spam", "eggs"]
r = Recipe(DummyBlankFieldsModel, blank_char_field=lambda: choice(l))
r.make(_quantity=3)
self.assertEqual(choice_mock.call_count, 3)
def test_make_recipes_with_args(self):
"""
Overriding some fields values at recipe execution
"""
person = self.person_recipe.make(name="Guido", age=56)
self.assertNotEqual(person.name, self.recipe_attrs["name"])
self.assertEqual(person.name, "Guido")
self.assertNotEqual(person.age, self.recipe_attrs["age"])
self.assertEqual(person.age, 56)
self.assertEqual(person.nickname, self.recipe_attrs["nickname"])
self.assertEqual(person.bio, self.recipe_attrs["bio"])
self.assertEqual(person.birthday, self.recipe_attrs["birthday"])
self.assertEqual(person.appointment, self.recipe_attrs["appointment"])
self.assertEqual(person.blog, self.recipe_attrs["blog"])
self.assertEqual(person.wanted_games_qtd, self.recipe_attrs["wanted_games_qtd"])
self.assertNotEqual(person.id, None)
def test_prepare_recipes_with_args(self):
"""
Overriding some fields values at recipe execution
"""
person = self.person_recipe.prepare(name="Guido", age=56)
self.assertNotEqual(person.name, self.recipe_attrs["name"])
self.assertEqual(person.name, "Guido")
self.assertNotEqual(person.age, self.recipe_attrs["age"])
self.assertEqual(person.age, 56)
self.assertEqual(person.nickname, self.recipe_attrs["nickname"])
self.assertEqual(person.bio, self.recipe_attrs["bio"])
self.assertEqual(person.birthday, self.recipe_attrs["birthday"])
self.assertEqual(person.appointment, self.recipe_attrs["appointment"])
self.assertEqual(person.blog, self.recipe_attrs["blog"])
#.........这里部分代码省略.........
示例12: TestDefiningRecipes
# 需要导入模块: from model_mommy.recipe import Recipe [as 别名]
# 或者: from model_mommy.recipe.Recipe import prepare [as 别名]
class TestDefiningRecipes(TestCase):
def setUp(self):
self.recipe_attrs = {
"name": "John Doe",
"nickname": "joe",
"age": 18,
"bio": "Someone in the crowd",
"birthday": now().date(),
"appointment": now(),
"blog": "http://joe.blogspot.com",
"wanted_games_qtd": 4,
"birth_time": now(),
}
self.person_recipe = Recipe(Person, **self.recipe_attrs)
def test_flat_model_make_recipe_with_the_correct_attributes(self):
"""
A 'flat model' means a model without associations, like
foreign keys, many to many and one to one
"""
person = self.person_recipe.make()
self.assertEqual(person.name, self.recipe_attrs["name"])
self.assertEqual(person.nickname, self.recipe_attrs["nickname"])
self.assertEqual(person.age, self.recipe_attrs["age"])
self.assertEqual(person.bio, self.recipe_attrs["bio"])
self.assertEqual(person.birthday, self.recipe_attrs["birthday"])
self.assertEqual(person.appointment, self.recipe_attrs["appointment"])
self.assertEqual(person.blog, self.recipe_attrs["blog"])
self.assertEqual(person.wanted_games_qtd, self.recipe_attrs["wanted_games_qtd"])
self.assertNotEqual(person.id, None)
def test_flat_model_prepare_recipe_with_the_correct_attributes(self):
person = self.person_recipe.prepare()
self.assertEqual(person.name, self.recipe_attrs["name"])
self.assertEqual(person.nickname, self.recipe_attrs["nickname"])
self.assertEqual(person.age, self.recipe_attrs["age"])
self.assertEqual(person.bio, self.recipe_attrs["bio"])
self.assertEqual(person.birthday, self.recipe_attrs["birthday"])
self.assertEqual(person.appointment, self.recipe_attrs["appointment"])
self.assertEqual(person.blog, self.recipe_attrs["blog"])
self.assertEqual(person.wanted_games_qtd, self.recipe_attrs["wanted_games_qtd"])
self.assertEqual(person.id, None)
def test_accepts_callable(self):
r = Recipe(DummyBlankFieldsModel, blank_char_field=lambda: "callable!!")
value = r.make().blank_char_field
self.assertEqual(value, "callable!!")
def test_make_recipes_with_args(self):
"""
Overriding some fields values at recipe execution
"""
person = self.person_recipe.make(name="Guido", age=56)
self.assertNotEqual(person.name, self.recipe_attrs["name"])
self.assertEqual(person.name, "Guido")
self.assertNotEqual(person.age, self.recipe_attrs["age"])
self.assertEqual(person.age, 56)
self.assertEqual(person.nickname, self.recipe_attrs["nickname"])
self.assertEqual(person.bio, self.recipe_attrs["bio"])
self.assertEqual(person.birthday, self.recipe_attrs["birthday"])
self.assertEqual(person.appointment, self.recipe_attrs["appointment"])
self.assertEqual(person.blog, self.recipe_attrs["blog"])
self.assertEqual(person.wanted_games_qtd, self.recipe_attrs["wanted_games_qtd"])
self.assertNotEqual(person.id, None)
def test_prepare_recipes_with_args(self):
"""
Overriding some fields values at recipe execution
"""
person = self.person_recipe.prepare(name="Guido", age=56)
self.assertNotEqual(person.name, self.recipe_attrs["name"])
self.assertEqual(person.name, "Guido")
self.assertNotEqual(person.age, self.recipe_attrs["age"])
self.assertEqual(person.age, 56)
self.assertEqual(person.nickname, self.recipe_attrs["nickname"])
self.assertEqual(person.bio, self.recipe_attrs["bio"])
self.assertEqual(person.birthday, self.recipe_attrs["birthday"])
self.assertEqual(person.appointment, self.recipe_attrs["appointment"])
self.assertEqual(person.blog, self.recipe_attrs["blog"])
self.assertEqual(person.wanted_games_qtd, self.recipe_attrs["wanted_games_qtd"])
self.assertEqual(person.id, None)
def test_make_recipe_without_all_model_needed_data(self):
person_recipe = Recipe(Person, name="John Doe")
person = person_recipe.make()
self.assertEqual("John Doe", person.name)
self.assertTrue(person.nickname)
self.assertTrue(person.age)
self.assertTrue(person.bio)
self.assertTrue(person.birthday)
self.assertTrue(person.appointment)
self.assertTrue(person.blog)
self.assertTrue(person.wanted_games_qtd)
self.assertTrue(person.id)
def test_prepare_recipe_without_all_model_needed_data(self):
#.........这里部分代码省略.........
示例13: MultipleChoiceResponseTests
# 需要导入模块: from model_mommy.recipe import Recipe [as 别名]
# 或者: from model_mommy.recipe.Recipe import prepare [as 别名]
class MultipleChoiceResponseTests(TestCase):
def setUp(self):
self.recipe = Recipe(
FormFieldResponse,
form_field__kind='multiple-choice',
form_field__details={
'choices': [{'label': 'A'}, {'label': 'B'}, {'label': 'C'}],
'required': True
}
)
def test_should_set_left_out_answers_to_false(self):
field_response = self.recipe.prepare(
details={'answers': [{'label': 'B', 'selected': True}]}
)
field_response.clean()
self.assertItemsEqual(field_response.details, {'answers': [
{'label': 'A', 'selected': False},
{'label': 'B', 'selected': True},
{'label': 'C', 'selected': False},
]})
def test_should_pass_when_required_and_at_least_one_answer_selected(self):
field_response = self.recipe.prepare(
details={'answers': [
{'label': 'A', 'selected': True},
{'label': 'B', 'selected': False},
{'label': 'C', 'selected': False},
]}
)
self.assertEqual(field_response.clean(), None)
def test_should_not_pass_when_required_and_any_answer_non_choice(self):
field_response = self.recipe.prepare(
details={'answers': [
{'label': 'A', 'selected': True},
{'label': 'X', 'selected': False},
{'label': 'C', 'selected': False},
]}
)
self.assertRaises(ValidationError, field_response.clean)
def test_should_not_pass_when_required_and_no_answers_provided(self):
field_response = self.recipe.prepare(details={})
self.assertRaises(ValidationError, field_response.clean)
def test_should_not_pass_when_required_and_all_answers_false(self):
field_response = self.recipe.prepare(
details={'answers': [
{'label': 'A', 'selected': False},
{'label': 'B', 'selected': False},
{'label': 'C', 'selected': False},
]}
)
self.assertRaises(ValidationError, field_response.clean)
def test_should_not_pass_when_not_required_and_any_answer_non_choice(self):
field_response = self.recipe.prepare(
details={'answers': [
{'label': 'A', 'selected': True},
{'label': 'X', 'selected': False},
{'label': 'C', 'selected': False},
]}
)
field_response.form_field.details['required'] = False
self.assertRaises(ValidationError, field_response.clean)
def test_should_pass_when_not_required_and_no_answers_provided(self):
field_response = self.recipe.prepare(details={})
field_response.form_field.details['required'] = False
self.assertEqual(field_response.clean(), None)
def test_should_pass_when_not_required_and_all_answers_false(self):
field_response = self.recipe.prepare(
details={'answers': [
{'label': 'A', 'selected': False},
{'label': 'B', 'selected': False},
{'label': 'C', 'selected': False},
]}
)
field_response.form_field.details['required'] = False
self.assertEqual(field_response.clean(), None)