Passed
Push — master ( 9ddb09...7fc5b5 )
by Jaisen
01:14
created

test_calls_print_debug_true()   A

Complexity

Conditions 3

Size

Total Lines 16
Code Lines 15

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nop 1
dl 16
loc 16
rs 9.65
c 0
b 0
f 0
1
from __future__ import absolute_import
2
# Project imports
3
4
import os
5
import sys
6
import unittest 
7
8
from json import dumps
9
from mock import patch
10
try:
11
    from StringIO import StringIO
12
except ImportError:
13
    from io import StringIO
14
15
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))))
16
17
from elodie import constants
18
from elodie import log
19
20
21
def call_log_and_assert(func, args, expected):
22
    saved_stdout = sys.stdout
23
    try:
24
        out = StringIO()
25
        sys.stdout = out
26
        func(*args)
27
        output = out.getvalue()
28
        assert output == expected, (expected, func, output)
29
    finally:
30
        sys.stdout = saved_stdout
31
32
def with_new_line(string):
33
    return "{}\n".format(string)
34
35 View Code Duplication
@patch('elodie.log')
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
36
@patch('elodie.constants.debug', True)
37
def test_calls_print_debug_true(fake_log):
38
    expected = 'some string'
39
    fake_log.info.return_value = expected
40
    fake_log.warn.return_value = expected
41
    fake_log.error.return_value = expected
42
    for func in [log.info, log.warn, log.error]:
43
        call_log_and_assert(func, [expected], with_new_line(expected))
44
45
    expected_json = {'foo':'bar'}
46
    fake_log.info.return_value = expected_json
47
    fake_log.warn.return_value = expected_json
48
    fake_log.error.return_value = expected_json
49
    for func in [log.info_json, log.warn_json, log.error_json]:
50
        call_log_and_assert(func, [expected_json], with_new_line(dumps(expected_json)))
51
52 View Code Duplication
@patch('elodie.log')
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
53
@patch('elodie.constants.debug', False)
54
def test_calls_print_debug_false(fake_log):
55
    expected = 'some other string'
56
    fake_log.info.return_value = expected
57
    fake_log.warn.return_value = expected
58
    fake_log.error.return_value = expected
59
    for func in [log.info, log.warn, log.error]:
60
        call_log_and_assert(func, [expected], '')
61
62
    expected_json = {'foo':'bar'}
63
    fake_log.info.return_value = expected_json
64
    fake_log.warn.return_value = expected_json
65
    fake_log.error.return_value = expected_json
66
    for func in [log.info_json, log.warn_json, log.error_json]:
67
        call_log_and_assert(func, [expected_json], '')
68
69
@patch('elodie.log')
70
def test_calls_print_progress_no_new_line(fake_log):
71
    expected = 'some other string'
72
    fake_log.info.return_value = expected
73
    fake_log.warn.return_value = expected
74
    fake_log.error.return_value = expected
75
    call_log_and_assert(log.progress, [expected], expected)
76
77
@patch('elodie.log')
78
def test_calls_print_progress_with_new_line(fake_log):
79
    expected = "some other string\n"
80
    fake_log.info.return_value = expected
81
    fake_log.warn.return_value = expected
82
    fake_log.error.return_value = expected
83
    call_log_and_assert(log.progress, [expected, True], with_new_line(expected))
84