Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tests/test_massops.py
blob: 47e6d7af59a433cb6b3f4ee0c044173a0daa333f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
"""Large number of operations intended for measuring performance."""

import dbus
import decorator
import os
import tempfile
import unittest


DS_DBUS_SERVICE = "org.laptop.sugar.DataStore"
DS_DBUS_INTERFACE = "org.laptop.sugar.DataStore"
DS_DBUS_PATH = "/org/laptop/sugar/DataStore"
NUM_RUNS = int(os.environ.get('MASSOPS_RUNS', '100'))
IGNORE_PROPERTIES = [
    'checksum',
    'timestamp',
    'uid',
]


@decorator.decorator
def repeat(func, *args, **kwargs):
    """Run the decorated function NUM_RUNS times."""
    for i_ in range(NUM_RUNS):
        func(*args, **kwargs)


class MassOpsTestCase(unittest.TestCase):
    """Large number of operations intended for measuring performance."""

    def setUp(self):
        # pylint: disable-msg=C0103
        self._bus = dbus.SessionBus()
        self._datastore = dbus.Interface(self._bus.get_object(DS_DBUS_SERVICE,
            DS_DBUS_PATH), DS_DBUS_INTERFACE)

    _create_properties = {
        'title': 'DS test object',
        'mime_type': 'text/plain',
        'activity': 'org.sugarlabs.DataStoreTest1',
    }
    _create_content = 'Foo bar\n'*1000

    @repeat
    def test_create(self):
        """Run create() lots of times."""
        content_file = tempfile.NamedTemporaryFile()
        content_file.write(self._create_content)
        content_file.flush()
        self._datastore.create(self._create_properties, content_file.name,
            False)
        content_file.close()

    @repeat
    def test_find_all(self):
        """Run find() to list all entries lots of times."""
        self._datastore.find({}, ['uid'], byte_arrays=True)

    def test_get_properties(self):
        """Run get_properties() on all entries and verify result."""
        for entry in self._datastore.find({}, ['uid'], byte_arrays=True)[0]:
            properties = self._datastore.get_properties(entry['uid'],
                byte_arrays=True)
            self._filter_properties(properties)
            self.assertEquals(properties, self._create_properties)

    def test_get_filename(self):
        """Run get_filename() on all entries and verify content."""
        for entry in self._datastore.find({}, ['uid'], byte_arrays=True)[0]:
            filename = self._datastore.get_filename(entry['uid'],
                byte_arrays=True)
            try:
                self.assertEquals(file(filename).read(), self._create_content)
            finally:
                os.remove(filename)

    _update_properties = {
        'title': 'DS test object (updated)',
        'mime_type': 'text/plain',
        'activity': 'org.sugarlabs.DataStoreTest1',
    }
    _update_content = 'Foo bar baz\n'*1000

    def test_update(self):
        """Update the content of all existing entries"""
        content_file = tempfile.NamedTemporaryFile()
        content_file.write(self._update_content)
        content_file.flush()
        for entry in self._datastore.find({}, ['uid'], byte_arrays=True)[0]:
            self._datastore.update(entry['uid'], self._update_properties,
                content_file.name, False)

    def test_update_verify(self):
        """Verify test_update() has changed the content of all entries."""
        for entry in self._datastore.find({}, [], byte_arrays=True)[0]:
            filename = self._datastore.get_filename(entry['uid'],
                byte_arrays=True)
            self._filter_properties(entry)
            self.assertEquals(entry, self._update_properties)
            try:
                self.assertEquals(file(filename).read(), self._update_content)
            finally:
                os.remove(filename)

    def _filter_properties(self, properties):
        for key in IGNORE_PROPERTIES:
            properties.pop(key, None)


def suite():
    test_suite = unittest.TestLoader().loadTestsFromTestCase(MassOpsTestCase)
    test_suite.__doc__ = MassOpsTestCase.__doc__
    return test_suite