Factory Squirrel

2010-10-27 Thread Phlip
Djangoes:

RoR has a fixture system called Factory Girl, which I suspect
constructs objects in native Ruby, not in JSON/YAML.

If nobody ports it to Django I would; under the name "Factory
Squirrel". This post is about the first method I'd add to such a
module.

Another kewt RoR fixture feature is the accessor. Given a table Order
and orders.yml, you can write order(:able) to fetch that Order record
from the sample database.

I miss that feature, so I wrote one better in Django:

class FactorySquirrel:

def __getattr__(self, attr):
sought = re.search(r'^(.*)_([^_]+$)', attr)

if sought:
record_name, model_name = sought.groups()
model = None
try:
model = eval(model_name.capitalize())
except NameError: pass
if model:
record = model.objects.get(name=attr)
setattr(self, attr, record)  #  So we needn't do it
again.
return record
raise AttributeError("%r object has no attribute %r" %
 (type(self).__name__, attr))

Here's how it works. Suppose you have tables Order and User, and they
have 'name' fields with 'kozmik' and 'bullfrog', respectively.

In your test, to fetch a record, you just name it:

   def test_kozmik_bullfrog(self):
   print self.kozmik_order  #  reads the fixture database here
   print self.bullfrog_user
   print self.kozmik_order  #  does not re-read the record

>From here, to be more useful, we need to think of details like records
without names (shameful!). Models with CamelCase already work -
kozmik_LineItem.

Any ideas how to improve this towards a true Squirrel?

--
  Phlip
  http://zeekland.zeroplayer.com/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Factory Squirrel

2010-10-27 Thread Łukasz Rekucki
On 27 October 2010 22:17, Phlip  wrote:
> Djangoes:
>
> RoR has a fixture system called Factory Girl, which I suspect
> constructs objects in native Ruby, not in JSON/YAML.
>
> If nobody ports it to Django I would; under the name "Factory
> Squirrel".

http://github.com/dnerdy/factory_boy

Also, there is a more general python solution:
http://farmdev.com/projects/fixture/ that supports django. You should
probably check it out for inspiration before writing a one from
scratch.

-- 
Łukasz Rekucki

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Factory Squirrel

2010-10-28 Thread Phlip
Tx! But!

> http://github.com/dnerdy/factory_boy

How does this do aggregation?

> Also, there is a more general python 
> solution:http://farmdev.com/projects/fixture/ that supports django.

That one's a little _too_ general. But it supports aggregation
(association, etc.)..

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.