Skip to content

Commit

Permalink
raise exception if upsert is attempted with an invalid attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-satish committed Sep 28, 2015
1 parent 7c585c2 commit 8633308
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions coaster/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ def upsert(cls, name, **fields):
for f in fields:
if hasattr(instance, f):
setattr(instance, f, fields[f])
else:
raise TypeError("'{arg}' is an invalid keyword argument for {instance_type}".format(arg=f, instance_type=instance.__class__.__name__))
else:
instance = cls(name=name, **fields)
cls.query.session.add(instance)
Expand Down Expand Up @@ -269,6 +271,8 @@ def upsert(cls, parent, name, **fields):
for f in fields:
if hasattr(instance, f):
setattr(instance, f, fields[f])
else:
raise TypeError("'{arg}' is an invalid keyword argument for {instance_type}".format(arg=f, instance_type=instance.__class__.__name__))
else:
instance = cls(parent=parent, name=name, **fields)
cls.query.session.add(instance)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ def test_named(self):
self.session.commit()
self.assertEqual(NamedDocument.get(u'hello4'), d4)

with self.assertRaises(TypeError) as insert_error:
NamedDocument.upsert(u'invalid1', title=u'Invalid1', non_existent_field=u"I don't belong here.")
self.assertEqual(TypeError, insert_error.expected)

with self.assertRaises(TypeError) as update_error:
NamedDocument.upsert(u'valid1', title=u'Valid1')
self.session.commit()
NamedDocument.upsert(u'valid1', title=u'Invalid1', non_existent_field=u"I don't belong here.")
self.session.commit()
self.assertEqual(TypeError, update_error.expected)

# TODO: Versions of this test are required for BaseNameMixin,
# BaseScopedNameMixin, BaseIdNameMixin and BaseScopedIdNameMixin
# since they replicate code without sharing it. Only BaseNameMixin
Expand Down Expand Up @@ -253,6 +264,17 @@ def test_scoped_named(self):
self.session.commit()
self.assertEqual(ScopedNamedDocument.get(c2, u'hello5'), d5)

with self.assertRaises(TypeError) as insert_error:
ScopedNamedDocument.upsert(c1, u'invalid1', title=u'Invalid1', non_existent_field=u"I don't belong here.")
self.assertEqual(TypeError, insert_error.expected)

with self.assertRaises(TypeError) as update_error:
ScopedNamedDocument.upsert(c1, u'valid1', title=u'Valid1')
self.session.commit()
ScopedNamedDocument.upsert(c1, u'valid1', title=u'Invalid1', non_existent_field=u"I don't belong here.")
self.session.commit()
self.assertEqual(TypeError, update_error.expected)

def test_scoped_named_short_title(self):
"""Test the short_title method of BaseScopedNameMixin."""
c1 = self.make_container()
Expand Down

0 comments on commit 8633308

Please sign in to comment.