def test_set_default_later_lookup(d): key = rand_string() value = rand_string() res = d.setdefault(key, value) assert res == value res = d[key] assert res == value
def test_set_default_there(d): key = rand_string() value = rand_string() default = rand_string() d[key] = value res = d.setdefault(key, default) assert res == value
def test_pop_removes(d): key = rand_string() value = rand_string() default = rand_string() d[key] = value _ = d.pop(key, default) assert key not in d
def test_copy_same_keys(d): keys = [rand_string() for _ in rand_len()] values = [rand_string() for _ in keys] d = PrehashedDict(zip(keys, values)) g = d.copy() for k in keys: assert k in g
def test_del_present(d): key = rand_string() value = rand_string() d[key] = value del d[key] with pytest.raises(KeyError): res = d[key]
def test(self): r = 'test-repo-' + rand_string() r_path = path.join(_repos_dir, r) f = 'rep-' + rand_string() f_path = path.join(r_path, f) c = 'contentsts' os.mkdir(r_path) try: fh = open(f_path, 'wb') try: fh.write(c) a = RepositoryFile(repo=r, repo_path=f) a.clean() a.save() self.assertEqual(a.path, path.join(_repos_dir, r, f)) self.assertEqual(urllib.unquote(a.url), _repos_url + r + '/' + f) finally: fh.close() os.remove(f_path) finally: os.rmdir(r_path) self.assertEqual(a.specific_instance(), a) self.assertEqual(a.document_ptr.__class__, Document) self.assertNotEqual(a.document_ptr.__class__, RepositoryFile) self.assertEqual(a.document_ptr.specific_instance(), a) self.assertEqual(a.document_ptr.specific_instance().__class__, RepositoryFile)
def test_copy_same_values(d): keys = [rand_string() for _ in rand_len()] values = [rand_string() for _ in keys] d = PrehashedDict(zip(keys, values)) g = d.copy() for k, v in zip(keys, values): assert g[k] == v
def test_overwrite(d): key = rand_string() value = rand_string() new_value = rand_string() d[key] = value assert d[key] == value d[key] = new_value assert d[key] == new_value
def test_pop_there_default(d): key = rand_string() value = rand_string() default = rand_string() d[key] = value res = d.pop(key, default) assert res != default assert res == value
def test_copy_is_shallow(d): keys = [rand_string() for _ in rand_len()] values = [rand_string() for _ in keys] d = PrehashedDict(zip(keys, values)) d['12'] = [1, 2, 3] g = d.copy() g['12'].append(4) assert d['12'] == [1, 2, 3, 4]
def test_insert(d): key = rand_string() value = rand_string() d[key] = value assert key in d assert super(PrehashedDict, d).__contains__(sha1_fn(key)) assert d[key] == value assert super(PrehashedDict, d).__getitem__(sha1_fn(key))
def test_copy_delete_one_old(d): keys = [rand_string() for _ in rand_len()] values = [rand_string() for _ in keys] d = PrehashedDict(zip(keys, values)) g = d.copy() del_key = random.choice(keys) del d[del_key] assert del_key in g assert del_key not in d
def test_copy_change_one_old(d): keys = [rand_string() for _ in rand_len()] values = [rand_string() for _ in keys] d = PrehashedDict(zip(keys, values)) g = d.copy() new_val = rand_string() changed_key = random.choice(keys) d[changed_key] = new_val assert d[changed_key] == new_val assert g[changed_key] != new_val
def test_initial_add(): key = rand_string() value = rand_string() new_value = rand_string() d = PrehashedDict(((key, value), )) new_key = d.initial_add(key, new_value) assert d[new_key] == new_value assert d[key] == value assert key != new_key assert len(new_key) > len(key)
def test_copy_add_one_old(d): keys = [rand_string() for _ in rand_len()] values = [rand_string() for _ in keys] d = PrehashedDict(zip(keys, values)) g = d.copy() new_key = rand_string() new_val = rand_string() d[new_key] = new_val assert d[new_key] == new_val assert new_key in d assert new_key not in g
def test_pickle(file_cleanup): file_name = file_cleanup keys = [rand_string() for _ in rand_len()] vals = [rand_string() for _ in keys] d = PrehashedDict({k: v for k, v in zip(keys, vals)}) pickle.dump(d, open(file_name, 'wb')) d1 = pickle.load(open(file_name, 'rb')) for k in keys: assert k in d assert k in d1 for k, v in zip(keys, vals): assert d1[k] == v assert d == d1
def test_init_calls_process_args(): d = PrehashedDict({rand_string(): rand_string()}) with patch('prehashed.prehashed.PrehashedDict._process_args') as mock: arg = random.choice([{ rand_string(): rand_string() }, ((rand_string(), rand_string())), (), d]) kwargs = {rand_string(): rand_string() for _ in rand_len(0, 2)} d = PrehashedDict(arg, **kwargs) mock.assert_called_once_with(arg, **kwargs)
def _make_pdf(self): colored_print('Generating PDFs...', 'OKGREEN') html_files = glob.glob(os.path.join(self.html_output, '*.html')) for html_file in html_files: pdf_filename = os.path.splitext(ntpath.basename(html_file))[0] pdf_filename += rand_string(5) pdf_filename += '.pdf' try: HTML(html_file).write_pdf(os.path.join(self.pdf_output, pdf_filename), zoom=1.75) except Exception as e: colored_print('PDF `%s` generation failed' % pdf_filename, 'FAIL')
def test_sign_verify(self): key = crypto.ZilKey.generate_new() for i in range(10): l = 1 + i * 512 msg = utils.rand_bytes(l) + utils.rand_string(l).encode() signature = key.sign(msg) assert isinstance(signature, bytes) assert key.verify(signature, msg) signature_str = key.sign_str(msg) assert isinstance(signature_str, str) assert key.verify(signature_str, msg)
def test_fs(self): test_filename = 'test-' + rand_string() contents = 'contestns' fs.save(test_filename, ContentFile(contents)) self.assertEqual(fs.exists(test_filename), True) self.assertEqual(fs.size(test_filename), len(contents)) self.assertEqual(fs.open(test_filename).read(), contents) fs.delete(test_filename) self.assertEqual(fs.exists(test_filename), False)
def _compile_sass(self): colored_print('Recompiling SaSS... ', 'OKGREEN') css_filename = os.path.join(self.html_css, 'main.' + rand_string(12) + '.css') try: compiled = sass.compile( filename=os.path.join(self.sass, 'main.scss'), output_style='compressed' ) with open(css_filename, 'w') as f: f.write(compiled) except Exception as e: colored_print(e, 'WARNING') return css_filename
def test(self): filename = 'up-' + rand_string() contents = 'upladded contestsn' a = UploadedFile( uploader = self.test_user, ) a.uploaded_file.save(filename, ContentFile(contents)) a.clean() a.save() # TODO self.assertEqual(a.url,?) # TODO self.assertEqual(a.path,?) self.assertEqual(a.specific_instance(), a) self.assertEqual(a.document_ptr.__class__, Document) self.assertNotEqual(a.document_ptr.__class__, UploadedFile) self.assertEqual(a.document_ptr.specific_instance(), a) self.assertEqual(a.document_ptr.specific_instance().__class__, UploadedFile)
# -*- coding: utf-8 -*- import os import time import random from utils import shoot, random, spinning_cursor, rand_string, weighted_choice i = 0 curr = 0 while True: disp = int((curr + random.uniform(1, 35)) / 2.) # smooth it out curr = disp attrs = None if disp > 25 else ['dark'] shoot('{0: <35}'.format("|" * disp), 'white', attrs=attrs) time.sleep(random.random() * 0.1) i += 1 # occasionally clear the screen and output rand string if i >= 100: if weighted_choice([(True, 1), (False, 9)]): os.system('clear') shoot("\n%s\n" % rand_string(random.randint(200, 800)), color='red') spinning_cursor(random.random() * 0.5) i = 0
def test_pop_not_there_default(d): key = rand_string() default = rand_string() res = d.pop(key, default) assert res == default
def test_sha1_type(): key = rand_string() assert isinstance(sha1_fn(key), int)
def test_pop_not_there_no_default(d): key = rand_string() with pytest.raises(KeyError): d.pop(key)
def test_from_keys_value(d): keys = [rand_string() for _ in rand_len()] value = rand_string() d = PrehashedDict.fromkeys(keys, value) for k in keys: assert d[k] == value
def test_from_keys(): keys = [rand_string() for _ in rand_len()] d = PrehashedDict.fromkeys(keys) for k in keys: assert d[k] == None
def test_process_args_iterable_kwargs(d): kwargs = {rand_string(): rand_string() for _ in rand_len()} it = [(rand_string(), rand_string()) for _ in rand_len()] res = list(d._process_args(it, **kwargs)) assert find(it, res) assert find(kwargs.items(), res)
def test_process_args_iterable(d): it = [(rand_string(), rand_string()) for _ in rand_len()] res = list(d._process_args(it)) assert find(it, res)
def test_process_args_prehased_kwargs(d): d1 = PrehashedDict({rand_string(): rand_string() for _ in rand_len()}) kwargs = {rand_string(): rand_string() for _ in rand_len()} res = list(d._process_args(d1, **kwargs)) assert find(d1.items(), res, hash_me=False) assert find(kwargs.items(), res)
def test_process_args_dict_kwargs(d): dic = {rand_string(): rand_string() for _ in rand_len()} kwargs = {rand_string(): rand_string() for _ in rand_len()} res = list(d._process_args(dic, **kwargs)) assert find(dic.items(), res) assert find(kwargs.items(), res)
def test_process_args_kwargs(d): kwargs = {rand_string(): rand_string() for _ in rand_len()} print(kwargs) res = list(d._process_args(**kwargs)) assert find(kwargs.items(), res)