Exemplo n.º 1
0
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
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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
Exemplo n.º 5
0
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)
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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
Exemplo n.º 9
0
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
Exemplo n.º 10
0
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]
Exemplo n.º 11
0
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))
Exemplo n.º 12
0
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
Exemplo n.º 13
0
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
Exemplo n.º 14
0
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)
Exemplo n.º 15
0
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
Exemplo n.º 16
0
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
Exemplo n.º 17
0
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)
Exemplo n.º 18
0
 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')
Exemplo n.º 19
0
 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)
Exemplo n.º 21
0
 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)
Exemplo n.º 23
0
# -*- 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
Exemplo n.º 24
0
def test_pop_not_there_default(d):
    key = rand_string()
    default = rand_string()
    res = d.pop(key, default)
    assert res == default
Exemplo n.º 25
0
def test_sha1_type():
    key = rand_string()
    assert isinstance(sha1_fn(key), int)
Exemplo n.º 26
0
def test_pop_not_there_no_default(d):
    key = rand_string()
    with pytest.raises(KeyError):
        d.pop(key)
Exemplo n.º 27
0
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
Exemplo n.º 28
0
def test_from_keys():
    keys = [rand_string() for _ in rand_len()]
    d = PrehashedDict.fromkeys(keys)
    for k in keys:
        assert d[k] == None
Exemplo n.º 29
0
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)
Exemplo n.º 30
0
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)
Exemplo n.º 31
0
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)
Exemplo n.º 32
0
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)
Exemplo n.º 33
0
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)