Skip to main content

Factoryboy helpers for Elasticsearch-dsl

Project description

Factoryboy-edsl

Factoryboy helpers for Elasticsearch-dsl documents.

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"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

factoryboy_edsl-0.1.0.tar.gz (3.8 kB view hashes)

Uploaded Source

Built Distribution

factoryboy_edsl-0.1.0-py3-none-any.whl (3.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page