[TIP] I need a DataSet fixture that points into its own table

Phlip phlip2005 at gmail.com
Mon Dec 27 14:50:36 PST 2010


> TreeData.leaf_one.owner = TreeData.branch
> TreeData.leaf_two.owner = TreeData.branch

Thanks, I thought I tried that.

> Mark S.

And, just to provide some closure, I have since replaced fixture with
a hand-made SQL (another incarnation of my "FactorySquirrel"
sketches!), that I hope to write-up soon.

It is 100 times faster, literally, because it does not fully rez
Django models and call .save() on them. It just pushes everyone into
the DB using INSERT.

I declare the fixtures like this:

( h('species_branch'),
                'branch',
                'species_branch',
                h('toby_cat_package'),
                None ),

    ( h('species_branch_leaves'),
                'leaves',
                None,
                h('species_branch'),
                None ),

        ( h('rat_leaf'),
                'leaf',
                'rat_leaf',
                h('species_branch_leaves'),
                'Rat' ),

        ( h('parrotlet_leaf'),
                'leaf',
                'parrotlet_leaf',
                h('species_branch_leaves'),
                'Parrotlet' ),

            ( h('parrotlet_leaf_correct'),
                '@*',
                'correct',
                h('parrotlet_leaf'),
                'true' ),

        ( h('minilop_leaf'),
                'leaf',
                'minilop_leaf',
                h('species_branch_leaves'),
                'Minilop' ),

Those are simply five database fields in a row - id, name, type,
owner_id, and payload.

The h() one-way encrypts a string as a short integer. (I tried to use
hash(), but MySQL kacked on the results. Don't know why; yes I tried
shifting out any overflow.)

That way if we see the h('species_branch'), first, we calculate its id
and INSERT it, and if a leaf needed a owner_id of h('species_branch')
first, we could have calculated and INSERTed that, too.

This gives me, without JSON, as close as I can get to how Ruby on
Rails does it. It inserts raw SQL, very rapidly, without wasting time
creating Django model objects. And it can links IDs without seeing
their records first, by hashing their names.

-- 
  Phlip
  http://c2.com/cgi/wiki?ZeekLand



More information about the testing-in-python mailing list