def test_remove_file(): from metrique.utils import remove_file, rand_chars, make_dirs assert remove_file(None) == [] assert remove_file('') == [] assert remove_file('DOES_NOT_EXIST') == [] path = os.path.join(cache_dir, rand_chars()) assert not exists(path) open(path, 'w').close() assert exists(path) assert remove_file(path) == path assert not exists(path) open(path, 'w').close() assert remove_file(path) == path assert not exists(path) assert remove_file('DOES_NOT_EXIST') == [] # build a simple nested directory tree path = os.path.join(cache_dir, rand_chars()) assert make_dirs(path) == path try: remove_file(path) except RuntimeError: pass else: assert False, "Managed to remove a random path" assert remove_file(path, force=True) == path
def test_move(): from metrique.utils import move, rand_chars, remove_file dest = tmp_dir rel_path_1 = rand_chars(prefix='move') path_1 = os.path.join(cache_dir, rel_path_1) _path_1 = os.path.join(dest, rel_path_1) open(path_1, 'a').close() rel_path_2 = rand_chars(prefix='move') path_2 = os.path.join(cache_dir, rel_path_2) open(path_2, 'a').close() paths = (path_1, path_2) assert exists(path_1) move(path_1, dest) assert not exists(path_1) move(_path_1, cache_dir) assert exists(path_2) move(paths, dest) assert not any((exists(path_1), exists(path_2))) remove_file(paths, force=True) remove_file(dest, force=True) remove_file(tmp_dir, force=True) try: move('DOES_NOT_EXST', 'SOMEWHERE') except IOError: pass else: assert False, "Moving DOES_NOT_EXIST to SOMEWHERE did not throw an "\ "exception" assert move('DOES_NOT_EXST', 'SOMEWHERE', quiet=True) == []
def test_file_is_empty(): from metrique.utils import file_is_empty, write_file, rand_chars from metrique.utils import remove_file f1 = os.path.join(cache_dir, rand_chars(prefix='empty_test_1')) f2 = os.path.join(cache_dir, rand_chars(prefix='not_empty_test_2')) write_file(f1, '') write_file(f2, 'not empty') assert file_is_empty(f1) assert exists(f1) assert file_is_empty(f1, remove=True) assert not exists(f1) assert not file_is_empty(f2) try: # not a valid path file_is_empty('DOES_NOT_EXIST') except RuntimeError: pass else: assert False try: # not a valid path file_is_empty(True) except RuntimeError: pass else: assert False remove_file(f2) assert not exists(f2)
def test_rand_chars(): from metrique.utils import rand_chars str_ = rand_chars() assert isinstance(str_, basestring) assert isinstance(str_, unicode) assert len(str_) == 6 OK = set(string.ascii_uppercase + string.digits) assert all(c in OK for c in str_) assert rand_chars() != rand_chars() assert rand_chars(prefix='rand_chars')[0:10] == 'rand_chars'
def test_backup(): from metrique.utils import backup, rand_chars, remove_file f1 = os.path.join(cache_dir, '%s' % rand_chars(prefix='backup')) f2 = os.path.join(cache_dir, '%s' % rand_chars(prefix='backup')) open(f1, 'w').close() open(f2, 'w').close() assert [exists(f) for f in (f1, f2)] saveas = backup('%s %s' % (f1, f2)) assert exists(saveas) remove_file(saveas) saveas = backup((f1, f2)) assert exists(saveas) remove_file((saveas, f1, f2))
def test_get_pid(): from metrique.utils import get_pid, rand_chars, remove_file assert get_pid() == 0 assert get_pid(None) == 0 path = os.path.join(cache_dir, '%s.pid' % rand_chars(prefix='get_pid')) # invalid path returns 0 assert get_pid(path) == 0 with open(path, 'w') as f: f.write("1") assert exists(path) assert get_pid(path) == 1 remove_file(path) with open(path, 'w') as f: f.write("a") try: get_pid(path) except ValueError: pass else: assert False remove_file(path) # invalid path returns 0 assert get_pid("boomboompow") == 0
def test_write_file(): from metrique.utils import write_file, rand_chars, read_file from metrique.utils import remove_file f1 = os.path.join(cache_dir, rand_chars()) write_file(f1, 'hello world') assert exists(f1) assert read_file(f1) == 'hello world' # can't overwrite files with default settings try: write_file(f1, 'hello world', force=False, exists_ext=None) except RuntimeError: pass else: assert False, "File overwritten without force=True" write_file(f1, 'hello metrique', force=True) assert exists(f1) assert read_file(f1) == 'hello metrique' write_file(f1, 'hello world', mode='a') assert exists(f1) assert read_file(f1) == 'hello metriquehello world' # done remove the file; write it again, second time will write '.new' file # .new file added to new file on write is exists_ext not null write_file(f1, 'hello world', force=False, exists_ext='new') assert read_file(f1) assert read_file('%s.new' % f1) remove_file(f1)
def test_make_dirs(): from metrique.utils import make_dirs, rand_chars, remove_file d_1 = rand_chars(prefix='make_dirs') d_2 = rand_chars() base = os.path.join(tmp_dir, d_1) rand_dirs = os.path.join(base, d_2) path = os.path.join(tmp_dir, rand_dirs) assert make_dirs(path) == path assert exists(path) remove_file(base, force=True) assert not exists(base) for _ in ['', 'relative/dir']: # requires absolute path! try: make_dirs(_) except OSError: pass else: assert False, "Made dirs with relative paths"
def test_rsync(): from metrique.utils import rsync, sys_call, rand_chars, remove_file from metrique.utils import read_file #remove_file(f_1) #remove_file(dest, force=True) if not sys_call('which rsync'): return # skip this test if rsync isn't available fname = rand_chars(prefix='rsync') path = os.path.join(cache_dir, fname) with open(path, 'w') as f: f.write('test') dest = os.path.join(tmp_dir, 'rsync') rsync(targets=path, dest=dest) assert read_file(os.path.join(dest, fname)) == 'test' with open(path, 'w') as f: f.write('test 2') rsync(targets=path, dest=dest) assert read_file(os.path.join(dest, fname)) == 'test 2' remove_file(path, force=True) remove_file(dest, force=True)
def test_postgresql(): from metrique.sqlalchemy import SQLAlchemyProxy from metrique.utils import rand_chars, configure config = configure(config_file=default_config, section_key='proxy', section_only=True) _db = config['db'] = 'test' _table = config['table'] = 'bla' config['dialect'] = 'postgresql' p = SQLAlchemyProxy(**config) _u = p.config.get('username') _p = p.config.get('password') _po = p.config.get('port') _expected_engine = 'postgresql://%s:%[email protected]:%s/%s' % (_u, _p, _po, _db) p.initialize() assert p._engine_uri == _expected_engine # FIXME: DROP ALL db_tester(p) schema = { 'col_1': { 'type': int }, 'col_3': { 'type': datetime }, } # FIXME: remove user! before test completes # new user new_u = rand_chars(chars='asdfghjkl') new_p = rand_chars(8) p.user_register(username=new_u, password=new_p) assert p.user_exists(new_u) is True # Sharing p.share(new_u) _new_table = 'blabla' # switch to the new users db p.config['username'] = new_u p.config['password'] = new_p p.config['db'] = new_u p.config['table'] = _new_table p.initialize() assert p.ls() == [] q = 'SELECT current_user;' assert p.execute(q)[0] == {'current_user': new_u} p.autotable(name=_new_table, schema=schema, create=True) assert p.ls() == [_new_table] # switch to admin's db p.config['username'] = _u p.config['password'] = _p p.config['db'] = _u p.config['table'] = _table p.initialize() p.autotable(schema=schema) assert p.ls() == [_table] p.drop() try: assert p.count() except RuntimeError: pass else: assert False assert p.ls() == []
pjoin = os.path.join env = os.environ USER = getpass.getuser() VIRTUAL_ENV = utils.active_virtualenv() NOW = datetime.datetime.utcnow().strftime('%FT%H%M%S') HOSTNAME = socket.gethostname() try: # try to get one of the local inet device ip addresses LOCAL_IP = socket.gethostbyname(HOSTNAME) except Exception: LOCAL_IP = '127.0.0.1' PASSWORD = utils.rand_chars(10) COOKIE_SECRET = utils.rand_chars(50) HOME_DIR = env.get('METRIQUE_HOME', os.path.expanduser('~/')) PREFIX_DIR = env.get('METRIQUE_PREFIX', pjoin(HOME_DIR, '.metrique')) # set cache dir so pip doesn't have to keep downloading over and over PIP_DIR = pjoin(PREFIX_DIR, '.pip') PIP_CACHE_DIR = pjoin(PIP_DIR, 'download-cache') PIP_ACCEL_DIR = pjoin(PREFIX_DIR, '.pip-accel') PIP_EGGS = pjoin(PREFIX_DIR, '.python-eggs') env['PIP_DOWNLOAD_CACHE'] = env.get('PIP_DOWNLOAD_CACHE', PIP_CACHE_DIR) env['PIP_ACCEL_CACHE'] = env.get('PIP_ACCEL_CACHE', PIP_ACCEL_DIR) env['PYTHON_EGG_CACHE'] = env.get('PYTHON_EGG_CACHE', PIP_EGGS) TRASH_DIR = env.get('METRIQUE_TRASH')
def test_postgresql(): from metrique.sqlalchemy import SQLAlchemyProxy from metrique.utils import rand_chars, configure config = configure(config_file=default_config, section_key='proxy', section_only=True) _db = config['db'] = 'test' _table = config['table'] = 'bla' config['dialect'] = 'postgresql' p = SQLAlchemyProxy(**config) _u = p.config.get('username') _p = p.config.get('password') _po = p.config.get('port') _expected_engine = 'postgresql://%s:%[email protected]:%s/%s' % (_u, _p, _po, _db) p.initialize() assert p._engine_uri == _expected_engine # FIXME: DROP ALL db_tester(p) schema = { 'col_1': {'type': int}, 'col_3': {'type': datetime}, } # FIXME: remove user! before test completes # new user new_u = rand_chars(chars='asdfghjkl') new_p = rand_chars(8) p.user_register(username=new_u, password=new_p) assert p.user_exists(new_u) is True # Sharing p.share(new_u) _new_table = 'blabla' # switch to the new users db p.config['username'] = new_u p.config['password'] = new_p p.config['db'] = new_u p.config['table'] = _new_table p.initialize() assert p.ls() == [] q = 'SELECT current_user;' assert p.execute(q)[0] == {'current_user': new_u} p.autotable(name=_new_table, schema=schema, create=True) assert p.ls() == [_new_table] # switch to admin's db p.config['username'] = _u p.config['password'] = _p p.config['db'] = _u p.config['table'] = _table p.initialize() p.autotable(schema=schema) assert p.ls() == [_table] p.drop() try: assert p.count() except RuntimeError: pass else: assert False assert p.ls() == []