Skip to content

Commit

Permalink
Improved coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jssuzanne committed Apr 7, 2021
1 parent 9eefbaa commit 8e27ba0
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 71 deletions.
23 changes: 10 additions & 13 deletions anyblok/bloks/anyblok_core/core/sqlbase.py
Expand Up @@ -51,7 +51,7 @@ def __repr__(self):
value = state.attrs.get(
anyblok_column_prefix + key).loaded_value
else:
continue
continue # pragma: no cover

if value == NO_VALUE:
value = NOT_LOADED_REPR
Expand Down Expand Up @@ -197,7 +197,7 @@ def get_where_clause_from_primary_keys(cls, **pks):
"""
_pks = cls.get_primary_keys()
for pk in _pks:
if pk not in pks:
if pk not in pks: # pragma: no cover
raise SqlBaseException("No primary key %s filled for %r" % (
pk, cls.__registry_name__))

Expand Down Expand Up @@ -226,10 +226,7 @@ def from_primary_keys(cls, **pks):
:rtype: instance of the model
"""
query = cls.query_from_primary_keys(**pks)
if query.count():
return query.first()

return None
return query.one_or_none()

@classmethod
def from_multi_primary_keys(cls, *pks):
Expand Down Expand Up @@ -415,7 +412,7 @@ def to_dict(self, *fields):
field_value, field_property = getattr(self, field), None
try:
field_property = getattr(getattr(cls, field), 'property', None)
except FieldException:
except FieldException: # pragma: no cover
pass

# Deal with this data
Expand Down Expand Up @@ -498,7 +495,7 @@ def find_remote_attribute_to_expire(cls, *fields):
not isinstance(_field, Many2Many) and
'backref' in _field.kwargs):
res.add_in_res(field, [_field.kwargs['backref'][0]])
elif isinstance(_field, FakeRelationShip):
elif isinstance(_field, FakeRelationShip): # pragma: no cover
res.add_in_res(field, [_field.mapper.attribute_name])

return res
Expand All @@ -518,7 +515,7 @@ def find_relationship(cls, *fields):
while _fields:
field = _fields.pop()
if not isinstance(field, str):
field = field.name
field = field.name # pragma: no cover

if field in res:
continue
Expand Down Expand Up @@ -546,7 +543,7 @@ def get_model_information(anyblok, registry_name):
if depend != registry_name:
for x, y in get_model_information(anyblok, depend).items():
if x not in model:
model[x] = y
model[x] = y # pragma: no cover

return model

Expand All @@ -562,7 +559,7 @@ def get_modified_fields(self):
modified_fields = {}
for attr in state.manager.attributes:
if not hasattr(attr.impl, 'get_history'):
continue
continue # pragma: no cover

added, unmodified, deleted = attr.impl.get_history(
state, state.dict)
Expand Down Expand Up @@ -681,7 +678,7 @@ def update(self, byquery=False, flush=False, **values):
setattr(self, x, v)

if flush:
self.anyblok.flush()
self.anyblok.flush() # pragma: no cover

return 1 if values else 0

Expand Down Expand Up @@ -715,7 +712,7 @@ def multi_insert(cls, *args):
"""
instances = cls.anyblok.InstrumentedList()
for kwargs in args:
if not isinstance(kwargs, dict):
if not isinstance(kwargs, dict): # pragma: no cover
raise SqlBaseException("multi_insert method wait list of dict")

instance = cls(**kwargs)
Expand Down
26 changes: 12 additions & 14 deletions anyblok/column.py
Expand Up @@ -306,8 +306,8 @@ def format_encrypt_key(self, registry, namespace):
encrypt_key = Configuration.get('default_encrypt_key')

if not encrypt_key:
raise FieldException("No encrypt_key defined in the "
"configuration")
raise FieldException( # pragma: no cover
"No encrypt_key defined in the configuration")

def wrapper():
"""Return encrypt_key wrapper
Expand Down Expand Up @@ -454,7 +454,7 @@ def setter_format_value(self, value):

def getter_format_value(self, value):
if value is None:
return None
return None # pragma: no cover

if self.encrypt_key:
value = decimal.Decimal(value)
Expand Down Expand Up @@ -537,7 +537,7 @@ def process_result_value(self, value, dialect):

@property
def python_type(self):
return datetime
return datetime # pragma: no cover


class DateTime(Column):
Expand Down Expand Up @@ -777,11 +777,7 @@ class MsSQLPasswordType(PasswordType):
impl = types.VARCHAR(1024)

def load_dialect_impl(self, dialect):
if dialect.name == 'mssql':
# Use a BLOB type for sqlite
return dialect.type_descriptor(types.VARCHAR(self.length))

return super(MsSQLPasswordType, self).load_dialect_impl(dialect)
return dialect.type_descriptor(types.VARCHAR(self.length))


class Password(Column):
Expand Down Expand Up @@ -952,15 +948,15 @@ def __init__(self, selections, size, registry=None, namespace=None):
elif isinstance(selections, (list, tuple)):
self.selections = dict(selections)
else:
raise FieldException(
raise FieldException( # pragma: no cover
"selection wainting 'dict', get %r" % type(selections))

if isinstance(self.selections, dict):
for k in self.selections.keys():
if not isinstance(k, str):
raise FieldException('The key must be a str')
if len(k) > 64:
raise Exception(
raise Exception( # pragma: no cover
'%r is too long %r, waiting max %s or use size arg' % (
k, len(k), size))

Expand Down Expand Up @@ -1301,6 +1297,7 @@ def wrap_default(self, registry, namespace, fieldname, properties):
:return:
"""
if not hasattr(registry, '_need_sequence_to_create_if_not_exist'):
# pragma: no cover
registry._need_sequence_to_create_if_not_exist = []
elif registry._need_sequence_to_create_if_not_exist is None:
registry._need_sequence_to_create_if_not_exist = []
Expand Down Expand Up @@ -1525,7 +1522,8 @@ def setter_format_value(self, value):
"""
if value is not None:
return value.lower()
return value

return value # pragma: no cover


class CountryType(types.TypeDecorator, ScalarCoercible):
Expand All @@ -1544,7 +1542,7 @@ def process_result_value(self, value, dialect):
if value:
return pycountry.countries.get(alpha_3=value)

return value
return value # pragma: no cover

def _coerce(self, value):
if value is not None and not isinstance(value, self.python_type):
Expand Down Expand Up @@ -1574,7 +1572,7 @@ class Test:
def __init__(self, mode='alpha_2', *args, **kwargs):
self.mode = mode
if pycountry is None:
raise FieldException(
raise FieldException( # pragma: no cover
"'pycountry' package is required for use 'CountryType'")

self.choices = {getattr(country, mode): country.name
Expand Down
26 changes: 13 additions & 13 deletions anyblok/config.py
Expand Up @@ -489,7 +489,7 @@ def set(cls, opt, value):
cls.configuration[opt].set(value)
else:
cls.add_argument(opt, value, type(value))
except Exception as e:
except Exception as e: # pragma: no cover
logger.exception("Error while setting the value %r to the option "
"%r" % (value, opt))
raise e
Expand Down Expand Up @@ -587,7 +587,7 @@ def load_config_for_test(cls):
join(ad.user_config_dir, 'conf.cfg'), False)
configfile = cls.get('configfile')
if configfile:
cls.parse_configfile(configfile, True)
cls.parse_configfile(configfile, True) # pragma: no cover

@classmethod
@log(logger, level='debug')
Expand All @@ -605,7 +605,7 @@ def load(cls, application, useseparator=False, **kwargs):
if application in cls.applications:
description.update(cls.applications[application])
else:
description.update(cls.applications['default'])
description.update(cls.applications['default']) # pragma: no cover

description.update(kwargs)
configuration_groups = description.pop(
Expand All @@ -614,7 +614,7 @@ def load(cls, application, useseparator=False, **kwargs):
if 'plugins' not in configuration_groups:
configuration_groups.append('plugins')

if useseparator:
if useseparator: # pragma: no cover
parser = getParser(**description)

try:
Expand All @@ -640,7 +640,7 @@ def _load(cls, parser, configuration_groups):
:param useseparator: boolean(default False)
"""
if configuration_groups is None:
return
return # pragma: no cover

for group in cls.groups:
if group not in configuration_groups:
Expand Down Expand Up @@ -673,13 +673,13 @@ def initialize_logging(cls):
logging_level_qualnames = cls.get('logging_level_qualnames')

if logging_configfile:
config.fileConfig(logging_configfile)
config.fileConfig(logging_configfile) # pragma: no cover
elif json_logging_configfile:
with open(json_logging_configfile, 'rt') as f:
with open(json_logging_configfile, 'rt') as f: # pragma: no cover
configfile = json.load(f.read())
config.dictConfig(configfile)
elif yaml_logging_configfile:
with open(yaml_logging_configfile, 'rt') as f:
with open(yaml_logging_configfile, 'rt') as f: # pragma: no cover
configfile = yaml.load(f.read())
config.dictConfig(configfile)

Expand All @@ -690,11 +690,11 @@ def initialize_logging(cls):
for x in Logger.manager.loggerDict.keys()
if x in logging_level_qualnames)
else:
qualnames = set(x.split('.')[0]
qualnames = set(x.split('.')[0] # pragma: no cover
for x in Logger.manager.loggerDict.keys())

for qualname in qualnames:
getLogger(qualname).setLevel(level)
getLogger(qualname).setLevel(level) # pragma: no cover

@classmethod
def parse_configfile(cls, configfile, required):
Expand All @@ -709,7 +709,7 @@ def parse_configfile(cls, configfile, required):
print('Loading config file %r' % configfile)
if not isfile(configfile):
if required:
raise ConfigurationException(
raise ConfigurationException( # pragma: no cover
"No such file or not a regular file: %r " % configfile)

return
Expand All @@ -725,7 +725,7 @@ def parse_configfile(cls, configfile, required):
for opt, value in sections:
if opt in ('logging_configfile', 'json_logging_configfile',
'yaml_logging_configfile'):
if value:
if value: # pragma: no cover
value = os.path.abspath(value)

configuration[opt] = value
Expand Down Expand Up @@ -766,7 +766,7 @@ def parse_options(cls, arguments):
cls.set(opt, value)

if 'logging_level' in cls.configuration:
cls.initialize_logging()
cls.initialize_logging() # pragma: no cover


@Configuration.add('plugins', label='Plugins',
Expand Down

0 comments on commit 8e27ba0

Please sign in to comment.