openlp/tests/openlp_core/common/test_db.py

98 lines
3.4 KiB
Python
Raw Normal View History

2016-01-05 15:14:58 +00:00
# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
2020-12-30 21:42:49 +00:00
# Copyright (c) 2008-2021 OpenLP Developers #
2019-04-13 13:00:22 +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, either version 3 of the License, or #
# (at your option) any later version. #
# #
# 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, see <https://www.gnu.org/licenses/>. #
##########################################################################
2016-01-05 15:14:58 +00:00
"""
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
"""
import gc
2016-01-05 15:14:58 +00:00
import os
import pytest
2016-01-05 15:14:58 +00:00
import shutil
2016-01-14 20:31:37 +00:00
import time
2016-03-31 16:14:28 +00:00
from tempfile import mkdtemp
2016-01-05 15:14:58 +00:00
2016-03-31 16:14:28 +00:00
import sqlalchemy
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
@pytest.fixture
def op():
tmp_folder = mkdtemp()
db_path = os.path.join(TEST_RESOURCES_PATH, 'songs', 'songs-1.9.7.sqlite')
db_tmp_path = os.path.join(tmp_folder, 'songs-1.9.7.sqlite')
shutil.copyfile(db_path, db_tmp_path)
db_url = 'sqlite:///' + db_tmp_path
session, metadata = init_db(db_url)
upgrade_op = get_upgrade_op(session)
yield upgrade_op
session.close()
session = None
gc.collect()
retries = 0
while retries < 5:
try:
if os.path.exists(tmp_folder):
shutil.rmtree(tmp_folder)
break
except Exception:
time.sleep(1)
retries += 1
2016-01-05 15:14:58 +00:00
def test_delete_column(op):
"""
Test deleting a single column in a table
"""
# GIVEN: A temporary song db
2016-01-05 15:14:58 +00:00
# WHEN: Deleting a columns in a table
drop_column(op, 'songs', 'song_book_id')
2016-01-05 15:14:58 +00:00
# THEN: The column should have been deleted
meta = sqlalchemy.MetaData(bind=op.get_bind())
meta.reflect()
columns = meta.tables['songs'].columns
2016-01-05 15:14:58 +00:00
for column in columns:
if column.name == 'song_book_id':
assert "The column 'song_book_id' should have been deleted."
2016-01-05 15:14:58 +00:00
def test_delete_columns(op):
"""
Test deleting multiple columns in a table
"""
# GIVEN: A temporary song db
2016-01-05 15:14:58 +00:00
# WHEN: Deleting a columns in a table
drop_columns(op, 'songs', ['song_book_id', 'song_number'])
2016-01-05 15:14:58 +00:00
# THEN: The columns should have been deleted
meta = sqlalchemy.MetaData(bind=op.get_bind())
meta.reflect()
columns = meta.tables['songs'].columns
2016-01-05 15:14:58 +00:00
for column in columns:
if column.name == 'song_book_id' or column.name == 'song_number':
assert "The column '%s' should have been deleted." % column.name