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):
|
|
|
|
|
2016-09-15 11:56:17 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.credfile = '/tmp/credentials.txt'
|
|
|
|
c.CREDENTIALS = self.credfile
|
|
|
|
if os.path.exists(self.credfile):
|
|
|
|
os.remove(self.credfile)
|
|
|
|
utils.create_credentials(self.credfile)
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_credentials(self):
|
|
|
|
assert os.path.exists(self.credfile)
|
|
|
|
os.remove(self.credfile)
|
|
|
|
utils.create_credentials() # From config
|
|
|
|
assert os.path.exists(self.credfile)
|
|
|
|
|
|
|
|
def test_add_credentials(self):
|
|
|
|
utils.add_credentials(self.credfile, user="test")
|
|
|
|
assert utils.get_credentials(self.credfile)
|
|
|
|
assert utils.get_credentials(user="test")
|
|
|
|
assert list(utils.get_credentials(user="test"))[0]["user"] == "test"
|
|
|
|
|
|
|
|
def test_get_credentials(self):
|
|
|
|
utils.add_credentials(self.credfile, user="test")
|
|
|
|
assert utils.get_credentials(user="test")
|
|
|
|
assert not utils.get_credentials(user="test", inverse=True)
|
|
|
|
|
|
|
|
def test_add_two_credentials(self):
|
|
|
|
utils.add_credentials(self.credfile, user="test")
|
|
|
|
utils.add_credentials(self.credfile, user="test2")
|
|
|
|
assert utils.get_credentials(user="test")
|
|
|
|
assert utils.get_credentials(user="test2")
|
|
|
|
|
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.credfile, user="test")
|
|
|
|
assert utils.get_credentials(user="test")
|
|
|
|
utils.delete_credentials(user="test")
|
|
|
|
print(utils.get_credentials())
|
|
|
|
assert not utils.get_credentials(user="test")
|
2016-09-23 16:36:01 +00:00
|
|
|
|
|
|
|
def test_parallel(self):
|
|
|
|
import time
|
|
|
|
def echo(i):
|
|
|
|
time.sleep(2)
|
|
|
|
return i
|
|
|
|
tic = time.time()
|
|
|
|
resp = utils.parallel(echo, [1,2,3])
|
|
|
|
assert isinstance(resp, types.GeneratorType)
|
|
|
|
assert list(resp) == [1,2,3]
|
|
|
|
toc = time.time()
|
|
|
|
assert (tic-toc) < 6000
|
2016-09-15 11:56:17 +00:00
|
|
|
|