~zeitgeist/zeitgeist/benchmark-extension

« back to all changes in this revision

Viewing changes to test/dbus/histogram-test.py

  • Committer: Stefano Candori
  • Date: 2011-12-07 12:17:29 UTC
  • Revision ID: stefano.candori@gmail.com-20111207121729-c59ku5a44dzcz1bt
[Histogram extension] Fix timezone's hell.

This commit fixes the timezone problem in the histogram extension and adds
the test for checking this.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/python
 
2
# -.- coding: utf-8 -.-
 
3
 
 
4
# histogram-test.py
 
5
#
 
6
# Copyright © 2011 Stefano Candori <stefano.candori@gmail.com>
 
7
#
 
8
# This program is free software: you can redistribute it and/or modify
 
9
# it under the terms of the GNU Lesser General Public License as published by
 
10
# the Free Software Foundation, either version 2.1 of the License, or
 
11
# (at your option) any later version.
 
12
#
 
13
# This program is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU Lesser General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU Lesser General Public License
 
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
 
 
21
# Update python path to use local zeitgeist module
 
22
 
 
23
import sys
 
24
import os
 
25
import time
 
26
import datetime
 
27
import calendar
 
28
import unittest
 
29
import gobject
 
30
 
 
31
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
 
32
from zeitgeist.client import ZeitgeistDBusInterface
 
33
from zeitgeist.datamodel import *
 
34
from testutils import RemoteTestCase, import_events
 
35
 
 
36
#
 
37
# EXPLANATION OF THE TEST:
 
38
# The test checks if the histogram extension works well despite the 
 
39
# ***timezone's hell***.
 
40
# For example the extension, for an user in the GMT+2 timezone, should count 
 
41
# an event inserted on the 2011/12/24 at 1:30 AM as belonging to the day 24.
 
42
# The problem is that in the engine the events are inserted as UTC-relative:
 
43
# for the example our event is inserted for the day 2011/12/23 at 23:30 UTC.
 
44
# The Histogram extension must revert this when collecting data, and this test 
 
45
# check this.
 
46
#
 
47
# ******************************************************************************
 
48
#
 
49
# In the test we create an event in the "borderline" time for the timezone and 
 
50
# then we insert it in the engine as UCT-relative. After, we retrieve the data
 
51
# from the extension and finally we check that the event belong to the right day
 
52
#
 
53
 
 
54
class HistogramTest(RemoteTestCase):
 
55
 
 
56
        def __init__(self, methodName):
 
57
                super(HistogramTest, self).__init__(methodName)
 
58
                self.histogram = None
 
59
 
 
60
        def setUp(self):
 
61
                # lazy import to get a chance to use the private bus
 
62
                import dbus
 
63
                
 
64
                # We set up the connection lazily in order to wait for the
 
65
                # engine to come up
 
66
                super(HistogramTest, self).setUp()
 
67
                obj = dbus.SessionBus().get_object("org.gnome.zeitgeist.Engine",
 
68
                        "/org/gnome/zeitgeist/journal/activity")
 
69
                self.histogram = dbus.Interface(obj, "org.gnome.zeitgeist.Histogram")
 
70
                
 
71
        def _createEventOne(self):
 
72
                ev = Event.new_for_values(interpretation=Interpretation.ACCESS_EVENT,
 
73
                        subject_uri="file://sisisisisisi")
 
74
                ev.manifestation = Manifestation.USER_ACTIVITY
 
75
                
 
76
                if time.timezone < 0 :
 
77
                        start_hour = 24 + int(time.timezone / 3600)
 
78
                else:
 
79
                        start_hour =  int(time.timezone / 3600) - 1
 
80
                        
 
81
                td = datetime.datetime.today()
 
82
                event_date = datetime.datetime(td.year, td.month, td.day, start_hour, 30)
 
83
                timestamp = calendar.timegm(event_date.timetuple())
 
84
                
 
85
                ev.timestamp = timestamp * 1000
 
86
                
 
87
                return ev, timestamp
 
88
                
 
89
        def testGetHistogramData(self):
 
90
                ev, ev_timestamp = self._createEventOne();
 
91
                
 
92
                inserted_ids = self.insertEventsAndWait([ev])
 
93
                self.assertEquals(1, len(inserted_ids))
 
94
                
 
95
                h_data = self.histogram.GetHistogramData()
 
96
                self.assertEquals(1, len(h_data))
 
97
                
 
98
                h_day_timestamp = h_data[0][0]
 
99
                
 
100
                #Check if the inserted event belong to the right day!
 
101
                day_ev = datetime.date.fromtimestamp(ev_timestamp)
 
102
                start_day = datetime.date.fromtimestamp(h_day_timestamp)
 
103
                self.assertEquals(day_ev.day , start_day.day)
 
104
 
 
105
if __name__ == "__main__":
 
106
        unittest.main()