Skip to content
Snippets Groups Projects
Commit ff5ae2ce authored by roadscape's avatar roadscape
Browse files

utils tests

parent 799cac91
No related branches found
No related tags found
No related merge requests found
......@@ -5,21 +5,7 @@ import configargparse
from hive.steem.client import SteemClient
from hive.db.adapter import Db
def strtobool(val):
"""Convert a booleany str to a bool.
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
'val' is anything else.
"""
val = val.lower()
if val in ('y', 'yes', 't', 'true', 'on', '1'):
return True
elif val in ('n', 'no', 'f', 'false', 'off', '0'):
return False
else:
raise ValueError("not booleany: %r" % (val,))
from hive.utils.normalize import strtobool
class Conf():
""" Manages sync/server configuration via args, ENVs, and hive.conf. """
......
......@@ -140,3 +140,18 @@ def safe_img_url(url, max_size=1024):
and url[0:4] == 'http'):
return url.strip()
return None
def strtobool(val):
"""Convert a booleany str to a bool.
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
'val' is anything else.
"""
val = val.lower()
if val in ('y', 'yes', 't', 'true', 'on', '1'):
return True
elif val in ('n', 'no', 'f', 'false', 'off', '0'):
return False
else:
raise ValueError("not booleany: %r" % (val,))
......@@ -5,9 +5,9 @@ import resource
USE_COLOR = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
def colorize(string, color='93'):
def colorize(string, color='93', force=False):
"""Colorizes a string for stdout, if attached to terminal"""
if not USE_COLOR:
if not USE_COLOR and not force:
return string
return "\033[%sm%s\033[0m" % (color, string)
......
#pylint: disable=missing-docstring
import pytest
from datetime import datetime
from decimal import Decimal
......@@ -18,6 +20,7 @@ from hive.utils.normalize import (
rep_log10,
safe_img_url,
secs_to_str,
strtobool,
)
def test_secs_to_str():
......@@ -85,3 +88,20 @@ def test_safe_img_url():
max_size = len(url) + 1
assert safe_img_url(url, max_size) == url
assert safe_img_url(url + 'x', max_size) is None
def test_strtobool():
assert strtobool('t') == True
assert strtobool('T') == True
assert strtobool('1') == True
assert strtobool('true') == True
assert strtobool('yes') == True
assert strtobool('f') == False
assert strtobool('F') == False
assert strtobool('0') == False
assert strtobool('false') == False
assert strtobool('n') == False
assert strtobool('no') == False
with pytest.raises(ValueError):
strtobool('foo')
#pylint: disable=missing-docstring,expression-not-assigned
from hive.utils.profiler import Profiler
def test_profiler():
p = Profiler('.tmp.test-prof')
with p:
[i for i in range(100000)]
p.save()
p.echo()
def test_profiler_passthru():
p = Profiler(None)
with p:
[i for i in range(100000)]
p.echo()
#pylint: disable=missing-docstring
from hive.utils.system import (
colorize,
peak_usage_mb,
)
def test_colorize():
plain = 'teststr'
colored = '\x1b[93mteststr\x1b[0m'
assert colorize(plain, color='93') in [plain, colored]
assert colorize(plain, color='93', force=True) == colored
def test_peak_usage_mb():
assert peak_usage_mb() > 1
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment