<commit_before>import requests
import json
import time
import re
import collections
import copy
import uuid
import sys
import types

STR_EXP = re.compile('\{\$([^}]+)\}')
JSON_INDENT = 2


class TestException(Exception):
    pass

class TestStatusFailed(TestException):
    pass

class TestExpectFailed(TestException):
    pass


class TestCaseReportFilter(object):
    def error(self, test_case):
        return True

class TestCaseReportFormatter(object):
    def error(self, test_case, error_msg):
        return '***************************************************\n' + \
            '%s\n' % error_msg + \
            'name: %r\n' % test_case.__class__.__name__ + \
            'method: %r\n' % test_case.method + \
            'path: %r\n' % test_case.path + \
            'expect_status: %s\n' % json.dumps(test_case.expect_status,
                indent=JSON_INDENT) + \
            'response_status: %s\n' % test_case.response_status + \
            'expect_headers: %s\n' % json.dumps(test_case.expect_headers,
                indent=JSON_INDENT) + \
            'response_headers: %s\n' % json.dumps(
                dict(test_case.response_headers.items()),
                indent=JSON_INDENT,
            ) + \
            'input_data: %s\n' % json.dumps(test_case.input_data,
                indent=JSON_INDENT) + \
            'expect_data: %s\n' % json.dumps(test_case.expect_data,
                indent=JSON_INDENT) + \
            'response_data: %s\n' % json.dumps(test_case.response_data,
                indent=JSON_INDENT) + \
            '***************************************************'

class TestCaseReportHandler(object):
    def error(self, error):
        print error

class Base(object):
    base_url = None
    filter = TestCaseReportFilter()
    formatter = TestCaseReportFormatter()
    handler = TestCaseReportHandler()

    def __init__(self):
        self.objects = {}
        self.requests = requests.request

    @property
    def TestCase(self):
        return type('TestCase', (TestCase,), {
            'base': self,
        })

    def main(self):
        module = __import__('__main__')

        for name in dir(module):
            obj = getattr(module, name)
            if isinstance(obj, type) and issubclass(obj, TestCase) and \
                    obj.base == self and obj not in self.objects:
                obj()

    def handle_request(self, method, url, **kwargs):
        if self.request_kwargs:
            kwargs.update(self.request_kwargs)

        return self.request(method, url, **kwargs)

    def request(self, method, url, headers=None, json=None, **kwargs):
        return self.requests.request(
            method,
            url,
            headers=headers,
            json=json,
            **kwargs
        )

class SessionBase(Base):
    def __init__(self):
        Base.__init__(self)
        self.requests = requests.Session()
        self.requests.request(
            self.method,
            self.base_url + self.path,
            json=self.input_data,
        )

class TestCase(object):
    require = None
    required = True
    method = 'GET'
    path = '/'
    expect_status = None
    expect_headers = None
    input_data = None
    expect_data = None

    def __init__(self):
        self._error_marked = False

        if self.__class__ in self.base.objects:
            raise ValueError('Test case %r already run' % self.__class__)

        if self.require:
            if not isinstance(self.require, (list, tuple)):
                self.require = (self.require,)

            requires = []
            for require in self.require:
                require_obj = self.base.objects.get(require)<commit_msg>Add missing vars<commit_after>import requests
import json
import time
import re
import collections
import copy
import uuid
import sys
import types

STR_EXP = re.compile('\{\$([^}]+)\}')
JSON_INDENT = 2


class TestException(Exception):
    pass

class TestStatusFailed(TestException):
    pass

class TestExpectFailed(TestException):
    pass


class TestCaseReportFilter(object):
    def error(self, test_case):
        return True

class TestCaseReportFormatter(object):
    def error(self, test_case, error_msg):
        return '***************************************************\n' + \
            '%s\n' % error_msg + \
            'name: %r\n' % test_case.__class__.__name__ + \
            'method: %r\n' % test_case.method + \
            'path: %r\n' % test_case.path + \
            'expect_status: %s\n' % json.dumps(test_case.expect_status,
                indent=JSON_INDENT) + \
            'response_status: %s\n' % test_case.response_status + \
            'expect_headers: %s\n' % json.dumps(test_case.expect_headers,
                indent=JSON_INDENT) + \
            'response_headers: %s\n' % json.dumps(
                dict(test_case.response_headers.items()),
                indent=JSON_INDENT,
            ) + \
            'input_data: %s\n' % json.dumps(test_case.input_data,
                indent=JSON_INDENT) + \
            'expect_data: %s\n' % json.dumps(test_case.expect_data,
                indent=JSON_INDENT) + \
            'response_data: %s\n' % json.dumps(test_case.response_data,
                indent=JSON_INDENT) + \
            '***************************************************'

class TestCaseReportHandler(object):
    def error(self, error):
        print error

class Base(object):
    base_url = None
    filter = TestCaseReportFilter()
    formatter = TestCaseReportFormatter()
    handler = TestCaseReportHandler()
    request_kwargs = None

    def __init__(self):
        self.objects = {}
        self.requests = requests.request

    @property
    def TestCase(self):
        return type('TestCase', (TestCase,), {
            'base': self,
        })

    def main(self):
        module = __import__('__main__')

        for name in dir(module):
            obj = getattr(module, name)
            if isinstance(obj, type) and issubclass(obj, TestCase) and \
                    obj.base == self and obj not in self.objects:
                obj()

    def handle_request(self, method, url, **kwargs):
        if self.request_kwargs:
            kwargs.update(self.request_kwargs)

        return self.request(method, url, **kwargs)

    def request(self, method, url, headers=None, json=None, **kwargs):
        return self.requests.request(
            method,
            url,
            headers=headers,
            json=json,
            **kwargs
        )

class SessionBase(Base):
    def __init__(self):
        Base.__init__(self)
        self.requests = requests.Session()
        self.requests.request(
            self.method,
            self.base_url + self.path,
            json=self.input_data,
        )

class TestCase(object):
    require = None
    required = True
    method = 'GET'
    path = '/'
    expect_status = None
    input_headers = None
    expect_headers = None
    input_data = None
    expect_data = None
    request_kwargs = None

    def __init__(self):
        self._error_marked = False

        if self.__class__ in self.base.objects:
            raise ValueError('Test case %r already run' % self.__class__)

        if self.require:
            if not isinstance(self.require, (list, tuple)):
                self.require = (self.require,)

            requires = []
            for require in self.require:
                require_obj = self.base.objects.get(require)
{'fraction_non_alphanumeric': 0.13400267737617136, 'fraction_numerical': 0.0002677376171352075, 'mean_word_length': 2.1, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1106414', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2118, 'n_words': 461}