-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_unit.py
53 lines (44 loc) · 1.61 KB
/
test_unit.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
import pytest
from faker import Factory
from nameko.testing.services import worker_factory
from paymentreceived import PaymentReceived
from paymentservice import PaymentService
from emailer import InvalidEmail,FailedSendingEmail
fake = Factory.create()
producer = PaymentService()
payload = producer.createData()
class FakeEmailer(object):
"""
We replace the emailer with a fake emailer
not much logic to test, we make sure the
templated email has the right substitutions
by searching and counting the the values in the
final email text.
"""
fields = [
('payee','name'),
('payment','amount'),
('payment','currency'),
('client','name'),
('client','email'),
]
def sendEmail(self,sender,recipient,text,key):
found = 0
for key1,key2 in self.fields:
if text.find(str(payload[key1][key2])) != -1:
found += 1
assert found == len(self.fields)
class InvalidEmailer(object):
def sendEmail(self,sender,recipient,text,key):
raise InvalidEmail()
class FailedEmailer(object):
def sendEmail(self,sender,recipient,text,key):
raise FailedSendingEmail()
def test_payment_service():
service = worker_factory(PaymentReceived,emailer=FakeEmailer())
service.handle_event(payload)
service = worker_factory(PaymentReceived,emailer=InvalidEmailer())
pytest.raises(InvalidEmail,service.handle_event,payload)
service = worker_factory(PaymentReceived,emailer=FailedEmailer())
pytest.raises(FailedSendingEmail,service.handle_event,payload)
test_payment_service()