~phill-ridout/openlp/ftw-json-theme-list

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4

###############################################################################
# OpenLP - Open Source Lyrics Projection                                      #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2019 OpenLP Developers                                   #
# --------------------------------------------------------------------------- #
# 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                          #
###############################################################################
"""
Package to test the openlp.core.common.db package.
"""
import gc
import os
import shutil
import time
from tempfile import mkdtemp
from unittest import TestCase

import sqlalchemy

from openlp.core.common.db import drop_column, drop_columns
from openlp.core.lib.db import get_upgrade_op, init_db
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()
        db_path = os.path.join(TEST_RESOURCES_PATH, 'songs', 'songs-1.9.7.sqlite')
        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
        self.session, metadata = init_db(db_url)
        self.op = get_upgrade_op(self.session)

    def tearDown(self):
        """
        Clean up
        """
        self.session.close()
        self.session = None
        gc.collect()
        retries = 0
        while retries < 5:
            try:
                if os.path.exists(self.tmp_folder):
                    shutil.rmtree(self.tmp_folder)
                break
            except Exception:
                time.sleep(1)
                retries += 1

    def test_delete_column(self):
        """
        Test deleting a single column in a table
        """
        # GIVEN: A temporary song db

        # WHEN: Deleting a columns in a table
        drop_column(self.op, 'songs', 'song_book_id')

        # THEN: The column should have been deleted
        meta = sqlalchemy.MetaData(bind=self.op.get_bind())
        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.")

    def test_delete_columns(self):
        """
        Test deleting multiple columns in a table
        """
        # GIVEN: A temporary song db

        # WHEN: Deleting a columns in a table
        drop_columns(self.op, 'songs', ['song_book_id', 'song_number'])

        # THEN: The columns should have been deleted
        meta = sqlalchemy.MetaData(bind=self.op.get_bind())
        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)