def wrapped_function(*args, **kwargs): # Create a counter monkey patch into dse.cqlengine.connection.execute count = StatementCounter(dse.cqlengine.connection.execute) original_function = dse.cqlengine.connection.execute # Monkey patch in our StatementCounter wrapper dse.cqlengine.connection.execute = count.wrapped_execute # Invoked the underlying unit test to_return = fn(*args, **kwargs) # Get the count from our monkey patched counter count.get_counter() # DeMonkey Patch our code dse.cqlengine.connection.execute = original_function # Check to see if we have a pre-existing test case to work from. if len(args) is 0: test_case = unittest.TestCase("__init__") else: test_case = args[0] # Check to see if the count is what you expect test_case.assertEqual( count.get_counter(), expected, msg= "Expected number of dse.cqlengine.connection.execute calls ({0}) doesn't match actual number invoked ({1})" .format(expected, count.get_counter())) return to_return
def testAssertMultiLineEqualTruncates(self): test = unittest2.TestCase('assertEqual') def truncate(msg, diff): return 'foo' test._truncateMessage = truncate try: test.assertMultiLineEqual('foo', 'bar') except self.failureException, e: self.assertEqual(str(e), 'foo')
def testAssertDictEqualTruncates(self): test = unittest2.TestCase('assertEqual') def truncate(msg, diff): return 'foo' test._truncateMessage = truncate try: test.assertDictEqual({}, {1: 0}) except self.failureException, e: self.assertEqual(str(e), 'foo')
def testPickle(self): # Issue 10326 # Can't use TestCase classes defined in Test class as # pickle does not work with inner classes test = unittest2.TestCase('run') for protocol in range(pickle.HIGHEST_PROTOCOL + 1): # blew up prior to fix pickled_test = pickle.dumps(test, protocol=protocol) unpickled_test = pickle.loads(pickled_test) self.assertEqual(test, unpickled_test) # exercise the TestCase instance in a way that will invoke # the type equality lookup mechanism unpickled_test.assertEqual(set(), set())
# borrowed from python-github2/tests/test_request.py try: from nose.tools import assert_dict_equal except ImportError: # for Python < 2.7 try: import unittest2 _binding = unittest2.TestCase('run') assert_dict_equal = _binding.assertDictEqual assert_is_instance = _binding.assertIsInstance assert_tuple_equal = _binding.assertTupleEqual except: raise
from datetime import datetime from elasticsearch import Elasticsearch import json from time import sleep from kafka import KafkaConsumer import unittest2 #connect to elasticsearch container es = Elasticsearch(["elasticsearch"], http_auth="asamasach:1qaz!QAZ", timeout=30, max_retries=10, retry_on_timeout=True) test_c = unittest2.TestCase('__init__') #for testing if __name__ == '__main__': print('Running Consumer..') #topic name which created in kafkaproduce code topic_name = 'asamasach_3' #consume messages in kafka continer and chech the elasticsearch connection consumer = KafkaConsumer( topic_name, bootstrap_servers=['kafka:9093'], api_version=(0, 10), auto_offset_reset='earliest', auto_commit_interval_ms=120 * 1000, value_deserializer=lambda m: json.loads(m.decode('ascii'))) print(es.info()) a = 0 b = 0