On Thu, 2006-10-12 at 13:06 +0100, Sam Newman wrote:
> I'm trying to write a simple unit test for one of my models. I have
> two models - Build and Project. A Project has multiple Builds. I want
> to be able to create a build object, with an association to a Project.
> However I seem unable to do this unless I actually save the Project
> instance to the database - even if I have no intention of ever saving
> the build. For example:

Somehow the object being referred to needs to get a primary key value so
that it can be stored in the referring object (otherwise, no association
would be possible). Normally, that happens by saving the first object.

Looks like we actually enforce that by trying to retrieve the original
from the database.

[...]
> In other persistence layers - Hibernate comes to mind - if I directly
> create the association as I have done here, I can query that
> association before persistence. 

It's not a common use-case, though, since if you are referring to
another object, you are presumably going to save them. Django asks you
to save first.

I'm not sure whether this is really worth working around just for the
slight testing speed up. However, I also haven't looked at the code and
tried to work out what the implications of doing so would be. Maybe it's
a reasonable change, maybe not. The risk is that if we don't require a
save first, then either the user or Django needs to ensure the saves are
done in the right order, otherwise the database check constraint is
going to be violated (the referred-to object must be saved first).
Asking Django to serialise the saves in the right order is something I
thought we had kind of decided not to do quite a few months ago in a
similar discussion, but my memory may be failing me on this point.

> If I have to create lots of fake data,
> and always hit the database it is going to add significant bloat to my
> unit testing.

The SQLite in-memory database (a database with the name of ":memory:")
is perfect for this sort of thing. No disk access involved.

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to