<commit_before>import unittest
import os
from mock import patch

from thclient import (TreeherderJob, TreeherderJobCollection,
    TreeherderRevision, TreeherderResultSet, TreeherderResultSetCollection,
    TreeherderClientError, TreeherderRequest)

try:
    import json
except ImportError:
    import simplejson as json

class DataSetup(unittest.TestCase):

    def setUp(self):

        # Load sample job data
        self.job_data = []

        job_data_file = 'job_data.json'

        self.job_data_file_path = os.path.join(
            os.path.dirname(__file__),
            'data',
            job_data_file
            )

        with open(self.job_data_file_path) as f:
            data = f.read()
            self.job_data = json.loads(data)

        # Load sample resultsets
        self.resultset_data = []

        resultset_file = 'resultset_data.json'

        self.resultset_data_file_path = os.path.join(
            os.path.dirname(__file__),
            'data',
            resultset_file
            )

        with open(self.resultset_data_file_path) as f:
            data = f.read()
            self.resultset_data = json.loads(data)
            for resultset in self.resultset_data:
                for index, revision in enumerate(resultset['revisions']):
                    del revision['branch']

                resultset['artifact'] = {
                    u'name':'push_data',
                    u'type':'push',
                    u'blob': { u'stuff':[1,2,3,4,5] }
                    }

                resultset['type'] = 'push'

        # Sample post response with oauth credentials
        post_file = 'post.txt'
        self.post_file_path = os.path.join(
            os.path.dirname(__file__),
            'data',
            post_file
            )
        with open(self.post_file_path) as f:
            self.post_data = f.read().strip()

        # Sample post response with no oauth credentials
        no_oauth_post_file = 'no_oauth_post.txt'
        self.no_oauth_post_file_path = os.path.join(
            os.path.dirname(__file__),
            'data',
            no_oauth_post_file
            )
        with open(self.no_oauth_post_file_path) as f:
            self.no_oauth_post_data = f.read().strip()

    def compare_structs(self, struct1, struct2):
        """Compare two dictionary structures"""

        for k1, v1 in struct1.iteritems():

            self.assertTrue(
                k1 in struct2,
                'struct1 key, {0}, not found in struct2'.format(k1) )

            if isinstance(v1, dict):

                self.assertTrue(
                    isinstance(struct2[k1], dict),
                    'struct2 value not a dict for key {0}'.format(k1) )

                # recursively iterate through any dicts found
                self.compare_structs(v1, struct2[k1])

            elif isinstance(v1, list):

                self.assertTrue(
                    isinstance(struct2[k1], list),
                    'struct2 not a list for key {0}'.format(k1) )

                self.assertEqual(
                    v1, struct2[k1],
                    ('struct1 and struct2 not equal for key {0}\n'
                     'struct1[{0}] = {1}\n'
                     'struct2[{0}] = {2}\n').format(k1, v1, struct2[k1])
                    )

            else:
                self.assertEqual(
                    v1, struct2[k1],
                    'struct1[{0}], {1} != struct2[{0}], {2}'.format(k1, v1, struct2[k1])
                    )

class TreeherderResultsetTest(DataSetup, unittest.TestCase):

    def test_sample_data_validation(self):
        """Confirm that the sample data validates"""

        for resultset in self.resultset_data:

            rs = TreeherderResultSet(resultset)
            rs.validate()

            for revision in resultset['revisions']:
                tr = TreeherderRevision(revision)
                tr.validate()

    def test_resultset_sample_data(self):
        """Test all add methods for building result sets"""

        trsc = TreeherderResultSetCollection()

        for resultset in self.resultset_data:

            trs = TreeherderResultSet()

            trs.add_push_timestamp( resultset['push_timestamp'] )
            trs.add_revision_hash( resultset['revision_hash'] )
            trs.add_type( 'push' )
            trs.add_artifact( 'push_data', 'push', { 'stuff':[1,2,3,4,5] } )

            for revision in resultset['revisions']:

                tr = TreeherderRevision()

                tr.add_revision( revision['revision'] )
                tr.add_author( revision['author'] )
                tr.add_comment( revision['comment'] )
                tr.add_files( revision['files'] )
                tr.add_repository( revision['repository'] )

                trs.add_revision(tr)

            self.compare_structs(trs.data, resultset)

            trsc.add(trs)

            # confirm we get the same thing if we initialize from
            # a resultset dict
            trs_struct = TreeherderResultSet(resultset)

            self.compare_structs(trs_struct.data, resultset)
<commit_msg>added more validation tests<commit_after>import unittest
import os
from mock import patch

from thclient import (TreeherderJob, TreeherderJobCollection,
    TreeherderRevision, TreeherderResultSet, TreeherderResultSetCollection,
    TreeherderClientError, TreeherderRequest)

try:
    import json
except ImportError:
    import simplejson as json

class DataSetup(unittest.TestCase):

    def setUp(self):

        # Load sample job data
        self.job_data = []

        job_data_file = 'job_data.json'

        self.job_data_file_path = os.path.join(
            os.path.dirname(__file__),
            'data',
            job_data_file
            )

        with open(self.job_data_file_path) as f:
            data = f.read()
            self.job_data = json.loads(data)

        # Load sample resultsets
        self.resultset_data = []

        resultset_file = 'resultset_data.json'

        self.resultset_data_file_path = os.path.join(
            os.path.dirname(__file__),
            'data',
            resultset_file
            )

        with open(self.resultset_data_file_path) as f:
            data = f.read()
            self.resultset_data = json.loads(data)
            for resultset in self.resultset_data:
                for index, revision in enumerate(resultset['revisions']):
                    del revision['branch']

                resultset['artifact'] = {
                    u'name':'push_data',
                    u'type':'push',
                    u'blob': { u'stuff':[1,2,3,4,5] }
                    }

                resultset['type'] = 'push'

        # Sample post response with oauth credentials
        post_file = 'post.txt'
        self.post_file_path = os.path.join(
            os.path.dirname(__file__),
            'data',
            post_file
            )
        with open(self.post_file_path) as f:
            self.post_data = f.read().strip()

        # Sample post response with no oauth credentials
        no_oauth_post_file = 'no_oauth_post.txt'
        self.no_oauth_post_file_path = os.path.join(
            os.path.dirname(__file__),
            'data',
            no_oauth_post_file
            )
        with open(self.no_oauth_post_file_path) as f:
            self.no_oauth_post_data = f.read().strip()

    def compare_structs(self, struct1, struct2):
        """Compare two dictionary structures"""

        for k1, v1 in struct1.iteritems():

            self.assertTrue(
                k1 in struct2,
                'struct1 key, {0}, not found in struct2'.format(k1) )

            if isinstance(v1, dict):

                self.assertTrue(
                    isinstance(struct2[k1], dict),
                    'struct2 value not a dict for key {0}'.format(k1) )

                # recursively iterate through any dicts found
                self.compare_structs(v1, struct2[k1])

            elif isinstance(v1, list):

                self.assertTrue(
                    isinstance(struct2[k1], list),
                    'struct2 not a list for key {0}'.format(k1) )

                self.assertEqual(
                    v1, struct2[k1],
                    ('struct1 and struct2 not equal for key {0}\n'
                     'struct1[{0}] = {1}\n'
                     'struct2[{0}] = {2}\n').format(k1, v1, struct2[k1])
                    )

            else:
                self.assertEqual(
                    v1, struct2[k1],
                    'struct1[{0}], {1} != struct2[{0}], {2}'.format(k1, v1, struct2[k1])
                    )

class TreeherderResultsetTest(DataSetup, unittest.TestCase):

    def test_sample_data_validation(self):
        """Confirm that the sample data validates"""

        for resultset in self.resultset_data:

            rs = TreeherderResultSet(resultset)
            rs.validate()

            for revision in resultset['revisions']:
                tr = TreeherderRevision(revision)
                tr.validate()

    def test_resultset_sample_data(self):
        """Test all add methods for building result sets"""

        trsc = TreeherderResultSetCollection()

        for resultset in self.resultset_data:

            trs = TreeherderResultSet()

            trs.add_push_timestamp( resultset['push_timestamp'] )
            trs.add_revision_hash( resultset['revision_hash'] )
            trs.add_type( 'push' )
            trs.add_artifact( 'push_data', 'push', { 'stuff':[1,2,3,4,5] } )

            for revision in resultset['revisions']:

                tr = TreeherderRevision()

                tr.add_revision( revision['revision'] )
                tr.add_author( revision['author'] )
                tr.add_comment( revision['comment'] )
                tr.add_files( revision['files'] )
                tr.add_repository( revision['repository'] )

                trs.add_revision(tr)

            self.compare_structs(trs.data, resultset)

            trsc.add(trs)

            # confirm we get the same thing if we initialize from
            # a resultset dict
            trs_struct = TreeherderResultSet(resultset)

            self.compare_structs(trs_struct.data, resultset)

    def test_revision_hash_validation(self):

        trs = TreeherderResultSet(self.resultset_data[0])
        trs.data['revision_hash'] = None

        self.assertRaises(TreeherderClientError, trs.validate)

    def test_revision_hash_len_validation(self):

        trs = TreeherderResultSet(self.resultset_data[0])
        trs.data['revision_hash'] = (
            'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
            'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
                )

        self.assertRaises(TreeherderClientError, trs.validate)

{'fraction_non_alphanumeric': 0.09891636565712698, 'fraction_numerical': 0.012225618227285358, 'mean_word_length': 1.7651728553137005, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '5829261', 'n_tokens_mistral': 3224, 'n_tokens_neox': 3054, 'n_words': 626}