<commit_before>import warnings
import six
import simplejson as json

from . import raml
from . import factory
from .utils import path_from_uri, merge_query_params, path_to_identifier
from .hooks import Hooks
from .validate import RAMLValidator


class APISuite(object):
    """Represents an API test suite.
    """
    instances = []

    def __new__(cls, *args, **kwargs):
        instance = super(APISuite, cls).__new__(cls)
        cls.instances.append(instance)
        return instance

    def __init__(self, raml_path_or_string, app, JSONEncoder=None):
        """Instantiates an API test suite for the given RAML and app.
        """
        self.app = app
        self.test_suite = TestSuite()

        self.raml_path, self.raml = _parse_raml(raml_path_or_string)
        self.path_prefix = path_from_uri(self.raml.base_uri)
        self.resource_scopes = []

        self.RequestClass = make_request_class(app, self.raml)

        self.JSONEncoder = JSONEncoder or json.JSONEncoder

        self.hooks = Hooks()
        self.examples = self._define_factories()

    def _define_factories(self):
        examples = factory.Examples()
        for resources in six.itervalues(self.raml.resources):
            try:
                post_resource = resources['POST']
                example = post_resource.body['application/json'].example
            except KeyError:
                continue
            else:
                resource_name = raml.resource_name_from_path(post_resource.path)
                examples.make_factory(resource_name, example)

        return examples

    def resource(self, path, factory=None, **uri_params):
        """Decorator for declaring a resource scope.

        Tests defined within the resource scope apply to that resource,
        and resource scopes can be nested within resource scopes. The
        nesting of resource scopes can match that of the RAML definition,
        though it's not required.

        Fixtures defined in a resource scope are local to that scope;
        they behave essentially like a module scope.

        :param path:        the resource path segment.
        :param factory:     factory callable for generating data for the
                            request body. The data will be JSON-encoded when
                            the request is made. The default factory uses
                            the example body from the RAML.
        :param uri_params:  kw args are used to fill in URI parameters in the
                            path (by default, the uri_params example value from
                            the RAML is used)
        """
        # pop parent arg passed in by the subresource decorator
        parent = uri_params.pop('parent', None)
        full_path = raml.resource_full_path(path, parent)

        if full_path in self.raml.resources:
            # get URI param example values:<commit_msg>Define all example factories when API created, not just POST<commit_after>import warnings
import six
import simplejson as json

from . import raml
from .factory import Examples
from .utils import path_from_uri, merge_query_params, path_to_identifier
from .hooks import Hooks
from .validate import RAMLValidator


class APISuite(object):
    """Represents an API test suite.
    """
    instances = []

    def __new__(cls, *args, **kwargs):
        instance = super(APISuite, cls).__new__(cls)
        cls.instances.append(instance)
        return instance

    def __init__(self, raml_path_or_string, app, JSONEncoder=None):
        """Instantiates an API test suite for the given RAML and app.
        """
        self.app = app
        self.test_suite = TestSuite()

        self.raml_path, self.raml = _parse_raml(raml_path_or_string)
        self.path_prefix = path_from_uri(self.raml.base_uri)
        self.resource_scopes = []

        self.RequestClass = make_request_class(app, self.raml)

        self.JSONEncoder = JSONEncoder or json.JSONEncoder

        self.hooks = Hooks()
        self.examples = self._define_factories()

    def _define_factories(self):
        """Create factories for example body values.

        Returns an instance of ``ra.factory.Examples`` with factories
        stored for each route with an example declared. The POST body
        factory for each collection is also keyed by resource name,
        e.g. "POST /users/{username}/profile" is also keyed as "user.profile".
        """
        examples = Examples()
        for path, nodes in six.iteritems(self.raml.resources):
            for method, node in six.iteritems(nodes):
                try:
                    example = node.body['application/json'].example
                except KeyError:
                    continue
                else:
                    route = "{} {}".format(method, path)
                    examples.make_factory(route, example)
                    if method == 'POST':
                        resource_name = raml.resource_name_from_path(path)
                        examples.make_factory(resource_name, example)
        return examples

    def resource(self, path, factory=None, **uri_params):
        """Decorator for declaring a resource scope.

        Tests defined within the resource scope apply to that resource,
        and resource scopes can be nested within resource scopes. The
        nesting of resource scopes can match that of the RAML definition,
        though it's not required.

        Fixtures defined in a resource scope are local to that scope;
        they behave essentially like a module scope.

        :param path:        the resource path segment.
        :param factory:     factory callable for generating data for the
                            request body. The data will be JSON-encoded when
                            the request is made. The default factory uses
                            the example body from the RAML.
        :param uri_params:  kw args are used to fill in URI parameters in the
                            path (by default, the uri_params example value from
                            the RAML is used)
        """
        # pop parent arg passed in by the subresource decorator
        parent = uri_params.pop('parent', None)
        full_path = raml.resource_full_path(path, parent)

        if full_path in self.raml.resources:
            # get URI param example values:
{'fraction_non_alphanumeric': 0.06688648139425342, 'fraction_numerical': 0.0, 'mean_word_length': 2.127147766323024, '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': '1000974', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1599, 'n_words': 575}