-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_lottus.py
299 lines (206 loc) · 8.47 KB
/
test_lottus.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import random
from typing import List
import pytest
from entities import Window, Option, Session, GeneratedWindow, Request
from exceptions import InvalidSelectedOptionError
from lottus import LottusContext, SessionProvider, GeneratedWindowProvider, Lottus
from processors import default_processor
def create_lottus_app():
"""
:return:
"""
class InMemorySessionProvider(SessionProvider):
"""
"""
def __init__(self):
"""
"""
self._sessions: List[Session] = []
def get(self, identifier, phone) -> Session:
"""
:param identifier:
:param phone:
:return:
"""
return next((s for s in self._sessions if s.identifier == identifier and s.phone == phone), None)
def save(self, session) -> None:
"""
:param session:
:return:
"""
sess = self.get(session.identifier, session.phone)
if not sess:
self._sessions.append(session)
else:
self._sessions.remove(session)
self._sessions.append(sess)
def finish(self, session: Session) -> None:
"""
:param session:
:return:
"""
sess = self.get(session.identifier, session.phone)
self._sessions.remove(sess)
class InMemoryGeneratedWindowProvider(GeneratedWindowProvider):
"""
"""
def __init__(self):
"""
"""
self.__windows: List[GeneratedWindow] = []
def get(self, name, session_id) -> GeneratedWindow:
"""
:param name:
:param session_id:
:return:
"""
return next((s for s in self.__windows if s.session_identifier == session_id and s.name == name), None)
def save(self, window: GeneratedWindow) -> None:
"""
:param window:
:return:
"""
win = self.get(window.name, window.session_identifier)
if not win:
self.__windows.append(window)
else:
self.__windows.remove(win)
self.__windows.append(window)
lottus_app = Lottus(initial_window='INITIAL',
session_provider=InMemorySessionProvider(),
window_provider=InMemoryGeneratedWindowProvider())
@lottus_app.processor('INFO_PT')
def info_pt(lottus_context: LottusContext, command: str) -> GeneratedWindow:
"""
:param lottus_context:
:param command:
:return:
"""
if lottus_context.current_window.name == "INFO_PT":
return default_processor(lottus_context=lottus_context, command=command)
else:
return GeneratedWindow.fromWindow(
Window(name="INFO_PT", title="Portugues", message="Por favor seleccione uma das opcoes",
options=[
Option(identifier="1", display="Informacao basica", window="BASIC_INFO_PT"),
Option(identifier="2", display="Habilidades", window="SKILLS_INFO_PT"),
Option(identifier="3", display="Contacto", window="CONTACT_INFO_PT"),
Option(identifier="4", display="Formacao academica", window="Academic_INFO_PT"),
Option(identifier="5", display="Mudar idioma", window="INITIAL")
]),
session_identifier=lottus_context.current_session.identifier
)
@lottus_app.processor('BASIC_INFO_PT')
def basic_info_pt(lottus_context: LottusContext, command: str) -> GeneratedWindow:
"""
:param lottus_context:
:param command:
:return:
"""
return GeneratedWindow.fromWindow(
Window(name="BASIC_INFO_PT", title="Portugues", message="Benjamim Chambule\nProject manager @ VM.CO.MZ"),
session_identifier=lottus_context.current_session.identifier
)
@lottus_app.processor("INITIAL")
def initial(lottus_context: LottusContext, command: str) -> GeneratedWindow:
"""
:param lottus_context:
:param command:
:return:
"""
if not lottus_context.current_window:
return GeneratedWindow.fromWindow(
Window(name="INITIAL", title="Ben Chambule's USSD - CV", message="Select language/Escolha idioma",
options=[
Option(identifier="1", display="Portugues", window="INFO_PT"),
Option(identifier="2", display="English", window="INFO_EN")
]),
session_identifier=lottus_context.current_session.identifier
)
else:
return default_processor(lottus_context=lottus_context, command=command)
@lottus_app.processor("INFO_EN")
def info_en(lottus_context: LottusContext, command: str) -> GeneratedWindow:
"""
:param lottus_context:
:param command:
:return:
"""
return GeneratedWindow.fromWindow(
Window(name="INFO_EN", title="English", message="Please select one of the options", options=[
Option(identifier="1", display="Basic Information", window="BASIC_INFO_EN"),
Option(identifier="2", display="Skills", window="SKILLS_INFO_EN"),
Option(identifier="3", display="Contact", window="CONTACT_INFO_EN"),
Option(identifier="4", display="Academic Formation", window="Academic_INFO_EN"),
Option(identifier="5", display="Change Language", window="INITIAL")
]),
session_identifier=lottus_context.current_session.identifier
)
return lottus_app
def test_must_return_portuguese_window() -> None:
"""
:return:
"""
app = create_lottus_app()
session_id = random.randint(1000000, 9999999)
phone = '258840000000'
window = app.process_request(Request(identifier=session_id, phone=phone, command="hello"))
assert window.name == 'INITIAL'
window = app.process_request(Request(identifier=session_id, phone=phone, command="1"))
assert window.name == 'INFO_PT'
def test_must_return_basic_info_pt_window() -> None:
"""
:return:
"""
app = create_lottus_app()
session_id = random.randint(1000000, 9999999)
phone = '258840000000'
window = app.process_request(Request(identifier=session_id, phone=phone, command="hello"))
assert window.name == 'INITIAL'
window = app.process_request(Request(identifier=session_id, phone=phone, command="1"))
assert window.name == 'INFO_PT'
window = app.process_request(Request(identifier=session_id, phone=phone, command="1"))
assert window.name == 'BASIC_INFO_PT'
def test_must_return_english_window() -> None:
"""
:return:
"""
app = create_lottus_app()
session_id = random.randint(1000000, 9999999)
phone = '258840000000'
window = app.process_request(Request(identifier=session_id, phone=phone, command="ola"))
assert window.name == 'INITIAL'
window = app.process_request(Request(identifier=session_id, phone=phone, command="2"))
assert window.name == 'INFO_EN'
def test_must_return_initial_window() -> None:
"""
:return:
"""
app = create_lottus_app()
session_id = random.randint(1000000, 9999999)
phone = '258840000000'
window = app.process_request(Request(identifier=session_id, phone=phone, command="hola"))
assert window.name == 'INITIAL'
def test_must_return_error_menu() -> None:
"""
:return:
"""
app = create_lottus_app()
session_id = random.randint(1000000, 9999999)
phone = '258840000000'
window = app.process_request(Request(identifier=session_id, phone=phone, command="hi"))
assert window.name == 'INITIAL'
with pytest.raises(Exception):
app.process_request(Request(identifier=session_id, phone=phone, command="3"))
@pytest.mark.xfail(raises=InvalidSelectedOptionError)
def test_must_raise_an_exception() -> None:
"""
:return:
"""
app = create_lottus_app()
session_id = random.randint(1000000, 9999999)
phone = '258840000000'
window = app.process_request(Request(identifier=session_id, phone=phone, command="hey"))
assert window.name == 'INITIAL'
with pytest.raises(Exception):
app.process_request(Request(identifier=session_id, phone=phone, command="3"))