<s> from collections import namedtuple <EOL> from mock import patch <EOL> from requests . exceptions import Timeout , ConnectionError <EOL> from django . core . cache import cache <EOL> from django . test import SimpleTestCase <EOL> from corehq . apps . repeaters . models import simple_post_with_cached_timeout <EOL> from corehq . apps . repeaters . exceptions import RequestConnectionError <EOL> MockResponse = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class SimplePostCacheTest ( SimpleTestCase ) : <EOL> def setUp ( self ) : <EOL> cache . clear ( ) <EOL> def tearDown ( self ) : <EOL> cache . clear ( ) <EOL> def test_simple_post_with_cached_timeout_success ( self ) : <EOL> with patch ( <EOL> '<STR_LIT>' , <EOL> side_effect = [ MockResponse ( status_code = <NUM_LIT:200> , reason = '<STR_LIT>' ) ] ) as mock_post : <EOL> resp = simple_post_with_cached_timeout ( '<STR_LIT:abc>' , '<STR_LIT>' ) <EOL> self . assertEqual ( mock_post . call_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> def test_simple_post_with_cached_timeout_error ( self ) : <EOL> """<STR_LIT>""" <EOL> with patch ( '<STR_LIT>' , side_effect = Timeout ( '<STR_LIT>' ) ) as mock_post : <EOL> with self . assertRaises ( RequestConnectionError ) : <EOL> simple_post_with_cached_timeout ( '<STR_LIT:abc>' , '<STR_LIT>' ) <EOL> self . assertEqual ( mock_post . call_count , <NUM_LIT:1> ) <EOL> with self . assertRaises ( RequestConnectionError ) : <EOL> simple_post_with_cached_timeout ( '<STR_LIT:abc>' , '<STR_LIT>' ) <EOL> self . assertEqual ( mock_post . call_count , <NUM_LIT:1> ) <EOL> def test_simple_post_with_cached_timeout_bad_http_response ( self ) : <EOL> """<STR_LIT>""" <EOL> with patch ( <EOL> '<STR_LIT>' , <EOL> side_effect = [ MockResponse ( status_code = <NUM_LIT> , reason = '<STR_LIT>' ) ] ) as mock_post : <EOL> with self . assertRaises ( RequestConnectionError ) : <EOL> simple_post_with_cached_timeout ( '<STR_LIT:abc>' , '<STR_LIT>' ) <EOL> self . assertEqual ( mock_post . call_count , <NUM_LIT:1> ) <EOL> with self . assertRaises ( RequestConnectionError ) : <EOL> simple_post_with_cached_timeout ( '<STR_LIT:abc>' , '<STR_LIT>' ) <EOL> self . assertEqual ( mock_post . call_count , <NUM_LIT:1> ) <EOL> def test_bust_cache_new_url ( self ) : <EOL> """<STR_LIT>""" <EOL> with patch ( <EOL> '<STR_LIT>' , <EOL> side_effect = ConnectionError ( '<STR_LIT>' ) ) as mock_post : <EOL> with self . assertRaises ( RequestConnectionError ) : <EOL> simple_post_with_cached_timeout ( '<STR_LIT:abc>' , '<STR_LIT>' ) <EOL> self . assertEqual ( mock_post . call_count , <NUM_LIT:1> ) <EOL> with self . assertRaises ( RequestConnectionError ) : <EOL> simple_post_with_cached_timeout ( '<STR_LIT:abc>' , '<STR_LIT>' ) <EOL> self . assertEqual ( mock_post . call_count , <NUM_LIT:2> ) <EOL> def test_force_send ( self ) : <EOL> with patch ( <EOL> '<STR_LIT>' , <EOL> side_effect = ConnectionError ( '<STR_LIT>' ) ) as mock_post : <EOL> with self . assertRaises ( RequestConnectionError ) : <EOL> simple_post_with_cached_timeout ( '<STR_LIT:abc>' , '<STR_LIT>' ) <EOL> self . assertEqual ( mock_post . call_count , <NUM_LIT:1> ) <EOL> with self . assertRaises ( RequestConnectionError ) : <EOL> simple_post_with_cached_timeout ( '<STR_LIT:abc>' , '<STR_LIT>' , force_send = True ) <EOL> self . assertEqual ( mock_post . call_count , <NUM_LIT:2> ) </s>
