~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/services/timeline/tests/test_timeline.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 Timeline 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 OverlappingActionError, Timeline
14
 
 
15
 
 
16
 
class TestTimeline(testtools.TestCase):
17
 
 
18
 
    def test_start_returns_action(self):
19
 
        timeline = Timeline()
20
 
        action = timeline.start("Sending mail", "Noone")
21
 
        self.assertIsInstance(action, TimedAction)
22
 
        self.assertEqual("Sending mail", action.category)
23
 
        self.assertEqual("Noone", action.detail)
24
 
        self.assertEqual(None, action.duration)
25
 
        self.assertEqual(timeline, action.timeline)
26
 
 
27
 
    def test_can_supply_list(self):
28
 
        actions = "foo"
29
 
        timeline = Timeline(actions)
30
 
        self.assertEqual(actions, timeline.actions)
31
 
 
32
 
    def test_start_with_unfinished_action_fails(self):
33
 
        # A design constraint of timeline says that overlapping actions are not
34
 
        # permitted. See the Timeline docstrings.
35
 
        timeline = Timeline()
36
 
        action = timeline.start("Sending mail", "Noone")
37
 
        self.assertRaises(OverlappingActionError, timeline.start,
38
 
            "Sending mail", "Noone")
39
 
 
40
 
    def test_nested_start_permitted(self):
41
 
        # When explicitly requested a nested start can be done
42
 
        timeline = Timeline()
43
 
        action = timeline.start("Calling openid", "hostname", allow_nested=True)
44
 
        child = timeline.start("SQL Callback", "SELECT...")
45
 
 
46
 
    def test_nested_start_is_not_transitive(self):
47
 
        # nesting is explicit at each level - not inherited.
48
 
        timeline = Timeline()
49
 
        action = timeline.start("Calling openid", "hostname", allow_nested=True)
50
 
        child = timeline.start("SQL Callback", "SELECT...")
51
 
        self.assertRaises(OverlappingActionError, timeline.start,
52
 
            "Sending mail", "Noone")
53
 
 
54
 
    def test_multiple_nested_children_permitted(self):
55
 
        # nesting is not reset by each action that is added.
56
 
        timeline = Timeline()
57
 
        action = timeline.start("Calling openid", "hostname", allow_nested=True)
58
 
        child = timeline.start("SQL Callback", "SELECT...")
59
 
        child.finish()
60
 
        child = timeline.start("SQL Callback", "SELECT...")
61
 
 
62
 
    def test_multiple_starts_after_nested_group_prevented(self):
63
 
        # nesting stops being permitted when the nesting action is finished.
64
 
        timeline = Timeline()
65
 
        action = timeline.start("Calling openid", "hostname", allow_nested=True)
66
 
        action.finish()
67
 
        child = timeline.start("SQL Callback", "SELECT...")
68
 
        self.assertRaises(OverlappingActionError, timeline.start,
69
 
            "Sending mail", "Noone")
70
 
 
71
 
    def test_nesting_within_nesting_permitted(self):
72
 
        timeline = Timeline()
73
 
        action = timeline.start("Calling openid", "hostname", allow_nested=True)
74
 
        middle = timeline.start("Calling otherlibrary", "", allow_nested=True)
75
 
        child = timeline.start("SQL Callback", "SELECT...")
76
 
 
77
 
    def test_finishing_nested_within_nested_leaves_outer_nested_nesting(self):
78
 
        timeline = Timeline()
79
 
        action = timeline.start("Calling openid", "hostname", allow_nested=True)
80
 
        middle = timeline.start("Calling otherlibrary", "", allow_nested=True)
81
 
        middle.finish()
82
 
        child = timeline.start("SQL Callback", "SELECT...")
83
 
 
84
 
    def test_nested_actions_recorded_as_two_zero_length_actions(self):
85
 
        timeline = Timeline()
86
 
        action = timeline.start("Calling openid", "hostname", allow_nested=True)
87
 
        child = timeline.start("SQL Callback", "SELECT...")
88
 
        child.finish()
89
 
        action.finish()
90
 
        self.assertEqual(3, len(timeline.actions))
91
 
        self.assertEqual(datetime.timedelta(), timeline.actions[0].duration)
92
 
        self.assertEqual(datetime.timedelta(), timeline.actions[2].duration)
93
 
 
94
 
    def test_nested_category_labels(self):
95
 
        # To identify start/stop pairs '-start' and '-stop' are put onto the
96
 
        # category of nested actions:
97
 
        timeline = Timeline()
98
 
        action = timeline.start("Calling openid", "hostname", allow_nested=True)
99
 
        action.finish()
100
 
        self.assertEqual('Calling openid-start', timeline.actions[0].category)
101
 
        self.assertEqual('Calling openid-stop', timeline.actions[1].category)
102
 
 
103
 
    def test_start_after_finish_works(self):
104
 
        timeline = Timeline()
105
 
        action = timeline.start("Sending mail", "Noone")
106
 
        action.finish()
107
 
        action = timeline.start("Sending mail", "Noone")
108
 
        action.finish()
109
 
        self.assertEqual(2, len(timeline.actions))
110
 
 
111
 
    def test_baseline(self):
112
 
        timeline = Timeline()
113
 
        self.assertIsInstance(timeline.baseline, datetime.datetime)