/
test_signals.py
98 lines (78 loc) · 3.64 KB
/
test_signals.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
import mock
import pytest
from pynamodb.connection import Connection
from pynamodb.signals import _FakeNamespace
from pynamodb.signals import pre_dynamodb_send, post_dynamodb_send
try:
import blinker
except ImportError:
blinker = None
PATCH_METHOD = 'pynamodb.connection.Connection._make_api_call'
@mock.patch(PATCH_METHOD)
@mock.patch('pynamodb.connection.base.uuid')
def test_signal(mock_uuid, mock_req):
pre_recorded = []
post_recorded = []
UUID = '123-abc'
def record_pre_dynamodb_send(sender, operation_name, table_name, req_uuid):
pre_recorded.append((operation_name, table_name, req_uuid))
def record_post_dynamodb_send(sender, operation_name, table_name, req_uuid):
post_recorded.append((operation_name, table_name, req_uuid))
pre_dynamodb_send.connect(record_pre_dynamodb_send)
post_dynamodb_send.connect(record_post_dynamodb_send)
try:
mock_uuid.uuid4.return_value = UUID
mock_req.return_value = {'TableDescription': {'TableName': 'table', 'TableStatus': 'Creating'}}
c = Connection()
c.dispatch('CreateTable', {'TableName': 'MyTable'})
assert ('CreateTable', 'MyTable', UUID) == pre_recorded[0]
assert ('CreateTable', 'MyTable', UUID) == post_recorded[0]
finally:
pre_dynamodb_send.disconnect(record_pre_dynamodb_send)
post_dynamodb_send.disconnect(record_post_dynamodb_send)
@mock.patch(PATCH_METHOD)
@mock.patch('pynamodb.connection.base.uuid')
def test_signal_exception_pre_signal(mock_uuid, mock_req):
post_recorded = []
UUID = '123-abc'
def record_pre_dynamodb_send(sender, operation_name, table_name, req_uuid):
raise ValueError()
def record_post_dynamodb_send(sender, operation_name, table_name, req_uuid):
post_recorded.append((operation_name, table_name, req_uuid))
pre_dynamodb_send.connect(record_pre_dynamodb_send)
post_dynamodb_send.connect(record_post_dynamodb_send)
try:
mock_uuid.uuid4.return_value = UUID
mock_req.return_value = {'TableDescription': {'TableName': 'table', 'TableStatus': 'Creating'}}
c = Connection()
c.dispatch('CreateTable', {'TableName': 'MyTable'})
assert ('CreateTable', 'MyTable', UUID) == post_recorded[0]
finally:
pre_dynamodb_send.disconnect(record_pre_dynamodb_send)
post_dynamodb_send.disconnect(record_post_dynamodb_send)
@mock.patch(PATCH_METHOD)
@mock.patch('pynamodb.connection.base.uuid')
def test_signal_exception_post_signal(mock_uuid, mock_req):
pre_recorded = []
UUID = '123-abc'
def record_pre_dynamodb_send(sender, operation_name, table_name, req_uuid):
pre_recorded.append((operation_name, table_name, req_uuid))
def record_post_dynamodb_send(sender, operation_name, table_name, req_uuid):
raise ValueError()
pre_dynamodb_send.connect(record_pre_dynamodb_send)
post_dynamodb_send.connect(record_post_dynamodb_send)
try:
mock_uuid.uuid4.return_value = UUID
mock_req.return_value = {'TableDescription': {'TableName': 'table', 'TableStatus': 'Creating'}}
c = Connection()
c.dispatch('CreateTable', {'TableName': 'MyTable'})
assert ('CreateTable', 'MyTable', UUID) == pre_recorded[0]
finally:
pre_dynamodb_send.disconnect(record_pre_dynamodb_send)
post_dynamodb_send.disconnect(record_post_dynamodb_send)
def test_fake_signals():
_signals = _FakeNamespace()
pre_dynamodb_send = _signals.signal('pre_dynamodb_send')
with pytest.raises(RuntimeError):
pre_dynamodb_send.connect(lambda x: x)
pre_dynamodb_send.send(object, operation_name="UPDATE", table_name="TEST", req_uuid="something")