예제 #1
0
    def test_write_to_file(self):
        folder = TestFolder()
        folder.save("token.ini", "")
        ini = folder.fullpath("token.ini")
        cfg = make_config_parser()
        parser = ConfigParserMixin(ini, cfg, serialize_keys=("token", "name"))

        bad_sections = [" "]
        ok_sections = ["A", "B"]
        sections = ok_sections + bad_sections
        data = {
            s: [
                dict(token="{}{}".format(s, i), name="{}{}".format(s, i))
                for i in range(3)
            ]
            for s in sections
        }
        txt = "[A]\nA0,A0\nA1,A1\nA2,A2\n\n[B]\nB0,B0\nB1,B1\nB2,B2\n\n"

        parser.update_config(data)
        folder.save("token.ini", "")
        parser.write_to_file()
        out = folder.load("token.ini")
        try:
            self.assertEqual(
                ok_sections,
                cfg.sections(),
            )
            self._test_ini_valid_sections(out, ok_sections)
            self.assertEqual(txt, out.replace("\r\n", "\n"),
                             "ini does not match expected")
        except Exception as e:
            self._log_error("sections:", dict(cfg._sections))
            self._log_error("ini content:\n", txt)
            raise e
예제 #2
0
    def test_read_from_file(self):
        txt = "[A]\nA0,A0\nA1,A1\nA2,A2\n\n[B]\nB0,B0\nB1,B1\nB2,B2\n\n"
        ok_sections = ["A", "B"]

        folder = TestFolder()
        folder.save("token.ini", txt)
        ini = folder.fullpath("token.ini")
        cfg = make_config_parser()
        parser = ConfigParserMixin(ini, cfg, serialize_keys=("token", "name"))
        parser.read_from_file()

        try:
            self.assertEqual(
                ok_sections,
                cfg.sections(),
            )

            data = parser.get_config()
            self.assertEqual(
                {
                    'A': [{
                        'token': 'A0',
                        'name': 'A0'
                    }, {
                        'token': 'A1',
                        'name': 'A1'
                    }, {
                        'token': 'A2',
                        'name': 'A2'
                    }],
                    'B': [{
                        'token': 'B0',
                        'name': 'B0'
                    }, {
                        'token': 'B1',
                        'name': 'B1'
                    }, {
                        'token': 'B2',
                        'name': 'B2'
                    }]
                }, data, "loaded config does not match expected")

            folder.save("token.ini", "")
            parser.write_to_file()
            out = folder.load("token.ini")
            self._test_ini_valid_sections(txt, ok_sections)
            self.assertEqual(txt, out.replace("\r\n", "\n"),
                             "ini does not match expected")
        except Exception as e:
            self._log_error("sections:", dict(cfg._sections))
            self._log_error("ini content:\n", txt)
            raise e
예제 #3
0
    def test_update_config(self):
        parser = ConfigParserMixin("",
                                   make_config_parser(),
                                   serialize_keys=("token", "name"))
        sections = ["A", "B"]
        data = {
            s: [
                dict(token="{}{}".format(s, i), name="{}{}".format(s, i))
                for i in range(3)
            ]
            for s in sections
        }
        parser.update_config(dict(A=data["A"]))
        parser.update_config(dict(B=data["B"]))

        try:
            self.assertEqual(data, parser.get_config())

            data["A"].reverse()
            self.assertNotEqual(data, parser.get_config())

            parser.update_config(dict(A=data["A"]))
            self.assertEqual(data, parser.get_config())

            data["B"].clear()
            self.assertNotEqual(data, parser.get_config())

            parser.update_config(dict(B=data["B"]))
            self.assertEqual(data, parser.get_config())

            data.update({
                s: [
                    dict(token="{}{}".format(s, i), name="{}{}".format(s, i))
                    for i in range(3)
                ]
                for s in ["C"]
            })

            parser.update_config(dict(C=data["C"]))
            self.assertEqual(data, parser.get_config())

            data_with_invalid_section = {
                s: [
                    dict(token="{}{}".format(s, i), name="{}{}".format(s, i))
                    for i in range(3)
                ]
                for s in [" ", ""]
            }
            parser.update_config(data_with_invalid_section)
            self.assertEqual(data, parser.get_config(),
                             "data with invalid section is not rejected")

            # # TODO: test invalid token in final token model
            # # writer does not know what is an invalid data (empty token or name)
            # # the token model should know
            # data_with_invalid_token = {s: [
            #     dict(token="", name="{}{}".format(s,i))
            #     for i in range(3)]
            #     for s in ["D"]
            # }
            # parser.update_config(data_with_invalid_token)
            # self.assertEqual(
            #     data,
            #     parser.get_config(), "data with invalid token is not rejected"
            # )

        except Exception as e:
            output = parser.get_config()
            self._log_error("data sections: {}".format(data.keys()))
            self._log_error("output sections: {}".format(output.keys()))
            self._log_error("data: {}".format(data))
            self._log_error("output: {}".format(output))
            raise e