diff options
author | Sascha Silbe <sascha@silbe.org> | 2009-08-11 10:37:14 (GMT) |
---|---|---|
committer | Sascha Silbe <sascha@silbe.org> | 2009-08-13 22:36:17 (GMT) |
commit | 93b04e6517baea3b795bc57d54f02d13975427a7 (patch) | |
tree | c1fbf133f6a40795d37ddb3f39f9a8fff2a9b73e | |
parent | 1577020f4764c86359efc08d0f5f22b0525ac435 (diff) |
pull potentially useful files from tests/ in 2a21c290bae0ced719920a784b17ad48b5c4adc6
-rw-r--r-- | tests/.gitignore | 1 | ||||
-rw-r--r-- | tests/Makefile | 27 | ||||
-rw-r--r-- | tests/__init__.py | 3 | ||||
-rw-r--r-- | tests/dateranges.txt | 42 | ||||
-rw-r--r-- | tests/properties.txt | 47 | ||||
-rw-r--r-- | tests/runalltests.py | 63 | ||||
-rw-r--r-- | tests/sugar_demo_may17.txt | 72 | ||||
-rw-r--r-- | tests/test.odt | bin | 0 -> 6851 bytes | |||
-rw-r--r-- | tests/test.pdf | bin | 0 -> 144785 bytes | |||
-rw-r--r-- | tests/test_sugar.py | 194 | ||||
-rw-r--r-- | tests/testutils.py | 10 |
11 files changed, 459 insertions, 0 deletions
diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..2460008 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +!Makefile diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..c2581cb --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,27 @@ +# howto inherit this properly from above? +# its not an option to configure +PYTHON=python + +all: clean test + +test: + @${PYTHON} runalltests.py + +valgrind: + @echo "Profiling the process. Run kcachegrind on the output" + valgrind --tool=callgrind --suppressions=valgrind-python.supp ${PYTHON} runalltests.py + +profile: + @find . -name "hotspot*" -exec rm {} \; + @${PYTHON} ./profilealltests.py + +clean: + @${PYTHON} ./cleaner.py + @find . -name "*.pyc" -exec rm {} \; + @find . -name "*~" -exec rm {} \; + @find . -name "hotspot*" -exec rm {} \; + @find . -name "callgrind.out*" -exec rm {} \; + +tags: + + diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..6f73726 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,3 @@ +# testing package + + diff --git a/tests/dateranges.txt b/tests/dateranges.txt new file mode 100644 index 0000000..886e7d2 --- /dev/null +++ b/tests/dateranges.txt @@ -0,0 +1,42 @@ +Test that date +First clean up from any other tests. +>>> import os, datetime +>>> assert os.system('rm -rf /tmp/test_ds/') == 0 + +>>> from olpc.datastore import DataStore +>>> from olpc.datastore import backingstore, model +>>> ds = DataStore() +>>> ds.registerBackend(backingstore.FileBackingStore) + +>>> assert ds.mount("/tmp/test_ds") + +>>> t1 = datetime.datetime(1995, 1, 1) +>>> t2 = datetime.datetime(2000, 1, 1) +>>> t3 = datetime.datetime(2005, 1, 1) + +>>> a = ds.create(dict(title="Content A", author="Bob", ctime=t1.isoformat()), '') +>>> b = ds.create(dict(title="Content B", author="Alice", ctime=t2.isoformat()), '') +>>> c = ds.create(dict(title="Content V", author="Clare", ctime=t3.isoformat()), '') + +>>> ds.complete_indexing() + + +Scan for ranges + +>>> result, count = ds.find({'ctime' : {'start' : t1.isoformat(), 'end' : t3.isoformat() }}) +>>> assert count == 3 + + +>>> result, count = ds.find({'ctime' : {'start' : t1.isoformat(), 'end' : t2.isoformat() }}) +>>> assert count == 2 + +>>> result, count = ds.find({'ctime' : {'start' : t2.isoformat(), 'end' : t3.isoformat() }}) +>>> assert count == 2 + +>>> result, count = ds.find({'ctime' : {'start' : t1.isoformat(), 'end' : t1.isoformat() }}) +>>> assert count == 1 + + +>>> ds.stop() +>>> del ds +>>> assert os.system('rm -rf /tmp/test_ds/') == 0 diff --git a/tests/properties.txt b/tests/properties.txt new file mode 100644 index 0000000..6c3c91b --- /dev/null +++ b/tests/properties.txt @@ -0,0 +1,47 @@ +This document shows off the range of features available for attaching +properties to content and managing them. + +(clean up) +>>> import os +>>> assert os.system('rm -rf /tmp/store1/') == 0 +>>> assert os.system('rm -rf /tmp/store2/') == 0 + + +>>> from olpc.datastore import DataStore +>>> from olpc.datastore import backingstore, model +>>> from testutils import tmpData +>>> import dbus + +Set up two mount points. + +>>> ds = DataStore() +>>> ds.registerBackend(backingstore.FileBackingStore) + +Extend the model to retain a 'year' property used below. + +Mount a couple of stores. + +>>> mp1 = ds.mount("/tmp/store1", {'title' : "Primary Storage",}) +>>> mp2 = ds.mount("/tmp/store2", {'title' : "Secondary Storage"}) + +Create some content on each. + +>>> u1 = ds.create({'title' : "Alpha doc", 'author' : "Ben", 'year:int' : 2000}, tmpData("""Document 1""")) +>>> u2 = ds.create({'title' : "Beta doc", 'author' : "Ben", 'year:int' : 2001} , tmpData("""Document 2""")) + +>>> u3 = ds.create({'title' : "Delta doc", 'author' :"HAL", 'year:int' : 2000, 'mountpoint' : mp2}, tmpData("""Document 3""")) +>>> u4 = ds.create({'title' : "Gamma doc", 'author' : "HAL", 'year:int' : 2001, 'mountpoint' : mp2}, tmpData("""Document 4""")) + +Now we should be able to discover things about the system properties. +>>> ds.complete_indexing() + +Here we test that we can extract the unique values for certain properties. +>>> assert set(ds.get_uniquevaluesfor('author')) == set(['Ben', 'HAL']) + +Here we try to gather the values for the property year. + +>>> assert set(ds.get_uniquevaluesfor('year')) == set([2000, 2001]) + + + +>>> ds.stop(); del ds diff --git a/tests/runalltests.py b/tests/runalltests.py new file mode 100644 index 0000000..8fee87e --- /dev/null +++ b/tests/runalltests.py @@ -0,0 +1,63 @@ +#!/usr/bin/python +# +# Runs all tests in the current directory +# +# Execute like: +# python runalltests.py +# +# Alternatively use the testrunner: +# python /path/to/Zope/utilities/testrunner.py -qa +# +import os, sys +import unittest +import doctest +from pkg_resources import resource_filename +import logging + +logging.basicConfig(level=logging.WARN, + format="%(asctime)-15s %(name)s %(levelname)s: %(message)s", + stream=sys.stderr) + + +doctests = [ + resource_filename(__name__, "xapianindex.txt"), + resource_filename(__name__, "milestone_1.txt"), + resource_filename(__name__, "sugar_demo_may17.txt"), + resource_filename(__name__, "milestone_2.txt"), + resource_filename(__name__, "mountpoints.txt"), + resource_filename(__name__, "properties.txt"), + resource_filename(__name__, "dateranges.txt"), + +] + +doctest_options = doctest.ELLIPSIS +#doctest_options |= doctest.REPORT_ONLY_FIRST_FAILURE + + +def test_suite(): + global doctests + suite = unittest.TestSuite() + if len(sys.argv) > 1: + doctests = sys.argv[1:] + + for dt in doctests: + suite.addTest(doctest.DocFileSuite(dt, + optionflags=doctest_options)) + + if len(sys.argv) <= 1: + tests = os.listdir(os.curdir) + tests = [n[:-3] for n in tests if n.startswith('test') and + n.endswith('.py')] + + for test in tests: + m = __import__(test) + if hasattr(m, 'test_suite'): + suite.addTest(m.test_suite()) + return suite + + +if __name__ == "__main__": + runner = unittest.TextTestRunner(verbosity=1) + suite = test_suite() + runner.run(suite) + diff --git a/tests/sugar_demo_may17.txt b/tests/sugar_demo_may17.txt new file mode 100644 index 0000000..64d49e5 --- /dev/null +++ b/tests/sugar_demo_may17.txt @@ -0,0 +1,72 @@ +How Sugar will interact with the DS for the May 17th demo in Argentina: + +>>> from olpc.datastore import DataStore +>>> from olpc.datastore import backingstore +>>> ds = DataStore() +>>> ds.registerBackend(backingstore.FileBackingStore) +>>> assert ds.mount("/tmp/test_ds") + + +Create an entry without data: +>>> uid = ds.create(dict(title="New entry"), '') +>>> ds.complete_indexing() + +>>> ds.get_filename(uid) +'' + +Update an entry without data: +>>> ds.update(uid, dict(title="New entry still without content"), '') + +>>> ds.complete_indexing() + +>>> ds.get_filename(uid) +'' + +Add some data to the same entry: +>>> fp = open('/tmp/sugar_ds_test', 'w') +>>> print >>fp, "some content" +>>> fp.close() +>>> ds.update(uid, dict(title="Same entry now with some content"), fp.name) +>>> ds.complete_indexing() + +Retrieve that data: +>>> fn = ds.get_filename(uid) +>>> fp = open(fn, 'r') +>>> fp.read() +'some content\n' +>>> fp.close() + +Update again: +>>> fp = open('/tmp/sugar_ds_test2', 'w') +>>> print >>fp, "some other content" +>>> fp.close() +>>> ds.update(uid, dict(title="Same entry with some other content"), fp.name) +>>> ds.complete_indexing() + +And retrieve again: +>>> fn = ds.get_filename(uid) +>>> fp = open(fn, 'r') +>>> fp.read() +'some other content\n' +>>> fp.close() + +Get all entries (only have one): +>>> results, count = ds.find({}) +>>> results[0]['title'] +'Same entry with some other content' + +Check content: +>>> fn = ds.get_filename(uid) +>>> fp = open(fn, 'r') +>>> fp.read() +'some other content\n' +>>> fp.close() + +Set content as pdf: +>>> ds.update(uid, dict(title="Same entry with some content in pdf"), 'test.pdf') +>>> ds.update(uid, dict(title="Same entry with some content in doc"), 'test.doc') +>>> ds.update(uid, dict(title="Same entry with some content in odt"), 'test.odt') +>>> ds.complete_indexing() + +>>> ds.stop() +>>> del ds diff --git a/tests/test.odt b/tests/test.odt Binary files differnew file mode 100644 index 0000000..a8594a4 --- /dev/null +++ b/tests/test.odt diff --git a/tests/test.pdf b/tests/test.pdf Binary files differnew file mode 100644 index 0000000..3478a64 --- /dev/null +++ b/tests/test.pdf diff --git a/tests/test_sugar.py b/tests/test_sugar.py new file mode 100644 index 0000000..1dbb607 --- /dev/null +++ b/tests/test_sugar.py @@ -0,0 +1,194 @@ +# Copyright (C) 2007 One Laptop Per Child +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +""" These tests try to cover how the DS is being used inside Sugar. +""" + +import sys +import os +import unittest +import time +import tempfile +import shutil +from datetime import datetime + +import dbus + +DS_DBUS_SERVICE = "org.laptop.sugar.DataStore" +DS_DBUS_INTERFACE = "org.laptop.sugar.DataStore" +DS_DBUS_PATH = "/org/laptop/sugar/DataStore" + +PROPS_WITHOUT_PREVIEW = {'activity_id': '37fa2f4013b17ae7fc6448f10fe5df53ef92de18', + 'title_set_by_user': '0', + 'title': 'Write Activity', + 'timestamp': str(int(time.time())), + 'activity': 'org.laptop.AbiWordActivity', + 'share-scope': 'private\nmoc', + 'keep': '0', + 'icon-color': '#00588C,#00EA11', + 'mtime': datetime.now().isoformat(), + 'preview': '', + 'mime_type': ''} + +PROPS_WITH_PREVIEW = {'activity_id': 'e8594bea74faa80539d93ef1a10de3c712bb2eac', + 'title_set_by_user': '0', + 'title': 'Write Activity', + 'share-scope': 'private', + 'timestamp': str(int(time.time())), + 'activity': 'org.laptop.AbiWordActivity', + 'fulltext': 'mec mac', + 'keep': '0', + 'icon-color': '#00588C,#00EA11', + 'mtime': datetime.now().isoformat(), + 'preview': dbus.ByteArray('\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\xd8\x00\x00\x00\xa2\x08\x02\x00\x00\x00\xac\xfb\x94\x1d\x00\x00\x00\x03sBIT\x08\x08\x08\xdb\xe1O\xe0\x00\x00\x03\x1dIDATx\x9c\xed\xd6\xbfJci\x00\xc6\xe1\xc4\xb5Qb\xb0Qa\xfc\xb3\x0cV\xda\xc8,\xa4\xf0^,\xbc;\xef@\x04kA\xb3(\x16b\x97 3("\xc9\x14\'\x13\xc5\x9c\x9cq\x8bmg\xdcj\x93\x17\xf3<\xedw\x8a\xf7\x83\x1f\xe7\x9c\xfa\xd1\xd1\xd1\xc6\xc6F\r\xa6\xe4\xf1\xf1\xf1\xfe\xfe~~{{\xbb\xd5jM{\x0c\xb3\xeb\xe1\xe1\xa1\xddn\xcf\xbf\xf3D\xaf\xd7\xbb\xbd\xbdm4\x1aooo\xbb\xbb\xbb\x97\x97\x97;;;\xa3\xd1\xa8\xd3\xe9\xb4Z\xad\xaa\xaa...\x96\x96\x96\xca\xb2\xdc\xdf\xdf???_[[[^^\xbe\xb9\xb9\xd9\xdb\xdbk6\x9b\x13\xbb\t\x1f\xc0{!\xd6\xeb\xf5n\xb7\xfb\xf4\xf4\xb4\xb0\xb0p}}\xbd\xbe\xbe~rrR\x14\xc5\xc1\xc1AY\x96\x8dF\xe3\xea\xea\xaa\xaa\xaa\xc5\xc5\xc5V\xab\xd5\xe9tNOOWVV\x0e\x0f\x0f\x87\xc3\xe1\xc4.\xc0\xc70\xf7\xce\xd9`0(\xcb\xf2\xc7`\xf0\xbd\xdf\xffsk\xebgU\xfd\xf5\xe5K\xb3\xd9\xfc\xbb\xdd\xfecn\xee\xf9\xf9\xf9\xf5\xf5\xb5(\x8a\xe1pxww\xd7\xef\xf7\x8b\xa2X]]=;;\x9b\xd8z>\x8cz\xbb\xdd\xfe\xed?\xe2\xb7o\xb5\xaf_\x7f}\xf4\xe9S\xed\xf3\xe7\xffo\x16\xb3\xe3\xbf\xff\x11k\x9b\x9b\xb5\xcd\xcdI\xeda\xa6\xbd\xf7i\x86\x89\x11"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!\x12A\x88D\x10"\x11\x84H\x04!2e\xe3\xf1x<\x1e\xcfO{\x06\xb3\xee\xf8\xf8\xb8\xd7\xeby#2e///UU\t\x91\x08B$\x82\x10\x89 D"\x08\x91\x08B$\x82\x10\x89 D"\x08\x91\x08B$\x82\x10\x89 D"\x08\x91\x08B$\x82\x10\x89 D"\x08\x91\x08B$\x82\x10\x89 D"\x08\x91\x08B$\x82\x10\x89 D"\x08\x91\x08B$\x82\x10\x89 D"\x08\x91\x08B$\x82\x10\x89 D"\x08\x91\x08B$\x82\x10\x89 D"\x08\x91\x08B$\x82\x10\x89 D"\x08\x91\x08B$\x82\x10\x89 D"\x08\x91\x08B$\x82\x10\x89 D"\x08\x91\x08B$\x82\x10\x89 D"\x08\x91\x08B$\x82\x10\x89 D"\xccw\xbb\xdd\xd1h4\xed\x19\xcc\xae\x7f\xf3\xfb\x07q8\x9emk8\x97\xda\x00\x00\x00\x00IEND\xaeB`\x82'), + 'mime_type': 'application/vnd.oasis.opendocument.text'} + +class CommonTest(unittest.TestCase): + + def setUp(self): + bus = dbus.SessionBus() + proxy = bus.get_object(DS_DBUS_SERVICE, DS_DBUS_PATH) + self._data_store = dbus.Interface(proxy, DS_DBUS_INTERFACE) + + def create(self): + file_path = self._prepare_file() + + t = time.time() + uid = self._data_store.create(PROPS_WITHOUT_PREVIEW, file_path, True) + t = time.time() - t + return t, uid + + def update(self, uid): + file_path = self._prepare_file() + t = time.time() + self._data_store.update(uid, PROPS_WITH_PREVIEW, file_path, True) + t = time.time() - t + return t + + def find(self): + query = {'order_by': ['-mtime'], + 'limit': 80} + t = time.time() + results, count = self._data_store.find(query, ['uid', 'title']) + t = time.time() - t + return t + + def _prepare_file(self): + file_path = os.path.join(os.getcwd(), 'tests/funkyabi.odt') + f, tmp_path = tempfile.mkstemp() + os.close(f) + shutil.copyfile(file_path, tmp_path) + return tmp_path + +class FunctionalityTest(CommonTest): + + def testcreation(self): + t, uid = self.create() + assert uid + + def testupdate(self): + t, uid = self.create() + t = self.update(uid) + + def testresume(self): + t, uid = self.create() + props = self._data_store.get_properties(uid, byte_arrays=True) + del props['uid'] + del props['mountpoint'] + del props['checksum'] + assert props == PROPS_WITHOUT_PREVIEW + + t = self.update(uid) + props = self._data_store.get_properties(uid, byte_arrays=True) + del props['uid'] + del props['mountpoint'] + del props['checksum'] + assert props == PROPS_WITH_PREVIEW + + file_name = self._data_store.get_filename(uid) + + assert os.path.exists(file_name) + f = open(file_name, 'r') + f.close() + + results, count = self._data_store.find({'uid': uid}, ['uid', 'title'], + byte_arrays=True) + assert count == 1 + assert results[0]['uid'] == uid + assert results[0]['title'] == 'Write Activity' + + """ + def testcustomproperties(self): + t, uid = self.create() + props = self._data_store.get_properties(uid) + props['custom_property'] = 'test' + self._data_store.update(uid, props, '', True) + + props = self._data_store.get_properties(uid) + assert props['custom_property'] == 'test' + + results, count = self._data_store.find({'custom_property': 'test'}, ['custom_property']) + for entry in results: + assert entry['custom_property'] == 'test' + uid = entry['uid'] + props = self._data_store.get_properties(uid) + assert props['custom_property'] == 'test' + """ + + def testfind(self): + results, count = self._data_store.find({}, ['uid']) + assert count > 0 + + print self.find() + +class PerformanceTest(CommonTest): + + def _avg(self, l): + total = 0 + for i in l: + total += i + return total / len(l) + + def _test_perf(self, label, function, iterations): + t_max = 0 + t_min = sys.maxint + times = [] + for i in range(1, iterations): + t = function() + t_max = max(t, t_max) + t_min = min(t, t_min) + times.append(t) + + print '%s max: %.3fms min: %.3fms avg: %.3fms' % \ + (label, t_max * 1000, t_min * 1000, self._avg(times) * 1000) + + def testperformance(self): + iterations = 100 + + self._test_perf('Create', lambda: self.create()[0], iterations) + + t, uid = self.create() + self._test_perf('Update', lambda: self.update(uid), iterations) + + self._test_perf('Find', lambda: self.find(), iterations) + + +if __name__ == '__main__': + suite = unittest.TestSuite() + suite.addTest(unittest.makeSuite(FunctionalityTest)) + #suite.addTest(unittest.makeSuite(PerformanceTest)) + unittest.TextTestRunner().run(suite) + diff --git a/tests/testutils.py b/tests/testutils.py new file mode 100644 index 0000000..fc667db --- /dev/null +++ b/tests/testutils.py @@ -0,0 +1,10 @@ +import tempfile +import os + +def tmpData(data): + """Put data into a temporary file returning the filename """ + fd, fn = tempfile.mkstemp() + os.write(fd, data) + os.close(fd) + return fn + |