From 43a46eb639e33eae7217950f66cb4b74f255f7f7 2014-02-28 02:38:53 From: Thomas Kluyver Date: 2014-02-28 02:38:53 Subject: [PATCH] Add tests for things in utils --- diff --git a/IPython/utils/pickleshare.py b/IPython/utils/pickleshare.py index 3113b0d..36211c1 100755 --- a/IPython/utils/pickleshare.py +++ b/IPython/utils/pickleshare.py @@ -280,51 +280,6 @@ class PickleShareLink: 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("""\ diff --git a/IPython/utils/tests/test_decorators.py b/IPython/utils/tests/test_decorators.py new file mode 100644 index 0000000..38e871d --- /dev/null +++ b/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 \ No newline at end of file diff --git a/IPython/utils/tests/test_openpy.py b/IPython/utils/tests/test_openpy.py index 182b041..58d0ff2 100644 --- a/IPython/utils/tests/test_openpy.py +++ b/IPython/utils/tests/test_openpy.py @@ -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() \ No newline at end of file diff --git a/IPython/utils/tests/test_pickleshare.py b/IPython/utils/tests/test_pickleshare.py new file mode 100644 index 0000000..f275b00 --- /dev/null +++ b/IPython/utils/tests/test_pickleshare.py @@ -0,0 +1,59 @@ +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=' ') + sys.stdout.flush() + if i % 10 == 0: + db.uncache() \ No newline at end of file diff --git a/IPython/utils/tests/test_text.py b/IPython/utils/tests/test_text.py index 96e714a..b3ba76b 100644 --- a/IPython/utils/tests/test_text.py +++ b/IPython/utils/tests/test_text.py @@ -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'])) \ No newline at end of file