adding warning when keyspaces are undefined. Since a default keyspace can be set on the setup function, we can't throw an exception, because it may not have been called by the time the model class is created
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
from collections import OrderedDict
|
||||
import re
|
||||
import warnings
|
||||
|
||||
from cqlengine import columns
|
||||
from cqlengine.exceptions import ModelException, CQLEngineException, ValidationError
|
||||
from cqlengine.query import ModelQuerySet, DMLQuery, AbstractQueryableColumn
|
||||
@@ -13,6 +15,9 @@ class PolyMorphicModelException(ModelException): pass
|
||||
|
||||
DEFAULT_KEYSPACE = None
|
||||
|
||||
class UndefinedKeyspaceWarning(Warning):
|
||||
pass
|
||||
|
||||
|
||||
class hybrid_classmethod(object):
|
||||
"""
|
||||
@@ -756,6 +761,17 @@ class ModelMetaClass(type):
|
||||
|
||||
#create the class and add a QuerySet to it
|
||||
klass = super(ModelMetaClass, cls).__new__(cls, name, bases, attrs)
|
||||
|
||||
if not klass.__abstract__ and klass.__keyspace__ is None:
|
||||
warnings.warn(
|
||||
"No keyspace defined on {}.{}\n"
|
||||
" The default keyspace 'cqlengine' was removed in 0.16.\n"
|
||||
" Please define a keyspace on the '__keyspace__' model class attribute\n"
|
||||
" or set a default keyspace with management.setup function\n"
|
||||
.format(klass.__module__, klass.__name__),
|
||||
UndefinedKeyspaceWarning
|
||||
)
|
||||
|
||||
return klass
|
||||
|
||||
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
from uuid import uuid4
|
||||
import warnings
|
||||
from cqlengine.query import QueryException, ModelQuerySet, DMLQuery
|
||||
from cqlengine.tests.base import BaseCassEngTestCase
|
||||
|
||||
from cqlengine.exceptions import ModelException, CQLEngineException
|
||||
from cqlengine.models import Model, ModelDefinitionException, ColumnQueryEvaluator
|
||||
from cqlengine.models import Model, ModelDefinitionException, ColumnQueryEvaluator, UndefinedKeyspaceWarning
|
||||
from cqlengine import columns
|
||||
import cqlengine
|
||||
|
||||
@@ -208,6 +209,23 @@ class TestModelClassFunction(BaseCassEngTestCase):
|
||||
except Exception:
|
||||
assert False, "Model2 exception should not be caught by Model1"
|
||||
|
||||
def test_no_keyspace_warning(self):
|
||||
with warnings.catch_warnings(record=True) as warn:
|
||||
class NoKeyspace(Model):
|
||||
key = columns.UUID(primary_key=True)
|
||||
|
||||
self.assertEqual(len(warn), 1)
|
||||
warn_message = warn[0]
|
||||
self.assertEqual(warn_message.category, UndefinedKeyspaceWarning)
|
||||
|
||||
def test_abstract_model_keyspace_warning_is_skipped(self):
|
||||
with warnings.catch_warnings(record=True) as warn:
|
||||
class NoKeyspace(Model):
|
||||
__abstract__ = True
|
||||
key = columns.UUID(primary_key=True)
|
||||
|
||||
self.assertEqual(len(warn), 0)
|
||||
|
||||
class TestManualTableNaming(BaseCassEngTestCase):
|
||||
|
||||
class RenamedTest(cqlengine.Model):
|
||||
@@ -223,6 +241,7 @@ class TestManualTableNaming(BaseCassEngTestCase):
|
||||
|
||||
class AbstractModel(Model):
|
||||
__abstract__ = True
|
||||
__keyspace__ = 'test'
|
||||
|
||||
class ConcreteModel(AbstractModel):
|
||||
pkey = columns.Integer(primary_key=True)
|
||||
|
||||
Reference in New Issue
Block a user