Skip to content

Factoryboy helpers for Elasticsearch-dsl documents

License

Notifications You must be signed in to change notification settings

etchegom/factoryboy-edsl

Repository files navigation

Factoryboy-edsl

Factoryboy helpers for Elasticsearch-dsl documents.

PyPI version Generic badge

Installation

pip install factoryboy-edsl

Usage

Simple usage:

from factory_edsl import EDSLDocumentFactory, EDSLInnerDocFactory
from elasticsearch_dsl import Document, InnerDoc

# Define elasticsearch-dsl documents

class CommentDocument(InnerDoc):
    author = Text()
    content = Text()

class PostDocument(Document):
    title = Text()
    comments = Nested(CommentDocument)

    class Index:
        name = "index_name"

# Define factories

class CommentInnerDocFactory(EDSLInnerDocFactory):
    class Meta:
        model = CommentDocument

    author = factory.Faker("name")
    content = factory.Faker("sentence", nb_words=4)


class PostDocumentFactory(EDSLDocumentFactory):
    class Meta:
        model = PostDocument

    title = factory.Faker("sentence", nb_words=4)

# Build your test data

comments = CommentInnerDocFactory.create_batch(10)
post = PostDocumentFactory(comments=comments)
print(post.to_dict())

The factory-boy strategies are applied:

post = PostDocumentFactory.build() # in-memory only
post = PostDocumentFactory.create() # write to elastic cluster

Use meta_id arg to update an existing document:

post = PostDocumentFactory(meta_id="999")
assert post.meta.id == "999"
assert PostDocument.exists(id="999")

post = PostDocumentFactory(meta_id="999", title="hello")
assert PostDocument.get(id="999").title == "hello"

Set option strip_unknown_fields to False to allow extra fields:

class PostDocumentFactory(EDSLDocumentFactory):
    class Meta:
        model = PostDocument
        strip_unknown_fields = False

post = PostDocumentFactory(title="hello", extra_field="extra")

assert doc.title == "hello"
assert doc.extra_field == "extra"

About

Factoryboy helpers for Elasticsearch-dsl documents

Resources

License

Stars

Watchers

Forks

Packages

No packages published