Skip to content

Commit

Permalink
Merge pull request #481 from MongoEngine/extend_tests
Browse files Browse the repository at this point in the history
Restricted: passing arguments on fields definition in WTF compatible mode (Fixes: #379)
  • Loading branch information
insspb committed Jul 20, 2022
2 parents 003675f + 7eabdda commit c1a4edd
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions example_app/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import flask
from flask_debugtoolbar import DebugToolbarExtension
from models import db
from pymongo import monitoring
from views import index, pagination

from example_app.models import db
from example_app.views import index, pagination
from flask_mongoengine.panels import mongo_command_logger

app = flask.Flask("example_app")
Expand Down
5 changes: 2 additions & 3 deletions flask_mongoengine/wtf/db_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,16 @@ class WtfFieldMixin:
number of field parameters, and settings on behalf
of document model form generator for WTForm.
:param args: arguments silently bypassed to normal mongoengine fields
:param validators: wtf model form field validators.
:param filters: wtf model form field filters.
:param kwargs: keyword arguments silently bypassed to normal mongoengine fields
"""

def __init__(self, *args, validators=None, filters=None, **kwargs):
def __init__(self, *, validators=None, filters=None, **kwargs):
self.validators = self._ensure_callable_or_list(validators, "validators")
self.filters = self._ensure_callable_or_list(filters, "filters")

super().__init__(*args, **kwargs)
super().__init__(**kwargs)

def _ensure_callable_or_list(self, field, msg_flag):
"""
Expand Down
7 changes: 4 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
from typing import NoReturn

import mongoengine
import pytest
Expand All @@ -9,7 +10,7 @@


@pytest.fixture(autouse=True, scope="session")
def session_clean_up():
def session_clean_up() -> NoReturn:
"""Mandatory tests environment clean up before/after test session."""
client = MongoClient("localhost", 27017)
client.drop_database("flask_mongoengine_test_db")
Expand All @@ -24,7 +25,7 @@ def session_clean_up():


@pytest.fixture()
def app():
def app() -> Flask:
app = Flask(__name__)
app.config["TESTING"] = True
app.config["WTF_CSRF_ENABLED"] = False
Expand All @@ -36,7 +37,7 @@ def app():


@pytest.fixture()
def db(app):
def db(app) -> MongoEngine:
app.config["MONGODB_HOST"] = "mongodb://localhost:27017/flask_mongoengine_test_db"
test_db = MongoEngine(app)
db_name = (
Expand Down
10 changes: 5 additions & 5 deletions tests/test_debug_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
- Independent of global configuration by design.
"""

import contextlib

import jinja2
Expand All @@ -18,14 +17,15 @@
from pytest_mock import MockerFixture

from flask_mongoengine.panels import (
MongoCommandLogger,
MongoDebugPanel,
_maybe_patch_jinja_loader,
mongo_command_logger,
)


@pytest.fixture()
def app_no_mongo_monitoring():
def app_no_mongo_monitoring() -> Flask:
app = Flask(__name__)
app.config["TESTING"] = True
app.config["WTF_CSRF_ENABLED"] = False
Expand All @@ -39,7 +39,7 @@ def app_no_mongo_monitoring():


@pytest.fixture(autouse=True)
def registered_monitoring():
def registered_monitoring() -> MongoCommandLogger:
"""Register/Unregister mongo_command_logger in required tests"""
monitoring.register(mongo_command_logger)
mongo_command_logger.reset_tracker()
Expand Down Expand Up @@ -69,7 +69,7 @@ class TestMongoDebugPanel:
"""Trivial tests to highlight any unexpected changes in namings or code."""

@pytest.fixture
def toolbar_with_no_flask(self):
def toolbar_with_no_flask(self) -> MongoDebugPanel:
"""Simple instance of MongoDebugPanel without flask application"""
jinja2_env = jinja2.Environment()
return MongoDebugPanel(jinja2_env)
Expand Down Expand Up @@ -186,7 +186,7 @@ class TestMongoCommandLogger:
"""By design tested with raw pymongo."""

@pytest.fixture(autouse=True)
def py_db(self, registered_monitoring):
def py_db(self, registered_monitoring) -> pymongo.MongoClient:
"""Clean up and returns special database for testing on pymongo driver level"""
client = pymongo.MongoClient("localhost", 27017)
db = client.pymongo_test_database
Expand Down
24 changes: 12 additions & 12 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_list_choices_coerce(app, db):
CHOICES = ((1, "blue"), (2, "red"))

class MyChoices(db.Document):
pill = db.ListField(db.IntField(choices=CHOICES))
pill = db.ListField(field=db.IntField(choices=CHOICES))

MyChoicesForm = model_form(MyChoices)
form = MyChoicesForm(MultiDict({"pill": "1"}))
Expand Down Expand Up @@ -88,7 +88,7 @@ class BlogPost(db.Document):
meta = {"allow_inheritance": True}
title = db.StringField(required=True, max_length=200)
posted = db.DateTimeField(default=datetime.datetime.now)
tags = db.ListField(db.StringField())
tags = db.ListField(field=db.StringField())

class TextPost(BlogPost):
email = db.EmailField(required=False)
Expand Down Expand Up @@ -189,7 +189,7 @@ def test_model_form_only(app, db):
class BlogPost(db.Document):
title = db.StringField(required=True, max_length=200)
posted = db.DateTimeField(default=datetime.datetime.now)
tags = db.ListField(db.StringField())
tags = db.ListField(field=db.StringField())

BlogPost.drop_collection()

Expand All @@ -215,7 +215,7 @@ def large_objects(cls, queryset):
return queryset(breed__in=["german sheppard", "wolfhound"])

class DogOwner(db.Document):
dog = db.ReferenceField(Dog)
dog = db.ReferenceField(document_type=Dog)

big_dogs = [Dog(breed="german sheppard"), Dog(breed="wolfhound")]
dogs = [Dog(breed="poodle")] + big_dogs
Expand All @@ -238,7 +238,7 @@ class Dog(db.Document):
name = db.StringField()

class DogOwner(db.Document):
dog = db.ReferenceField(Dog)
dog = db.ReferenceField(document_type=Dog)

DogOwnerForm = model_form(DogOwner, field_args={"dog": {"allow_blank": True}})

Expand Down Expand Up @@ -276,7 +276,7 @@ class Dog(db.Document):
name = db.StringField()

class DogOwner(db.Document):
dogs = db.ListField(db.ReferenceField(Dog))
dogs = db.ListField(field=db.ReferenceField(document_type=Dog))

DogOwnerForm = model_form(DogOwner, field_args={"dogs": {"allow_blank": True}})

Expand Down Expand Up @@ -317,7 +317,7 @@ class Dog(db.Document):
name = db.StringField()

class DogOwner(db.Document):
dogs = db.ListField(db.ReferenceField(Dog))
dogs = db.ListField(field=db.ReferenceField(document_type=Dog))

DogOwnerForm = model_form(DogOwner)

Expand Down Expand Up @@ -402,7 +402,7 @@ def test_sub_field_args(app, db):
with app.test_request_context("/"):

class TestModel(db.Document):
lst = db.ListField(db.StringField())
lst = db.ListField(field=db.StringField())

field_args = {
"lst": {
Expand Down Expand Up @@ -438,7 +438,7 @@ def __unicode__(self):
return self.name

class DogOwner(db.Document):
dogs = db.ListField(db.ReferenceField(Dog))
dogs = db.ListField(field=db.ReferenceField(document_type=Dog))

DogOwnerForm = model_form(DogOwner)

Expand Down Expand Up @@ -508,8 +508,8 @@ class Content(db.EmbeddedDocument):

class Post(db.Document):
title = db.StringField(max_length=120, required=True)
tags = db.ListField(db.StringField(max_length=30))
content = db.EmbeddedDocumentField("Content")
tags = db.ListField(field=db.StringField(max_length=30))
content = db.EmbeddedDocumentField(document_type="Content")

PostForm = model_form(Post)
form = PostForm()
Expand All @@ -524,7 +524,7 @@ class FoodItem(db.Document):

class FoodStore(db.Document):
title = db.StringField(max_length=120, required=True)
food_items = db.ListField(db.ReferenceField(FoodItem))
food_items = db.ListField(field=db.ReferenceField(document_type=FoodItem))

def food_items_label_modifier(obj):
return obj.title
Expand Down

0 comments on commit c1a4edd

Please sign in to comment.