This repository has been archived by the owner on Jun 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
test_go_system_stats.py
190 lines (166 loc) · 6.86 KB
/
test_go_system_stats.py
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# -*- coding: utf-8 -*-
from datetime import datetime
from django.core.management.base import CommandError
from django.core.management import call_command
from go.base.tests.helpers import (
GoDjangoTestCase, DjangoVumiApiHelper, CommandIO)
from go.vumitools.tests.helpers import GoMessageHelper
class TestGoSystemStatsCommand(GoDjangoTestCase):
def setUp(self):
self.vumi_helper = self.add_helper(DjangoVumiApiHelper())
self.user_helper = self.vumi_helper.make_django_user()
self.msg_helper = self.add_helper(
GoMessageHelper(vumi_helper=self.vumi_helper))
def run_command(self, **kw):
cmd_io = CommandIO()
call_command('go_system_stats',
stdout=cmd_io.stdout, stderr=cmd_io.stderr, **kw)
return cmd_io
def mk_msgs_for_conv(self, conv, inbounds=(), outbounds=()):
""" Utility for adding messages to a conversation.
:param list inbounds:
List of numbers of inbound messages to add. Each set of
inbound messages reuses the same from_addr values which
allows us to tweak the set of inbound uniques.
:param list outbounds:
List of numbers of outbound messages to add. Each set of
outbound messages reuses the same to_addr values which
allows us to tweak the set of outbound uniques.
"""
for inbound in inbounds:
self.msg_helper.add_inbound_to_conv(conv, inbound)
for outbound in outbounds:
self.msg_helper.add_outbound_to_conv(conv, outbound)
def mk_conversations(self, **conv_types):
""" Utility for making conversations in various statuses.
:param dict conv_types:
Mapping from conversation type to a list of status_descriptions.
:param dict status_description:
A dictionary with the keys count (how many conversations to
create, default: 1), status (the conversation status, default:
running), archive_status (default: active) and created_at (
default: now).
"""
for conv_type, conv_descs in conv_types.items():
for conv_desc in conv_descs:
for i in range(conv_desc.get('count', 1)):
conv = self.user_helper.create_conversation(
unicode(conv_type), name=u"%s_%d" % (conv_type, i),
status=unicode(conv_desc.get('status', 'running')),
archive_status=unicode(
conv_desc.get('archive_status', 'active')),
created_at=conv_desc.get(
'created_at', datetime.utcnow()))
self.mk_msgs_for_conv(
conv, inbounds=conv_desc.get('inbounds', ()),
outbounds=conv_desc.get('outbounds', ()))
def assert_csv_output(self, cmd, rows):
self.assertEqual(
cmd.stdout.getvalue().split("\r\n"),
rows + [""])
self.assertEqual(cmd.stderr.getvalue(), "")
def test_help(self):
self.assertRaisesRegexp(
CommandError,
"^Please specify one of the following actions:"
" --conversation-types"
" --conversation-types-by-month"
" --message-counts-by-month$",
self.run_command)
def test_conversation_types_no_conversations(self):
cmd = self.run_command(command=["conversation_types"])
self.assert_csv_output(cmd, [
"type,total",
])
def test_conversation_types(self):
self.mk_conversations(
bulk_message=[
{"count": 3},
{"count": 2, "archive_status": "archived"},
],
jsbox=[
{"count": 1, "status": "stopped"},
{"count": 2},
],
)
cmd = self.run_command(command=["conversation_types"])
self.assert_csv_output(cmd, [
"type,total,running,stopped,active,archived",
"bulk_message,5,3,0,3,2",
"jsbox,3,2,1,3,0",
])
def test_conversation_types_by_month_no_conversations(self):
cmd = self.run_command(command=["conversation_types_by_month"])
self.assert_csv_output(cmd, [
"date",
])
def test_conversation_types_by_month(self):
days = [
datetime(2013, m, d) for m, d in [
(9, 1), (11, 1), (11, 5), (12, 2)
]]
self.mk_conversations(
bulk_message=[
{"count": 3, "created_at": days[0]},
{"count": 2, "created_at": days[1]},
],
jsbox=[
{"count": 1, "created_at": days[2]},
{"count": 2, "created_at": days[3]},
],
)
cmd = self.run_command(command=["conversation_types_by_month"])
self.assert_csv_output(cmd, [
"date,bulk_message,jsbox",
"09/01/2013,3,0",
"11/01/2013,2,1",
"12/01/2013,0,2",
])
def test_message_counts_by_month_no_conversations(self):
cmd = self.run_command(command=["message_counts_by_month"])
self.assert_csv_output(cmd, [
"date,conversations_started,"
"inbound_message_count,outbound_message_count,"
"inbound_uniques,outbound_uniques,total_uniques",
])
def test_message_counts_by_month(self):
days = [
datetime(2013, m, d) for m, d in [
(9, 1), (11, 1), (11, 5), (12, 2)
]]
self.mk_conversations(
bulk_message=[
{"count": 1, "created_at": days[0],
"inbounds": [1, 1, 1], "outbounds": [2, 2, 2]},
{"count": 2, "created_at": days[1],
"inbounds": [1], "outbounds": [1]},
],
jsbox=[
{"count": 1, "created_at": days[2]},
{"count": 2, "created_at": days[3],
"inbounds": [1, 1], "outbounds": [3]},
],
)
cmd = self.run_command(command=["message_counts_by_month"])
# TODO fix once we support uniques properly again
self.assert_csv_output(cmd, [
"date,conversations_started,"
"inbound_message_count,outbound_message_count,"
"inbound_uniques,outbound_uniques,total_uniques",
"09/01/2013,1,3,6,0,0,0",
"11/01/2013,3,2,2,0,0,0",
"12/01/2013,2,4,6,0,0,0",
])
def test_custom_date_format(self):
day = datetime(2013, 11, 1)
self.mk_conversations(
bulk_message=[
{"count": 1, "created_at": day},
],
)
cmd = self.run_command(
command=["conversation_types_by_month"], date_format="%Y-%m-%d")
self.assert_csv_output(cmd, [
"date,bulk_message",
"2013-11-01,1",
])