[PATCH V2] New factory class to create arbitrary model objects, to be used in tests

Jeremy Kerr jk at ozlabs.org
Wed Apr 27 13:39:16 EST 2011


Hi Guilherme,

> I think there's value in having this factory because some tests will need
> either more stuff than is provided by the basic fixture or just different
> stuff, and changing the fixture to accomodate the needs of every possible test
> is a well known cause of obscure tests: 
>     <http://xunitpatterns.com/Obscure%20Test.html#General%20Fixture>
> 
> I already have some tests using this and I'm planning to write more for the
> 'my patches' page that I'm working on, which I hope to submit soon.

OK, this is looking pretty good, but I have a couple of concerns though:

> +# Patchwork - automated patch tracking system
> +# Copyright (C) 2011 Jeremy Kerr <jk at ozlabs.org>

Probably best to add your name here :)

> +
> +    def __init__(self):
> +        # Initialise the unique identifier.
> +        self.integer = count(randint(0, 1000000))

As a general rule, I'm averse to using random data in test cases. We may
see non-repeatable failures here, if we create multiple ObjectFactories
that happen to have colliding unique IDs.

So, perhaps we should make ObjectFactory a singleton (referenced though
an exported module variable, something like 'factory'), and just use:

    self.integer = count()

How does that sound?

> +    def makePatch(self, project=None, submitter=None, date=None):
> +        if date is None:
> +            date = datetime.now()
> +        if project is None:
> +            project = self.makeProject()
> +        if submitter is None:
> +            submitter = self.makePerson()
> +        msgid = email.utils.make_msgid(idstring=self.getUniqueString())
> +        patch = Patch(
> +            project=project, msgid=msgid, name=self.getUniqueString(),
> +            submitter=submitter, date=date,
> +            state=State.objects.get(name='New'))
> +        patch.save()
> +        return patch

Might be good to put a minimal patch in patch.content here.

Cheers,


Jeremy




More information about the Patchwork mailing list