Ejemplo n.º 1
0
    def export_events(self):
        """ Creates a ZIP archive containing the time series of the variables to be exported.

        The generated file name is placed in the private attribute ''self._archive'' for later use
        by the sending step.

        :return: the exported events count
        """
        evt_count = 0
        self._archive = None

        filter_ = EventsExportFilter(self._config.site_code, prefix_with_type=False)
        extract_date = self._parms[PARM_EXTRACT_DATE]
        with evtdao.get_dao(gs.get('dao_name')) as dao:
            events = dao.get_events_for_day(extract_date, var_type=VarTypes.ENERGY)
            if events:
                evt_count, series_files = filter_.export_events(events)
                self._archive = self.create_archive(series_files, time_stamp=datetime.datetime.utcnow())

        return evt_count
Ejemplo n.º 2
0
 def setUp(self):
     self.filter = EventsExportFilter("unittest")
     requests.post = self.mock_post
     self.tmp = None
Ejemplo n.º 3
0
class TestCase01(unittest.TestCase):
    class MockResponse(object):
        ok = None
        message = None

    def mock_post(self, url, files=None, **kwargs):
        zip = files['zip']
        zip.seek(0)

        tmp = tempfile.NamedTemporaryFile(suffix='.zip', delete=False)
        tmp.write(zip.read())
        tmp.close()
        self.tmp = tmp

        resp = self.MockResponse()
        resp.ok = True
        resp.text = json.dumps({
            'message': 'OK',
            'jobID': 42
        })

        return resp

    @classmethod
    def setUpClass(cls):
        cls.events = _create_events()

    def setUp(self):
        self.filter = EventsExportFilter("unittest")
        requests.post = self.mock_post
        self.tmp = None

    def test_01(self):
        count, files = self.filter.export_events(events=self.events)

        self.assertEqual(count, 6)
        self.assertEqual(len(files), 5)
        self.assertSetEqual(set(files), {
            '/tmp/type1_var10.tsv',
            '/tmp/type1_var11.tsv',
            '/tmp/type2_var20.tsv',
            '/tmp/type2_var21.tsv',
            '/tmp/type3_var30.tsv',
        })

    def test_02(self):
        job_cfg = ProcessConfiguration()
        job_cfg.load_dict({
            ProcessConfiguration.Props.SITE_CODE: 'unit-test',
            ProcessConfiguration.Props.REPORT_TO: '*****@*****.**',
            ProcessConfiguration.Props.SERVER: {
                ProcessConfiguration.Props.HOST: 'unittest',
                ProcessConfiguration.Props.AUTH: {
                    ProcessConfiguration.Props.LOGIN: '******',
                    ProcessConfiguration.Props.PASSWORD: '******'
                }
            }
        })
        job_parms = {
            PARM_EXTRACT_DATE: datetime.datetime(2015, 11, 03, 0, 0)
        }
        job = DWHEventsExportJob(jobname='unittest', jobid=42, config=job_cfg, parms=job_parms)
        # avoid cluttering unit tests report with logging
        job.log_setLevel(logging.ERROR)

        count, files = self.filter.export_events(events=self.events)
        arch_name = job.create_archive(files, datetime.datetime(2015, 11, 04, 0, 0))
        self.assertTrue(arch_name)
        self.assertTrue(os.path.isfile(arch_name))

        try:
            job._archive = arch_name
            job.send_data()

            self.assertIsNotNone(self.tmp)
            arch = zipfile.ZipFile(self.tmp.name)
            self.assertTrue(arch.filelist)
            self.assertEqual(len(arch.filelist), 5)

        finally:
            job.cleanup()