Completed
Push — master ( b657e5...d8feac )
by George
04:13 queued 02:05
created

test_sqs_metadata_extract()   A

Complexity

Conditions 4

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
dl 0
loc 7
rs 9.2
c 0
b 0
f 0
1
import json
2
3
import pytest
4
5
from loafer.ext.aws.message_translators import (SQSMessageTranslator,
6
                                                SNSMessageTranslator)
7
8
# sqs
9
10
11
@pytest.fixture
12
def sqs_translator():
13
    return SQSMessageTranslator()
14
15
16
def test_translate_sqs(sqs_translator):
17
    original = {'Body': json.dumps('some-content')}
18
    content = sqs_translator.translate(original)
19
    assert 'content' in content
20
    assert content['content'] == 'some-content'
21
22
    original = {'Body': json.dumps({'key': 'value'})}
23
    content = sqs_translator.translate(original)
24
    assert content['content'] == {'key': 'value'}
25
26
27
def test_sqs_metadata_extract(sqs_translator):
28
    original = {'Body': json.dumps('some-content'), 'whatever': 'whatever'}
29
    content = sqs_translator.translate(original)
30
    metadata = content['metadata']
31
    assert metadata
32
    assert 'whatever' in metadata
33
    assert metadata['whatever'] == 'whatever'
34
35
36
@pytest.fixture(params=[{'invalid': 'format'}, 'invalid format',
37
                        42, {}, [], (), ''])
38
def parametrize_invalid_messages(request):
39
    return request.param
40
41
42
def test_translate_sqs_handles_invalid_format(sqs_translator, parametrize_invalid_messages):
0 ignored issues
show
Coding Style Naming introduced by
The name test_translate_sqs_handles_invalid_format does not conform to the function naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
43
    content = sqs_translator.translate(parametrize_invalid_messages)
44
    assert content['content'] is None
45
46
47
def test_translate_sqs_handles_json_error(sqs_translator):
0 ignored issues
show
Coding Style Naming introduced by
The name test_translate_sqs_handles_json_error does not conform to the function naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
48
    original = {'Body': 'invalid: json'}
49
    content = sqs_translator.translate(original)
50
    assert content['content'] is None
51
52
# sns
53
54
55
@pytest.fixture
56
def sns_translator():
57
    return SNSMessageTranslator()
58
59
60
def test_translate_sns(sns_translator):
61
    message_content = 'here I am'
62
    message = json.dumps({'Message': json.dumps(message_content)})
63
    original = {'Body': message}
64
    content = sns_translator.translate(original)
65
    assert content['content'] == message_content
66
67
    message_content = {'here': 'I am'}
68
    message = json.dumps({'Message': json.dumps(message_content)})
69
    original = {'Body': message}
70
    content = sns_translator.translate(original)
71
    assert content['content'] == message_content
72
73
74
def test_sns_metadata_extract(sns_translator):
75
    message_content = 'here I am'
76
    message = json.dumps({'Message': json.dumps(message_content), 'foo': 'nested'})
77
    original = {'Body': message, 'bar': 'not nested'}
78
    content = sns_translator.translate(original)
79
    metadata = content['metadata']
80
    assert metadata
81
    assert 'foo' in metadata
82
    assert metadata['foo'] == 'nested'
83
    assert 'bar' in metadata
84
    assert metadata['bar'] == 'not nested'
85
86
87
def test_translate_sns_handles_invalid_content(sns_translator, parametrize_invalid_messages):
0 ignored issues
show
Coding Style Naming introduced by
The name test_translate_sns_handles_invalid_content does not conform to the function naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
88
    message = json.dumps({'Message': parametrize_invalid_messages})
89
    original = {'Body': message}
90
    content = sns_translator.translate(original)
91
    assert content['content'] is None
92
93
94
def test_translate_sns_handles_invalid_format(sns_translator, parametrize_invalid_messages):
0 ignored issues
show
Coding Style Naming introduced by
The name test_translate_sns_handles_invalid_format does not conform to the function naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
95
    content = sns_translator.translate(parametrize_invalid_messages)
96
    assert content['content'] is None
97