lbry-sdk/tests/test_utils.py

46 lines
1.1 KiB
Python
Raw Normal View History

2016-09-30 06:06:07 +02:00
import datetime
import time
2017-09-29 12:44:22 +02:00
import os
import tempfile
import shutil
from unittest import mock
from binascii import hexlify
2016-09-30 06:06:07 +02:00
DEFAULT_TIMESTAMP = datetime.datetime(2016, 1, 1)
DEFAULT_ISO_TIME = time.mktime(DEFAULT_TIMESTAMP.timetuple())
def mk_db_and_blob_dir():
db_dir = tempfile.mkdtemp()
blob_dir = tempfile.mkdtemp()
return db_dir, blob_dir
2018-02-20 19:46:17 +01:00
def rm_db_and_blob_dir(db_dir, blob_dir):
shutil.rmtree(db_dir, ignore_errors=True)
shutil.rmtree(blob_dir, ignore_errors=True)
2018-02-20 19:46:17 +01:00
def random_lbry_hash():
return hexlify(os.urandom(48)).decode()
2018-02-20 19:46:17 +01:00
def reset_time(test_case, timestamp=DEFAULT_TIMESTAMP):
2016-09-30 06:06:07 +02:00
iso_time = time.mktime(timestamp.timetuple())
patcher = mock.patch('time.time')
patcher.start().return_value = iso_time
test_case.addCleanup(patcher.stop)
patcher = mock.patch('lbry.utils.now')
2016-09-30 06:06:07 +02:00
patcher.start().return_value = timestamp
test_case.addCleanup(patcher.stop)
patcher = mock.patch('lbry.utils.utcnow')
2016-09-30 06:06:07 +02:00
patcher.start().return_value = timestamp
test_case.addCleanup(patcher.stop)
2018-11-07 21:15:05 +01:00
def is_android():
return 'ANDROID_ARGUMENT' in os.environ # detect Android using the Kivy way