<commit_before>import os
from io import StringIO
import unittest

from django.db import connections

from openquake.engine.db.models import OqJob
from openquake.engine.tools.dump_hazards import HazardDumper
from openquake.engine.tools.restore_hazards import hazard_restore
from openquake.engine.tools.import_gmf_scenario import import_gmf_scenario

# fake gmfs like the ones generated by a scenario calculator
test_data = StringIO(unicode('''\
SA	0.025	\N	{0.2}	POINT(0.0 0.0)
SA	0.025	\N	{1.4}	POINT(1.0 0.0)
SA	0.025	\N	{0.6}	POINT(0.0 1.0)
PGA	\N	\N	{0.2,0.3}	POINT(0.0 0.0)
PGA	\N	\N	{1.4,1.5}	POINT(1.0 0.0)
PGA	\N	\N	{0.6,0.7}	POINT(0.0 1.0)
PGV	\N	\N	{0.2}	POINT(0.0 0.0)
PGV	\N	\N	{1.4}	POINT(1.0 0.0)
'''))
test_data.name = 'test_data'


class DumperTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        connections['admin'].cursor()  # open a connection
        cls.conn = connections['admin'].connection
        cls.output, cls.hc = import_gmf_scenario(test_data, 'openquake')
        cls.output.oq_job = OqJob.objects.create(
            owner_id=1, hazard_calculation=cls.hc)  # fake job
        cls.output.save()

    def test_dump_and_restore(self):
        # dump the GmfCollection generated in setUpClass into a tarfile
        hd = HazardDumper(self.conn)
        hd.dump(self.hc.id)
        tar = hd.mktar()
        curs = self.conn.cursor()
        try:
            # delete the original hazard calculation
            curs.execute('DELETE FROM uiapi.hazard_calculation '
                         'WHERE id=%s', (self.hc.id,))
            # restore the deleted GmfCollection
            hazard_restore(self.conn, tar)
            curs.execute('SELECT 1 FROM uiapi.hazard_calculation WHERE id=%s',
                         (self.hc.id,))
            self.assertEqual(curs.fetchall(), [(1,)])
        finally:
            os.remove(tar)
<commit_msg>Fixed dump_hazard_test.py<commit_after>import os
from io import StringIO
import unittest

from django.db import connections

from openquake.engine.db.models import OqJob
from openquake.engine.tools.dump_hazards import HazardDumper
from openquake.engine.tools.restore_hazards import hazard_restore
from openquake.engine.tools.import_gmf_scenario import import_gmf_scenario

# fake gmfs like the ones generated by a scenario calculator
test_data = StringIO(unicode('''\
SA	0.025	5.0	{0.2}	POINT(0.0 0.0)
SA	0.025	5.0	{1.4}	POINT(1.0 0.0)
SA	0.025	5.0	{0.6}	POINT(0.0 1.0)
PGA	\N	\N	{0.2,0.3}	POINT(0.0 0.0)
PGA	\N	\N	{1.4,1.5}	POINT(1.0 0.0)
PGA	\N	\N	{0.6,0.7}	POINT(0.0 1.0)
PGV	\N	\N	{0.2}	POINT(0.0 0.0)
PGV	\N	\N	{1.4}	POINT(1.0 0.0)
'''))
test_data.name = 'test_data'


class DumperTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        connections['admin'].cursor()  # open a connection
        cls.conn = connections['admin'].connection
        cls.output, cls.hc = import_gmf_scenario(test_data, 'openquake')
        cls.output.oq_job = OqJob.objects.create(
            owner_id=1, hazard_calculation=cls.hc)  # fake job
        cls.output.save()

    def test_dump_and_restore(self):
        # dump the GmfCollection generated in setUpClass into a tarfile
        hd = HazardDumper(self.conn)
        hd.dump(self.hc.id)
        tar = hd.mktar()
        curs = self.conn.cursor()
        try:
            # delete the original hazard calculation
            curs.execute('DELETE FROM uiapi.hazard_calculation '
                         'WHERE id=%s', (self.hc.id,))
            # restore the deleted GmfCollection
            hazard_restore(self.conn, tar)
            curs.execute('SELECT 1 FROM uiapi.hazard_calculation WHERE id=%s',
                         (self.hc.id,))
            self.assertEqual(curs.fetchall(), [(1,)])
        finally:
            os.remove(tar)

{'fraction_non_alphanumeric': 0.12345354040536984, 'fraction_numerical': 0.037904711766254276, 'mean_word_length': 2.595080416272469, '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': '7306808', 'n_tokens_mistral': 1467, 'n_tokens_neox': 1395, 'n_words': 319}