Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some tests for utils #5241

Merged
merged 2 commits into from Mar 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 0 additions & 45 deletions IPython/utils/pickleshare.py
Expand Up @@ -280,51 +280,6 @@ def __repr__(self):
self.__dict__['keydir'],
";".join([Path(k).basename() for k in keys]))


def test():
db = PickleShareDB('~/testpickleshare')
db.clear()
print("Should be empty:",db.items())
db['hello'] = 15
db['aku ankka'] = [1,2,313]
db['paths/nest/ok/keyname'] = [1,(5,46)]
db.hset('hash', 'aku', 12)
db.hset('hash', 'ankka', 313)
print("12 =",db.hget('hash','aku'))
print("313 =",db.hget('hash','ankka'))
print("all hashed",db.hdict('hash'))
print(db.keys())
print(db.keys('paths/nest/ok/k*'))
print(dict(db)) # snapsot of whole db
db.uncache() # frees memory, causes re-reads later

# shorthand for accessing deeply nested files
lnk = db.getlink('myobjects/test')
lnk.foo = 2
lnk.bar = lnk.foo + 5
print(lnk.bar) # 7

def stress():
db = PickleShareDB('~/fsdbtest')
import time,sys
for i in range(1000):
for j in range(1000):
if i % 15 == 0 and i < 200:
if str(j) in db:
del db[str(j)]
continue

if j%33 == 0:
time.sleep(0.02)

db[str(j)] = db.get(str(j), []) + [(i,j,"proc %d" % os.getpid())]
db.hset('hash',j, db.hget('hash',j,15) + 1 )

print(i, end=' ')
sys.stdout.flush()
if i % 10 == 0:
db.uncache()

def main():
import textwrap
usage = textwrap.dedent("""\
Expand Down
10 changes: 10 additions & 0 deletions IPython/utils/tests/test_decorators.py
@@ -0,0 +1,10 @@
from IPython.utils import decorators

def test_flag_calls():
@decorators.flag_calls
def f():
pass

assert not f.called
f()
assert f.called
18 changes: 17 additions & 1 deletion IPython/utils/tests/test_openpy.py
Expand Up @@ -20,4 +20,20 @@ def test_read_file():

read_strip_enc_cookie = openpy.read_py_file(nonascii_path, skip_encoding_cookie=True)
assert u'coding: iso-8859-5' not in read_strip_enc_cookie


def test_source_to_unicode():
with io.open(nonascii_path, 'rb') as f:
source_bytes = f.read()
nt.assert_equal(openpy.source_to_unicode(source_bytes, skip_encoding_cookie=False),
source_bytes.decode('iso-8859-5'))

source_no_cookie = openpy.source_to_unicode(source_bytes, skip_encoding_cookie=True)
nt.assert_not_in(u'coding: iso-8859-5', source_no_cookie)

def test_list_readline():
l = ['a', 'b']
readline = openpy._list_readline(l)
nt.assert_equal(readline(), 'a')
nt.assert_equal(readline(), 'b')
with nt.assert_raises(StopIteration):
readline()
61 changes: 61 additions & 0 deletions IPython/utils/tests/test_pickleshare.py
@@ -0,0 +1,61 @@
from __future__ import print_function

import os
from unittest import TestCase

from IPython.testing.decorators import skip
from IPython.utils.tempdir import TemporaryDirectory
from IPython.utils.pickleshare import PickleShareDB


class PickleShareDBTestCase(TestCase):
def setUp(self):
self.tempdir = TemporaryDirectory()

def tearDown(self):
self.tempdir.cleanup()

def test_picklesharedb(self):
db = PickleShareDB(self.tempdir.name)
db.clear()
print("Should be empty:",db.items())
db['hello'] = 15
db['aku ankka'] = [1,2,313]
db['paths/nest/ok/keyname'] = [1,(5,46)]
db.hset('hash', 'aku', 12)
db.hset('hash', 'ankka', 313)
self.assertEqual(db.hget('hash','aku'), 12)
self.assertEqual(db.hget('hash','ankka'), 313)
print("all hashed",db.hdict('hash'))
print(db.keys())
print(db.keys('paths/nest/ok/k*'))
print(dict(db)) # snapsot of whole db
db.uncache() # frees memory, causes re-reads later

# shorthand for accessing deeply nested files
lnk = db.getlink('myobjects/test')
lnk.foo = 2
lnk.bar = lnk.foo + 5
self.assertEqual(lnk.bar, 7)

@skip("Too slow for regular running.")
def test_stress(self):
db = PickleShareDB('~/fsdbtest')
import time,sys
for i in range(1000):
for j in range(1000):
if i % 15 == 0 and i < 200:
if str(j) in db:
del db[str(j)]
continue

if j%33 == 0:
time.sleep(0.02)

db[str(j)] = db.get(str(j), []) + [(i,j,"proc %d" % os.getpid())]
db.hset('hash',j, db.hget('hash',j,15) + 1 )

print(i, end=' ')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs print_function future

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

sys.stdout.flush()
if i % 10 == 0:
db.uncache()
13 changes: 13 additions & 0 deletions IPython/utils/tests/test_text.py
Expand Up @@ -175,3 +175,16 @@ def test_strip_email2():
src = '> > > list()'
cln = 'list()'
nt.assert_equal(text.strip_email_quotes(src), cln)

def test_LSString():
lss = text.LSString("abc\ndef")
nt.assert_equal(lss.l, ['abc', 'def'])
nt.assert_equal(lss.s, 'abc def')

def test_SList():
sl = text.SList(['a 11', 'b 1', 'a 2'])
nt.assert_equal(sl.n, 'a 11\nb 1\na 2')
nt.assert_equal(sl.s, 'a 11 b 1 a 2')
nt.assert_equal(sl.grep(lambda x: x.startswith('a')), text.SList(['a 11', 'a 2']))
nt.assert_equal(sl.fields(0), text.SList(['a', 'b', 'a']))
nt.assert_equal(sl.sort(field=1, nums=True), text.SList(['b 1', 'a 2', 'a 11']))