From a88f38a930469a405146cd5d58065f903d79dc26 Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Wed, 28 May 2008 19:38:24 +0000 Subject: Add some more tests. --- diff --git a/tests/test_sugar.py b/tests/test_sugar.py new file mode 100644 index 0000000..39a0e8c --- /dev/null +++ b/tests/test_sugar.py @@ -0,0 +1,182 @@ +# 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': int(time.time()), + 'activity': 'org.laptop.AbiWordActivity', + 'share-scope': 'private', + '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': int(time.time()), + 'activity': 'org.laptop.AbiWordActivity', + 'fulltext': 'mec mac', + 'keep': '0', + 'icon-color': '#00588C,#00EA11', + 'mtime': datetime.now().isoformat(), + 'preview': dbus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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, []) + 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'] + assert props == PROPS_WITHOUT_PREVIEW + + t = self.update(uid) + props = self._data_store.get_properties(uid, byte_arrays=True) + #del props['uid'] + 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() + + """ + 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'}, []) + assert count > 1 + 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): + t = 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) + -- cgit v0.9.1