def test_date_id_helpers(self):
     with app.app_context():
         backup = Backup()
         reference = gmtime()
         date_id = backup.create_id(reference)
         self.assertEqual(backup.parsed_id(date_id),
                          strftime('%b %d, %Y at %H:%M:%S', reference))
    def test_valid(self):
        with app.app_context():

            # create some files
            backup = Backup()
            date_ids = [
                '20110824045557',
                '20100106120931',
                '20090728192328',
                '20100112115416'
            ]
            class_names = ['Post', 'User']
            for date_id in date_ids:
                for class_name in class_names:
                    name = backup.get_name(date_id, class_name)
                    backup.create_file(name, '')

            # restart Backup() and assert
            backup = Backup()
            self.assertTrue(backup.valid('20110824045557'))
            self.assertFalse(backup.valid('20110824045--'))

            # clean up to avoid messing up other tests
            for name in backup.files:
                backup.delete_file(name)
            backup = Backup()
            self.assertEqual(len(backup.files), 0)
 def test_date_id_helpers(self):
     with app.app_context():
         backup = Backup()
         reference = gmtime()
         date_id = backup.create_id(reference)
         self.assertEqual(backup.parsed_id(date_id),
                          strftime('%b %d, %Y at %H:%M:%S', reference))
 def test_filter_files(self):
     with app.app_context():
         backup = Backup()
         date_ids = ['20141115172107', '20141112113214']
         class_names = ['User', 'Post']
         template = 'db-bkp-{}-{}.gz'
         files = list()
         for date_id in date_ids:
             for class_name in class_names:
                 files.append(template.format(date_id, class_name))
         expected = [template.format(date_ids[0], c) for c in class_names]
         backup.files = files
         self.assertEqual(backup.filter_files(date_ids[0]), expected)
 def test_filter_files(self):
     with app.app_context():
         backup = Backup()
         date_ids = ['20141115172107', '20141112113214']
         class_names = ['User', 'Post']
         template = 'db-bkp-{}-{}.gz'
         files = list()
         for date_id in date_ids:
             for class_name in class_names:
                 files.append(template.format(date_id, class_name))
         expected = [template.format(date_ids[0], c) for c in class_names]
         backup.files = files
         self.assertEqual(backup.filter_files(date_ids[0]), expected)
    def test_create_restore_remove(self):

        with app.app_context():

            # assert data was inserted
            posts = Post.query.count()
            authors = User.query.count()
            self.assertEqual(posts, 2)
            self.assertEqual(authors, 1)

            # create and assert backup files
            os.system('python tests/app_ftp.py alchemydumps create')
            backup = Backup()
            self.assertEqual(len(backup.files), 2)

            # clean up and recreate database
            self.db.drop_all()
            self.db.create_all()

            # assert database is empty
            posts = Post.query.count()
            authors = User.query.count()
            self.assertEqual(posts, 0)
            self.assertEqual(authors, 0)

            # restore backup
            date_id = backup.get_ids()
            command = 'python tests/app_ftp.py alchemydumps restore -d {}'
            os.system(command.format(date_id[0]))

            # assert data was restored
            posts = Post.query.count()
            authors = User.query.count()
            self.assertEqual(posts, 2)
            self.assertEqual(authors, 1)

            # assert data is accurate
            post = Post.query.first()
            self.assertEqual(post.author.email, '*****@*****.**')

            # remove backup
            command = 'python tests/app_ftp.py alchemydumps remove -d {} -y'
            os.system(command.format(date_id[0]))

            # assert there is no backup left
            backup = Backup()
            self.assertEqual(len(backup.files), 0)
    def test_create_restore_remove(self):

        with app.app_context():

            # assert data was inserted
            posts = Post.query.count()
            authors = User.query.count()
            self.assertEqual(posts, 2)
            self.assertEqual(authors, 1)

            # create and assert backup files
            os.system('python tests/app_ftp.py alchemydumps create')
            backup = Backup()
            self.assertEqual(len(backup.files), 2)

            # clean up and recreate database
            self.db.drop_all()
            self.db.create_all()

            # assert database is empty
            posts = Post.query.count()
            authors = User.query.count()
            self.assertEqual(posts, 0)
            self.assertEqual(authors, 0)

            # restore backup
            date_id = backup.get_ids()
            command = 'python tests/app_ftp.py alchemydumps restore -d {}'
            os.system(command.format(date_id[0]))

            # assert data was restored
            posts = Post.query.count()
            authors = User.query.count()
            self.assertEqual(posts, 2)
            self.assertEqual(authors, 1)

            # assert data is accurate
            post = Post.query.first()
            self.assertEqual(post.author.email, '*****@*****.**')

            # remove backup
            command = 'python tests/app_ftp.py alchemydumps remove -d {} -y'
            os.system(command.format(date_id[0]))

            # assert there is no backup left
            backup = Backup()
            self.assertEqual(len(backup.files), 0)
    def test_create_restore_remove(self):

        with app.app_context():

            # assert data was inserted
            posts = Post.query.count()
            authors = User.query.count()
            controls = SomeControl.query.count()
            comments = Comments.query.count()
            self.assertEqual(posts, 2)
            self.assertEqual(authors, 1)
            self.assertEqual(controls, 1)
            self.assertEqual(comments, 0)

            # create and assert backup files
            os.system('python tests/app_ftp.py alchemydumps create')
            backup = Backup()
            self.assertEqual(len(backup.files), 4)

            # clean up and recreate database
            self.db.drop_all()
            self.db.create_all()

            # assert database is empty
            posts = Post.query.count()
            authors = User.query.count()
            controls = SomeControl.query.count()
            comments = Comments.query.count()
            self.assertEqual(posts, 0)
            self.assertEqual(authors, 0)
            self.assertEqual(controls, 0)
            self.assertEqual(comments, 0)

            # restore backup
            date_id = backup.get_ids()
            command = 'python tests/app_ftp.py alchemydumps restore -d {}'
            os.system(command.format(date_id[0]))

            # assert data was restored
            posts = Post.query.count()
            authors = User.query.count()
            controls = SomeControl.query.count()
            comments = Comments.query.count()
            self.assertEqual(posts, 2)
            self.assertEqual(authors, 1)
            self.assertEqual(controls, 1)
            self.assertEqual(comments, 0)

            # assert data is accurate
            posts= Post.query.all()
            for num in range(1):
                self.assertEqual(posts[num].author.email, '*****@*****.**')
                self.assertEqual(posts[num].title, u'Post {}'.format(num + 1))
                self.assertEqual(posts[num].content, u'Lorem ipsum...')

            # remove backup
            command = 'python tests/app_ftp.py alchemydumps remove -d {} -y'
            os.system(command.format(date_id[0]))

            # assert there is no backup left
            backup = Backup()
            self.assertEqual(len(backup.files), 0)
    def test_autoclean(self):

        with app.app_context():

            # create fake backup dir
            backup = Backup()
            date_ids = [
                '20110824045557',
                '20100106120931',
                '20090728192328',
                '20070611074712',
                '20130729044443',
                '20070611090332',
                '20090927181422',
                '20060505063150',
                '20090608052756',
                '20050413201344',
                '20111015194547',
                '20090711221957',
                '20140425202739',
                '20130808133229',
                '20120111210958',
                '20120419224811',
                '20060519170013',
                '20090111042034',
                '20100112115416'
            ]
            class_names = ['Post', 'User', 'SomeControl', 'Comments']
            for date_id in date_ids:
                for class_name in class_names:
                    name = backup.get_name(date_id, class_name)
                    backup.create_file(name, '')

            # assert files were created
            backup = Backup()
            expected_count = len(class_names) * len(date_ids)
            self.assertEqual(len(backup.files), expected_count)

            # run auto clean
            os.system('python tests/app_ftp.py alchemydumps autoclean -y')

            # assert some files were deleted
            backup = Backup()
            white_list = [
                '20140425202739',
                '20130808133229',
                '20120419224811',
                '20111015194547',
                '20100112115416',
                '20090927181422',
                '20070611090332',
                '20060519170013',
                '20050413201344'
            ]
            expected_count = len(class_names) * len(white_list)
            self.assertEqual(len(backup.files), expected_count)

            # assert only white listed files exists,
            # and only black listed were deleted
            self.assertEqual(sorted(white_list), sorted(backup.get_ids()))

            # clean up to avoid messing up other tests
            for name in backup.files:
                backup.delete_file(name)
            backup = Backup()
            self.assertEqual(len(backup.files), 0)
    def test_autoclean(self):

        with app.app_context():

            # create fake backup dir
            backup = Backup()
            date_ids = [
                '20110824045557', '20100106120931', '20090728192328',
                '20070611074712', '20130729044443', '20070611090332',
                '20090927181422', '20060505063150', '20090608052756',
                '20050413201344', '20111015194547', '20090711221957',
                '20140425202739', '20130808133229', '20120111210958',
                '20120419224811', '20060519170013', '20090111042034',
                '20100112115416'
            ]
            class_names = ['Post', 'User']
            for date_id in date_ids:
                for class_name in class_names:
                    name = backup.get_name(date_id, class_name)
                    backup.create_file(name, '')

            # assert files were created
            backup = Backup()
            expected_count = len(class_names) * len(date_ids)
            self.assertEqual(len(backup.files), expected_count)

            # run auto clean
            os.system('python tests/app_ftp.py alchemydumps autoclean -y')

            # assert some files were deleted
            backup = Backup()
            white_list = [
                '20140425202739', '20130808133229', '20120419224811',
                '20111015194547', '20100112115416', '20090927181422',
                '20070611090332', '20060519170013', '20050413201344'
            ]
            expected_count = len(class_names) * len(white_list)
            self.assertEqual(len(backup.files), expected_count)

            # assert only white listed files exists,
            # and only black listed were deleted
            self.assertEqual(sorted(white_list), sorted(backup.get_ids()))

            # clean up to avoid messing up other tests
            for name in backup.files:
                backup.delete_file(name)
            backup = Backup()
            self.assertEqual(len(backup.files), 0)
    def test_valid(self):
        with app.app_context():

            # create some files
            backup = Backup()
            date_ids = [
                '20110824045557', '20100106120931', '20090728192328',
                '20100112115416'
            ]
            class_names = ['Post', 'User']
            for date_id in date_ids:
                for class_name in class_names:
                    name = backup.get_name(date_id, class_name)
                    backup.create_file(name, '')

            # restart Backup() and assert
            backup = Backup()
            self.assertTrue(backup.valid('20110824045557'))
            self.assertFalse(backup.valid('20110824045--'))

            # clean up to avoid messing up other tests
            for name in backup.files:
                backup.delete_file(name)
            backup = Backup()
            self.assertEqual(len(backup.files), 0)