<commit_before># Copyright (c) 2010-2012, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.

import nrml
import os
import unittest

import openquake

from lxml import etree
from numpy import median

from tests.utils import helpers
from openquake.db import models


class BaseQATestCase(unittest.TestCase):
    """
    Base QA test case class with some general functionality built in for
    running QA tests.
    """

    def run_hazard(self, cfg, exports=None):
        """
        Given the path to job config file, run the job and assert that it was
        successful. If this assertion passes, return the completed job.

        :param str cfg:
            Path to a job config file.
        :param list exports:
            A list of export format types. Currently only 'xml' is supported.
        :returns:
            The completed :class:`~openquake.db.models.OqJob`.
        :raises:
            :exc:`AssertionError` if the job was not successfully run.
        """
        # backup the OQ_NO_DISTRIBUTE state:
        old_nd = os.environ.get(openquake.NO_DISTRIBUTE_VAR)

        # by default, set OQ_NO_DISTRIBUTE to true, so we can benefit from
        # including these tests in our code coverage
        os.environ[openquake.NO_DISTRIBUTE_VAR] = '1'

        completed_job = helpers.run_hazard_job(cfg, exports=exports)

        self.assertEqual('complete', completed_job.status)

        # restore the OQ_NO_DISTRIBUTE state:
        if old_nd is None:
            os.environ.pop(openquake.NO_DISTRIBUTE_VAR)
        else:
            os.environ[openquake.NO_DISTRIBUTE_VAR] = old_nd

        return completed_job

    def assert_xml_equal(self, a, b):
        """<commit_msg>qa_tests/_utils:<commit_after># Copyright (c) 2010-2012, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.

import nrml
import os
import unittest

import openquake

from lxml import etree
from mock import patch
from numpy import median

from tests.utils import helpers
from openquake.db import models


class BaseQATestCase(unittest.TestCase):
    """
    Base QA test case class with some general functionality built in for
    running QA tests.
    """

    def run_hazard(self, cfg, exports=None):
        """
        Given the path to job config file, run the job and assert that it was
        successful. If this assertion passes, return the completed job.

        :param str cfg:
            Path to a job config file.
        :param list exports:
            A list of export format types. Currently only 'xml' is supported.
        :returns:
            The completed :class:`~openquake.db.models.OqJob`.
        :raises:
            :exc:`AssertionError` if the job was not successfully run.
        """
        # Set OQ_NO_DISTRIBUTE to true, so we can benefit from including these
        # tests in our code coverage
        with patch.dict('os.environ', {openquake.NO_DISTRIBUTE_VAR: '1'}):
            completed_job = helpers.run_hazard_job(cfg, exports=exports)

            self.assertEqual('complete', completed_job.status)

        return completed_job

    def assert_xml_equal(self, a, b):
        """
{'fraction_non_alphanumeric': 0.06836027713625865, 'fraction_numerical': 0.004618937644341801, 'mean_word_length': 2.905320108205591, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, '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': '1503148', 'n_tokens_mistral': 1225, 'n_tokens_neox': 1194, 'n_words': 502}