~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/services/timeline/tests/test_timedaction.py

  • Committer: Robert Collins
  • Date: 2011-08-10 01:00:53 UTC
  • mto: This revision was merged to the branch mainline in revision 13644.
  • Revision ID: robertc@robertcollins.net-20110810010053-w2tcm21i87fwqr3s
Use the new extracted timeline module.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2010 Canonical Ltd.  This software is licensed under the
2
 
# GNU Affero General Public License version 3 (see the file LICENSE).
3
 
 
4
 
"""Tests of the TimedAction class."""
5
 
 
6
 
__metaclass__ = type
7
 
 
8
 
import datetime
9
 
 
10
 
import testtools
11
 
 
12
 
from lp.services.timeline.timedaction import TimedAction
13
 
from lp.services.timeline.timeline import Timeline
14
 
 
15
 
 
16
 
class TestTimedAction(testtools.TestCase):
17
 
 
18
 
    def test_starts_now(self):
19
 
        action = TimedAction("Sending mail", None)
20
 
        self.assertIsInstance(action.start, datetime.datetime)
21
 
 
22
 
    def test_finish_sets_duration(self):
23
 
        action = TimedAction("Sending mail", None)
24
 
        self.assertEqual(None, action.duration)
25
 
        action.finish()
26
 
        self.assertIsInstance(action.duration, datetime.timedelta)
27
 
 
28
 
    def test__init__sets_category(self):
29
 
        action = TimedAction("Sending mail", None)
30
 
        self.assertEqual("Sending mail", action.category)
31
 
 
32
 
    def test__init__sets_detail(self):
33
 
        action = TimedAction(None, "fred.jones@example.com")
34
 
        self.assertEqual("fred.jones@example.com", action.detail)
35
 
 
36
 
    def test_logTuple(self):
37
 
        timeline = Timeline()
38
 
        action = TimedAction("foo", "bar", timeline)
39
 
        # Set variable for deterministic results
40
 
        action.start = timeline.baseline + datetime.timedelta(0, 0, 0, 2)
41
 
        action.duration = datetime.timedelta(0, 0, 0, 4)
42
 
        log_tuple = action.logTuple()
43
 
        self.assertEqual(4, len(log_tuple), "!= 4 elements %s" % (log_tuple,))
44
 
        # The first element is the start offset in ms.
45
 
        self.assertAlmostEqual(2, log_tuple[0])
46
 
        # The second element is the end offset in ms.
47
 
        self.assertAlmostEqual(6, log_tuple[1])
48
 
        self.assertEqual("foo", log_tuple[2])
49
 
        self.assertEqual("bar", log_tuple[3])
50
 
 
51
 
    def test_logTupleIncomplete(self):
52
 
        # Things that start and hit a timeout *may* not get recorded as
53
 
        # finishing in normal operation.
54
 
        timeline = Timeline()
55
 
        action = TimedAction("foo", "bar", timeline)
56
 
        # Set variable for deterministic results
57
 
        action.start = timeline.baseline + datetime.timedelta(0, 0, 0, 2)
58
 
        action._interval_to_now = lambda: datetime.timedelta(0, 0, 0, 3)
59
 
        log_tuple = action.logTuple()
60
 
        self.assertEqual(4, len(log_tuple), "!= 4 elements %s" % (log_tuple,))
61
 
        self.assertAlmostEqual(2, log_tuple[0])
62
 
        self.assertAlmostEqual(5, log_tuple[1])
63
 
        self.assertEqual("foo", log_tuple[2])
64
 
        self.assertEqual("bar", log_tuple[3])