New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading Django models from JSON #184

Closed
LiamK opened this Issue Feb 15, 2015 · 3 comments

Comments

Projects
None yet
2 participants
@LiamK

LiamK commented Feb 15, 2015

I've been using Factory Boy, and it's great. However I have an issue that I'm not sure how to deal with.

models.py --
class Location(models.Model):
    city = ForeignKey(City)
class Type(models.Model):
    name = CharField()
    key = CharField()
class Rideshare(models.Model):
    type = ForeignKey(Type)
    location = ForeignKey(Location)

The first question is maybe easier -- how to represent Type. Type basically only has two db rows, and ideally I'd like to cycle between those two objects. If I make it a SubFactory, I'm not sure how to have the name and key fields linked -- they are not arbitrary/independent values. (One is a key for language translation, the other is a related English string.)
I was using this in the RideshareFactory

type = factory.fuzzy.FuzzyChoice(Type.objects.all())

But that is wrong because it hits the database before the test_ database is set up, causing hard to trace errors.

The more general problem that comes with Location is that City is several levels deep, and here again, it doesn't make sense to make up random Cities that don't actually exist. I need to test with real ones.

In both cases, I really just want to load some data into the test database first, and /then/ start creating factory objects, using the loaded data to populate the foreign key fields of the factory objects.
Is there some place I could just insert something like

from django.core.management import call_command
call_command('loaddata', 'api_test_data.json', verbosity=0)

Any help very much appreciated!

@LiamK

This comment has been minimized.

Show comment
Hide comment
@LiamK

LiamK Feb 15, 2015

The type = factory.fuzzy.FuzzyChoice(Type.objects.all()) call executes when the module is loaded, it seems, which is before the test database has been set up.
I am already separately loading the json data, so if there were some way to call FuzzyChoice but not have it execute the Type.objects.all() query until I actually create the object, that would work great, but I think that is not possible.

LiamK commented Feb 15, 2015

The type = factory.fuzzy.FuzzyChoice(Type.objects.all()) call executes when the module is loaded, it seems, which is before the test database has been set up.
I am already separately loading the json data, so if there were some way to call FuzzyChoice but not have it execute the Type.objects.all() query until I actually create the object, that would work great, but I think that is not possible.

@rbarrois

This comment has been minimized.

Show comment
Hide comment
@rbarrois

rbarrois Mar 26, 2015

Member

Hi,

This has just been fixed in 69befae: the Type.objects.all() part will only be evaluated with the first use of the factory, no longer at import time.

Member

rbarrois commented Mar 26, 2015

Hi,

This has just been fixed in 69befae: the Type.objects.all() part will only be evaluated with the first use of the factory, no longer at import time.

@LiamK

This comment has been minimized.

Show comment
Hide comment
@LiamK

LiamK Apr 3, 2015

Thanks so much! I look forward to testing it out soon.

LiamK commented Apr 3, 2015

Thanks so much! I look forward to testing it out soon.

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment