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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file factoryboy_edsl-0.1.0.tar.gz.

File metadata

  • Download URL: factoryboy_edsl-0.1.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.6 Linux/6.2.0-1014-azure

File hashes

Hashes for factoryboy_edsl-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3e66546988b70f22c4bfed1a9ea16ecf39ee333a3d3a7e6c068e625d818736a7
MD5 cbf0cb8a7dd6619b69aa8a6fbb1eb0e5
BLAKE2b-256 42e8c2b07c3ddd4b86f23522b35760f0ae07a9705ea7276cfdf1892a3feac416

See more details on using hashes here.

File details

Details for the file factoryboy_edsl-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: factoryboy_edsl-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.6 Linux/6.2.0-1014-azure

File hashes

Hashes for factoryboy_edsl-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ea3eb31925ddeb73292f02835533b0e09d7b3ba29825c792f2cf4daaa7c930c6
MD5 61e19045792663f08622ce299e98394c
BLAKE2b-256 e86f8f73d76e310a611d2b3470246f14fd341aeee1f2d32138ad430464736d43

See more details on using hashes here.

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