Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Super basic twill processor. Mostly just a CP of the django processor…

…. This just outputs the URL and the code, punting on the ability to check for content. That is a hard problem.

A lot of the shared functions between this one and the Django processor should be abstracted to the base, or some utils functionality.
  • Loading branch information...
commit 53f317dc91787e048c46555088f98d066537b889 1 parent 2c41f7b
@ericholscher authored
View
2  test_utils/testmaker/processors/__init__.py
@@ -18,7 +18,7 @@
TEST_PROCESSORS = {
'django': 'test_utils.testmaker.processors.django_processor',
- #'twill': 'test_utils.testmaker.processors.twill',
+ 'twill': 'test_utils.testmaker.processors.twill_processor',
}
_test_processors = {}
View
100 test_utils/testmaker/processors/twill_processor.py
@@ -0,0 +1,100 @@
+import time
+import re
+import base
+
+from django.template import Template, Context
+from django.utils.encoding import force_unicode
+from django.utils.safestring import mark_safe
+
+TEST_TEMPLATE = """go {{ path }}"""
+
+STATUS_TEMPLATE = """code {{ status_code }}"""
+
+#CONTEXT_TEMPLATE = '''find {{value}}'''
+CONTEXT_TEMPLATE = ''
+
+def safe_dict(dict):
+ new_dic = {}
+ for key,val in dict.iteritems():
+ new_dic[key] = mark_safe(val)
+ return new_dic
+
+class Processor(base.Processer):
+ """Processes the serialized data. Generally to create some sort of test cases"""
+
+ def __init__(self, name='django'):
+ super(Processor, self).__init__(name)
+
+ def save_request(self, request):
+ """ Actually write the request out to a file """
+ if self.shall_we_proceed(request):
+ self._log_request(request)
+
+ def save_response(self, request, response):
+ if self.shall_we_proceed(request):
+ self._log_status(response)
+ '''#TODO make this log sanely.
+ if response.context and response.status_code != 404:
+ context = self._get_context(response.context)
+ self._log_context(context)
+ #This is where template tag outputting would go
+ '''
+
+ def _log_request(self, request):
+ method = request.method.lower()
+ request_str = "'%s', {" % request.path
+ for dikt in request.REQUEST.dicts:
+ for arg in dikt:
+ request_str += "'%s': '%s', " % (arg, request.REQUEST[arg])
+ request_str += "}"
+
+ template = Template(TEST_TEMPLATE)
+ context = {
+ 'path': request.path,
+ 'time': base.slugify(time.time()),
+ 'method': method,
+ 'request_str': request_str,
+ }
+ context = Context(safe_dict(context))
+ self.log.info(template.render(context))
+
+ def _log_status(self, response):
+ template = Template(STATUS_TEMPLATE)
+ context = {
+ 'status_code': response.status_code,
+ }
+ if response.status_code in [301, 302]:
+ context['location'] = response['Location']
+ context = Context(safe_dict(context))
+ self.log.info(template.render(context))
+
+ def _get_context(self, context_list):
+ #Ugly Hack. Needs to be a better way
+ if isinstance(context_list, list):
+ context_list = context_list[-1] #Last context rendered
+ ret = context_list.dicts[-1]
+ if ret == {}:
+ ret = context_list.dicts[0]
+ try:
+ return ret.copy()
+ except Exception, e:
+ return dict()
+
+ else:
+ return context_list
+
+ def _log_context(self, context):
+ template = Template(CONTEXT_TEMPLATE)
+ for var in context:
+ val = force_unicode(context[var])
+ con = {
+ 'key': var,
+ 'value': val,
+ }
+ con = Context(safe_dict(con))
+ try:
+ #Avoid memory addy's which will change.
+ if not re.search("0x\w+", val):
+ self.log.info(template.render(con))
+ except UnicodeDecodeError, e:
+ pass
Please sign in to comment.
Something went wrong with that request. Please try again.