Skip to content

Commit

Permalink
Adopt ruff's SIM rules (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
amureki authored Oct 27, 2023
1 parent 2444664 commit 92603c8
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 76 deletions.
8 changes: 3 additions & 5 deletions model_bakery/baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,7 @@ def is_rel_field(x: str):
}
self.model_attrs = {k: v for k, v in attrs.items() if not is_rel_field(k)}
self.rel_attrs = {k: v for k, v in attrs.items() if is_rel_field(k)}
self.rel_fields = [
x.split("__")[0] for x in self.rel_attrs.keys() if is_rel_field(x)
]
self.rel_fields = [x.split("__")[0] for x in self.rel_attrs if is_rel_field(x)]

def _skip_field(self, field: Field) -> bool:
from django.contrib.contenttypes.fields import GenericRelation
Expand Down Expand Up @@ -598,7 +596,7 @@ def _skip_field(self, field: Field) -> bool:
if isinstance(field, (AutoField, GenericRelation, OrderWrt)):
return True

if all(
if all( # noqa: SIM102
[
field.name not in self.model_attrs,
field.name not in self.rel_fields,
Expand All @@ -614,7 +612,7 @@ def _skip_field(self, field: Field) -> bool:
):
return True

if field.name not in self.model_attrs:
if field.name not in self.model_attrs: # noqa: SIM102
if field.name not in self.rel_fields and (
field.null and not field.fill_optional
):
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ select = [
"S", # flake8-bandit
"B", # flake8-bugbear
"C", # flake8-comprehensions
"SIM", # flake8-simplify
"I", # isort
"E", # pycodestyle errors
"W", # pycodestyle warnings
Expand Down
70 changes: 35 additions & 35 deletions tests/test_baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_non_abstract_model_creation(self):
models.NonAbstractPerson, name="bob", enjoy_jards_macale=False
)
assert isinstance(person, models.NonAbstractPerson)
assert "bob" == person.name
assert person.name == "bob"
assert person.enjoy_jards_macale is False

def test_abstract_model_subclass_creation(self):
Expand Down Expand Up @@ -210,16 +210,16 @@ def test_accepts_generators_with_quantity(self):
)
assert models.Person.objects.count() == 5
p1, p2, p3, p4, p5 = models.Person.objects.all().order_by("pk")
assert "a" == p1.name
assert "d1" == p1.id_document
assert "b" == p2.name
assert "d2" == p2.id_document
assert "c" == p3.name
assert "d3" == p3.id_document
assert "a" == p4.name
assert "d4" == p4.id_document
assert "b" == p5.name
assert "d5" == p5.id_document
assert p1.name == "a"
assert p1.id_document == "d1"
assert p2.name == "b"
assert p2.id_document == "d2"
assert p3.name == "c"
assert p3.id_document == "d3"
assert p4.name == "a"
assert p4.id_document == "d4"
assert p5.name == "b"
assert p5.id_document == "d5"

def test_accepts_generators_with_quantity_for_unique_fields(self):
baker.make(
Expand All @@ -229,9 +229,9 @@ def test_accepts_generators_with_quantity_for_unique_fields(self):
)
assert models.DummyUniqueIntegerFieldModel.objects.count() == 3
num_1, num_2, num_3 = models.DummyUniqueIntegerFieldModel.objects.all()
assert 1 == num_1.value
assert 2 == num_2.value
assert 3 == num_3.value
assert num_1.value == 1
assert num_2.value == 2
assert num_3.value == 3

def test_generators_work_with_user_model(self):
from django.contrib.auth import get_user_model
Expand All @@ -240,9 +240,9 @@ def test_generators_work_with_user_model(self):
baker.make(User, username=itertools.cycle(["a", "b", "c"]), _quantity=3)
assert User.objects.count() == 3
u1, u2, u3 = User.objects.all()
assert "a" == u1.username
assert "b" == u2.username
assert "c" == u3.username
assert u1.username == "a"
assert u2.username == "b"
assert u3.username == "c"


@pytest.mark.django_db
Expand Down Expand Up @@ -335,14 +335,14 @@ def test_save_object_instances_when_handling_one_to_many_relations(self):
_quantity=2,
)

assert 0 == models.Dog.objects.count() # ensure there are no dogs in our db
assert models.Dog.objects.count() == 0 # ensure there are no dogs in our db
home = baker.make(
models.Home,
owner=owner,
dogs=dogs_set,
)
assert home.dogs.count() == 2
assert 2 == models.Dog.objects.count() # dogs in dogs_set were created
assert models.Dog.objects.count() == 2 # dogs in dogs_set were created

def test_prepare_fk(self):
dog = baker.prepare(models.Dog)
Expand Down Expand Up @@ -476,7 +476,7 @@ def test_does_not_create_nullable_many_to_many_for_relations(self):

def test_nullable_many_to_many_is_not_created_even_if_flagged(self):
classroom = baker.make(models.Classroom, make_m2m=True)
assert not classroom.students.count()
assert classroom.students.count() == 0

def test_m2m_changed_signal_is_fired(self):
# TODO: Use object attrs instead of mocks for Django 1.4 compat
Expand Down Expand Up @@ -507,17 +507,17 @@ def test_creating_person_from_factory_using_parameters(self):

def test_ForeignKey_model_field_population(self):
dog = baker.make(models.Dog, breed="X1", owner__name="Bob")
assert "X1" == dog.breed
assert "Bob" == dog.owner.name
assert dog.breed == "X1"
assert dog.owner.name == "Bob"

def test_ForeignKey_model_field_population_should_work_with_prepare(self):
dog = baker.prepare(models.Dog, breed="X1", owner__name="Bob")
assert "X1" == dog.breed
assert "Bob" == dog.owner.name
assert dog.breed == "X1"
assert dog.owner.name == "Bob"

def test_ForeignKey_model_field_population_for_not_required_fk(self):
user = baker.make(models.User, profile__email="[email protected]")
assert "[email protected]" == user.profile.email
assert user.profile.email == "[email protected]"

def test_does_not_creates_null_ForeignKey(self):
user = baker.make(models.User)
Expand All @@ -529,18 +529,18 @@ def test_passing_m2m_value(self):

def test_ensure_recursive_ForeignKey_population(self):
bill = baker.make(models.PaymentBill, user__profile__email="[email protected]")
assert "[email protected]" == bill.user.profile.email
assert bill.user.profile.email == "[email protected]"

def test_field_lookup_for_m2m_relationship(self):
store = baker.make(models.Store, suppliers__gender="M")
suppliers = store.suppliers.all()
assert suppliers
for supplier in suppliers:
assert "M" == supplier.gender
assert supplier.gender == "M"

def test_field_lookup_for_one_to_one_relationship(self):
lonely_person = baker.make(models.LonelyPerson, only_friend__name="Bob")
assert "Bob" == lonely_person.only_friend.name
assert lonely_person.only_friend.name == "Bob"

def test_allow_create_fk_related_model(self):
try:
Expand All @@ -561,9 +561,9 @@ def test_field_lookup_for_related_field(self):

assert person.pk
assert person.one_related.pk
assert 1, person.fk_related.count()
assert "Foo" == person.one_related.name
assert "Bar" == person.fk_related.get().name
assert person.fk_related.count() == 1
assert person.one_related.name == "Foo"
assert person.fk_related.get().name == "Bar"

def test_field_lookup_for_related_field_does_not_work_with_prepare(self):
person = baker.prepare(
Expand All @@ -573,7 +573,7 @@ def test_field_lookup_for_related_field_does_not_work_with_prepare(self):
)

assert not person.pk
assert 0 == models.RelatedNamesModel.objects.count()
assert models.RelatedNamesModel.objects.count() == 0

def test_ensure_reverse_fk_for_many_to_one_is_working(self):
"""This is a regression test to make sure issue 291 is fixed."""
Expand Down Expand Up @@ -621,7 +621,7 @@ def test_create_model_with_contenttype_field(self):
class TestHandlingContentTypeFieldNoQueries:
def test_create_model_with_contenttype_field(self):
# Clear ContentType's internal cache so that it *will* try to connect to
# the database in order to fetch the corresponding ContentType model for
# the database to fetch the corresponding ContentType model for
# a randomly chosen model.
ContentType.objects.clear_cache()

Expand Down Expand Up @@ -788,7 +788,7 @@ def test_creates_instance_for_model_with_next(self):
assert instance.id
assert instance.fk.id
assert instance.fk.attr
assert "foo" == instance.fk.next()
assert instance.fk.next() == "foo"


@pytest.mark.django_db
Expand Down Expand Up @@ -930,7 +930,7 @@ def test_recipe_related_fk_database_specified_via_using_kwarg_and_quantity(self)
dog_qs = models.Dog.objects.using(settings.EXTRA_DB).all()
person_qs = models.Person.objects.using(settings.EXTRA_DB).all()
assert dog_qs.count() == 5
# since we're using recipes, all dogs belongs to the same owner
# since we're using recipes, all dogs belong to the same owner
assert person_qs.count() == 1
for dog in dogs:
dog.refresh_from_db()
Expand Down
14 changes: 7 additions & 7 deletions tests/test_filling_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def test_uses_generator_defined_on_settings_for_custom_field(self, custom_cfg):
}
settings.BAKER_CUSTOM_FIELDS_GEN = generator_dict
obj = baker.make(models.CustomFieldWithGeneratorModel)
assert "value" == obj.custom_value
assert obj.custom_value == "value"

def test_uses_generator_defined_as_string_on_settings_for_custom_field(
self, custom_cfg
Expand All @@ -389,7 +389,7 @@ def test_uses_generator_defined_as_string_on_settings_for_custom_field(
# fmt: on
settings.BAKER_CUSTOM_FIELDS_GEN = generator_dict
obj = baker.make(models.CustomFieldWithGeneratorModel)
assert "value" == obj.custom_value
assert obj.custom_value == "value"

def test_uses_generator_defined_on_settings_for_custom_foreignkey(self, custom_cfg):
"""Should use the function defined in the import path for a foreign key field."""
Expand All @@ -400,7 +400,7 @@ def test_uses_generator_defined_on_settings_for_custom_foreignkey(self, custom_c
obj = baker.make(
models.CustomForeignKeyWithGeneratorModel, custom_fk__email="[email protected]"
)
assert "[email protected]" == obj.custom_fk.email
assert obj.custom_fk.email == "[email protected]"

def test_uses_generator_defined_as_string_for_custom_field(self, custom_cfg):
"""Should import and use the generator function used in the add method."""
Expand All @@ -409,15 +409,15 @@ def test_uses_generator_defined_as_string_for_custom_field(self, custom_cfg):
"tests.generic.generators.gen_value_string",
)
obj = baker.make(models.CustomFieldWithGeneratorModel)
assert "value" == obj.custom_value
assert obj.custom_value == "value"

def test_uses_generator_function_for_custom_foreignkey(self, custom_cfg):
"""Should use the generator function passed as a value for the add method."""
baker.generators.add("tests.generic.fields.CustomForeignKey", gen_related)
obj = baker.make(
models.CustomForeignKeyWithGeneratorModel, custom_fk__email="[email protected]"
)
assert "[email protected]" == obj.custom_fk.email
assert obj.custom_fk.email == "[email protected]"

def test_can_override_django_default_field_functions_generator(self, custom_cfg):
def gen_char():
Expand All @@ -427,11 +427,11 @@ def gen_char():

person = baker.make(models.Person)

assert "Some value" == person.name
assert person.name == "Some value"

def test_ensure_adding_generators_via_settings_works(self):
obj = baker.make(models.CustomFieldViaSettingsModel)
assert "always the same text" == obj.custom_value
assert obj.custom_value == "always the same text"


@pytest.mark.django_db
Expand Down
40 changes: 20 additions & 20 deletions tests/test_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def test_prepare_recipes_with_args(self):
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.name == "John Doe"
assert person.nickname
assert person.age
assert person.bio
Expand All @@ -154,7 +154,7 @@ def test_make_recipe_without_all_model_needed_data(self):
def test_prepare_recipe_without_all_model_needed_data(self):
person_recipe = Recipe(Person, name="John Doe")
person = person_recipe.prepare()
assert "John Doe" == person.name
assert person.name == "John Doe"
assert person.nickname
assert person.age
assert person.bio
Expand Down Expand Up @@ -235,7 +235,7 @@ def test_make_recipe_with_quantity_parameter(self):
def test_make_extended_recipe(self):
extended_dog = baker.make_recipe("tests.generic.extended_dog")
assert extended_dog.breed == "Super basset"
# No side effects happened due to extension
# No side effects happened due to a recipe extension
base_dog = baker.make_recipe("tests.generic.dog")
assert base_dog.breed == "Pug"

Expand All @@ -251,7 +251,7 @@ def test_save_related_instances_on_prepare_recipe(self):
assert not dog.id
assert dog.owner.id

def test_make_recipe_with_quantity_parameter_respection_model_args(self):
def test_make_recipe_with_quantity_parameter_respecting_model_args(self):
people = baker.make_recipe(
"tests.generic.person", _quantity=3, name="Dennis Ritchie", age=70
)
Expand Down Expand Up @@ -284,7 +284,7 @@ def test_prepare_recipe_with_quantity_parameter(self):
assert isinstance(person, Person)
assert person.id is None

def test_prepare_recipe_with_quantity_parameter_respection_model_args(self):
def test_prepare_recipe_with_quantity_parameter_respecting_model_args(self):
people = baker.prepare_recipe(
"tests.generic.person", _quantity=3, name="Dennis Ritchie", age=70
)
Expand Down Expand Up @@ -338,10 +338,10 @@ def test_pass_save_kwargs_in_recipe_definition(self):
def test_ip_fields_with_start(self):
first, second = baker.make_recipe("tests.generic.ip_fields", _quantity=2)

assert "127.0.0.2" == first.ipv4_field
assert "2001:12f8:0:28::4" == first.ipv6_field
assert "127.0.0.4" == second.ipv4_field
assert "2001:12f8:0:28::6" == second.ipv6_field
assert first.ipv4_field == "127.0.0.2"
assert first.ipv6_field == "2001:12f8:0:28::4"
assert second.ipv4_field == "127.0.0.4"
assert second.ipv6_field == "2001:12f8:0:28::6"


@pytest.mark.django_db
Expand Down Expand Up @@ -537,9 +537,9 @@ def test_creates_unique_field_recipe_using_for_iterator(self):

def test_creates_unique_field_recipe_using_quantity_argument(self):
dummies = baker.make_recipe("tests.generic.dummy_unique_field", _quantity=3)
assert 11 == dummies[0].value
assert 12 == dummies[1].value
assert 13 == dummies[2].value
assert dummies[0].value == 11
assert dummies[1].value == 12
assert dummies[2].value == 13

def test_increment_by_3(self):
dummy = baker.make_recipe("tests.generic.serial_numbers_by")
Expand Down Expand Up @@ -600,20 +600,20 @@ def test_increment_after_override_definition_field(self):
class TestIterators:
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
assert r.make().blank_char_field == "a"
assert r.make().blank_char_field == "b"
assert r.make().blank_char_field == "a"

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
assert r.make().blank_char_field == "a"
assert r.make().blank_char_field == "b"
assert r.make().blank_char_field == "c"

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
assert r.make().blank_char_field == "a"
assert r.make().blank_char_field == "b"
with pytest.raises(RecipeIteratorEmpty):
r.make()

Expand Down
Loading

0 comments on commit 92603c8

Please sign in to comment.