import unittest


class TestOpen(unittest.TestCase):
    def test_open_plain(self):
        import unrar

        rar = unrar.Unrar(filename='tests/test.rar', mode='RAR_OM_EXTRACT')
        self.assertIsInstance(rar, unrar.Unrar)
        del rar

    def test_open_password(self):
        import unrar

        rar = unrar.Unrar(filename='tests/test-password.rar',
                          password='testing', mode='RAR_OM_EXTRACT')
        self.assertIsInstance(rar, unrar.Unrar)
        del rar

    def test_bad_filename_plain(self):
        import unrar

        with self.assertRaises(RuntimeError):
            unrar.Unrar(filename='whoah-dude.rar', mode='RAR_OM_EXTRACT')

    def test_bad_filename_password(self):
        import unrar

        with self.assertRaises(RuntimeError):
            unrar.Unrar(filename='whoah-dude.rar',
                        password='testing', mode='RAR_OM_EXTRACT')

    def test_open_rar_list(self):
        import unrar

        rar = unrar.Unrar(filename='tests/test.rar', mode='RAR_OM_LIST')
        self.assertIsInstance(rar, unrar.Unrar)
        del rar

    def test_open_rar_list_password(self):
        import unrar

        rar = unrar.Unrar(filename='tests/test-password.rar',
                          password='testing', mode='RAR_OM_LIST')
        self.assertIsInstance(rar, unrar.Unrar)
        del rar

    def test_bad_filename_bad_password(self):
        import unrar

        with self.assertRaises(RuntimeError):
            unrar.Unrar(filename='whoah-dude.rar',
                        password='BLARGH', mode='RAR_OM_EXTRACT')

    def test_good_filename_bad_password(self):
        import unrar

        with self.assertRaises(RuntimeError):
            unrar.Unrar(filename='test-password.rar',
                        password='testing', mode='RAR_OM_EXTRACT')

    def test_bad_args_one(self):
        import unrar

        with self.assertRaises(TypeError):
            unrar.Unrar()

    def test_bad_args_two(self):
        import unrar

        with self.assertRaises(TypeError):
            unrar.Unrar(filename='whoah-dude.rar',
                        badkey1='testing', mode='RAR_OM_EXTRACT')

    def test_bad_args_three(self):
        import unrar

        with self.assertRaises(TypeError):
            unrar.Unrar(filename='whoah-dude.rar',
                        password='testing', badkey2='RAR_OM_EXTRACT')

    def test_bad_args_four(self):
        import unrar

        with self.assertRaises(TypeError):
            unrar.Unrar(badkey3='whoah-dude.rar',
                        password='testing', mode='RAR_OM_EXTRACT')

if __name__ == '__main__':
    unittest.main()
