1# Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. 2# 3# Use of this source code is governed by a BSD-style license 4# that can be found in the LICENSE file in the root of the source 5# tree. An additional intellectual property rights grant can be found 6# in the file PATENTS. All contributing project authors may 7# be found in the AUTHORS file in the root of the source tree. 8"""Unit tests for the export module. 9""" 10 11import logging 12import os 13import shutil 14import tempfile 15import unittest 16 17import pyquery as pq 18 19from . import audioproc_wrapper 20from . import collect_data 21from . import eval_scores_factory 22from . import evaluation 23from . import export 24from . import simulation 25from . import test_data_generation_factory 26 27 28class TestExport(unittest.TestCase): 29 """Unit tests for the export module. 30 """ 31 32 _CLEAN_TMP_OUTPUT = True 33 34 def setUp(self): 35 """Creates temporary data to export.""" 36 self._tmp_path = tempfile.mkdtemp() 37 38 # Run a fake experiment to produce data to export. 39 simulator = simulation.ApmModuleSimulator( 40 test_data_generator_factory=( 41 test_data_generation_factory.TestDataGeneratorFactory( 42 aechen_ir_database_path='', 43 noise_tracks_path='', 44 copy_with_identity=False)), 45 evaluation_score_factory=( 46 eval_scores_factory.EvaluationScoreWorkerFactory( 47 polqa_tool_bin_path=os.path.join( 48 os.path.dirname(os.path.abspath(__file__)), 49 'fake_polqa'), 50 echo_metric_tool_bin_path=None)), 51 ap_wrapper=audioproc_wrapper.AudioProcWrapper( 52 audioproc_wrapper.AudioProcWrapper. 53 DEFAULT_APM_SIMULATOR_BIN_PATH), 54 evaluator=evaluation.ApmModuleEvaluator()) 55 simulator.Run( 56 config_filepaths=['apm_configs/default.json'], 57 capture_input_filepaths=[ 58 os.path.join(self._tmp_path, 'pure_tone-440_1000.wav'), 59 os.path.join(self._tmp_path, 'pure_tone-880_1000.wav'), 60 ], 61 test_data_generator_names=['identity', 'white_noise'], 62 eval_score_names=['audio_level_peak', 'audio_level_mean'], 63 output_dir=self._tmp_path) 64 65 # Export results. 66 p = collect_data.InstanceArgumentsParser() 67 args = p.parse_args(['--output_dir', self._tmp_path]) 68 src_path = collect_data.ConstructSrcPath(args) 69 self._data_to_export = collect_data.FindScores(src_path, args) 70 71 def tearDown(self): 72 """Recursively deletes temporary folders.""" 73 if self._CLEAN_TMP_OUTPUT: 74 shutil.rmtree(self._tmp_path) 75 else: 76 logging.warning(self.id() + ' did not clean the temporary path ' + 77 (self._tmp_path)) 78 79 def testCreateHtmlReport(self): 80 fn_out = os.path.join(self._tmp_path, 'results.html') 81 exporter = export.HtmlExport(fn_out) 82 exporter.Export(self._data_to_export) 83 84 document = pq.PyQuery(filename=fn_out) 85 self.assertIsInstance(document, pq.PyQuery) 86 # TODO(alessiob): Use PyQuery API to check the HTML file. 87