示例#1
0
class Replay(object):
    def __init__(self, file_name, replay_file='replay_file'):
        self.file_name = file_name
        self.stream = open(self.file_name).readlines()
        self.tm = Testmaker()
        self.tm.setup_logging(replay_file, '/dev/null')
        self.processor = Processor('replay_processor')
        self.serial_obj = pickle

    def process(self):
        self.log = []

        buffer = []
        req_re = re.compile('---REQUEST_BREAK---')
        res_re = re.compile('---RESPONSE_BREAK---')

        for line in self.stream:
            if req_re.search(line):
                #process request
                to_pickle = ''.join(buffer)
                request = MockRequest(self.serial_obj.loads(to_pickle))
                self.processor.save_request(request)
                print(request['path'], request['time'])
                buffer = []
            elif res_re.search(line):
                #process response
                to_pickle = ''.join(buffer)
                response = MockRequest(self.serial_obj.loads(to_pickle))
                self.log.append(request, response)
                self.processer.save_response(request, response)
                print(response['status_code'], response['time'])
                buffer = []
            else:
                buffer.append(line)
示例#2
0
 def __init__(self, file_name, replay_file='replay_file'):
     self.file_name = file_name
     self.stream = open(self.file_name).readlines()
     self.tm = Testmaker()
     self.tm.setup_logging(replay_file, '/dev/null')
     self.processor = Processor('replay_processor')
     self.serial_obj = pickle
示例#3
0
class Replay(object):

    def __init__(self, file_name, replay_file='replay_file'):
        self.file_name = file_name
        self.stream = open(self.file_name).readlines()
        self.tm = Testmaker()
        self.tm.setup_logging(replay_file, '/dev/null')
        self.processor = Processor('replay_processor')
        self.serial_obj = pickle

    def process(self):
        self.log = []

        buffer = []
        req_re = re.compile('---REQUEST_BREAK---')
        res_re = re.compile('---RESPONSE_BREAK---')

        for line in self.stream:
            if req_re.search(line):
                #process request
                to_pickle = ''.join(buffer)
                request = MockRequest(self.serial_obj.loads(to_pickle))
                self.processor.save_request(request)
                print request['path'], request['time']
                buffer = []
            elif res_re.search(line):
                #process response
                to_pickle = ''.join(buffer)
                response = MockRequest(self.serial_obj.loads(to_pickle))
                self.log.append(request, response)
                self.processer.save_response(request, response)
                print response['status_code'], response['time']
                buffer = []
            else:
                buffer.append(line)
class Parsing(TestCase):
   """
   Tests to test the parsing API
   """

   def setUp(self):
      self.tm = Testmaker()
      self.tm.setup_logging('test_file', 'serialize_file')

   def tearDown(self):
      #Teardown logging somehow?
      os.remove('test_file')
      os.remove('serialize_file')

   def test_basic_parsing(self):
      user = User.objects.create_user('john', '*****@*****.**', 'johnpassword')
      user.save()
      c = Context({'object': user})
      t = TemplateParser('{% load comments %}{% get_comment_list for object as as_var %}{{ as_var }}', c)
      t.parse()
      self.assertEquals(t.template_calls[0], '{% get_comment_list for object as as_var %}')
      self.assertEquals(t.loaded_classes[0], '{% load comments %}')
      t.create_tests()
      logs = open('test_file')
      output = logs.read()
      self.assertTrue(output.find("{'object': get_model('auth', 'user')") != -1)
示例#5
0
def set_logging(request, filename=None):
    if not filename:
        filename = request.REQUEST['filename']
    filename = slugify(filename)
    log_file = '/tmp/testmaker/tests/%s_tests_custom.py' % filename
    serialize_file = '/tmp/testmaker/tests/%s_serial_custm.py' % filename
    tm = Testmaker()
    tm.setup_logging(test_file=log_file, serialize_file=serialize_file)
    #tm.app_name = 'tmp'
    #tm.prepare_test_file()
    return HttpResponse('Setup logging %s' % tm.test_file)
示例#6
0
 def __init__(self, file_name, replay_file='replay_file'):
     self.file_name = file_name
     self.stream = open(self.file_name).readlines()
     self.tm = Testmaker()
     self.tm.setup_logging(replay_file, '/dev/null')
     self.processor = Processor('replay_processor')
     self.serial_obj = pickle
示例#7
0
 def process_response(self, request, response):
     if 'test_client_true' not in request.REQUEST \
     and SHOW_TESTMAKER_HEADER:
         c = Context({'file': Testmaker.logfile()})
         s = RESPONSE_TEMPLATE.render(c)
         response.content = str(s) + str(response.content)
     return response
示例#8
0
 def process_request(self, request):
     """
     Run the request through the testmaker middleware.
     This outputs the requests to the chosen Serializers.
     Possible running it through one or many Processors
     """
     # This is request.REQUEST to catch POST and GET
     if 'test_client_true' not in request.GET:
         request.logfile = Testmaker.logfile()
         self.serializer.save_request(request)
         self.processor.save_request(request)
         # We only want to re-run the request on idempotent requests
         if request.method.lower() == "get":
             try:
                 setup_test_environment()
                 c = Client(REMOTE_ADDR='127.0.0.1')
                 getdict = request.GET.copy()
                 getdict['test_client_true'] = 'yes'  # avoid recursion
                 response = c.get(request.path, getdict)
                 self.serializer.save_response(request, response)
                 self.processor.save_response(request, response)
                 teardown_test_environment()
             except RuntimeError:
                 teardown_test_environment()
                 setup_test_environment()
                 c = Client(REMOTE_ADDR='127.0.0.1')
                 getdict = request.GET.copy()
                 getdict['test_client_true'] = 'yes'  # avoid recursion
                 response = c.get(request.path, getdict)
                 self.serializer.save_response(request, response)
                 self.processor.save_response(request, response)
                 teardown_test_environment()
     return None
示例#9
0
 def process_response(self, request, response):
     if 'test_client_true' not in request.REQUEST \
     and SHOW_TESTMAKER_HEADER:
         c = Context({'file': Testmaker.logfile()})
         s = RESPONSE_TEMPLATE.render(c)
         response.content = str(s) + str(response.content)
     return response
示例#10
0
    def handle(self, addrport='', *args, **options):

        app = options.get("application")
        verbosity = int(options.get('verbosity', 1))
        create_fixtures = options.get('fixture', False)
        logdir = options.get('logdirectory')
        fixture_format = options.get('format', 'xml')

        if app:
            app = models.get_app(app)

        if not app:
            #Don't serialize the whole DB :)
            create_fixtures = False

        testmaker = Testmaker(app, verbosity, create_fixtures, fixture_format,
                              addrport)
        testmaker.prepare(insert_middleware=True)
        try:
            call_command('runserver', addrport=addrport, use_reloader=False)
        except SystemExit:
            if create_fixtures:
                testmaker.make_fixtures()
            else:
                raise
class TestMakerTests(TestCase):
    """
    Tests to test basic testmaker functionality.
    """
    urls = "polls.urls"
    fixtures = ['polls_testmaker.json']

    def setUp(self):
        self.tm = Testmaker()
        self.tm.setup_logging('test_file', 'serialize_file')

    def tearDown(self):
        #Teardown logging somehow?
        os.remove('test_file')
        os.remove('serialize_file')

    def test_basic_testmaker(self):
        self.tm.insert_middleware()
        self.client.get('/')
        logs = open('test_file')
        output = logs.read()
        self.assertTrue(output.find('[<Poll: What\'s up?>, <Poll: Test poll>]') != -1)
    def handle(self, addrport='', *args, **options):

        app = options.get("application")
        verbosity = int(options.get('verbosity', 1))
        create_fixtures = options.get('fixture', False)
        logdir = options.get('logdirectory')
        fixture_format = options.get('format', 'xml')

        if app:
            app = models.get_app(app)

        if not app:
            #Don't serialize the whole DB :)
            create_fixtures = False

        testmaker = Testmaker(app, verbosity, create_fixtures, fixture_format, addrport)
        testmaker.prepare(insert_middleware=True)
        try:
            call_command('runserver', addrport=addrport, use_reloader=False)
        except SystemExit:
            if create_fixtures:
                testmaker.make_fixtures()
            else:
                raise
 def process_request(self, request):
     """
     Run the request through the testmaker middleware.
     This outputs the requests to the chosen Serializers.
     Possible running it through one or many Processors
     """
     # This is request.REQUEST to catch POST and GET
     if "test_client_true" not in request.REQUEST:
         request.logfile = Testmaker.logfile()
         self.serializer.save_request(request)
         self.processor.save_request(request)
         # We only want to re-run the request on idempotent requests
         if request.method.lower() == "get":
             setup_test_environment()
             c = Client(REMOTE_ADDR="127.0.0.1")
             getdict = request.GET.copy()
             getdict["test_client_true"] = "yes"  # avoid recursion
             response = c.get(request.path, getdict)
             self.serializer.save_response(request, response)
             self.processor.save_response(request, response)
     return None
示例#14
0
class TestMakerTests(TestCase):
    """
    Tests to test basic testmaker functionality.
    """
    urls = "test_project.polls.urls"
    fixtures = ['polls_testmaker.json']

    def setUp(self):
        self.tm = Testmaker()
        self.tm.setup_logging('test_file', 'serialize_file')
        Testmaker.enabled = True
        self.tm.insert_middleware()

    def tearDown(self):
        #Teardown logging somehow?
        os.remove('test_file')
        os.remove('serialize_file')

    def test_basic_testmaker(self):
        self.client.get('/')
        logs = open('test_file')
        output = logs.read()
        self.assertTrue(
            output.find('[<Poll: What\'s up?>, <Poll: Test poll>]') != -1)

    def test_twill_processor(self):
        settings.TESTMAKER_PROCESSOR = 'twill'
        self.client.get('/')
        self.client.get('/1/')
        logs = open('test_file')
        output = logs.read()
        self.assertTrue(output.find('code 200') != -1)

    def test_not_inserting_multiple_times(self):
        """
        Test that the middleware will only be inserted once.
        """
        self.tm.insert_middleware()
        self.tm.insert_middleware()
        middleware = settings.MIDDLEWARE_CLASSES
        #A set of the middleware should be the same, meaning the item isn't in twice.
        self.assertEqual(sorted(list(middleware)),
                         sorted(list(set(middleware))))
class TestMakerTests(TestCase):
    """
    Tests to test basic testmaker functionality.
    """
    urls = "test_project.polls.urls"
    fixtures = ['polls_testmaker.json']

    def setUp(self):
        self.tm = Testmaker()
        self.tm.setup_logging('test_file', 'serialize_file')
        Testmaker.enabled = True
        self.tm.insert_middleware()

    def tearDown(self):
        #Teardown logging somehow?
        os.remove('test_file')
        os.remove('serialize_file')

    def test_basic_testmaker(self):
        self.client.get('/')
        logs = open('test_file')
        output = logs.read()
        self.assertTrue(output.find('[<Poll: What\'s up?>, <Poll: Test poll>]') != -1)

    def test_twill_processor(self):
        settings.TESTMAKER_PROCESSOR = 'twill'
        self.client.get('/')
        self.client.get('/1/')
        logs = open('test_file')
        output = logs.read()
        self.assertTrue(output.find('code 200') != -1)

    def test_not_inserting_multiple_times(self):
        """
        Test that the middleware will only be inserted once.
        """
        self.tm.insert_middleware()
        self.tm.insert_middleware()
        middleware = settings.MIDDLEWARE_CLASSES
        #A set of the middleware should be the same, meaning the item isn't in twice.
        self.assertEqual(sorted(list(middleware)), sorted(list(set(middleware))))
 def setUp(self):
     self.tm = Testmaker()
     self.tm.setup_logging('test_file', 'serialize_file')
     Testmaker.enabled = True
示例#17
0
 def setUp(self):
     self.tm = Testmaker()
     self.tm.setup_logging('test_file', 'serialize_file')
     Testmaker.enabled = True
     self.tm.insert_middleware()
from test_utils.testmaker import processors
from test_utils.testmaker import serializers
from test_utils.testmaker import Testmaker

# Remove at your own peril.
# Thar be sharks in these waters.
debug = getattr(settings, "DEBUG", False)
"""
if not debug:
    print "THIS CODE IS NOT MEANT FOR USE IN PRODUCTION"
else:
    print "Loaded Testmaker Middleware"
"""

if not Testmaker.enabled:
    testmaker = Testmaker(verbosity=0)
    testmaker.prepare()


SHOW_TESTMAKER_HEADER = getattr(settings, "SHOW_TESTMAKER_HEADER", False)

RESPONSE_TEMPLATE = Template(
    """
<div class="wrapper" style="background-color: red; padding: 5px; color: #fff; width: 100%;">
Testmaker: Logging to: {{ file }}
<form action="/test_utils/set_logging/">
    <input type="text" name="filename">
    <input type="submit" value="New Test">
</form>
<a href="/test_utils/show_log/">Show Log</a>
</div>
 def setUp(self):
    self.tm = Testmaker()
    self.tm.setup_logging('test_file', 'serialize_file')
示例#20
0
def show_log(request):
    file = Testmaker.logfile()
    contents = open(file)
    return HttpResponse(contents.read(), content_type='text/plain')
    HttpResponse()
示例#21
0
from test_utils.testmaker import processors
from test_utils.testmaker import serializers
from test_utils.testmaker import Testmaker

#Remove at your own peril.
#Thar be sharks in these waters.
debug = getattr(settings, 'DEBUG', False)
"""
if not debug:
    print "THIS CODE IS NOT MEANT FOR USE IN PRODUCTION"
else:
    print "Loaded Testmaker Middleware"
"""

if not Testmaker.enabled:
    testmaker = Testmaker(verbosity=0)
    testmaker.prepare()

serializer_pref = getattr(settings, 'TESTMAKER_SERIALIZER', 'pickle')
processor_pref = getattr(settings, 'TESTMAKER_PROCESSOR', 'django')
SHOW_TESTMAKER_HEADER = getattr(settings, 'SHOW_TESTMAKER_HEADER', False)

RESPONSE_TEMPLATE = Template("""
<div class="wrapper" style="background-color: red; padding: 5px; color: #fff; width: 100%;">
Testmaker: Logging to: {{ file }}
<form action="/test_utils/set_logging/">
    <input type="text" name="filename">
    <input type="submit" value="New Test">
</form>
<a href="/test_utils/show_log/">Show Log</a>
</div>