finalsa-common-models 0.0.4__tar.gz → 1.0.0__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.
Files changed (19) hide show
  1. {finalsa-common-models-0.0.4/finalsa_common_models.egg-info → finalsa_common_models-1.0.0}/PKG-INFO +2 -2
  2. {finalsa-common-models-0.0.4 → finalsa_common_models-1.0.0}/finalsa/common/models/__init__.py +1 -1
  3. {finalsa-common-models-0.0.4 → finalsa_common_models-1.0.0}/finalsa/common/models/models/sqs_message.py +6 -4
  4. {finalsa-common-models-0.0.4 → finalsa_common_models-1.0.0}/finalsa/common/models/models/sqs_response.py +53 -39
  5. {finalsa-common-models-0.0.4 → finalsa_common_models-1.0.0/finalsa_common_models.egg-info}/PKG-INFO +2 -2
  6. {finalsa-common-models-0.0.4 → finalsa_common_models-1.0.0}/setup.py +1 -1
  7. finalsa_common_models-1.0.0/tests/test_client.py +355 -0
  8. finalsa-common-models-0.0.4/tests/test_client.py +0 -141
  9. {finalsa-common-models-0.0.4 → finalsa_common_models-1.0.0}/LICENSE.md +0 -0
  10. {finalsa-common-models-0.0.4 → finalsa_common_models-1.0.0}/README.md +0 -0
  11. {finalsa-common-models-0.0.4 → finalsa_common_models-1.0.0}/finalsa/common/models/models/__init__.py +0 -0
  12. {finalsa-common-models-0.0.4 → finalsa_common_models-1.0.0}/finalsa/common/models/models/functions.py +0 -0
  13. {finalsa-common-models-0.0.4 → finalsa_common_models-1.0.0}/finalsa/common/models/py.typed +0 -0
  14. {finalsa-common-models-0.0.4 → finalsa_common_models-1.0.0}/finalsa_common_models.egg-info/SOURCES.txt +0 -0
  15. {finalsa-common-models-0.0.4 → finalsa_common_models-1.0.0}/finalsa_common_models.egg-info/dependency_links.txt +0 -0
  16. {finalsa-common-models-0.0.4 → finalsa_common_models-1.0.0}/finalsa_common_models.egg-info/requires.txt +0 -0
  17. {finalsa-common-models-0.0.4 → finalsa_common_models-1.0.0}/finalsa_common_models.egg-info/top_level.txt +0 -0
  18. {finalsa-common-models-0.0.4 → finalsa_common_models-1.0.0}/finalsa_common_models.egg-info/zip-safe +0 -0
  19. {finalsa-common-models-0.0.4 → finalsa_common_models-1.0.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: finalsa-common-models
3
- Version: 0.0.4
3
+ Version: 1.0.0
4
4
  Summary: An utils package for using finalsa common models.
5
5
  Home-page: https://github.com/finalsa/finalsa-common-models
6
6
  Author: Luis Jimenez
@@ -16,7 +16,7 @@ Classifier: Programming Language :: Python :: 3.7
16
16
  Classifier: Programming Language :: Python :: 3.8
17
17
  Classifier: Programming Language :: Python :: 3.9
18
18
  Classifier: Programming Language :: Python :: 3.10
19
- Requires-Python: >=3.8
19
+ Requires-Python: >=3.10
20
20
  Description-Content-Type: text/markdown
21
21
  License-File: LICENSE.md
22
22
  Requires-Dist: boto3>=1.20.3
@@ -5,7 +5,7 @@ from finalsa.common.models.models import (
5
5
  to_message_attributes
6
6
  )
7
7
 
8
- __version__ = "0.0.4"
8
+ __version__ = "1.0.0"
9
9
 
10
10
  __all__ = [
11
11
  "SqsMessage",
@@ -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
- return loads(self.payload)
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
- try:
42
- s = self.get_payload()['correlation_id']
43
- return s
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
- def parse_from_sns(self) -> Optional[Dict]:
48
- payload = self.get_payload()
49
- if 'Type' not in payload or payload['Type'] != 'Notification':
50
- return None
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
- return self.get_payload()
62
+ try:
63
+ return loads(payload['Message'])
64
+ except Exception:
65
+ return payload['Message']
56
66
 
57
67
  def parse(self) -> Optional[Dict]:
58
- sns_response = self.parse_from_sns()
59
- if sns_response is not None:
60
- return sns_response
61
- return self.get_payload()
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 get_sqs_message(self) -> SqsMessage:
64
- sns_payload = self.parse_from_sns()
65
- if sns_payload:
66
- if 'correlation_id' not in sns_payload:
67
- sns_payload['correlation_id'] = str(self.get_correlation_id())
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(sns_payload['id']),
72
- topic=sns_payload['topic'],
73
- payload=sns_payload['payload'],
74
- correlation_id=sns_payload['correlation_id'],
75
- timestamp=sns_payload['timestamp']
79
+ id=UUID(content['id']),
80
+ topic=content['topic'],
81
+ payload=content['payload'],
82
+ correlation_id=content['correlation_id'],
83
+ timestamp=content['timestamp']
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=UUID(payload['id']),
82
- topic=payload['topic'],
83
- payload=payload['payload'],
84
- correlation_id=payload['correlation_id'],
85
- timestamp=payload['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(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: finalsa-common-models
3
- Version: 0.0.4
3
+ Version: 1.0.0
4
4
  Summary: An utils package for using finalsa common models.
5
5
  Home-page: https://github.com/finalsa/finalsa-common-models
6
6
  Author: Luis Jimenez
@@ -16,7 +16,7 @@ Classifier: Programming Language :: Python :: 3.7
16
16
  Classifier: Programming Language :: Python :: 3.8
17
17
  Classifier: Programming Language :: Python :: 3.9
18
18
  Classifier: Programming Language :: Python :: 3.10
19
- Requires-Python: >=3.8
19
+ Requires-Python: >=3.10
20
20
  Description-Content-Type: text/markdown
21
21
  License-File: LICENSE.md
22
22
  Requires-Dist: boto3>=1.20.3
@@ -54,7 +54,7 @@ setup(
54
54
  package_data={PACKAGE: ["py.typed"]},
55
55
  include_package_data=True,
56
56
  zip_safe=True,
57
- python_requires=">=3.8",
57
+ python_requires=">=3.10",
58
58
  data_files=[("", ["LICENSE.md"])],
59
59
  install_requires=[
60
60
  "boto3>=1.20.3",
@@ -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,141 +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
- sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')))
11
-
12
-
13
- def test_version():
14
- assert __version__ is not None
15
-
16
-
17
- def test_parse_message_attributes():
18
- attributes = {
19
- 'string': {'Type': 'String', 'Value': 'test'},
20
- 'number': {'Type': 'Number', 'Value': '1'},
21
- 'binary': {'Type': 'Binary', 'Value': 'test'}
22
- }
23
- result = parse_message_attributes(attributes)
24
- assert result['string'] == 'test'
25
- assert result['number'] == 1
26
- assert result['binary'] == b'test'
27
-
28
-
29
- def test_sqs_message():
30
- message = SqsMessage(
31
- id='123e4567-e89b-12d3-a456-426614174000',
32
- topic='test',
33
- payload='{"test": "test"}',
34
- correlation_id='123e4567-e89b-12d3-a456-426614174000'
35
- )
36
- assert str(message.id) == '123e4567-e89b-12d3-a456-426614174000'
37
- assert message.topic == 'test'
38
- assert message.get_payload() == {'test': 'test'}
39
- assert message.correlation_id == '123e4567-e89b-12d3-a456-426614174000'
40
- assert message.timestamp is not None
41
- assert message.to_dict() == {
42
- 'id': '123e4567-e89b-12d3-a456-426614174000',
43
- 'topic': 'test',
44
- 'payload': '{"test": "test"}',
45
- 'correlation_id': '123e4567-e89b-12d3-a456-426614174000',
46
- 'timestamp': message.timestamp.isoformat()
47
- }
48
-
49
-
50
- def test_sqs_response():
51
- response = SqsReponse(
52
- message_id='test',
53
- receipt_handle='test',
54
- md5_of_body='test',
55
- body='test',
56
- attributes={'test': 'test',
57
- 'correlation_id': '123e4567-e89b-12d3-a456-426614174000'},
58
- topic='test',
59
- md5_of_message_attributes='test',
60
- message_attributes={'correlation_id': {'Type': 'String',
61
- 'Value': '123e4567-e89b-12d3-a456-426614174000'}}
62
-
63
- )
64
- assert response.body == 'test'
65
- assert str(response.get_correlation_id()) == '123e4567-e89b-12d3-a456-426614174000'
66
- assert response.topic == 'test'
67
-
68
-
69
- def test_sqs_response_from_boto_response():
70
- boto_response = {
71
- 'MessageId': 'test',
72
- 'ReceiptHandle': 'test',
73
- 'MD5OfBody': 'test',
74
- 'Body': 'test',
75
- 'Attributes': {'test': 'test', 'correlation_id': '123e4567-e89b-12d3-a456-426614174000'},
76
- 'MessageAttributes': {'correlation_id': {'Type': 'String', 'Value': '123e4567-e89b-12d3-a456-426614174000'}}
77
-
78
- }
79
-
80
- response = SqsReponse.from_boto_response(boto_response)
81
- assert response.body == 'test'
82
- assert str(response.get_correlation_id()) == '123e4567-e89b-12d3-a456-426614174000'
83
- assert response.topic == ''
84
- assert response.message_attributes == {
85
- 'correlation_id': "123e4567-e89b-12d3-a456-426614174000"}
86
- assert response.attributes == {
87
- 'test': 'test', 'correlation_id': '123e4567-e89b-12d3-a456-426614174000'}
88
-
89
-
90
- def test_sqs_response_no_correlation_id():
91
- response = SqsReponse(
92
- message_id='test',
93
- receipt_handle='test',
94
- md5_of_body='test',
95
- body='test',
96
- attributes={'test': 'test'},
97
- topic='test',
98
- md5_of_message_attributes='test',
99
- )
100
- assert response.body == 'test'
101
- assert response.get_correlation_id() is not None
102
- assert response.topic == 'test'
103
-
104
-
105
- def test_sqs_message_from_dict():
106
-
107
- message = SqsMessage(
108
- id='123e4567-e89b-12d3-a456-426614174000',
109
- topic='test',
110
- payload='{"test": "test"}',
111
- correlation_id='123e4567-e89b-12d3-a456-426614174000'
112
- )
113
- message_dict = message.to_dict()
114
- assert str(message.id) == message_dict['id']
115
- assert message.topic == message_dict['topic']
116
- assert message.get_payload() == {'test': 'test'}
117
- assert message.correlation_id == message_dict['correlation_id']
118
-
119
-
120
- def test_to_message_attributes():
121
- attributes = {
122
- 'string': 'test',
123
- 'number': 1,
124
- 'binary': b'test',
125
- 'uuid': uuid.uuid4(),
126
- 'datetime': datetime.datetime.now(),
127
- 'decimal': decimal.Decimal('1.0')
128
- }
129
- result = to_message_attributes(attributes)
130
- assert result['string']['DataType'] == 'String'
131
- assert result['string']['StringValue'] == 'test'
132
- assert result['number']['DataType'] == 'Number'
133
- assert result['number']['StringValue'] == '1'
134
- assert result['binary']['DataType'] == 'Binary'
135
- assert result['binary']['BinaryValue'] == b'test'
136
- assert result['uuid']['DataType'] == 'String'
137
- assert result['uuid']['StringValue'] == str(attributes['uuid'])
138
- assert result['datetime']['DataType'] == 'String'
139
- assert result['datetime']['StringValue'] == attributes['datetime'].isoformat()
140
- assert result['decimal']['DataType'] == 'Number'
141
- assert result['decimal']['StringValue'] == '1.0'