2013-01-15 11 views
5

Sto provando a utilizzare factory_boy per aiutare a generare alcuni documenti MongoEngine per i miei test. Ho problemi a definire gli oggetti EmbeddedDocumentField.Come si usa factory_boy per modellare un EmbeddedDocument MongoEngine?

Ecco la mia MongoEngine Document:

class Comment(EmbeddedDocument): 
    content = StringField() 
    name = StringField(max_length=120) 

class Post(Document): 
    title = StringField(required=True) 
    tags = ListField(StringField(), required=True) 
    comments = ListField(EmbeddedDocumentField(Comment)) 

Ecco la mia parzialmente completata factory_boy Factory:

class CommentFactory(factory.Factory): 
    FACTORY_FOR = Comment 
    content = "Platinum coins worth a trillion dollars are great" 
    name = "John Doe" 

class BlogFactory(factory.Factory): 
    FACTORY_FOR = Blog 
    title = "On Using MongoEngine with factory_boy" 
    tags = ['python', 'mongoengine', 'factory-boy', 'django'] 
    comments = [factory.SubFactory(CommentFactory)] # this doesn't work 

Delle idee come specificare il campo comments? Il problema è che factory-boy tenta di creare il documento Comment EmbeddedDocument.

risposta

2

Il modo in cui lo sto facendo adesso è impedire la costruzione delle fabbriche basate su EmbeddedDocuments. Così, ho l'installazione di un EmbeddedDocumentFactory, in questo modo:

class EmbeddedDocumentFactory(factory.Factory): 

    ABSTRACT_FACTORY = True 

    @classmethod 
    def _prepare(cls, create, **kwargs):           
     return super(EmbeddedDocumentFactory, cls)._prepare(False, **kwargs) 

Poi ho ereditato da quello di creare fabbriche per EmbeddedDocuments:

class CommentFactory(EmbeddedDocumentFactory): 

    FACTORY_FOR = Comment 

    content = "Platinum coins worth a trillion dollars are great" 
    name = "John Doe" 

questo potrebbe non essere la soluzione migliore, quindi mi attendere che qualcun altro risponda prima di accettare questo come risposta.

3

io non sono sicuro se questo è ciò che si vuole, ma ho appena iniziato a guardare questo problema e questo sembra funzionare:

from mongoengine import EmbeddedDocument, Document, StringField, ListField, EmbeddedDocumentField 
import factory 

class Comment(EmbeddedDocument): 
    content = StringField() 
    name = StringField(max_length=120) 

class Post(Document): 
    title = StringField(required=True) 
    tags = ListField(StringField(), required=True) 
    comments = ListField(EmbeddedDocumentField(Comment)) 


class CommentFactory(factory.Factory): 
    FACTORY_FOR = Comment 
    content = "Platinum coins worth a trillion dollars are great" 
    name = "John Doe" 

class PostFactory(factory.Factory): 
    FACTORY_FOR = Post 
    title = "On Using MongoEngine with factory_boy" 
    tags = ['python', 'mongoengine', 'factory-boy', 'django'] 
    comments = factory.LazyAttribute(lambda a: [CommentFactory()]) 

>>> b = PostFactory() 
>>> b.comments[0].content 
'Platinum coins worth a trillion dollars are great' 

non sarei sorpreso se mi manca qualcosa però.

+0

È necessario includere EmbeddedDocumentFactory come il primo ragazzo ha incluso per creare la fabbrica per la fabbrica di documenti incorporati quando inizializziamo la fabbrica principale. –