Completed
Pull Request — master (#9)
by George
02:20
created

translator()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 3
rs 10
1
# -*- coding: utf-8 -*-
2
# vi:si:et:sw=4:sts=4:ts=4
3
4
import json
5
6
import pytest
7
8
from loafer.aws.message_translator import (SQSMessageTranslator,
9
                                           SNSMessageTranslator)
10
11
# sqs
12
13
14
@pytest.fixture
15
def sqs_translator():
16
    return SQSMessageTranslator()
17
18
19
def test_translate_sqs(sqs_translator):
20
    original = {'Body': json.dumps('some-content')}
21
    content = sqs_translator.translate(original)
22
    assert 'content' in content
23
    assert content['content'] == 'some-content'
24
25
    original = {'Body': json.dumps({'key': 'value'})}
26
    content = sqs_translator.translate(original)
27
    assert content['content'] == {'key': 'value'}
28
29
30
@pytest.fixture(params=[{'invalid': 'format'}, 'invalid format',
31
                        42, {}, [], (), ''])
32
def parametrize_invalid_messages(request):
33
    return request.param
34
35
36
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...
37
    content = sqs_translator.translate(parametrize_invalid_messages)
38
    assert content['content'] is None
39
40
41
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...
42
    original = {'Body': 'invalid: json'}
43
    content = sqs_translator.translate(original)
44
    assert content['content'] is None
45
46
# sns
47
48
49
@pytest.fixture
50
def sns_translator():
51
    return SNSMessageTranslator()
52
53
54
def test_translate_sns(sns_translator):
55
    message_content = 'here I am'
56
    message = json.dumps({'Message': json.dumps(message_content)})
57
    original = {'Body': message}
58
    content = sns_translator.translate(original)
59
    assert content['content'] == message_content
60
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
68
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...
69
    message = json.dumps({'Message': parametrize_invalid_messages})
70
    original = {'Body': message}
71
    content = sns_translator.translate(original)
72
    assert content['content'] is None
73
74
75
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...
76
    content = sns_translator.translate(parametrize_invalid_messages)
77
    assert content['content'] is None
78