コード例 #1
0
ファイル: test_inputparser.py プロジェクト: wguanicedew/aqua
class TestInputParser(QiskitChemistryTestCase):
    """InputParser tests."""
    def setUp(self):
        super().setUp()
        filepath = self._get_resource_path('test_input_parser.txt')
        self.parser = InputParser(filepath)
        self.parser.parse()

    def test_save(self):
        """ save test """
        save_path = self._get_resource_path('output.txt')
        self.parser.save_to_file(save_path)

        parse = InputParser(save_path)
        parse.parse()
        os.remove(save_path)
        dict1 = json.loads(json.dumps(self.parser.to_dictionary()))
        dict2 = json.loads(json.dumps(parse.to_dictionary()))
        self.assertEqual(dict1, dict2)

    def test_load_from_dict(self):
        """ load from dict test """
        json_dict = self.parser.get_sections()

        parse = InputParser(json_dict)
        parse.parse()
        dict1 = json.loads(json.dumps(self.parser.to_dictionary()))
        dict2 = json.loads(json.dumps(parse.to_dictionary()))
        self.assertEqual(dict1, dict2)

    def test_is_modified(self):
        """ is modified test """
        json_dict = self.parser.get_sections()

        parse = InputParser(json_dict)
        parse.parse()
        parse.set_section_property('optimizer', 'maxfun', 1002)
        self.assertTrue(parse.is_modified())
        self.assertEqual(parse.get_section_property('optimizer', 'maxfun'),
                         1002)

    def test_validate(self):
        """ validate test """
        json_dict = self.parser.get_sections()

        parse = InputParser(json_dict)
        parse.parse()
        try:
            parse.validate_merge_defaults()
        except Exception as ex:  # pylint: disable=broad-except
            self.fail(str(ex))

        with self.assertRaises(AquaError):
            parse.set_section_property('backend', 'max_credits', -1)
コード例 #2
0
class TestInputParser(QiskitAquaChemistryTestCase):
    """InputParser tests."""
    def setUp(self):
        filepath = self._get_resource_path('test_input_parser.txt')
        self.parser = InputParser(filepath)
        self.parser.parse()

    def test_save(self):
        save_path = self._get_resource_path('output.txt')
        self.parser.save_to_file(save_path)

        p = InputParser(save_path)
        p.parse()
        os.remove(save_path)
        dict1 = json.loads(json.dumps(self.parser.to_dictionary()))
        dict2 = json.loads(json.dumps(p.to_dictionary()))
        self.assertEqual(dict1, dict2)

    def test_load_from_dict(self):
        json_dict = self.parser.get_sections()

        p = InputParser(json_dict)
        p.parse()
        dict1 = json.loads(json.dumps(self.parser.to_dictionary()))
        dict2 = json.loads(json.dumps(p.to_dictionary()))
        self.assertEqual(dict1, dict2)

    def test_is_modified(self):
        json_dict = self.parser.get_sections()

        p = InputParser(json_dict)
        p.parse()
        p.set_section_property('optimizer', 'maxfun', 1002)
        self.assertTrue(p.is_modified())
        self.assertEqual(p.get_section_property('optimizer', 'maxfun'), 1002)

    def test_validate(self):
        json_dict = self.parser.get_sections()

        p = InputParser(json_dict)
        p.parse()
        try:
            p.validate_merge_defaults()
        except Exception as e:
            self.fail(str(e))

        p.set_section_property('optimizer', 'dummy', 1002)
        self.assertRaises(AquaError, p.validate_merge_defaults)
コード例 #3
0
    def test_load_from_dict(self):
        json_dict = self.parser.get_sections()

        p = InputParser(json_dict)
        p.parse()
        dict1 = json.loads(json.dumps(self.parser.to_dictionary()))
        dict2 = json.loads(json.dumps(p.to_dictionary()))
        self.assertEqual(dict1, dict2)
コード例 #4
0
    def test_save(self):
        save_path = self._get_resource_path('output.txt')
        self.parser.save_to_file(save_path)

        p = InputParser(save_path)
        p.parse()
        os.remove(save_path)
        dict1 = json.loads(json.dumps(self.parser.to_dictionary()))
        dict2 = json.loads(json.dumps(p.to_dictionary()))
        self.assertEqual(dict1, dict2)