paulocheque / django-dynamic-fixture

A complete library to create dynamic model instances for testing purposes.
http://django-dynamic-fixture.readthedocs.io/
Other
390 stars 67 forks source link

Doesn't work properly with django-hvad for translations #35

Closed qris closed 4 years ago

qris commented 10 years ago

This works with django-hvad, provided that you give values for all required fields:

    item1 = NewsItem.objects.create(title="Item One",
        publication_date=date.today())

But this doesn't:

    item2 = G(NewsItem, title="Another One")

Because the title attribute is a translated field. I'm not sure whether the problem is in hvad or DDF. I think it occurs here, in DynamicFixture.new():

for field in get_fields_from_model(model_class):
    ...
    self.set_data_for_a_field(model_class, instance, field, persist_dependencies=persist_dependencies, **configuration)

Because get_fields_from_model doesn't return translated fields (they're not in model_class._meta.fields either):

(Pdb) p get_fields_from_model(model_class)
[<django.db.models.fields.AutoField: id>,
<django.db.models.fields.DateField: publication_date>,
<django.db.models.fields.files.ImageField: photo>,
<django.db.models.fields.CharField: image_caption>, 
<django.db.models.fields.CharField: location>]

That may be a mistake by hvad, but you can actually assign to those fields:

(Pdb) p instance.title
u''
(Pdb) instance.title = 'hello'
(Pdb) p instance.title
'hello'

so if DynamicFixture would actually assign all supplied kwargs, then it would work with hvad. Additionally it would not ignore supplied values for fields that it doesn't know about, which can lead to subtle errors.

paulocheque commented 10 years ago

Interesting behavior! If you have some pull request with tests I would be happy to merge!

qris commented 10 years ago

Do you want tests that depend on django-hvad? Or mock the model to check that we can assign all fields supplied in kwargs?

paulocheque commented 10 years ago

Hi qris, sorry by the late response.

What about trying the new field plugin system? http://django-dynamic-fixture.readthedocs.org/en/latest/data_fixtures.html#custom-field-fixture

pgcd commented 9 years ago

Having the same problem, and I don't think I understand how the custom fixtures can be used in this case - I already have them set for Charfields but that doesn't really help...

paulocheque commented 4 years ago

@pgcd Can you try the new teach method to teach DDF how to create this instance?

In your PyTest conftest.py module:

from ddf import teach

teach(NewsItem, title='Another one or some lambda function')

Then in your tests:

from ddf import G

G(NewsItem)
# or
G(NewsItem title='replace the default value')
pgcd commented 4 years ago

@paulocheque I'm very very sorry but a long time has passed, and I no longer use either. I'm so so sorry.

paulocheque commented 4 years ago

@pgcd No problem at all. I am sorry for the delay. Thanks again for the contribution.