# Path: yithlibraryserver/compat.py
# PY3 = sys.version_info[0] == 3
#     def encode_header(obj):  # pragma: no cover
#     def encode_header(obj):  # pragma: no cover
# 
# Path: yithlibraryserver/scripts/migrations.py
# def migrate():
#     usage = "migrate: %prog config_uri migration_name"
#     description = "Add a 'send_email_periodically' preference to every user."
#     parser = optparse.OptionParser(
#         usage=usage,
#         description=textwrap.dedent(description)
#         )
#     options, args = parser.parse_args(sys.argv[1:])
#     if len(args) != 2:
#         safe_print('You must provide two arguments. '
#                    'The first one is the config file and the '
#                    'second one is the migration name.')
#         return 2
#     config_uri = args[0]
#     migration_name = args[1]
#     env = bootstrap(config_uri)
#     settings, closer = env['registry'].settings, env['closer']
# 
#     try:
#         db = settings['mongodb'].get_database()
# 
#         if migration_name in migration_registry:
#             migration = migration_registry[migration_name]
#             migration(db)
#         else:
#             safe_print('The migration "%s" does not exist.' % migration_name)
#             return 3
#     finally:
#         closer()
# 
# Path: yithlibraryserver/scripts/testing.py
# class ScriptTests(unittest.TestCase):
# 
#     clean_collections = tuple()
# 
#     def setUp(self):
#         super(ScriptTests, self).setUp()
# 
#         fd, self.conf_file_path = tempfile.mkstemp()
#         os.write(fd, CONFIG.encode('ascii'))
#         mdb = MongoDB(MONGO_URI)
#         self.db = mdb.get_database()
# 
#     def tearDown(self):
#         super(ScriptTests, self).tearDown()
#         os.unlink(self.conf_file_path)
#         for col in self.clean_collections:
#             self.db.drop_collection(col)
# 
#     def add_passwords(self, user, n):
#         for i in range(n):
#             self.db.passwords.insert({
#                     'service': 'service-%d' % (i + 1),
#                     'secret': 's3cr3t',
#                     'owner': user,
#                     })
#         return user


import sys
from yithlibraryserver.compat import StringIO
from yithlibraryserver.scripts.migrations import migrate
from yithlibraryserver.scripts.testing import ScriptTests
        # Call migrate with a config file and wrong migration name
        sys.argv = ['notused', self.conf_file_path, 'bad_migration']
        sys.stdout = StringIO()
        result = migrate()
        self.assertEqual(result, 3)
        stdout = sys.stdout.getvalue()
        self.assertEqual(stdout, 'The migration "bad_migration" does not exist.\n')

        # Good call
        sys.argv = ['notused', self.conf_file_path, 'add_send_email_preference']
        sys.stdout = StringIO()
        result = migrate()
        self.assertEqual(result, None)
        stdout = sys.stdout.getvalue()
        self.assertEqual(stdout, '')

        # Add some users
        u1_id = self.db.users.insert({
                'first_name': 'John',
                'last_name': 'Doe',
                'email': 'john@example.com',
                })
        self.db.users.insert({
                'first_name': 'John2',
                'last_name': 'Doe2',
                'email': 'john2@example.com',
                'send_passwords_periodically': False,
                })
        sys.argv = ['notused', self.conf_file_path, 'add_send_email_preference']
        sys.stdout = StringIO()

# Path: yithlibraryserver/compat.py
# PY3 = sys.version_info[0] == 3
#     def encode_header(obj):  # pragma: no cover
#     def encode_header(obj):  # pragma: no cover
# 
# Path: yithlibraryserver/scripts/migrations.py
# def migrate():
#     usage = "migrate: %prog config_uri migration_name"
#     description = "Add a 'send_email_periodically' preference to every user."
#     parser = optparse.OptionParser(
#         usage=usage,
#         description=textwrap.dedent(description)
#         )
#     options, args = parser.parse_args(sys.argv[1:])
#     if len(args) != 2:
#         safe_print('You must provide two arguments. '
#                    'The first one is the config file and the '
#                    'second one is the migration name.')
#         return 2
#     config_uri = args[0]
#     migration_name = args[1]
#     env = bootstrap(config_uri)
#     settings, closer = env['registry'].settings, env['closer']
# 
#     try:
#         db = settings['mongodb'].get_database()
# 
#         if migration_name in migration_registry:
#             migration = migration_registry[migration_name]
#             migration(db)
#         else:
#             safe_print('The migration "%s" does not exist.' % migration_name)
#             return 3
#     finally:
#         closer()
# 
# Path: yithlibraryserver/scripts/testing.py
# class ScriptTests(unittest.TestCase):
# 
#     clean_collections = tuple()
# 
#     def setUp(self):
#         super(ScriptTests, self).setUp()
# 
#         fd, self.conf_file_path = tempfile.mkstemp()
#         os.write(fd, CONFIG.encode('ascii'))
#         mdb = MongoDB(MONGO_URI)
#         self.db = mdb.get_database()
# 
#     def tearDown(self):
#         super(ScriptTests, self).tearDown()
#         os.unlink(self.conf_file_path)
#         for col in self.clean_collections:
#             self.db.drop_collection(col)
# 
#     def add_passwords(self, user, n):
#         for i in range(n):
#             self.db.passwords.insert({
#                     'service': 'service-%d' % (i + 1),
#                     'secret': 's3cr3t',
#                     'owner': user,
#                     })
#         return user


# Path: yithlibraryserver/scripts/tests/test_migrations.py
import sys
from yithlibraryserver.compat import StringIO
from yithlibraryserver.scripts.migrations import migrate
from yithlibraryserver.scripts.testing import ScriptTests

        # Call migrate with a config file and wrong migration name
        sys.argv = ['notused', self.conf_file_path, 'bad_migration']
        sys.stdout = StringIO()
        result = migrate()
        self.assertEqual(result, 3)
        stdout = sys.stdout.getvalue()
        self.assertEqual(stdout, 'The migration "bad_migration" does not exist.\n')

        # Good call
        sys.argv = ['notused', self.conf_file_path, 'add_send_email_preference']
        sys.stdout = StringIO()
        result = migrate()
        self.assertEqual(result, None)
        stdout = sys.stdout.getvalue()
        self.assertEqual(stdout, '')

        # Add some users
        u1_id = self.db.users.insert({
                'first_name': 'John',
                'last_name': 'Doe',
                'email': 'john@example.com',
                })
        self.db.users.insert({
                'first_name': 'John2',
                'last_name': 'Doe2',
                'email': 'john2@example.com',
                'send_passwords_periodically': False,
                })
        sys.argv = ['notused', self.conf_file_path, 'add_send_email_preference']
        sys.stdout = StringIO()

        result = migrate()