Skip to content

Commit

Permalink
Merge branch 'gnouc-none-fields'
Browse files Browse the repository at this point in the history
Closes #155
Fixes #154
  • Loading branch information
dkellner committed May 20, 2018
2 parents 8666aff + 3f01495 commit 232fa39
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
0.5.1 (unreleased)
~~~~~~~~~~~~~~~~~~

- Return None-values again (#155) [Cuong Manh Le]
- Allow to supply own Flask-SQLAlchemy driver (#86) [fubu]
- Support columns with server_default (#160) [Asif Mahmud Shimon]

Expand Down
49 changes: 49 additions & 0 deletions eve_sqlalchemy/tests/integration/get_none_values.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from sqlalchemy import Column, DateTime, Integer, String, func
from sqlalchemy.ext.declarative import declarative_base

from eve_sqlalchemy.config import DomainConfig, ResourceConfig
from eve_sqlalchemy.tests import TestMinimal

Base = declarative_base()


class BaseModel(Base):
__abstract__ = True
_created = Column(DateTime, default=func.now())
_updated = Column(DateTime, default=func.now(), onupdate=func.now())
_etag = Column(String(40))


class Node(BaseModel):
__tablename__ = 'node'
id = Column(Integer, primary_key=True)
none_field = Column(Integer)


SETTINGS = {
'SQLALCHEMY_DATABASE_URI': 'sqlite:///',
'SQLALCHEMY_TRACK_MODIFICATIONS': False,
'RESOURCE_METHODS': ['GET', 'POST', 'DELETE'],
'ITEM_METHODS': ['GET', 'PATCH', 'DELETE', 'PUT'],
'DOMAIN': DomainConfig({
'nodes': ResourceConfig(Node),
}).render()
}


class TestGetNoneValues(TestMinimal):

def setUp(self, url_converters=None):
super(TestGetNoneValues, self).setUp(SETTINGS, url_converters, Base)

def bulk_insert(self):
self.app.data.insert('nodes', [{'id': k} for k in range(1, 5)])

def test_get_can_return_none_value(self):
response, status = self.get('nodes/1')
self.assert200(status)
self.assertIn('none_field', response)
self.assertIsNone(response['none_field'])
56 changes: 35 additions & 21 deletions eve_sqlalchemy/tests/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,13 @@ def test_patch_objectid(self):
def test_patch_null_objectid(self):
pass

@pytest.mark.xfail(True, run=False, reason='not applicable to SQLAlchemy')
def test_patch_defaults(self):
# Eve assumes that our stored records don't contain the title, even it
# has a default value. We manually remove the title from the first
# record.
with self.app.app_context():
self.app.data.update(self.known_resource, self.item_id,
{'title': None}, None)
super(TestPatch, self).test_patch_defaults()
pass

@pytest.mark.xfail(True, run=False, reason='not applicable to SQLAlchemy')
def test_patch_defaults_with_post_override(self):
# Eve assumes that our stored records don't contain the title, even it
# has a default value. We manually remove the title from the first
# record.
with self.app.app_context():
self.app.data.update(self.known_resource, self.item_id,
{'title': None}, None)
super(TestPatch, self).test_patch_defaults_with_post_override()
pass

@pytest.mark.xfail(True, run=False, reason='not applicable to SQLAlchemy')
def test_patch_write_concern_fail(self):
Expand Down Expand Up @@ -100,13 +90,37 @@ def test_patch_nested_document_nullable_missing(self):
pass

def test_patch_dependent_field_on_origin_document(self):
# Eve assumes that our stored records don't contain the title, even it
# has a default value. We manually remove the title from the first
# record.
with self.app.app_context():
self.app.data.update(self.known_resource, self.item_id,
{'dependency_field1': None}, None)
super(TestPatch, self).test_patch_dependent_field_on_origin_document()
""" Test that when patching a field which is dependent on another and
this other field is not provided with the patch but is still present
on the target document, the patch will be accepted. See #363.
"""
# Eve remove the default-setting on 'dependency_field1', which we
# cannot do easily with SQLAlchemy.
# TODO: Fix directly in Eve and remove this override.

# this will fail as dependent field is missing even in the
# document we are trying to update.
schema = self.domain['contacts']['schema']
schema['dependency_field2']['dependencies'] = \
['dependency_field1_without_default']
changes = {'dependency_field2': 'value'}
r, status = self.patch(self.item_id_url, data=changes,
headers=[('If-Match', self.item_etag)])
self.assert422(status)

# update the stored document by adding dependency field.
changes = {'dependency_field1_without_default': 'value'}
r, status = self.patch(self.item_id_url, data=changes,
headers=[('If-Match', self.item_etag)])
self.assert200(status)

# now the field2 update will be accepted as the dependency field is
# present in the stored document already.
etag = r['_etag']
changes = {'dependency_field2': 'value'}
r, status = self.patch(self.item_id_url, data=changes,
headers=[('If-Match', etag)])
self.assert200(status)

def test_id_field_in_document_fails(self):
# Eve test uses ObjectId as id.
Expand Down
1 change: 1 addition & 0 deletions eve_sqlalchemy/tests/test_sql_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Contacts(CommonColumns):
# id_list_of_dict
# id_list_fixed_len
dependency_field1 = Column(String(25), default='default')
dependency_field1_without_default = Column(String(25))
dependency_field2 = Column(String(25))
dependency_field3 = Column(String(25))
read_only_field = Column(String(25), default='default')
Expand Down
5 changes: 4 additions & 1 deletion eve_sqlalchemy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ def sqla_object_to_dict(obj, fields):
# (may be wrong embedding parameter)
pass

remove_none_values(result)
# We have to remove the ETAG if it's None so Eve will add it later again.
if result.get(config.ETAG, False) is None:
del(result[config.ETAG])

return result


Expand Down

0 comments on commit 232fa39

Please sign in to comment.