示例#1
0
    def test_07(self):
        """
        Test Case 07:
        Convert a non-string object to bytes.

        Test is passed if the returned result is a byte sequence and equals the expected result.
        """
        test_string = [1, 2, 3]
        test_bytes = b"[1, 2, 3]"
        self.assertIsInstance(to_bytes(test_string), bytes)
        self.assertSequenceEqual(to_bytes(test_string), test_bytes)
示例#2
0
    def test_06(self):
        """
        Test Case 06:
        Convert False to bytes.

        Test is passed if the returned result is a byte sequence and equals the expected result.
        """
        test_string = False
        test_bytes = b"False"
        self.assertIsInstance(to_bytes(test_string), bytes)
        self.assertSequenceEqual(to_bytes(test_string), test_bytes)
示例#3
0
    def test_04(self):
        """
        Test Case 04:
        Convert a byte array to bytes.

        Test is passed if the returned result is a byte sequence and equals the expected result.
        """
        test_string = bytearray(b"test")
        test_bytes = b"test"
        self.assertIsInstance(to_bytes(test_string), bytes)
        self.assertSequenceEqual(to_bytes(test_string), test_bytes)
示例#4
0
    def test_02(self):
        """
        Test Case 02:
        Convert an empty string to bytes.

        Test is passed if the returned result is a byte sequence and equals the expected result.
        """
        test_string = ""
        test_bytes = b""
        self.assertIsInstance(to_bytes(test_string), bytes)
        self.assertSequenceEqual(to_bytes(test_string), test_bytes)
示例#5
0
    def test_05(self):
        """
        Test Case 05:
        Open a file using :py:func:`magrathea.utils.compat.comp_open`.

        Test is passed if file content can be read and equals the given input.
        """
        test_string = "String with Ünicøde characters"
        fp, name = tempfile.mkstemp()
        os.write(fp, to_bytes(test_string))
        os.close(fp)
        fd = comp_open(name, mode='r', encoding=get_conf('DEFAULT_CHARSET'))
        result_string = fd.read()
        fd.close()
        os.unlink(name)
        self.assertEqual(test_string, result_string)
示例#6
0
    def test_06(self):
        """
        Test Case 06:
        Use :py:func:`magrathea.utils.compat.comp_open` as :py:keyword:`with` statement context manager.

        Test is passed if file content can be read, equals the given input and the file pointer is closed.
        """
        test_string = "String with Ünicøde characters"
        fp, name = tempfile.mkstemp()
        os.write(fp, to_bytes(test_string))
        os.close(fp)
        with comp_open(name, mode='r', encoding=get_conf('DEFAULT_CHARSET')) as fd:
            result_string = fd.read()
        os.unlink(name)
        self.assertTrue(fd.closed)
        self.assertEqual(test_string, result_string)
示例#7
0
    def test_08(self):
        """
        Test Case 08:
        Read simple configuration from file.

        Test is passed if expected information is present within
        :py:class:`magrathea.utils.compat.CompConfigParser` object.
        """
        test_string = "[test]\nfoo = bar"
        fp, name = tempfile.mkstemp()
        os.write(fp, to_bytes(test_string))
        os.close(fp)
        obj = CompConfigParser()
        obj.read(name)
        os.unlink(name)
        self.assertTrue(obj.has_section('test'))
        self.assertTrue(obj.has_option('test', 'foo'))
        self.assertEqual(obj.get('test', 'foo'), 'bar')