# -*- coding: utf-8 -*-
# ###
# Copyright (c) 2016, Rice University
# This software is subject to the provisions of the GNU Affero General
# Public License version 3 (AGPLv3).
# See LICENCE.txt for details.
# ###

import sys
import unittest

import pkg_resources
import psycopg2

from . import testing


class BaseTestCase(unittest.TestCase):
    def tearDown(self):
        with psycopg2.connect(testing.db_connection_string) as db_conn:
            with db_conn.cursor() as cursor:
                cursor.execute('DROP TABLE IF EXISTS schema_migrations')


class VersionTestCase(BaseTestCase):
    def test(self):
        from ..cli import main

        version = pkg_resources.get_distribution('db-migrator').version
        with testing.captured_output() as (out, err):
            self.assertRaises(SystemExit, main, ['-V'])

        stdout = out.getvalue()
        stderr = err.getvalue()
        if sys.version_info[0] == 3:
            self.assertEqual(stdout.strip(), version)
            self.assertEqual(stderr, '')
        else:
            self.assertEqual(stdout, '')
            self.assertEqual(stderr.strip(), version)


class ListTestCase(BaseTestCase):
    def test_no_migrations_directory(self):
        from ..cli import main

        cmd = ['--db-connection-string', testing.db_connection_string]
        main(cmd + ['init'])
        with testing.captured_output() as (out, err):
            main(cmd + ['list'])

        stdout = out.getvalue()
        stderr = err.getvalue()

        self.assertEqual(stdout, """\
name                      | is applied | date applied
----------------------------------------------------------------------\n""")
        self.assertEqual(stderr, """\
context undefined, using current directory name "['db-migrator']"
migrations directory undefined\n""")

@@ -233,16 +233,87 @@
 unittest
+%0Atry:%0A    from unittest import mock%0Aexcept ImportError:%0A    import mock
 %0A%0Aimport
@@ -1198,24 +1198,608 @@
  version)%0A%0A%0A
+class VerboseTestCase(BaseTestCase):%0A    @mock.patch('dbmigrator.logger.debug')%0A    def test(self, debug):%0A        from ..cli import main%0A        from .. import logger%0A%0A        with testing.captured_output() as (out, err):%0A            main(%5B'-v', '--config', testing.test_config_path, 'init'%5D)%0A%0A        stdout = out.getvalue()%0A        stderr = err.getvalue()%0A%0A        self.assertEqual(1, debug.call_count)%0A        self.assertTrue(debug.call_args%5B0%5D%5B0%5D.startswith('args: %7B'))%0A%0A        self.assertEqual('Schema migrations initialized.%5Cn', stdout)%0A        self.assertEqual('', stderr)%0A%0A%0A
 class ListTe

Add test case for cli verbose option