My approach to a similar problem is to instantiate a user within my test
setup and pass that user to the model.save function.

i.e
class Device(models.Model):
    foo = models....
    bar = models....

    def  save(self, user=None):
        model_instance = save_function(self, user)
        super(Device, model_instance).save()

def save_function(model_instance, user=None):
    if user == None:
        user = threadlocals.get_current_user()
    model_instance.owner = user

    return model_instance

having the kwarg user in the save and save_function definition allows my
test to pass the user, while through the browser during normal operations it
is gotten from threadlocals.  I also can then manipulate devices within the
shell as well.

hope this helps.

-richard



On 10/1/07, Lic. José M. Rodriguez Bacallao <[EMAIL PROTECTED]> wrote:
>
> hi everybody, I need to test how my models work outside the browser, with
> unit tests but one of my models had an attribute "created_by" representing
> the user who created it. Right now I'm using "ThreadLocals" middleware to
> get the current user outside views but I don't know how can I test it in
> other places other than the browser?
>
> --
> Lic. José M. Rodriguez Bacallao
> Cupet
> -----------------------------------------------------------------
> Todos somos muy ignorantes, lo que ocurre es que no todos ignoramos lo
> mismo.
>
> Recuerda: El arca de Noe fue construida por aficionados, el titanic por
> profesionales
> -----------------------------------------------------------------
> >
>

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

  • test models Lic. José M. Rodriguez Bacallao
    • Re: test models Richard Dahl

Reply via email to