def test_defining_recipes_str(self): from model_bakery.recipe import seq p = Recipe('generic.Person', name=seq('foo')) try: p.make(_quantity=5) except AttributeError as e: self.fail('%s' % e)
def test_always_calls_with_quantity(self): with patch("tests.test_recipes.choice") as choice_mock: choice_mock.return_value = "foo" lst = ["foo", "bar", "spam", "eggs"] r = Recipe(DummyBlankFieldsModel, blank_char_field=lambda: choice_mock(lst)) r.make(_quantity=3) assert choice_mock.call_count == 3
def test_scramble_v3_counts_command(self): from django.db.models import Q, F from model_bakery.recipe import Recipe from random import randint group_aggregate_recipe = Recipe(GroupAggregate, count=randint(0, 1000), count_scrambled=None) group_aggregate_recipe.make(_quantity=100) for ga in GroupAggregate.objects.all(): self.assertIsNotNone(ga.count) self.assertIsNone(ga.count_scrambled) # Do the scrambling call_man_command('scramble_v3_counts') differ_count = 0 for ga in GroupAggregate.objects.all(): self.assertIsNotNone(ga.count_scrambled) self.assertIn(ga.count_scrambled, (ga.count - 1, ga.count, ga.count + 1)) if ga.count_scrambled != ga.count: differ_count += 1 # check all records have their scrambled counts set assert not GroupAggregate.objects.filter(count_scrambled=None).exists() # check that all scrambled counts are within valid range assert not GroupAggregate.objects.filter( Q(count_scrambled__gt=F('count') + 1) | Q(count_scrambled__lt=F('count') - 1)) # Make sure that a significant amount of counts_scrambled were actually changed from the original self.assertGreater(differ_count, 50)
def test_always_calls_with_quantity(self): with patch('tests.test_recipes.choice') as choice_mock: choice_mock.return_value = 'foo' lst = ['foo', 'bar', 'spam', 'eggs'] r = Recipe(DummyBlankFieldsModel, blank_char_field=lambda: choice_mock(lst)) r.make(_quantity=3) assert choice_mock.call_count == 3
def test_defining_recipes_str(self): from model_bakery.recipe import seq p = Recipe("generic.Person", name=seq("foo")) try: p.make(_quantity=5) except AttributeError as e: self.fail("%s" % e)
def test_scramble_v2_counts_command(self): count_aggregate_recipe = Recipe( CountAggregate, count_in=randint(0, 1000), count_out=randint(0, 1000), count=randint(0, 1000), count_in_scrambled=None, count_out_scrambled=None, count_scrambled=None, ) count_aggregate_recipe.make(_quantity=100) # Do the scrambling call_man_command('scramble_v2_counts') differ_count_in = 0 differ_count_out = 0 differ_count = 0 for ca in CountAggregate.objects.all(): assert ca.count_in_scrambled is not None assert ca.count_in_scrambled in (ca.count_in - 1, ca.count_in, ca.count_in + 1) if ca.count_in_scrambled != ca.count_in: differ_count_in += 1 assert ca.count_out_scrambled is not None assert ca.count_out_scrambled in (ca.count_out - 1, ca.count_out, ca.count_out + 1) if ca.count_out_scrambled != ca.count_out: differ_count_out += 1 assert ca.count_scrambled is not None assert ca.count_scrambled in (ca.count - 1, ca.count, ca.count + 1) if ca.count_scrambled != ca.count: differ_count += 1 # check all records have their scrambled counts set assert not CountAggregate.objects.filter( Q(count_in_scrambled=None) | Q(count_out_scrambled=None) | Q(count_scrambled=None)).exists() # check that all scrambled counts are within valid range assert not CountAggregate.objects.filter( Q(count_in_scrambled__gt=F('count_in') + 1) | Q(count_in_scrambled__lt=F('count_in') - 1), Q(count_out_scrambled__gt=F('count_out') + 1) | Q(count_out_scrambled__lt=F('count_out') - 1), Q(count_scrambled__gt=F('count') + 1) | Q(count_scrambled__lt=F('count') - 1), ) # Make sure that a significant amount of counts_scrambled were actually changed from the original assert differ_count_in > 50 assert differ_count_out > 50 assert differ_count > 50
def test_do_query_lookup_empty_recipes(self): """It should not create another object when using query lookup syntax.""" dog_recipe = Recipe(Dog) dog = dog_recipe.make(owner__name="James") assert Person.objects.count() == 1 assert dog.owner.name == "James" dog = dog_recipe.prepare(owner__name="Zezin") assert Person.objects.count() == 1 assert dog.owner.name == "Zezin"
def test_make_recipe_without_all_model_needed_data(self): person_recipe = Recipe(Person, name='John Doe') person = person_recipe.make() assert 'John Doe' == person.name assert person.nickname assert person.age assert person.bio assert person.birthday assert person.appointment assert person.blog assert person.wanted_games_qtd assert person.id
def test_make_recipe_without_all_model_needed_data(self): person_recipe = Recipe(Person, name="John Doe") person = person_recipe.make() assert "John Doe" == person.name assert person.nickname assert person.age assert person.bio assert person.birthday assert person.appointment assert person.blog assert person.days_since_last_login assert person.id
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') assert Person.objects.count() == 1 assert dog.owner.name == 'James' dog = dog_recipe.prepare(owner__name='Zezin') assert Person.objects.count() == 1 assert dog.owner.name == 'Zezin'
def test_only_iterators_not_iteratables_are_iterated(self): """Ensure we only iterate explicit iterators. Consider "iterable" vs "iterator": Something like a string is "iterable", but not an "iterator". We don't want to iterate "iterables", only explicit "iterators". """ r = Recipe(DummyBlankFieldsModel, blank_text_field="not an iterator, so don't iterate!") assert r.make( ).blank_text_field == "not an iterator, so don't iterate!"
def test_empty_iterator_exception(self): r = Recipe(DummyBlankFieldsModel, blank_char_field=iter(["a", "b"])) assert "a" == r.make().blank_char_field assert "b" == r.make().blank_char_field with pytest.raises(RecipeIteratorEmpty): r.make()
def test_accepts_iterators(self): r = Recipe(DummyBlankFieldsModel, blank_char_field=iter(["a", "b", "c"])) assert "a" == r.make().blank_char_field assert "b" == r.make().blank_char_field assert "c" == r.make().blank_char_field
def test_accepts_generators(self): r = Recipe(DummyBlankFieldsModel, blank_char_field=itertools.cycle(["a", "b"])) assert "a" == r.make().blank_char_field assert "b" == r.make().blank_char_field assert "a" == r.make().blank_char_field
def test_accepts_generators(self): r = Recipe(DummyBlankFieldsModel, blank_char_field=itertools.cycle(['a', 'b'])) assert 'a' == r.make().blank_char_field assert 'b' == r.make().blank_char_field assert 'a' == r.make().blank_char_field
class BadgeAssertionTestModel(TenantTestCase): def setUp(self): self.client = TenantClient(self.tenant) self.sem = SiteConfig.get().active_semester self.teacher = Recipe(User, is_staff=True).make( ) # need a teacher or student creation will fail. self.student = baker.make(User) self.assertion = baker.make(BadgeAssertion, semester=self.sem) self.badge = Recipe(Badge, xp=20).make() self.badge_assertion_recipe = Recipe(BadgeAssertion, user=self.student, badge=self.badge, semester=self.sem) def test_badge_assertion_creation(self): self.assertIsInstance(self.assertion, BadgeAssertion) self.assertEqual(str(self.assertion), self.assertion.badge.name) def test_badge_assertion_url(self): self.assertEqual( self.client.get(self.assertion.get_absolute_url(), follow=True).status_code, 200) def test_badge_assertion_count(self): num = 5 for _ in range(num): badge_assertion = BadgeAssertion.objects.create_assertion( self.student, self.badge, issued_by=self.teacher) # Why doesn't below work? # badge_assertion = self.badge_assertion_recipe.make() count = badge_assertion.count() # print(num, count) self.assertEqual(num, count) def test_badge_assertion_count_bootstrap_badge(self): """Returns empty string if count < 2, else returns proper count""" badge_assertion = baker.make(BadgeAssertion, semester=self.sem) self.assertEqual(badge_assertion.count_bootstrap_badge(), "") num = 4 for _ in range(num): badge_assertion = BadgeAssertion.objects.create_assertion( self.student, self.badge, issued_by=self.teacher) # Why doesn't below work? # badge_assertion = self.badge_assertion_recipe.make() count = badge_assertion.count_bootstrap_badge() # print(num, count) self.assertEqual(num, count) def test_badge_assertion_get_duplicate_assertions(self): num = 5 values = [] for _ in range(num): badge_assertion = self.badge_assertion_recipe.make() values.append(repr(badge_assertion)) qs = badge_assertion.get_duplicate_assertions() self.assertQuerysetEqual( list(qs), values, ) def test_badge_assertion_manager_create_assertion(self): # no semester new_assertion = BadgeAssertion.objects.create_assertion( self.student, baker.make(Badge), self.teacher) self.assertIsInstance(new_assertion, BadgeAssertion) # no teacher new_assertion = BadgeAssertion.objects.create_assertion( self.student, baker.make(Badge), ) self.assertIsInstance(new_assertion, BadgeAssertion) def test_badge_assertion_manager_xp_to_date(self): xp = BadgeAssertion.objects.calculate_xp_to_date( self.student, timezone.now()) self.assertEqual(xp, 0) # give them a badge assertion and make sure the XP works BadgeAssertion.objects.create_assertion(self.student, self.badge, self.teacher) xp = BadgeAssertion.objects.calculate_xp_to_date( self.student, timezone.now()) self.assertEqual(xp, self.badge.xp) def test_badge_assertion_manager_get_by_type_for_user(self): badge_list_by_type = BadgeAssertion.objects.get_by_type_for_user( self.student) self.assertIsInstance(badge_list_by_type, list) # TODO need to test this properly def test_badge_assertion_manager_check_for_new_assertions(self): BadgeAssertion.objects.check_for_new_assertions(self.student) # TODO need to tefrom django.contrib.auth import get_user_model def test_fraction_of_active_users_granted_this(self): num_students_with_badge = 3 students_with_badge = baker.make(User, _quantity=num_students_with_badge) self.assertEqual(len(students_with_badge), num_students_with_badge) total_students = User.objects.filter(is_active=True).count() badge = baker.make(Badge) for student in students_with_badge: baker.make(BadgeAssertion, user=student, badge=badge) fraction = badge.fraction_of_active_users_granted_this() self.assertEqual(fraction, num_students_with_badge / total_students) percentile = badge.percent_of_active_users_granted_this() self.assertEqual(percentile, num_students_with_badge / total_students * 100)
def test_accepts_iterators(self): r = Recipe(DummyBlankFieldsModel, blank_char_field=iter(['a', 'b', 'c'])) assert 'a' == r.make().blank_char_field assert 'b' == r.make().blank_char_field assert 'c' == r.make().blank_char_field
def test_accepts_callable(self): r = Recipe(DummyBlankFieldsModel, blank_char_field=lambda: "callable!!") value = r.make().blank_char_field assert value == "callable!!"
from sample_app.models import * from model_bakery.recipe import Recipe, foreign_key fake = Faker() for k in range(100): author = Recipe( Author, name=fake.name(), createdDate=fake.future_datetime(end_date="+30d", tzinfo=None), updatedDate=fake.future_datetime(end_date="+30d", tzinfo=None), ) question = Recipe( Question, question_text=fake.sentence(nb_words=6, variable_nb_words=True), pub_date=fake.future_datetime(end_date="+30d", tzinfo=None), refAuthor=foreign_key(author), createdDate=fake.future_datetime(end_date="+30d", tzinfo=None), updatedDate=fake.future_datetime(end_date="+30d", tzinfo=None), ) choice = Recipe( Choice, question=foreign_key(question), choice_text=fake.sentence(nb_words=1, variable_nb_words=True), createdDate=fake.future_datetime(end_date="+30d", tzinfo=None), updatedDate=fake.future_datetime(end_date="+30d", tzinfo=None), ) choice.make()