1
0
mirror of https://github.com/balkian/bitter.git synced 2024-12-22 16:38:13 +00:00
bitter/tests/test_utils.py

71 lines
2.4 KiB
Python
Raw Normal View History

2016-01-14 20:41:14 +00:00
from unittest import TestCase
2016-09-15 11:56:17 +00:00
import os
2016-09-23 16:36:01 +00:00
import types
2016-09-15 11:56:17 +00:00
from bitter import utils
from bitter import config as c
2016-01-14 20:41:14 +00:00
class TestUtils(TestCase):
configfile = '/tmp/bitter.yaml'
2016-09-15 11:56:17 +00:00
def setUp(self):
c.CONFIG_FILE = self.configfile
if os.path.exists(self.configfile):
os.remove(self.configfile)
assert not os.path.exists(self.configfile)
utils.create_config_file(self.configfile)
assert os.path.exists(self.configfile)
2016-09-15 11:56:17 +00:00
def test_add_credentials(self):
utils.add_credentials(self.configfile, user="test")
assert utils.get_credentials(self.configfile)
assert utils.get_credentials(self.configfile, user="test")
assert list(utils.get_credentials(self.configfile, user="test"))[0]["user"] == "test"
2016-09-15 11:56:17 +00:00
def test_get_credentials(self):
utils.add_credentials(self.configfile, user="test")
assert utils.get_credentials(self.configfile, user="test")
assert not utils.get_credentials(self.configfile, user="test", inverse=True)
2016-09-15 11:56:17 +00:00
def test_add_two_credentials(self):
utils.add_credentials(self.configfile, user="test")
utils.add_credentials(self.configfile, user="test2")
assert utils.get_credentials(self.configfile, user="test")
assert utils.get_credentials(self.configfile, user="test2")
2016-09-15 11:56:17 +00:00
2016-01-14 20:41:14 +00:00
2016-09-15 11:56:17 +00:00
def test_delete_credentials(self):
utils.add_credentials(self.configfile, user="test")
assert utils.get_credentials(self.configfile, user="test")
utils.delete_credentials(self.configfile, user="test")
assert not utils.get_credentials(self.configfile, user="test")
2016-09-23 16:36:01 +00:00
def test_parallel(self):
import time
def echo(i):
time.sleep(0.5)
2016-09-23 16:36:01 +00:00
return i
tic = time.time()
2019-09-19 10:18:56 +00:00
resp = utils.parallel(echo, [1, 2, 3])
2016-09-23 16:36:01 +00:00
assert isinstance(resp, types.GeneratorType)
2019-09-19 10:18:56 +00:00
assert sorted(list(resp)) == [1, 2, 3]
2016-09-23 16:36:01 +00:00
toc = time.time()
assert (tic-toc) < 600
resp2 = utils.parallel(echo, [1,2,3,4], chunksize=2)
2019-09-19 10:18:56 +00:00
assert sorted(list(resp2)) == [1, 2, 3, 4]
class TestUtilsEnv(TestUtils):
configfile = None
def setUp(self):
if 'BITTER_CONFIG' in os.environ:
self.oldenv = os.environ['BITTER_CONFIG']
os.environ['BITTER_CONFIG'] = ''
def tearDown(self):
if hasattr(self, 'oldenv'):
os.environ['BITTER_CONFIG'] = self.oldenv