<s> import os <EOL> import time <EOL> import pytest <EOL> from slackpy . slackpy import SlackLogger , LOG_LEVELS , LogLv <EOL> __author__ = '<STR_LIT>' <EOL> DUMMY_WEB_HOOK = '<STR_LIT>' <EOL> try : <EOL> VALID_WEB_HOOK = os . environ [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> print ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> class TestSlackLogger : <EOL> def test_channel_value_error ( self ) : <EOL> with pytest . raises ( ValueError ) : <EOL> SlackLogger ( DUMMY_WEB_HOOK , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_build_payload_with_all_parameters ( self ) : <EOL> logger = SlackLogger ( DUMMY_WEB_HOOK , '<STR_LIT>' , '<STR_LIT>' ) <EOL> actual = logger . _SlackLogger__build_payload ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> expected = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:text>' : '<STR_LIT>' , <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT:text>' , '<STR_LIT>' , '<STR_LIT:title>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ] <EOL> } <EOL> assert expected == actual <EOL> def test_build_payload_without_specifying_optional_parameters ( self ) : <EOL> logger = SlackLogger ( DUMMY_WEB_HOOK ) <EOL> actual = logger . _SlackLogger__build_payload ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> expected = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:text>' : '<STR_LIT>' , <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT:text>' , '<STR_LIT>' , '<STR_LIT:title>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ] <EOL> } <EOL> assert expected == actual <EOL> def test_build_payload_with_custom_fields ( self ) : <EOL> logger = SlackLogger ( DUMMY_WEB_HOOK , '<STR_LIT>' , '<STR_LIT>' ) <EOL> test_fields = list ( ) <EOL> test_fields . append ( { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT:value>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True <EOL> } ) <EOL> test_fields . append ( { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT:value>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True <EOL> } ) <EOL> actual = logger . _SlackLogger__build_payload ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> test_fields ) <EOL> expected = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:text>' : '<STR_LIT>' , <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT:text>' , '<STR_LIT>' , '<STR_LIT:title>' ] , <EOL> '<STR_LIT>' : [ <EOL> { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT:value>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True <EOL> } , { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT:value>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True <EOL> } <EOL> ] , <EOL> } ] <EOL> } <EOL> assert expected == actual <EOL> def test_default_log_level ( self ) : <EOL> logger = SlackLogger ( DUMMY_WEB_HOOK ) <EOL> assert logger . log_level == <NUM_LIT:20> <EOL> def test_values_in_log_levels ( self ) : <EOL> assert LOG_LEVELS == [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> , <NUM_LIT> ] <EOL> def test_to_set_valid_value_log_level ( self ) : <EOL> logger = SlackLogger ( DUMMY_WEB_HOOK ) <EOL> for lv in [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> , <NUM_LIT> ] : <EOL> logger . set_log_level ( lv ) <EOL> assert logger . log_level == lv <EOL> def test_to_set_invalid_value_log_level ( self ) : <EOL> logger = SlackLogger ( DUMMY_WEB_HOOK ) <EOL> for lv in [ <NUM_LIT:0> , <NUM_LIT:50> , <NUM_LIT> , '<STR_LIT>' ] : <EOL> with pytest . raises ( ValueError ) : <EOL> logger . set_log_level ( lv ) <EOL> with pytest . raises ( TypeError ) : <EOL> logger . set_log_level ( ) <EOL> def test_log_level_threshold ( self ) : <EOL> logger = SlackLogger ( DUMMY_WEB_HOOK ) <EOL> actual = logger . debug ( '<STR_LIT>' ) <EOL> assert actual is None <EOL> logger . set_log_level ( LogLv . WARN ) <EOL> actual = logger . info ( '<STR_LIT>' ) <EOL> assert actual is None <EOL> logger . set_log_level ( LogLv . ERROR ) <EOL> actual = logger . warn ( '<STR_LIT>' ) <EOL> assert actual is None <EOL> def test_post_to_valid_web_hook ( self ) : <EOL> logger = SlackLogger ( VALID_WEB_HOOK ) <EOL> logger . set_log_level ( LogLv . DEBUG ) <EOL> fields = [ { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT:value>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True <EOL> } , { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT:value>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True <EOL> } <EOL> ] <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> response = logger . debug ( '<STR_LIT>' , '<STR_LIT>' , fields = fields ) <EOL> assert response . status_code == <NUM_LIT:200> <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> response = logger . info ( '<STR_LIT>' , '<STR_LIT>' , fields = fields ) <EOL> assert response . status_code == <NUM_LIT:200> <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> response = logger . warn ( '<STR_LIT>' , '<STR_LIT>' , fields = fields ) <EOL> assert response . status_code == <NUM_LIT:200> <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> response = logger . error ( '<STR_LIT>' , '<STR_LIT>' , fields = fields ) <EOL> assert response . status_code == <NUM_LIT:200> <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> response = logger . message ( '<STR_LIT>' , '<STR_LIT>' , <EOL> title_link = '<STR_LIT>' , <EOL> fields = fields , color = '<STR_LIT>' , log_level = <NUM_LIT:20> ) <EOL> assert response . status_code == <NUM_LIT:200> <EOL> def test_post_to_invalid_web_hook ( self ) : <EOL> logger = SlackLogger ( DUMMY_WEB_HOOK ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> with pytest . raises ( Exception ) as exc_info : <EOL> logger . error ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert '<STR_LIT>' in str ( exc_info . value ) <EOL> def test_title_link ( self ) : <EOL> logger = SlackLogger ( VALID_WEB_HOOK ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> response = logger . info ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert response . status_code == <NUM_LIT:200> <EOL> def test_omit_title_link ( self ) : <EOL> logger = SlackLogger ( VALID_WEB_HOOK ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> response = logger . info ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert response . status_code == <NUM_LIT:200> <EOL> def test_minimum_post_to_valid_web_hook ( self ) : <EOL> logger = SlackLogger ( VALID_WEB_HOOK ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> response = logger . info ( '<STR_LIT>' ) <EOL> assert response . status_code == <NUM_LIT:200> </s>
