Use s to convert from bytes to str everywhere

This commit is contained in:
2015-12-24 12:43:28 +05:30
parent 288ef9e7ef
commit b735e4fe5c
2 changed files with 41 additions and 14 deletions
+37 -11
View File
@@ -37,6 +37,32 @@ command_status_gui_gen = "pytest_gui_status \"{norm_dir_name}\""
# make bulk changes with pipeline # make bulk changes with pipeline
def s(input_):
''' Convert str or uncode or bytes to str.
If list, do it for all of them.
If others, return as is. '''
if hasattr(input_, "__iter__"):
return [s(ele) for ele in input_]
try:
assert(type(input_) in [str, unicode, bytes])
except AssertionError:
return input_
import sys
PY3 = sys.version_info > (3,)
if PY3:
if type(input_) == bytes:
str_ = bytes.decode(input_)
return str_
# either str or unicode
str_ = str(input_)
return str_
class Helpers(object): class Helpers(object):
@staticmethod @staticmethod
@@ -59,12 +85,12 @@ class Helpers(object):
else: else:
print("Redis couldnt start there, trying to check if already running on that port") print("Redis couldnt start there, trying to check if already running on that port")
redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0)
if redis_db.ping() and redis_db.get("PYTEST_STATUS_DB") == "1": if redis_db.ping() and s(redis_db.get("PYTEST_STATUS_DB")) == "1":
print("Yup, it was already running") print("Yup, it was already running")
else: else:
print("** Not found existing redis, couldnt connect, check! **") print("** Not found existing redis, couldnt connect, check! **")
print("debug pinging... {ping_result}".format(ping_result=redis_db.ping())) print("debug pinging... {ping_result}".format(ping_result=redis_db.ping()))
print("debug PYTEST_STATUS_DB ==... {0}".format(redis_db.get("PYTEST_STATUS_DB"))) print("debug PYTEST_STATUS_DB ==... {0}".format(s(redis_db.get("PYTEST_STATUS_DB"))))
sys.exit() sys.exit()
hash_dir_name = hash(dir_name) hash_dir_name = hash(dir_name)
@@ -90,10 +116,10 @@ class Helpers(object):
''' '''
# craete redis connection # craete redis connection
redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0)
hash_dir_name = redis_db.hget("directories_to_hash", dir_name) hash_dir_name = s(redis_db.hget("directories_to_hash", dir_name))
assert hash_dir_name is not None assert hash_dir_name is not None
existing_gui_pid = redis_db.get("{hash_a}_gui_pid".format(hash_a=hash_dir_name)) existing_gui_pid = s(redis_db.get("{hash_a}_gui_pid".format(hash_a=hash_dir_name)))
# can be string or None. If string, make it int # can be string or None. If string, make it int
if existing_gui_pid: if existing_gui_pid:
existing_gui_pid = int(existing_gui_pid) existing_gui_pid = int(existing_gui_pid)
@@ -109,7 +135,7 @@ class Helpers(object):
def on_collectstart(dir_name): def on_collectstart(dir_name):
# craete redis connection # craete redis connection
redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0)
hash_dir_name = redis_db.hget("directories_to_hash", dir_name) hash_dir_name = s(redis_db.hget("directories_to_hash", dir_name))
assert hash_dir_name is not None assert hash_dir_name is not None
redis_db.set("{hash_a}_state".format(hash_a=hash_dir_name), "collect") redis_db.set("{hash_a}_state".format(hash_a=hash_dir_name), "collect")
@@ -121,7 +147,7 @@ class Helpers(object):
def on_collectend(dir_name, list_test_name): def on_collectend(dir_name, list_test_name):
# craete redis connection # craete redis connection
redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0)
hash_dir_name = redis_db.hget("directories_to_hash", dir_name) hash_dir_name = s(redis_db.hget("directories_to_hash", dir_name))
assert hash_dir_name is not None assert hash_dir_name is not None
redis_db.rpush("{hash_a}_collect".format(hash_a=hash_dir_name), *list_test_name) redis_db.rpush("{hash_a}_collect".format(hash_a=hash_dir_name), *list_test_name)
@@ -133,7 +159,7 @@ class Helpers(object):
def on_test_eachstart(dir_name): def on_test_eachstart(dir_name):
# craete redis connection # craete redis connection
redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0)
hash_dir_name = redis_db.hget("directories_to_hash", dir_name) hash_dir_name = s(redis_db.hget("directories_to_hash", dir_name))
assert hash_dir_name is not None assert hash_dir_name is not None
redis_db.set("{hash_a}_state".format(hash_a=hash_dir_name), "runtest") redis_db.set("{hash_a}_state".format(hash_a=hash_dir_name), "runtest")
@@ -145,7 +171,7 @@ class Helpers(object):
def on_test_eachend(dir_name, list_test_result): def on_test_eachend(dir_name, list_test_result):
# craete redis connection # craete redis connection
redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0)
hash_dir_name = redis_db.hget("directories_to_hash", dir_name) hash_dir_name = s(redis_db.hget("directories_to_hash", dir_name))
assert hash_dir_name is not None assert hash_dir_name is not None
list_testname_pass = [test_result.nodeid for test_result in list_testname_pass = [test_result.nodeid for test_result in
@@ -175,7 +201,7 @@ class Helpers(object):
def on_end(dir_name): def on_end(dir_name):
# craete redis connection # craete redis connection
redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0)
hash_dir_name = redis_db.hget("directories_to_hash", dir_name) hash_dir_name = s(redis_db.hget("directories_to_hash", dir_name))
assert hash_dir_name is not None assert hash_dir_name is not None
redis_db.set("{hash_a}_state".format(hash_a=hash_dir_name), "end") redis_db.set("{hash_a}_state".format(hash_a=hash_dir_name), "end")
@@ -187,7 +213,7 @@ class Helpers(object):
def on_start_reset(dir_name): def on_start_reset(dir_name):
redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0)
hash_dir_name = redis_db.hget("directories_to_hash", dir_name) hash_dir_name = s(redis_db.hget("directories_to_hash", dir_name))
assert hash_dir_name is not None assert hash_dir_name is not None
list_gen_varname = ["{hash_a}_state", "{hash_a}_last_updated", "{hash_a}_collect", "{hash_a}_pass", "{hash_a}_fail", "{hash_a}_skip"] list_gen_varname = ["{hash_a}_state", "{hash_a}_last_updated", "{hash_a}_collect", "{hash_a}_pass", "{hash_a}_fail", "{hash_a}_skip"]
@@ -201,7 +227,7 @@ class Helpers(object):
# update_last_updated in redis. If pipe provided, queue the command instead. # update_last_updated in redis. If pipe provided, queue the command instead.
redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0)
hash_dir_name = redis_db.hget("directories_to_hash", dir_name) hash_dir_name = s(redis_db.hget("directories_to_hash", dir_name))
assert hash_dir_name is not None assert hash_dir_name is not None
cur_iso_datetime = datetime.datetime.now().isoformat() cur_iso_datetime = datetime.datetime.now().isoformat()
+4 -3
View File
@@ -8,6 +8,7 @@ import tempfile
import shutil import shutil
REDIS_TEST_PORT = status_plugin.REDIS_PORT + 1 REDIS_TEST_PORT = status_plugin.REDIS_PORT + 1
s = status_plugin.s
class patched_chdir(object): class patched_chdir(object):
@@ -56,13 +57,13 @@ def test_whole_1(tmpdir):
dir_name = os.getcwd() dir_name = os.getcwd()
assert (dir_name == path_case) assert (dir_name == path_case)
redis_db = redis.StrictRedis(host='localhost', port=REDIS_TEST_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_TEST_PORT, db=0)
hash_dir_name = redis_db.hget("directories_to_hash", dir_name) hash_dir_name = s(redis_db.hget("directories_to_hash", dir_name))
assert (redis_db.llen("{hash_a}_pass".format(hash_a=hash_dir_name)) == 1) assert (redis_db.llen("{hash_a}_pass".format(hash_a=hash_dir_name)) == 1)
assert (redis_db.llen("{hash_a}_fail".format(hash_a=hash_dir_name)) == 1) assert (redis_db.llen("{hash_a}_fail".format(hash_a=hash_dir_name)) == 1)
assert (redis_db.llen("{hash_a}_skip".format(hash_a=hash_dir_name)) == 0) assert (redis_db.llen("{hash_a}_skip".format(hash_a=hash_dir_name)) == 0)
collected_tests = redis_db.lrange("{hash_a}_collect".format(hash_a=hash_dir_name), 0, -1) collected_tests = s(redis_db.lrange("{hash_a}_collect".format(hash_a=hash_dir_name), 0, -1))
assert (collected_tests == ["test_1.py::test_pass", "test_1.py::test_fail"]) assert (collected_tests == ["test_1.py::test_pass", "test_1.py::test_fail"])
@@ -84,7 +85,7 @@ def test_whole_2(tmpdir):
dir_name = path_case dir_name = path_case
redis_db = redis.StrictRedis(host='localhost', port=REDIS_TEST_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_TEST_PORT, db=0)
hash_dir_name = redis_db.hget("directories_to_hash", dir_name) hash_dir_name = s(redis_db.hget("directories_to_hash", dir_name))
assert (redis_db.llen("{hash_a}_pass".format(hash_a=hash_dir_name)) == 1) assert (redis_db.llen("{hash_a}_pass".format(hash_a=hash_dir_name)) == 1)
assert (redis_db.llen("{hash_a}_fail".format(hash_a=hash_dir_name)) == 2) assert (redis_db.llen("{hash_a}_fail".format(hash_a=hash_dir_name)) == 2)