2016-01-05 15:14:58 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2017-12-29 09:15:48 +00:00
|
|
|
# Copyright (c) 2008-2018 OpenLP Developers #
|
2016-01-05 15:14:58 +00:00
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# This program is free software; you can redistribute it and/or modify it #
|
|
|
|
# under the terms of the GNU General Public License as published by the Free #
|
|
|
|
# Software Foundation; version 2 of the License. #
|
|
|
|
# #
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT #
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
|
|
|
# more details. #
|
|
|
|
# #
|
|
|
|
# You should have received a copy of the GNU General Public License along #
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
|
|
|
|
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
|
|
|
###############################################################################
|
|
|
|
"""
|
2016-03-31 16:24:44 +00:00
|
|
|
Package to test the openlp.core.common.db package.
|
2016-01-05 15:14:58 +00:00
|
|
|
"""
|
2016-01-15 17:50:57 +00:00
|
|
|
import gc
|
2016-01-05 15:14:58 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
2016-01-14 20:31:37 +00:00
|
|
|
import time
|
2016-03-31 16:14:28 +00:00
|
|
|
from tempfile import mkdtemp
|
|
|
|
from unittest import TestCase
|
2016-01-05 15:14:58 +00:00
|
|
|
|
2016-03-31 16:14:28 +00:00
|
|
|
import sqlalchemy
|
2016-01-15 17:50:57 +00:00
|
|
|
|
2016-03-31 16:14:28 +00:00
|
|
|
from openlp.core.common.db import drop_column, drop_columns
|
2018-10-02 04:39:42 +00:00
|
|
|
from openlp.core.lib.db import get_upgrade_op, init_db
|
2016-01-05 15:14:58 +00:00
|
|
|
from tests.utils.constants import TEST_RESOURCES_PATH
|
|
|
|
|
|
|
|
|
|
|
|
class TestUtilsDBFunctions(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Create temp folder for keeping db file
|
|
|
|
"""
|
|
|
|
self.tmp_folder = mkdtemp()
|
2016-01-14 20:25:58 +00:00
|
|
|
db_path = os.path.join(TEST_RESOURCES_PATH, 'songs', 'songs-1.9.7.sqlite')
|
2016-01-15 17:50:57 +00:00
|
|
|
self.db_tmp_path = os.path.join(self.tmp_folder, 'songs-1.9.7.sqlite')
|
|
|
|
shutil.copyfile(db_path, self.db_tmp_path)
|
|
|
|
db_url = 'sqlite:///' + self.db_tmp_path
|
2016-01-14 20:25:58 +00:00
|
|
|
self.session, metadata = init_db(db_url)
|
|
|
|
self.op = get_upgrade_op(self.session)
|
2016-01-05 15:14:58 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""
|
|
|
|
Clean up
|
|
|
|
"""
|
2016-01-14 20:25:58 +00:00
|
|
|
self.session.close()
|
2016-01-15 17:11:02 +00:00
|
|
|
self.session = None
|
2016-01-15 17:50:57 +00:00
|
|
|
gc.collect()
|
2016-01-15 17:58:21 +00:00
|
|
|
retries = 0
|
|
|
|
while retries < 5:
|
|
|
|
try:
|
2016-01-15 19:14:24 +00:00
|
|
|
if os.path.exists(self.tmp_folder):
|
|
|
|
shutil.rmtree(self.tmp_folder)
|
2016-01-15 17:58:21 +00:00
|
|
|
break
|
2016-01-15 19:14:24 +00:00
|
|
|
except:
|
2016-01-15 17:58:21 +00:00
|
|
|
time.sleep(1)
|
|
|
|
retries += 1
|
2016-01-05 15:14:58 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_delete_column(self):
|
2016-01-05 15:14:58 +00:00
|
|
|
"""
|
|
|
|
Test deleting a single column in a table
|
|
|
|
"""
|
|
|
|
# GIVEN: A temporary song db
|
|
|
|
|
|
|
|
# WHEN: Deleting a columns in a table
|
2016-01-14 20:25:58 +00:00
|
|
|
drop_column(self.op, 'songs', 'song_book_id')
|
2016-01-05 15:14:58 +00:00
|
|
|
|
|
|
|
# THEN: The column should have been deleted
|
2016-01-14 20:25:58 +00:00
|
|
|
meta = sqlalchemy.MetaData(bind=self.op.get_bind())
|
2016-01-05 15:14:58 +00:00
|
|
|
meta.reflect()
|
|
|
|
columns = meta.tables['songs'].columns
|
|
|
|
|
|
|
|
for column in columns:
|
|
|
|
if column.name == 'song_book_id':
|
|
|
|
self.fail("The column 'song_book_id' should have been deleted.")
|
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_delete_columns(self):
|
2016-01-05 15:14:58 +00:00
|
|
|
"""
|
|
|
|
Test deleting multiple columns in a table
|
|
|
|
"""
|
|
|
|
# GIVEN: A temporary song db
|
|
|
|
|
|
|
|
# WHEN: Deleting a columns in a table
|
2016-01-14 20:25:58 +00:00
|
|
|
drop_columns(self.op, 'songs', ['song_book_id', 'song_number'])
|
2016-01-05 15:14:58 +00:00
|
|
|
|
|
|
|
# THEN: The columns should have been deleted
|
2016-01-14 20:25:58 +00:00
|
|
|
meta = sqlalchemy.MetaData(bind=self.op.get_bind())
|
2016-01-05 15:14:58 +00:00
|
|
|
meta.reflect()
|
|
|
|
columns = meta.tables['songs'].columns
|
|
|
|
|
|
|
|
for column in columns:
|
|
|
|
if column.name == 'song_book_id' or column.name == 'song_number':
|
|
|
|
self.fail("The column '%s' should have been deleted." % column.name)
|