factory-boy

How to test signals when using factory_boy with muted signals

拜拜、爱过 提交于 2021-02-18 15:18:48
问题 I am using factory_boy package and DjangoModelFactory to generate a factory model with muted signals @factory.django.mute_signals(signals.post_save) class SomeModelTargetFactory(DjangoModelFactory): name = factory.Sequence(lambda x: "Name #{}".format(x)) ... I have a post_save signal connected to the model: def send_notification(sender, instance, created, **kwargs): if created: send_email(...) post_save.connect(send_notification, SomeModel) How can I test the signals works when I create an

How to use Faker from Factory_boy

点点圈 提交于 2021-02-04 14:25:28
问题 Factory_boy uses fake-factory (Faker) to generate random values, I would like to generate some random values in my Django tests using Faker directly. Factory_boy docs suggests using factory.Faker and its provider as : class RandomUserFactory(factory.Factory): class Meta: model = models.User first_name = factory.Faker('first_name') But this isn't generating any name: >>> import factory >>> factory.Faker('name') <factory.faker.Faker object at 0x7f1807bf5278> >>> type(factory.Faker('name'))

In FactoryBoy, how do I set a many-to-many field in my factory?

痴心易碎 提交于 2020-06-17 09:34:06
问题 I'm using Django 2.2 and FactoryBoy to attempt to create some factories to produce the following models ... class CoopType(models.Model): name = models.CharField(max_length=200, null=False) objects = CoopTypeManager() class Meta: # Creates a new unique constraint with the `name` field constraints = [models.UniqueConstraint(fields=['name'], name='coop_type_unq')] class Coop(models.Model): objects = CoopManager() name = models.CharField(max_length=250, null=False) types = models.ManyToManyField

Passing Trait Value to SubFactory Django

半腔热情 提交于 2020-04-30 09:28:08
问题 I have two factories. class DispatchDataFactory(factory.django.DjangoModelFactory): class Meta: model = models.DispatchData order = factory.SelfAttribute('order_data.order') sku = factory.LazyAttribute(lambda obj: '%d' % obj.order_data.sku) category = SKUCategory.SINGLE quantity = 50 class Params: combo_sku=False order_data = factory.SubFactory(OrderDataFactory, combo_sku=factory.SelfAttribute('combo_sku')) combo_sku = factory.Trait( sku=factory.LazyAttribute(lambda obj: '%d' % obj.order_data

How to make FactoryBoy's ImageField generate image before save() is called?

℡╲_俬逩灬. 提交于 2020-03-01 01:59:17
问题 Subj. Right now (Factory Boy ver. 2.4.1.) with this code: class ImageFactory(factory.django.DjangoModelFactory): class Meta: model = Image image = factory.django.ImageField(width=1024, height=768) image will be None at save time, so if the Image model has save overridden and its operates with the image , it will fail. And thats exactly my case. So - how to make image generated before save call? 回答1: I've found a workaround: class ImageFactory(factory.django.DjangoModelFactory): class Meta:

How to specify the database for Factory Boy?

孤者浪人 提交于 2020-01-02 03:28:07
问题 FactoryBoy seem to always create the instances in the default database. But I have the following problem. cpses = CanonPerson.objects.filter(persons__vpd=6, persons__country="United States").using("global") The code is pointing to the global database. I haven't found a way to specify the database within the factory: class CanonPersonFactory(django_factory.DjangoModelFactory): class Meta: model = CanonPerson django_get_or_create = ('name_first', 'p_id') p_id = 1 name_first = factory.Sequence

Why does the naming of RelatedFactory variables in factory-boy affect when the factories are executed/instantiated?

梦想的初衷 提交于 2019-12-23 16:01:06
问题 When I named my RelatedFactory variables pv_something , the factories wouldn't be run until after the post_generation method. When I renamed the variables to param_val_something they would be run before the post_generation method. In the following code, RelatedFactory is not run until after post_generation , so self.something_set.all() is empty, and the line t.something_else = 'abc' is never executed. class ThingFactory(factory.DjangoModelFactory): class Meta: model = Thing name = 'a thing'

save a field as JSON using factoryboy

一笑奈何 提交于 2019-12-22 06:47:33
问题 I am trying to create an instance of the model in which one of the field is JSONField . When creating the instance of model via factoryboy , I want to save the field as JSONField in the test database. On simply passing the field as JSON ,it gets stored in Unicode data type. I am stuck on this. 回答1: You can create dict data using a factory.Dict. You can then control the final form of the data using the dict_factory attribute. e.g. if you want to have dict data that is serialised to a JSON

Is it possible to parametrize a test with fixtures?

房东的猫 提交于 2019-12-20 03:25:28
问题 I would like to pass @pytest.mark.parametrize not particular values but fixtures. Like so. Given a conftest with: @pytest.fixture def name1(): return 'foo' @pytest.fixture def name2(): return 'bar' within my test.py this works of course: @pytest.mark.parametrize('name', ['foo', 'bar', 'baz']) def test_name(name): print(name) This does not: @pytest.mark.parametrize('name', [name1, name2]) def test_name(name): print(name) I am aware that in this trivial case I could just create one name fixture

Is passing too many arguments to the constructor considered an anti-pattern?

夙愿已清 提交于 2019-12-19 06:29:28
问题 I am considering using the factory_boy library for API testing. An example from the documentation is: class UserFactory(factory.Factory): class Meta: model = base.User first_name = "John" last_name = "Doe" For this to work, we need first_name , last_name , etc to be passed as parameters to the __init__() method of the base.User() class . However, if you have many parameters this leads to something like: class User(object): GENDER_MALE = 'mr' GENDER_FEMALE = 'ms' def __init__(self, title=None,