Skip to content

Commit

Permalink
[-] Add more specificity in unit test assertions
Browse files Browse the repository at this point in the history
- This tests to make sure `_bulk_create` works with models that
  have custom Managers.
- This adds an assertion that without `_bulk_create=True` there
  will be the number of queries to the DB equal to the value of
  `_quantity`.
  • Loading branch information
timjklein36 committed Nov 30, 2020
1 parent 7ddcac2 commit 3ee3668
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
6 changes: 6 additions & 0 deletions tests/generic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,9 @@ class Meta(object):

class SubclassOfAbstract(AbstractModel):
height = models.IntegerField()


class NonStandardManager(models.Model):
name = models.CharField(max_length=30)

manager = models.Manager()
28 changes: 24 additions & 4 deletions tests/test_baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,17 @@ def test_multiple_inheritance_creation(self):
@pytest.mark.django_db
class TestsBakerRepeatedCreatesSimpleModel:
def test_make_should_create_objects_respecting_quantity_parameter(self):
baker.make(models.Person, _quantity=5)
assert models.Person.objects.count() == 5
queries = QueryCount()

people = baker.make(models.Person, _quantity=5, name="George Washington")
assert all(p.name == "George Washington" for p in people)
with queries.start_count():
baker.make(models.Person, _quantity=5)
assert queries.count == 5
assert models.Person.objects.count() == 5

with queries.start_count():
people = baker.make(models.Person, _quantity=5, name="George Washington")
assert all(p.name == "George Washington" for p in people)
assert queries.count == 5

def test_make_quantity_respecting_bulk_create_parameter(self):
queries = QueryCount()
Expand All @@ -175,6 +181,20 @@ def test_make_quantity_respecting_bulk_create_parameter(self):
assert all(p.name == "George Washington" for p in people)
assert queries.count == 1

with queries.start_count():
baker.make(models.NonStandardManager, _quantity=3, _bulk_create=True)
assert queries.count == 1
assert getattr(models.NonStandardManager, "objects", None) is None
assert (
models.NonStandardManager._base_manager
== models.NonStandardManager.manager
)
assert (
models.NonStandardManager._default_manager
== models.NonStandardManager.manager
)
assert models.NonStandardManager.manager.count() == 3

def test_make_raises_correct_exception_if_invalid_quantity(self):
with pytest.raises(InvalidQuantityException):
baker.make(_model=models.Person, _quantity="hi")
Expand Down

0 comments on commit 3ee3668

Please sign in to comment.