finalsa-common-models 0.1.0__tar.gz → 1.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {finalsa_common_models-0.1.0/finalsa_common_models.egg-info → finalsa_common_models-1.0.1}/PKG-INFO +1 -1
- {finalsa_common_models-0.1.0 → finalsa_common_models-1.0.1}/finalsa/common/models/__init__.py +1 -1
- {finalsa_common_models-0.1.0 → finalsa_common_models-1.0.1}/finalsa/common/models/models/sqs_message.py +6 -4
- {finalsa_common_models-0.1.0 → finalsa_common_models-1.0.1}/finalsa/common/models/models/sqs_response.py +53 -39
- {finalsa_common_models-0.1.0 → finalsa_common_models-1.0.1/finalsa_common_models.egg-info}/PKG-INFO +1 -1
- finalsa_common_models-1.0.1/tests/test_client.py +355 -0
- finalsa_common_models-0.1.0/tests/test_client.py +0 -177
- {finalsa_common_models-0.1.0 → finalsa_common_models-1.0.1}/LICENSE.md +0 -0
- {finalsa_common_models-0.1.0 → finalsa_common_models-1.0.1}/README.md +0 -0
- {finalsa_common_models-0.1.0 → finalsa_common_models-1.0.1}/finalsa/common/models/models/__init__.py +0 -0
- {finalsa_common_models-0.1.0 → finalsa_common_models-1.0.1}/finalsa/common/models/models/functions.py +0 -0
- {finalsa_common_models-0.1.0 → finalsa_common_models-1.0.1}/finalsa/common/models/py.typed +0 -0
- {finalsa_common_models-0.1.0 → finalsa_common_models-1.0.1}/finalsa_common_models.egg-info/SOURCES.txt +0 -0
- {finalsa_common_models-0.1.0 → finalsa_common_models-1.0.1}/finalsa_common_models.egg-info/dependency_links.txt +0 -0
- {finalsa_common_models-0.1.0 → finalsa_common_models-1.0.1}/finalsa_common_models.egg-info/requires.txt +0 -0
- {finalsa_common_models-0.1.0 → finalsa_common_models-1.0.1}/finalsa_common_models.egg-info/top_level.txt +0 -0
- {finalsa_common_models-0.1.0 → finalsa_common_models-1.0.1}/finalsa_common_models.egg-info/zip-safe +0 -0
- {finalsa_common_models-0.1.0 → finalsa_common_models-1.0.1}/setup.cfg +0 -0
- {finalsa_common_models-0.1.0 → finalsa_common_models-1.0.1}/setup.py +0 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
from datetime import datetime, timezone
|
|
2
|
-
from typing import Optional
|
|
2
|
+
from typing import Optional, Union, Dict
|
|
3
3
|
from uuid import UUID
|
|
4
4
|
from pydantic import BaseModel
|
|
5
5
|
from json import loads
|
|
@@ -8,12 +8,14 @@ from json import loads
|
|
|
8
8
|
class SqsMessage(BaseModel):
|
|
9
9
|
id: UUID
|
|
10
10
|
topic: str
|
|
11
|
-
payload: str
|
|
11
|
+
payload: Union[str, Dict]
|
|
12
12
|
correlation_id: str
|
|
13
13
|
timestamp: Optional[datetime] = datetime.now(timezone.utc)
|
|
14
14
|
|
|
15
|
-
def get_payload(self):
|
|
16
|
-
|
|
15
|
+
def get_payload(self) -> Dict:
|
|
16
|
+
if isinstance(self.payload, str):
|
|
17
|
+
self.payload = loads(self.payload)
|
|
18
|
+
return self.payload
|
|
17
19
|
|
|
18
20
|
def to_dict(self):
|
|
19
21
|
return {
|
|
@@ -17,9 +17,6 @@ class SqsReponse(BaseModel):
|
|
|
17
17
|
md5_of_message_attributes: Optional[str] = ""
|
|
18
18
|
message_attributes: Optional[Dict] = {}
|
|
19
19
|
|
|
20
|
-
def get_payload(self) -> Dict:
|
|
21
|
-
return loads(self.body)
|
|
22
|
-
|
|
23
20
|
@staticmethod
|
|
24
21
|
def correlation_id_from_attributes(attributes: Dict) -> Optional[str]:
|
|
25
22
|
correlation_id = attributes.get('correlation_id', None)
|
|
@@ -31,60 +28,77 @@ class SqsReponse(BaseModel):
|
|
|
31
28
|
return correlation_id["Value"]
|
|
32
29
|
return None
|
|
33
30
|
|
|
34
|
-
def get_correlation_id(self) -> Union[str, UUID]:
|
|
31
|
+
def get_correlation_id(self, payload: Optional[Dict] = {}) -> Union[str, UUID]:
|
|
35
32
|
correlation_id = self.correlation_id_from_attributes(self.message_attributes)
|
|
36
33
|
if correlation_id:
|
|
37
34
|
return correlation_id
|
|
38
35
|
correlation_id = self.correlation_id_from_attributes(self.attributes)
|
|
39
36
|
if correlation_id:
|
|
40
37
|
return correlation_id
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
except Exception:
|
|
45
|
-
return str(uuid4())
|
|
38
|
+
if 'correlation_id' in payload:
|
|
39
|
+
return payload['correlation_id']
|
|
40
|
+
return str(uuid4())
|
|
46
41
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
42
|
+
@staticmethod
|
|
43
|
+
def __is_sns_message__(content: Dict) -> bool:
|
|
44
|
+
return 'Type' in content and content['Type'] == 'Notification'
|
|
45
|
+
|
|
46
|
+
@staticmethod
|
|
47
|
+
def __is_sqs_message__(content: Dict) -> bool:
|
|
48
|
+
return ('id' in content and
|
|
49
|
+
'topic' in content and
|
|
50
|
+
'payload' in content)
|
|
51
|
+
|
|
52
|
+
def parse_from_sns(self) -> Dict:
|
|
53
|
+
payload = loads(self.body)
|
|
54
|
+
if self.__is_sns_message__(payload):
|
|
55
|
+
return self.__parse_from_sns__(payload)
|
|
56
|
+
raise ValueError('The message is not a SNS message')
|
|
57
|
+
|
|
58
|
+
def __parse_from_sns__(self, payload: Dict) -> Union[str, Dict]:
|
|
51
59
|
self.topic = str(payload['TopicArn'].split(':')[-1]).lower()
|
|
52
|
-
self.body = payload['Message']
|
|
53
60
|
self.message_attributes = parse_message_attributes(
|
|
54
61
|
payload.get('MessageAttributes', {}))
|
|
55
|
-
|
|
62
|
+
try:
|
|
63
|
+
return loads(payload['Message'])
|
|
64
|
+
except Exception:
|
|
65
|
+
return payload['Message']
|
|
56
66
|
|
|
57
67
|
def parse(self) -> Optional[Dict]:
|
|
58
|
-
|
|
59
|
-
if
|
|
60
|
-
|
|
61
|
-
return
|
|
68
|
+
content = loads(self.body)
|
|
69
|
+
if self.__is_sns_message__(content):
|
|
70
|
+
content = self.__parse_from_sns__(content)
|
|
71
|
+
return content
|
|
62
72
|
|
|
63
|
-
def
|
|
64
|
-
|
|
65
|
-
if
|
|
66
|
-
if 'correlation_id' not in
|
|
67
|
-
|
|
68
|
-
if 'timestamp' not in sns_payload:
|
|
69
|
-
sns_payload['timestamp'] = datetime.now(timezone.utc).isoformat()
|
|
73
|
+
def __get_sqs_message__(self, content: Union[str, Dict]) -> SqsMessage:
|
|
74
|
+
|
|
75
|
+
if isinstance(content, dict) and self.__is_sqs_message__(content):
|
|
76
|
+
if 'correlation_id' not in content:
|
|
77
|
+
content['correlation_id'] = str(self.get_correlation_id(content))
|
|
70
78
|
return SqsMessage(
|
|
71
|
-
id=UUID(
|
|
72
|
-
topic=
|
|
73
|
-
payload=
|
|
74
|
-
correlation_id=
|
|
75
|
-
timestamp=
|
|
79
|
+
id=UUID(content['id']),
|
|
80
|
+
topic=content['topic'],
|
|
81
|
+
payload=content['payload'],
|
|
82
|
+
correlation_id=content['correlation_id'],
|
|
83
|
+
timestamp=content.get('timestamp', datetime.now(timezone.utc).isoformat())
|
|
76
84
|
)
|
|
77
|
-
payload = self.get_payload()
|
|
78
|
-
if 'correlation_id' not in payload:
|
|
79
|
-
payload['correlation_id'] = str(self.get_correlation_id())
|
|
80
85
|
return SqsMessage(
|
|
81
|
-
id=
|
|
82
|
-
topic=
|
|
83
|
-
payload=
|
|
84
|
-
correlation_id=
|
|
85
|
-
timestamp=
|
|
86
|
+
id=uuid4(),
|
|
87
|
+
topic=self.topic,
|
|
88
|
+
payload=content,
|
|
89
|
+
correlation_id=self.get_correlation_id(content),
|
|
90
|
+
timestamp=datetime.now(timezone.utc).isoformat()
|
|
86
91
|
)
|
|
87
92
|
|
|
93
|
+
def get_sqs_message(self) -> SqsMessage:
|
|
94
|
+
try:
|
|
95
|
+
content = loads(self.body)
|
|
96
|
+
except Exception:
|
|
97
|
+
return self.__get_sqs_message__(self.body)
|
|
98
|
+
if self.__is_sns_message__(content):
|
|
99
|
+
content = self.__parse_from_sns__(content)
|
|
100
|
+
return self.__get_sqs_message__(content)
|
|
101
|
+
|
|
88
102
|
@classmethod
|
|
89
103
|
def from_boto_response(cls, response: Dict):
|
|
90
104
|
return cls(
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
from finalsa.common.models import (
|
|
2
|
+
SqsMessage, SqsReponse, parse_message_attributes,
|
|
3
|
+
to_message_attributes, __version__)
|
|
4
|
+
import os
|
|
5
|
+
import sys
|
|
6
|
+
import uuid
|
|
7
|
+
import datetime
|
|
8
|
+
import decimal
|
|
9
|
+
|
|
10
|
+
from json import dumps
|
|
11
|
+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def test_version():
|
|
15
|
+
assert __version__ is not None
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_parse_message_attributes():
|
|
19
|
+
attributes = {
|
|
20
|
+
'string': {'Type': 'String', 'Value': 'test'},
|
|
21
|
+
'number': {'Type': 'Number', 'Value': '1'},
|
|
22
|
+
'binary': {'Type': 'Binary', 'Value': 'test'}
|
|
23
|
+
}
|
|
24
|
+
result = parse_message_attributes(attributes)
|
|
25
|
+
assert result['string'] == 'test'
|
|
26
|
+
assert result['number'] == 1
|
|
27
|
+
assert result['binary'] == b'test'
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def test_sqs_message():
|
|
31
|
+
message = SqsMessage(
|
|
32
|
+
id='123e4567-e89b-12d3-a456-426614174000',
|
|
33
|
+
topic='test',
|
|
34
|
+
payload='{"test": "test"}',
|
|
35
|
+
correlation_id='123e4567-e89b-12d3-a456-426614174000'
|
|
36
|
+
)
|
|
37
|
+
assert str(message.id) == '123e4567-e89b-12d3-a456-426614174000'
|
|
38
|
+
assert message.topic == 'test'
|
|
39
|
+
assert message.get_payload() == {'test': 'test'}
|
|
40
|
+
assert message.correlation_id == '123e4567-e89b-12d3-a456-426614174000'
|
|
41
|
+
assert message.timestamp is not None
|
|
42
|
+
assert message.to_dict() == {
|
|
43
|
+
'id': '123e4567-e89b-12d3-a456-426614174000',
|
|
44
|
+
'topic': 'test',
|
|
45
|
+
'payload': {'test': 'test'},
|
|
46
|
+
'correlation_id': '123e4567-e89b-12d3-a456-426614174000',
|
|
47
|
+
'timestamp': message.timestamp.isoformat()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def test_sqs_message_str():
|
|
52
|
+
message = SqsMessage(
|
|
53
|
+
id='123e4567-e89b-12d3-a456-426614174000',
|
|
54
|
+
topic='test',
|
|
55
|
+
payload='{"test: "test"}',
|
|
56
|
+
correlation_id='123e4567-e89b-12d3-a456-426614174000'
|
|
57
|
+
)
|
|
58
|
+
assert str(message.id) == '123e4567-e89b-12d3-a456-426614174000'
|
|
59
|
+
assert message.topic == 'test'
|
|
60
|
+
assert message.correlation_id == '123e4567-e89b-12d3-a456-426614174000'
|
|
61
|
+
assert message.timestamp is not None
|
|
62
|
+
assert message.to_dict() == {
|
|
63
|
+
'id': '123e4567-e89b-12d3-a456-426614174000',
|
|
64
|
+
'topic': 'test',
|
|
65
|
+
'payload': '{"test: "test"}',
|
|
66
|
+
'correlation_id': '123e4567-e89b-12d3-a456-426614174000',
|
|
67
|
+
'timestamp': message.timestamp.isoformat()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def test_sqs_response():
|
|
72
|
+
response = SqsReponse(
|
|
73
|
+
message_id='test',
|
|
74
|
+
receipt_handle='test',
|
|
75
|
+
md5_of_body='test',
|
|
76
|
+
body='test',
|
|
77
|
+
attributes={'test': 'test',
|
|
78
|
+
'correlation_id': '123e4567-e89b-12d3-a456-426614174000'},
|
|
79
|
+
topic='test',
|
|
80
|
+
md5_of_message_attributes='test',
|
|
81
|
+
message_attributes={'correlation_id': {'Type': 'String',
|
|
82
|
+
'Value': '123e4567-e89b-12d3-a456-426614174000'}}
|
|
83
|
+
|
|
84
|
+
)
|
|
85
|
+
assert response.body == 'test'
|
|
86
|
+
assert str(response.get_correlation_id()) == '123e4567-e89b-12d3-a456-426614174000'
|
|
87
|
+
assert response.topic == 'test'
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_sqs_response_from_boto_response():
|
|
91
|
+
boto_response = {
|
|
92
|
+
'MessageId': 'test',
|
|
93
|
+
'ReceiptHandle': 'test',
|
|
94
|
+
'MD5OfBody': 'test',
|
|
95
|
+
'Body': 'test',
|
|
96
|
+
'Attributes': {'test': 'test', 'correlation_id': '123e4567-e89b-12d3-a456-426614174000'},
|
|
97
|
+
'MessageAttributes': {'correlation_id': {'Type': 'String', 'Value': '123e4567-e89b-12d3-a456-426614174000'}}
|
|
98
|
+
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
response = SqsReponse.from_boto_response(boto_response)
|
|
102
|
+
assert response.body == 'test'
|
|
103
|
+
assert str(response.get_correlation_id()) == '123e4567-e89b-12d3-a456-426614174000'
|
|
104
|
+
assert response.topic == ''
|
|
105
|
+
assert response.message_attributes == {
|
|
106
|
+
'correlation_id': "123e4567-e89b-12d3-a456-426614174000"}
|
|
107
|
+
assert response.attributes == {
|
|
108
|
+
'test': 'test', 'correlation_id': '123e4567-e89b-12d3-a456-426614174000'}
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def test_sqs_response_no_correlation_id():
|
|
112
|
+
response = SqsReponse(
|
|
113
|
+
message_id='test',
|
|
114
|
+
receipt_handle='test',
|
|
115
|
+
md5_of_body='test',
|
|
116
|
+
body='test',
|
|
117
|
+
attributes={'test': 'test'},
|
|
118
|
+
topic='test',
|
|
119
|
+
md5_of_message_attributes='test',
|
|
120
|
+
)
|
|
121
|
+
assert response.body == 'test'
|
|
122
|
+
assert response.get_correlation_id() is not None
|
|
123
|
+
assert response.topic == 'test'
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def test_sqs_message_from_dict():
|
|
127
|
+
|
|
128
|
+
message = SqsMessage(
|
|
129
|
+
id='123e4567-e89b-12d3-a456-426614174000',
|
|
130
|
+
topic='test',
|
|
131
|
+
payload='{"test": "test"}',
|
|
132
|
+
correlation_id='123e4567-e89b-12d3-a456-426614174000'
|
|
133
|
+
)
|
|
134
|
+
message_dict = message.to_dict()
|
|
135
|
+
assert str(message.id) == message_dict['id']
|
|
136
|
+
assert message.topic == message_dict['topic']
|
|
137
|
+
assert message.get_payload() == {'test': 'test'}
|
|
138
|
+
assert message.correlation_id == message_dict['correlation_id']
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def test_to_message_attributes():
|
|
142
|
+
attributes = {
|
|
143
|
+
'string': 'test',
|
|
144
|
+
'number': 1,
|
|
145
|
+
'binary': b'test',
|
|
146
|
+
'uuid': uuid.uuid4(),
|
|
147
|
+
'datetime': datetime.datetime.now(),
|
|
148
|
+
'decimal': decimal.Decimal('1.0')
|
|
149
|
+
}
|
|
150
|
+
result = to_message_attributes(attributes)
|
|
151
|
+
assert result['string']['DataType'] == 'String'
|
|
152
|
+
assert result['string']['StringValue'] == 'test'
|
|
153
|
+
assert result['number']['DataType'] == 'Number'
|
|
154
|
+
assert result['number']['StringValue'] == '1'
|
|
155
|
+
assert result['binary']['DataType'] == 'Binary'
|
|
156
|
+
assert result['binary']['BinaryValue'] == b'test'
|
|
157
|
+
assert result['uuid']['DataType'] == 'String'
|
|
158
|
+
assert result['uuid']['StringValue'] == str(attributes['uuid'])
|
|
159
|
+
assert result['datetime']['DataType'] == 'String'
|
|
160
|
+
assert result['datetime']['StringValue'] == attributes['datetime'].isoformat()
|
|
161
|
+
assert result['decimal']['DataType'] == 'Number'
|
|
162
|
+
assert result['decimal']['StringValue'] == '1.0'
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def test_parse_from_sns_response():
|
|
166
|
+
real_message_body = {
|
|
167
|
+
"test": "test"
|
|
168
|
+
}
|
|
169
|
+
body = dumps({
|
|
170
|
+
'Type': 'Notification',
|
|
171
|
+
'TopicArn': 'mytopic',
|
|
172
|
+
'Message': dumps(real_message_body),
|
|
173
|
+
'MessageAttributes': {'correlation_id': {
|
|
174
|
+
'Type': 'String', 'Value': '123e4567-e89b-12d3-a456-426614174000'
|
|
175
|
+
}}
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
boto_response = {
|
|
179
|
+
'MessageId': 'test',
|
|
180
|
+
'ReceiptHandle': 'test',
|
|
181
|
+
'MD5OfBody': 'test',
|
|
182
|
+
'Body': body,
|
|
183
|
+
'Attributes': {'test': 'test', 'correlation_id': '123e4567-e89b-12d3-a456-426614174000'},
|
|
184
|
+
'MessageAttributes': {'correlation_id': {'Type': 'String', 'Value': '123e4567-e89b-12d3-a456-426614174000'}}
|
|
185
|
+
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
response = SqsReponse.from_boto_response(boto_response)
|
|
189
|
+
assert response.body == body
|
|
190
|
+
assert response.get_correlation_id() == '123e4567-e89b-12d3-a456-426614174000'
|
|
191
|
+
assert response.topic == ''
|
|
192
|
+
assert response.message_attributes == {
|
|
193
|
+
'correlation_id': "123e4567-e89b-12d3-a456-426614174000"}
|
|
194
|
+
assert response.attributes == {
|
|
195
|
+
'test': 'test', 'correlation_id': '123e4567-e89b-12d3-a456-426614174000'}
|
|
196
|
+
assert response.parse_from_sns() == real_message_body
|
|
197
|
+
assert response.parse() == real_message_body
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def test_parse_from_sns_response_sqs_message():
|
|
201
|
+
real_message_body = {
|
|
202
|
+
"id": "123e4567-e89b-12d3-a456-426614174000",
|
|
203
|
+
}
|
|
204
|
+
sqs_message_payload = {
|
|
205
|
+
"id": "123e4567-e89b-12d3-a456-426614174000",
|
|
206
|
+
"topic": "test",
|
|
207
|
+
"payload": dumps(real_message_body),
|
|
208
|
+
"correlation_id": "123e4567-e89b-12d3-a456-426614174000",
|
|
209
|
+
"timestamp": datetime.datetime.now().isoformat()
|
|
210
|
+
}
|
|
211
|
+
body = dumps({
|
|
212
|
+
'Type': 'Notification',
|
|
213
|
+
'TopicArn': 'mytopic',
|
|
214
|
+
'Message': dumps(sqs_message_payload),
|
|
215
|
+
'MessageAttributes': {'correlation_id': {
|
|
216
|
+
'Type': 'String', 'Value': '123e4567-e89b-12d3-a456-426614174000'
|
|
217
|
+
}}
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
boto_response = {
|
|
221
|
+
'MessageId': 'test',
|
|
222
|
+
'ReceiptHandle': 'test',
|
|
223
|
+
'MD5OfBody': 'test',
|
|
224
|
+
'Body': body,
|
|
225
|
+
'Attributes': {'test': 'test', 'correlation_id': '123e4567-e89b-12d3-a456-426614174000'},
|
|
226
|
+
'MessageAttributes': {'correlation_id': {'Type': 'String', 'Value': '123e4567-e89b-12d3-a456-426614174000'}}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
response = SqsReponse.from_boto_response(boto_response)
|
|
230
|
+
assert response.body == body
|
|
231
|
+
assert response.get_correlation_id() == '123e4567-e89b-12d3-a456-426614174000'
|
|
232
|
+
assert response.topic == ''
|
|
233
|
+
assert response.message_attributes == {
|
|
234
|
+
'correlation_id': "123e4567-e89b-12d3-a456-426614174000"}
|
|
235
|
+
assert response.attributes == {
|
|
236
|
+
'test': 'test', 'correlation_id': '123e4567-e89b-12d3-a456-426614174000'}
|
|
237
|
+
sqs_message = response.get_sqs_message()
|
|
238
|
+
assert sqs_message.id == uuid.UUID(sqs_message_payload['id'])
|
|
239
|
+
assert sqs_message.topic == sqs_message_payload['topic']
|
|
240
|
+
assert sqs_message.get_payload() == real_message_body
|
|
241
|
+
assert sqs_message.correlation_id == sqs_message_payload['correlation_id']
|
|
242
|
+
assert sqs_message.timestamp.isoformat() == sqs_message_payload['timestamp']
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def test_parse_from_sqs_response_sqs_message():
|
|
246
|
+
real_message_body = {
|
|
247
|
+
"id": "123e4567-e89b-12d3-a456-426614174000",
|
|
248
|
+
}
|
|
249
|
+
sqs_message_payload = {
|
|
250
|
+
"id": "123e4567-e89b-12d3-a456-426614174000",
|
|
251
|
+
"topic": "test",
|
|
252
|
+
"payload": dumps(real_message_body),
|
|
253
|
+
"correlation_id": "123e4567-e89b-12d3-a456-426614174000",
|
|
254
|
+
"timestamp": datetime.datetime.now().isoformat()
|
|
255
|
+
}
|
|
256
|
+
body = dumps(sqs_message_payload)
|
|
257
|
+
boto_response = {
|
|
258
|
+
'MessageId': 'test',
|
|
259
|
+
'ReceiptHandle': 'test',
|
|
260
|
+
'MD5OfBody': 'test',
|
|
261
|
+
'Body': body,
|
|
262
|
+
'Attributes': {'test': 'test', 'correlation_id': '123e4567-e89b-12d3-a456-426614174000'},
|
|
263
|
+
'MessageAttributes': {'correlation_id': {'Type': 'String', 'Value': '123e4567-e89b-12d3-a456-426614174000'}}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
response = SqsReponse.from_boto_response(boto_response)
|
|
267
|
+
assert response.body == body
|
|
268
|
+
assert response.get_correlation_id() == '123e4567-e89b-12d3-a456-426614174000'
|
|
269
|
+
assert response.topic == ''
|
|
270
|
+
assert response.message_attributes == {
|
|
271
|
+
'correlation_id': "123e4567-e89b-12d3-a456-426614174000"}
|
|
272
|
+
assert response.attributes == {
|
|
273
|
+
'test': 'test', 'correlation_id': '123e4567-e89b-12d3-a456-426614174000'}
|
|
274
|
+
sqs_message = response.get_sqs_message()
|
|
275
|
+
assert sqs_message.id == uuid.UUID(sqs_message_payload['id'])
|
|
276
|
+
assert sqs_message.topic == sqs_message_payload['topic']
|
|
277
|
+
assert sqs_message.get_payload() == real_message_body
|
|
278
|
+
assert sqs_message.correlation_id == sqs_message_payload['correlation_id']
|
|
279
|
+
assert sqs_message.timestamp.isoformat() == sqs_message_payload['timestamp']
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def test_parse_from_sns_response_non_valid_sqs_message():
|
|
283
|
+
real_message_body = {
|
|
284
|
+
"id": "123e4567-e89b-12d3-a456-426614174000",
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
body = dumps({
|
|
288
|
+
'Type': 'Notification',
|
|
289
|
+
'TopicArn': 'mytopic',
|
|
290
|
+
'Message': dumps(real_message_body),
|
|
291
|
+
'MessageAttributes': {'correlation_id': {
|
|
292
|
+
'Type': 'String', 'Value': '123e4567-e89b-12d3-a456-426614174000'
|
|
293
|
+
}}
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
boto_response = {
|
|
297
|
+
'MessageId': 'test',
|
|
298
|
+
'ReceiptHandle': 'test',
|
|
299
|
+
'MD5OfBody': 'test',
|
|
300
|
+
'Body': body,
|
|
301
|
+
'Attributes': {'test': 'test', 'correlation_id': '123e4567-e89b-12d3-a456-426614174000'},
|
|
302
|
+
'MessageAttributes': {'correlation_id': {'Type': 'String', 'Value': '123e4567-e89b-12d3-a456-426614174000'}}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
response = SqsReponse.from_boto_response(boto_response)
|
|
306
|
+
assert response.body == body
|
|
307
|
+
assert response.get_correlation_id() == '123e4567-e89b-12d3-a456-426614174000'
|
|
308
|
+
assert response.topic == ''
|
|
309
|
+
assert response.message_attributes == {
|
|
310
|
+
'correlation_id': "123e4567-e89b-12d3-a456-426614174000"}
|
|
311
|
+
assert response.attributes == {
|
|
312
|
+
'test': 'test', 'correlation_id': '123e4567-e89b-12d3-a456-426614174000'}
|
|
313
|
+
sqs_message = response.get_sqs_message()
|
|
314
|
+
assert sqs_message.id is not None
|
|
315
|
+
assert sqs_message.get_payload() == real_message_body
|
|
316
|
+
assert sqs_message.correlation_id == '123e4567-e89b-12d3-a456-426614174000'
|
|
317
|
+
assert sqs_message.timestamp is not None
|
|
318
|
+
assert sqs_message.topic == 'mytopic'
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def test_parse_from_sns_response_non_valid_str_sqs_message():
|
|
323
|
+
real_message_body = "a"
|
|
324
|
+
body = dumps({
|
|
325
|
+
'Type': 'Notification',
|
|
326
|
+
'TopicArn': 'mytopic',
|
|
327
|
+
'Message': real_message_body,
|
|
328
|
+
'MessageAttributes': {'correlation_id': {
|
|
329
|
+
'Type': 'String', 'Value': '123e4567-e89b-12d3-a456-426614174000'
|
|
330
|
+
}}
|
|
331
|
+
})
|
|
332
|
+
|
|
333
|
+
boto_response = {
|
|
334
|
+
'MessageId': 'test',
|
|
335
|
+
'ReceiptHandle': 'test',
|
|
336
|
+
'MD5OfBody': 'test',
|
|
337
|
+
'Body': body,
|
|
338
|
+
'Attributes': {'test': 'test', 'correlation_id': '123e4567-e89b-12d3-a456-426614174000'},
|
|
339
|
+
'MessageAttributes': {'correlation_id': {'Type': 'String', 'Value': '123e4567-e89b-12d3-a456-426614174000'}}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
response = SqsReponse.from_boto_response(boto_response)
|
|
343
|
+
assert response.body == body
|
|
344
|
+
assert response.get_correlation_id() == '123e4567-e89b-12d3-a456-426614174000'
|
|
345
|
+
assert response.topic == ''
|
|
346
|
+
assert response.message_attributes == {
|
|
347
|
+
'correlation_id': "123e4567-e89b-12d3-a456-426614174000"}
|
|
348
|
+
assert response.attributes == {
|
|
349
|
+
'test': 'test', 'correlation_id': '123e4567-e89b-12d3-a456-426614174000'}
|
|
350
|
+
sqs_message = response.get_sqs_message()
|
|
351
|
+
assert sqs_message.id is not None
|
|
352
|
+
assert sqs_message.payload == real_message_body
|
|
353
|
+
assert sqs_message.correlation_id == '123e4567-e89b-12d3-a456-426614174000'
|
|
354
|
+
assert sqs_message.timestamp is not None
|
|
355
|
+
assert sqs_message.topic == 'mytopic'
|
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
from finalsa.common.models import (
|
|
2
|
-
SqsMessage, SqsReponse, parse_message_attributes,
|
|
3
|
-
to_message_attributes, __version__)
|
|
4
|
-
import os
|
|
5
|
-
import sys
|
|
6
|
-
import uuid
|
|
7
|
-
import datetime
|
|
8
|
-
import decimal
|
|
9
|
-
|
|
10
|
-
from json import dumps
|
|
11
|
-
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')))
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
def test_version():
|
|
15
|
-
assert __version__ is not None
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
def test_parse_message_attributes():
|
|
19
|
-
attributes = {
|
|
20
|
-
'string': {'Type': 'String', 'Value': 'test'},
|
|
21
|
-
'number': {'Type': 'Number', 'Value': '1'},
|
|
22
|
-
'binary': {'Type': 'Binary', 'Value': 'test'}
|
|
23
|
-
}
|
|
24
|
-
result = parse_message_attributes(attributes)
|
|
25
|
-
assert result['string'] == 'test'
|
|
26
|
-
assert result['number'] == 1
|
|
27
|
-
assert result['binary'] == b'test'
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
def test_sqs_message():
|
|
31
|
-
message = SqsMessage(
|
|
32
|
-
id='123e4567-e89b-12d3-a456-426614174000',
|
|
33
|
-
topic='test',
|
|
34
|
-
payload='{"test": "test"}',
|
|
35
|
-
correlation_id='123e4567-e89b-12d3-a456-426614174000'
|
|
36
|
-
)
|
|
37
|
-
assert str(message.id) == '123e4567-e89b-12d3-a456-426614174000'
|
|
38
|
-
assert message.topic == 'test'
|
|
39
|
-
assert message.get_payload() == {'test': 'test'}
|
|
40
|
-
assert message.correlation_id == '123e4567-e89b-12d3-a456-426614174000'
|
|
41
|
-
assert message.timestamp is not None
|
|
42
|
-
assert message.to_dict() == {
|
|
43
|
-
'id': '123e4567-e89b-12d3-a456-426614174000',
|
|
44
|
-
'topic': 'test',
|
|
45
|
-
'payload': '{"test": "test"}',
|
|
46
|
-
'correlation_id': '123e4567-e89b-12d3-a456-426614174000',
|
|
47
|
-
'timestamp': message.timestamp.isoformat()
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
def test_sqs_response():
|
|
52
|
-
response = SqsReponse(
|
|
53
|
-
message_id='test',
|
|
54
|
-
receipt_handle='test',
|
|
55
|
-
md5_of_body='test',
|
|
56
|
-
body='test',
|
|
57
|
-
attributes={'test': 'test',
|
|
58
|
-
'correlation_id': '123e4567-e89b-12d3-a456-426614174000'},
|
|
59
|
-
topic='test',
|
|
60
|
-
md5_of_message_attributes='test',
|
|
61
|
-
message_attributes={'correlation_id': {'Type': 'String',
|
|
62
|
-
'Value': '123e4567-e89b-12d3-a456-426614174000'}}
|
|
63
|
-
|
|
64
|
-
)
|
|
65
|
-
assert response.body == 'test'
|
|
66
|
-
assert str(response.get_correlation_id()) == '123e4567-e89b-12d3-a456-426614174000'
|
|
67
|
-
assert response.topic == 'test'
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
def test_sqs_response_from_boto_response():
|
|
71
|
-
boto_response = {
|
|
72
|
-
'MessageId': 'test',
|
|
73
|
-
'ReceiptHandle': 'test',
|
|
74
|
-
'MD5OfBody': 'test',
|
|
75
|
-
'Body': 'test',
|
|
76
|
-
'Attributes': {'test': 'test', 'correlation_id': '123e4567-e89b-12d3-a456-426614174000'},
|
|
77
|
-
'MessageAttributes': {'correlation_id': {'Type': 'String', 'Value': '123e4567-e89b-12d3-a456-426614174000'}}
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
response = SqsReponse.from_boto_response(boto_response)
|
|
82
|
-
assert response.body == 'test'
|
|
83
|
-
assert str(response.get_correlation_id()) == '123e4567-e89b-12d3-a456-426614174000'
|
|
84
|
-
assert response.topic == ''
|
|
85
|
-
assert response.message_attributes == {
|
|
86
|
-
'correlation_id': "123e4567-e89b-12d3-a456-426614174000"}
|
|
87
|
-
assert response.attributes == {
|
|
88
|
-
'test': 'test', 'correlation_id': '123e4567-e89b-12d3-a456-426614174000'}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
def test_sqs_response_no_correlation_id():
|
|
92
|
-
response = SqsReponse(
|
|
93
|
-
message_id='test',
|
|
94
|
-
receipt_handle='test',
|
|
95
|
-
md5_of_body='test',
|
|
96
|
-
body='test',
|
|
97
|
-
attributes={'test': 'test'},
|
|
98
|
-
topic='test',
|
|
99
|
-
md5_of_message_attributes='test',
|
|
100
|
-
)
|
|
101
|
-
assert response.body == 'test'
|
|
102
|
-
assert response.get_correlation_id() is not None
|
|
103
|
-
assert response.topic == 'test'
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
def test_sqs_message_from_dict():
|
|
107
|
-
|
|
108
|
-
message = SqsMessage(
|
|
109
|
-
id='123e4567-e89b-12d3-a456-426614174000',
|
|
110
|
-
topic='test',
|
|
111
|
-
payload='{"test": "test"}',
|
|
112
|
-
correlation_id='123e4567-e89b-12d3-a456-426614174000'
|
|
113
|
-
)
|
|
114
|
-
message_dict = message.to_dict()
|
|
115
|
-
assert str(message.id) == message_dict['id']
|
|
116
|
-
assert message.topic == message_dict['topic']
|
|
117
|
-
assert message.get_payload() == {'test': 'test'}
|
|
118
|
-
assert message.correlation_id == message_dict['correlation_id']
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
def test_to_message_attributes():
|
|
122
|
-
attributes = {
|
|
123
|
-
'string': 'test',
|
|
124
|
-
'number': 1,
|
|
125
|
-
'binary': b'test',
|
|
126
|
-
'uuid': uuid.uuid4(),
|
|
127
|
-
'datetime': datetime.datetime.now(),
|
|
128
|
-
'decimal': decimal.Decimal('1.0')
|
|
129
|
-
}
|
|
130
|
-
result = to_message_attributes(attributes)
|
|
131
|
-
assert result['string']['DataType'] == 'String'
|
|
132
|
-
assert result['string']['StringValue'] == 'test'
|
|
133
|
-
assert result['number']['DataType'] == 'Number'
|
|
134
|
-
assert result['number']['StringValue'] == '1'
|
|
135
|
-
assert result['binary']['DataType'] == 'Binary'
|
|
136
|
-
assert result['binary']['BinaryValue'] == b'test'
|
|
137
|
-
assert result['uuid']['DataType'] == 'String'
|
|
138
|
-
assert result['uuid']['StringValue'] == str(attributes['uuid'])
|
|
139
|
-
assert result['datetime']['DataType'] == 'String'
|
|
140
|
-
assert result['datetime']['StringValue'] == attributes['datetime'].isoformat()
|
|
141
|
-
assert result['decimal']['DataType'] == 'Number'
|
|
142
|
-
assert result['decimal']['StringValue'] == '1.0'
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
def test_parse_from_sns_response():
|
|
146
|
-
real_message_body = {
|
|
147
|
-
"test": "test"
|
|
148
|
-
}
|
|
149
|
-
body = dumps({
|
|
150
|
-
'Type': 'Notification',
|
|
151
|
-
'TopicArn': 'mytopic',
|
|
152
|
-
'Message': dumps(real_message_body),
|
|
153
|
-
'MessageAttributes': {'correlation_id': {
|
|
154
|
-
'Type': 'String', 'Value': '123e4567-e89b-12d3-a456-426614174000'
|
|
155
|
-
}}
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
boto_response = {
|
|
159
|
-
'MessageId': 'test',
|
|
160
|
-
'ReceiptHandle': 'test',
|
|
161
|
-
'MD5OfBody': 'test',
|
|
162
|
-
'Body': body,
|
|
163
|
-
'Attributes': {'test': 'test', 'correlation_id': '123e4567-e89b-12d3-a456-426614174000'},
|
|
164
|
-
'MessageAttributes': {'correlation_id': {'Type': 'String', 'Value': '123e4567-e89b-12d3-a456-426614174000'}}
|
|
165
|
-
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
response = SqsReponse.from_boto_response(boto_response)
|
|
169
|
-
assert response.body == body
|
|
170
|
-
assert response.get_correlation_id() == '123e4567-e89b-12d3-a456-426614174000'
|
|
171
|
-
assert response.topic == ''
|
|
172
|
-
assert response.message_attributes == {
|
|
173
|
-
'correlation_id': "123e4567-e89b-12d3-a456-426614174000"}
|
|
174
|
-
assert response.attributes == {
|
|
175
|
-
'test': 'test', 'correlation_id': '123e4567-e89b-12d3-a456-426614174000'}
|
|
176
|
-
assert response.parse_from_sns() == real_message_body
|
|
177
|
-
assert response.parse() == real_message_body
|
|
File without changes
|
|
File without changes
|
{finalsa_common_models-0.1.0 → finalsa_common_models-1.0.1}/finalsa/common/models/models/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{finalsa_common_models-0.1.0 → finalsa_common_models-1.0.1}/finalsa_common_models.egg-info/zip-safe
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|