コード例 #1
0
    def test_header_constructor_without_separator(self):
        """
        Tests the header constructor when a table separator is not needed.
        """

        expected = False
        actual = self.file_instance.exists()

        self.assertEqual(expected, actual)

        expected = ["hello world  here    is       PyFunceble"]
        actual = Prints(
            None, None, output_file=None, only_on_file=False
        ).header_constructor(self.to_print["basic"], None)

        self.assertEqual(expected, actual)

        # Test of the case that we want to print the hosts file format.
        expected = [" ".join(self.to_print["hosts"].keys())]

        actual = Prints(
            None, None, output_file=None, only_on_file=False
        ).header_constructor(self.to_print["hosts"], None)

        self.assertEqual(expected, actual)
コード例 #2
0
    def test_colorify(self):
        """
        Tests the method which colors a line depending of the status.
        """

        # pylint: disable=protected-access

        expected = False
        actual = self.file_instance.exists()

        self.assertEqual(expected, actual)

        # Test with a template that is not designed for colorify
        expected = self.to_print["basic_string"]
        actual = Prints(None, "Hehehe", output_file=None, only_on_file=False)._colorify(
            self.to_print["basic_string"]
        )

        self.assertEqual(expected, actual)

        # Test with a template that is designed for colorify + Status is UP
        expected = Fore.BLACK + Back.GREEN + self.to_print["basic_string"]
        actual = Prints(
            ["This is a test", PyFunceble.STATUS.official.up],
            "Generic",
            output_file=None,
            only_on_file=False,
        )._colorify(self.to_print["basic_string"])

        self.assertEqual(expected, actual)

        # Test with a template that is designed for colorify + Status is DOWN
        expected = Fore.BLACK + Back.RED + self.to_print["basic_string"]
        actual = Prints(
            ["This is a test", PyFunceble.STATUS.official.down],
            "Generic",
            output_file=None,
            only_on_file=False,
        )._colorify(self.to_print["basic_string"])

        self.assertEqual(expected, actual)

        # Test with a template that is designed for colorify + Status is
        # UNKNOWN or INVALID
        expected = Fore.BLACK + Back.CYAN + self.to_print["basic_string"]
        actual = Prints(
            ["This is a test", PyFunceble.STATUS.official.invalid],
            "Generic",
            output_file=None,
            only_on_file=False,
        )._colorify(self.to_print["basic_string"])

        self.assertEqual(expected, actual)
コード例 #3
0
    def test_data_constructor(self):
        """
        Tests the data constructor.
        """

        expected = False
        actual = self.file_instance.exists()

        self.assertEqual(expected, actual)

        expected = OrderedDict()
        to_print = []

        chars = ["H", "E", "L", "L", "O", "!"]

        for i, size in enumerate(self.to_print["size_constructor"]):
            index = chars[i] * size
            expected[index] = size
            to_print.append(index)

        actual = Prints(
            to_print, None, output_file=None, only_on_file=False
        ).data_constructor(self.to_print["size_constructor"])

        self.assertEqual(expected, actual)

        # Test the case that there is an issue.
        expected = OrderedDict()
        to_print = []

        chars = ["H", "E", "L", "L", "O", "!"]

        for i, size in enumerate(self.to_print["size_constructor"]):
            index = chars[i] * size
            expected[index] = size
            to_print.append(index)

        del to_print[-1]

        self.assertRaisesRegex(
            Exception,
            "Inputed: %d; Size: %d"
            % (
                len(self.to_print["size_constructor"]) - 1,
                len(self.to_print["size_constructor"]),
            ),
            lambda: Prints(
                to_print, None, output_file=None, only_on_file=False
            ).data_constructor(self.to_print["size_constructor"]),
        )
コード例 #4
0
    def test_size_from_header(self):
        """
        Tests the method which extracts the sizes from the declared
        headers.
        """

        # pylint: disable=protected-access

        expected = False
        actual = self.file_instance.exists()

        self.assertEqual(expected, actual)

        expected = [element for _, element in self.to_print["basic"].items()]

        actual = Prints(
            None, None, output_file=None, only_on_file=False
        )._size_from_header(self.to_print["basic"])

        self.assertEqual(expected, actual)
コード例 #5
0
    def test_header_constructor_with_separator(self):
        """
        Tests the header constructor when a table separator is needed.
        """

        expected = False
        actual = self.file_instance.exists()

        self.assertEqual(expected, actual)

        expected = [
            "hello world  here    is       PyFunceble",
            "----- ------ ------- -------- ----------",
        ]

        actual = Prints(
            None, None, output_file=None, only_on_file=False
        ).header_constructor(self.to_print["basic"])

        self.assertEqual(expected, actual)
コード例 #6
0
    def test_before_header(self, datetime_patch):
        """
        Tests the method which prints some information before
        the generation of the content of a file.
        """

        expected = False
        actual = self.file_instance.exists()

        self.assertEqual(expected, actual)

        datetime_patch = Mock(wraps=datetime)
        datetime_patch.utcnow = Mock(
            return_value=datetime(1970, 1, 1, 1, 0, 2, 0, tzinfo=TZ("+", hours=1).get())
        )
        patch("PyFunceble.output.prints.datetime", new=datetime_patch).start()

        # pylint: disable=line-too-long
        expected = f"""# Generated by {PyFunceble.NAME} (v{PyFunceble.VERSION.split()[0]}) / {PyFunceble.abstracts.Infrastructure.REPO_LINK}
# Date of generation: {datetime_patch.utcnow().isoformat()}

"""

        Prints(
            None, None, output_file=self.file_instance.path, only_on_file=False
        ).before_header()

        self.assertEqual(expected, self.file_instance.read())

        self.file_instance.delete()

        expected = False
        actual = self.file_instance.exists()

        self.assertEqual(expected, actual)

        # pylint: disable=line-too-long
        expected = f"""# Generated by {PyFunceble.NAME} (v{PyFunceble.VERSION.split()[0]}) / {PyFunceble.abstracts.Infrastructure.REPO_LINK}
# Date of generation: {datetime_patch.utcnow().isoformat()}

Hello World
"""

        printer = Prints(
            None,
            "Generic_File",
            output_file=self.file_instance.path,
            only_on_file=False,
        )

        printer.currently_used_header = OrderedDict(
            zip(
                [
                    "Hello",
                    "World",
                    "Expiration Date",
                    "Source",
                    "HTTP Code",
                    "Analyze Date",
                ],
                [5, 5],
            )
        )

        printer.before_header()

        self.assertEqual(expected, self.file_instance.read())