Switch to unified view

a/ForgeTracker/forgetracker/tests/unit/test_globals_model.py b/ForgeTracker/forgetracker/tests/unit/test_globals_model.py
1
import mock
2
from nose.tools import assert_equal
3
from datetime import datetime, timedelta
4
5
import forgetracker
1
from forgetracker.model import Globals
6
from forgetracker.model import Globals
2
from forgetracker.tests.unit import TrackerTestWithModel
7
from forgetracker.tests.unit import TrackerTestWithModel
3
from pylons import c
8
from pylons import c
4
from allura.lib import helpers as h
9
from allura.lib import helpers as h
5
10
...
...
25
    def test_ticket_numbers_are_independent(self):
30
    def test_ticket_numbers_are_independent(self):
26
        assert Globals.next_ticket_num() == 1
31
        assert Globals.next_ticket_num() == 1
27
        h.set_context('test', 'doc-bugs', neighborhood='Projects')
32
        h.set_context('test', 'doc-bugs', neighborhood='Projects')
28
        assert Globals.next_ticket_num() == 1
33
        assert Globals.next_ticket_num() == 1
29
34
35
    @mock.patch('forgetracker.model.ticket.datetime')
36
    def test_bin_count(self, mock_dt):
37
        now = datetime.utcnow()
38
        mock_dt.utcnow.return_value = now
39
        gbl = Globals()
40
        gbl._bin_counts_data = [{'summary': 'foo', 'hits': 1}, {'summary': 'bar', 'hits': 2}]
41
        gbl.invalidate_bin_counts = mock.Mock()
42
43
        # not expired, finds bin
44
        gbl._bin_counts_expire = now + timedelta(minutes=5)
45
        bin = gbl.bin_count('bar')
46
        assert_equal(bin['hits'], 2)
47
        assert not gbl.invalidate_bin_counts.called
48
49
        # expired, returns value for missing bin
50
        gbl._bin_counts_expire = now - timedelta(minutes=5)
51
        bin = gbl.bin_count('qux')
52
        assert_equal(bin['hits'], 0)
53
        assert gbl.invalidate_bin_counts.called
54
55
    @mock.patch('forgetracker.tasks.update_bin_counts')
56
    @mock.patch('forgetracker.model.ticket.datetime')
57
    def test_invalidate_bin_counts(self, mock_dt, mock_task):
58
        now = datetime.utcnow().replace(microsecond=0)
59
        mock_dt.utcnow.return_value = now
60
        gbl = Globals()
61
62
        # invalidated recently, don't dog-pile
63
        gbl._bin_counts_invalidated = now - timedelta(minutes=1)
64
        gbl.invalidate_bin_counts()
65
        assert not mock_task.post.called
66
67
        # invalidated too long ago, call again
68
        gbl._bin_counts_invalidated = now - timedelta(minutes=6)
69
        gbl.invalidate_bin_counts()
70
        assert mock_task.post.called
71
        assert_equal(gbl._bin_counts_invalidated, now)
72
73
        # never invalidated
74
        mock_task.reset_mock()
75
        gbl._bin_counts_invalidated = None
76
        gbl.invalidate_bin_counts()
77
        assert mock_task.post.called
78
        assert_equal(gbl._bin_counts_invalidated, now)
79
80
    @mock.patch('forgetracker.model.ticket.Bin')
81
    @mock.patch('forgetracker.model.ticket.search_artifact')
82
    @mock.patch('forgetracker.model.ticket.datetime')
83
    def test_update_bin_counts(self, mock_dt, mock_search, mock_bin):
84
        now = datetime.utcnow().replace(microsecond=0)
85
        mock_dt.utcnow.return_value = now
86
        gbl = Globals()
87
        gbl._bin_counts_invalidated = now - timedelta(minutes=1)
88
        mock_bin.query.find.return_value = [mock.Mock(summary='foo', terms='bar')]
89
        mock_search().hits = 5
90
91
        assert_equal(gbl._bin_counts_data, [])  # sanity pre-check
92
        gbl.update_bin_counts()
93
        assert mock_bin.query.find.called
94
        mock_search.assert_called_with(forgetracker.model.Ticket, 'bar', rows=0, short_timeout=False)
95
        assert_equal(gbl._bin_counts_data, [{'summary': 'foo', 'hits': 5}])
96
        assert_equal(gbl._bin_counts_expire, now + timedelta(minutes=60))
97
        assert_equal(gbl._bin_counts_invalidated, None)
98
30
99
31
class TestCustomFields(TrackerTestWithModel):
100
class TestCustomFields(TrackerTestWithModel):
32
    def test_it_has_sortable_custom_fields(self):
101
    def test_it_has_sortable_custom_fields(self):
33
        tracker_globals = globals_with_custom_fields(
102
        tracker_globals = globals_with_custom_fields(
34
            [dict(label='Iteration Number',
103
            [dict(label='Iteration Number',