<commit_before>from os import path
from unittest import TestCase

from jsontest import JsonTest
from dictdiffer import diff as dictdiff

from elasticquery import Filter, Query

CLASS_NAMES = {
    "_filter": Filter,
    "_query": Query
}


def _test_filterquery(self, filterquery, test_name, test_data):
    method = getattr(filterquery, test_name)

    if test_name.endswith('_'):
        test_name = test_name[:1]

    def parse_arg(arg):
        if isinstance(arg, list):
            return [parse_arg(a) for a in arg]
        else:
            return CLASS_NAMES[arg](arg, {}) if (isinstance(arg, basestring) and arg.startswith('_')) else arg

    args = test_data.get('args', [])
    args = parse_arg(args)

    kwargs = test_data.get('kwargs', {})
    kwargs = {
        k: parse_arg(v) if isinstance(v, list) else parse_arg(v)
        for k, v in kwargs.iteritems()
    }

    output = method(*args, **kwargs).dict()

    try:
        self.assertEqual(output[test_name], test_data['output'])
    except AssertionError as e:
        print 'OUTPUT', output[test_name]
        print 'EXPECTED', test_data['output']

        diff = list(dictdiff(output[test_name], test_data['output']))
        for d in diff:
            print d

        raise e


class TestFilters(TestCase):
    __metaclass__ = JsonTest

    jsontest_files = path.join('tests', 'filters')
    jsontest_function = lambda self, test_name, test_data: (
        _test_filterquery(self, Filter, test_name, test_data)
    )

class TestQueries(TestCase):
    __metaclass__ = JsonTest

    jsontest_files = path.join('tests', 'queries')
    jsontest_function = lambda self, test_name, test_data: (
        _test_filterquery(self, Query, test_name, test_data)
    )
<commit_msg>Update to test all output, add aggregate tests<commit_after>from os import path
from unittest import TestCase

from jsontest import JsonTest
from dictdiffer import diff as dictdiff

from elasticquery import Filter, Query, Aggregate

CLASS_NAMES = {
    "_filter": Filter,
    "_query": Query
}


def _test_filterquery(self, filterquery, test_name, test_data):
    method = getattr(filterquery, test_name)

    def parse_arg(arg):
        if isinstance(arg, list):
            return [parse_arg(a) for a in arg]
        else:
            return CLASS_NAMES[arg](arg, {}) if (isinstance(arg, basestring) and arg.startswith('_')) else arg

    args = test_data.get('args', [])
    args = parse_arg(args)

    kwargs = test_data.get('kwargs', {})
    kwargs = {
        k: parse_arg(v) if isinstance(v, list) else parse_arg(v)
        for k, v in kwargs.iteritems()
    }

    output = method(*args, **kwargs).dict()

    try:
        self.assertEqual(output, test_data['output'])
    except AssertionError as e:
        print 'OUTPUT', output
        print 'EXPECTED', test_data['output']

        diff = list(dictdiff(output, test_data['output']))
        for d in diff:
            print d

        raise e


class TestFilters(TestCase):
    __metaclass__ = JsonTest

    jsontest_files = path.join('tests', 'filters')
    jsontest_function = lambda self, test_name, test_data: (
        _test_filterquery(self, Filter, test_name, test_data)
    )

class TestQueries(TestCase):
    __metaclass__ = JsonTest

    jsontest_files = path.join('tests', 'queries')
    jsontest_function = lambda self, test_name, test_data: (
        _test_filterquery(self, Query, test_name, test_data)
    )

class TestAggregates(TestCase):
    __metaclass__ = JsonTest

    jsontest_files = path.join('tests', 'aggregates')
    jsontest_function = lambda self, test_name, test_data: (
        _test_filterquery(self, Aggregate, test_name, test_data)
    )

{'fraction_non_alphanumeric': 0.10824041338047322, 'fraction_numerical': 0.0002719608376393799, 'mean_word_length': 3.2275862068965515, 'pattern_counts': {'":': 4, '<': 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': '4563831', 'n_tokens_mistral': 1182, 'n_tokens_neox': 1162, 'n_words': 298}