Skip to content

Commit

Permalink
Merge pull request #12 from Nobatek/devDF_fields_email_url
Browse files Browse the repository at this point in the history
Conversion fields: update mongoengine email and url fields to marshmallow email and url fields
  • Loading branch information
touilleMan committed Jun 28, 2016
2 parents 7500443 + 641cd98 commit 4090196
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
4 changes: 2 additions & 2 deletions marshmallow_mongoengine/conversion/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class Builder(MetaFieldBuilder):
available_params=(params.SizeParam, params.PrecisionParam))
register_field(me.fields.DictField, ma_fields.Raw)
register_field(me.fields.DynamicField, ma_fields.Raw)
register_field(me.fields.EmailField, ma_fields.String,
register_field(me.fields.EmailField, ma_fields.Email,
available_params=(params.LenghtParam,))
register_field(me.fields.FloatField, ma_fields.Float,
available_params=(params.SizeParam,))
Expand All @@ -186,7 +186,7 @@ class Builder(MetaFieldBuilder):
available_params=(params.SizeParam,)) # TODO: handle value_decorator
register_field(me.fields.StringField, ma_fields.String,
available_params=(params.LenghtParam,))
register_field(me.fields.URLField, ma_fields.String,
register_field(me.fields.URLField, ma_fields.URL,
available_params=(params.LenghtParam,))
register_field_builder(me.fields.EmbeddedDocumentField, EmbeddedDocumentBuilder)
register_field_builder(me.fields.ListField, ListBuilder)
Expand Down
27 changes: 24 additions & 3 deletions tests/test_marshmallow_mongoengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class Student(me.Document):
help_text='date the student was created')
current_school = me.ReferenceField('School')
courses = me.ListField(me.ReferenceField('Course'))
email = me.EmailField(max_length=100)
profile_uri = me.URLField(max_length=200)

# So that we can access models with dot-notation, e.g. models.Course
class _models(object):
Expand Down Expand Up @@ -117,6 +119,8 @@ def test_fields_for_model_types(self, models):
assert type(fields_['dob']) is fields.DateTime
assert type(fields_['current_school']) is fields.Reference
assert type(fields_['date_created']) is fields.DateTime
assert type(fields_['email']) is fields.Email
assert type(fields_['profile_uri']) is fields.URL

def test_dict_field(self, models):
fields_ = fields_for_model(models.Course)
Expand All @@ -140,6 +144,12 @@ def test_length_validator_set(self, models):
validator = contains_validator(fields_['full_name'], validate.Length)
assert validator
assert validator.max == 255
validator = contains_validator(fields_['email'], validate.Length)
assert validator
assert validator.max == 100
validator = contains_validator(fields_['profile_uri'], validate.Length)
assert validator
assert validator.max == 200
validator = contains_validator(fields_['age'], validate.Range)
assert validator
assert validator.max == 99
Expand Down Expand Up @@ -197,7 +207,9 @@ def student(self, models, school):
full_name='Monty Python',
age=10,
dob=datetime.utcnow(),
current_school=school).save()
current_school=school,
email='terry.gilliam@montypython.com',
profile_uri='http://www.imdb.com/name/nm0000416/').save()
return student_

@pytest.fixture
Expand Down Expand Up @@ -244,14 +256,17 @@ def test_model_schema_bad_loading(self, models, schemas, school):
schema = schemas.StudentSchema()
default_payload = {'full_name': 'John Doe', 'age': 25, 'dob': None,
'date_created': datetime.utcnow().isoformat(),
'current_school': school.pk}
'current_school': school.pk,
'email': 'john@doe.com',
'profile_uri': 'http://www.perdu.com/'}
# Make sure default_payload is valid
result = schema.load(default_payload)
assert not result.errors
for key, value in (
('current_school', 'not an objectId'), ('current_school', None),
('current_school', '5578726b7a58012298a5a7e2'),
('age', 100), ('age', 'nan'),
('email', 'johndoe@badmail'), ('profile_uri', 'bad_uri')
):
payload = default_payload.copy()
payload[key] = value
Expand Down Expand Up @@ -325,6 +340,8 @@ class Meta:
assert 'dob' not in data
assert 'age' not in data
assert 'id' not in data
assert 'email' not in data
assert 'profile_uri' not in data
assert len(data.keys()) == 2

def test_exclude_option(self, student, models):
Expand All @@ -341,6 +358,8 @@ class Meta:
assert 'id' in data
assert 'age' in data
assert 'dob' in data
assert 'email' in data
assert 'profile_uri' in data
assert 'date_created' not in data

def test_additional_option(self, student, models):
Expand Down Expand Up @@ -429,5 +448,7 @@ class Meta:
'dob': None,
'age': None,
'courses': [],
'date_created': '2016-02-14T00:00:00+00:00'
'date_created': '2016-02-14T00:00:00+00:00',
'email': None,
'profile_uri': None
}

0 comments on commit 4090196

Please sign in to comment.