Skip to content
This repository has been archived by the owner on Feb 2, 2018. It is now read-only.

Commit

Permalink
Test model class attribute invariants.
Browse files Browse the repository at this point in the history
Make sure required class attributes are populated.
  • Loading branch information
mbarnes authored and ashcrow committed Jun 23, 2017
1 parent 2109ba4 commit 1d277f8
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions test/test_models.py
Expand Up @@ -31,6 +31,45 @@ class TestModel(TestCase):
Tests for the commissaire.models.Model using a subclass.
"""

def test_class_attributes(self):
"""
Verify all model types comply with class attribute invariants
"""
abstract_model_types = (
models.Model,
models.ListModel,
models.SecretModel)
model_types = [mt for mt in models.__dict__.values()
if isinstance(mt, type) and
issubclass(mt, models.Model) and
mt not in abstract_model_types]

for mt in model_types:
name = mt.__name__

# All model types must populate _attribute_map.
self.assertTrue(
mt._attribute_map,
'Class "{}" must specify an attribute map'.format(name))

# ListModel types must populate _list_attr and _list_class.
if issubclass(mt, models.ListModel):
self.assertIsNotNone(
mt._list_attr,
'Class "{}" must specify the list attribute'.format(name))
self.assertIsNotNone(
mt._list_class,
'Class "{}" must specify a list item class'.format(name))

# SecretModel types must populate _key_container and _primary_key.
if issubclass(mt, models.SecretModel):
self.assertIsNotNone(
mt._key_container,
'Class "{}" must specify a key container'.format(name))
self.assertIsNotNone(
mt._primary_key,
'Class "{}" must specify a primary key'.format(name))

def test_new(self):
"""
Verify using new on a model creates a default instance.
Expand Down

0 comments on commit 1d277f8

Please sign in to comment.