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

2631.1.2 by Tim Bentley
fix tests
1
# -*- coding: utf-8 -*-
2
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
3
4
###############################################################################
5
# OpenLP - Open Source Lyrics Projection                                      #
6
# --------------------------------------------------------------------------- #
2844.1.1 by Tomas Groth
Updated to 2019 in copyright headers
7
# Copyright (c) 2008-2019 OpenLP Developers                                   #
2631.1.2 by Tim Bentley
fix tests
8
# --------------------------------------------------------------------------- #
9
# This program is free software; you can redistribute it and/or modify it     #
10
# under the terms of the GNU General Public License as published by the Free  #
11
# Software Foundation; version 2 of the License.                              #
12
#                                                                             #
13
# This program is distributed in the hope that it will be useful, but WITHOUT #
14
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
15
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for    #
16
# more details.                                                               #
17
#                                                                             #
18
# You should have received a copy of the GNU General Public License along     #
19
# with this program; if not, write to the Free Software Foundation, Inc., 59  #
20
# Temple Place, Suite 330, Boston, MA 02111-1307 USA                          #
21
###############################################################################
22
"""
23
Package to test the openlp.core.common.db package.
24
"""
25
import gc
26
import os
27
import shutil
28
import time
29
from tempfile import mkdtemp
30
from unittest import TestCase
31
32
import sqlalchemy
33
34
from openlp.core.common.db import drop_column, drop_columns
2766.3.62 by Raoul Snyman
HEAD
35
from openlp.core.lib.db import get_upgrade_op, init_db
2631.1.2 by Tim Bentley
fix tests
36
from tests.utils.constants import TEST_RESOURCES_PATH
37
38
39
class TestUtilsDBFunctions(TestCase):
40
41
    def setUp(self):
42
        """
43
        Create temp folder for keeping db file
44
        """
45
        self.tmp_folder = mkdtemp()
46
        db_path = os.path.join(TEST_RESOURCES_PATH, 'songs', 'songs-1.9.7.sqlite')
47
        self.db_tmp_path = os.path.join(self.tmp_folder, 'songs-1.9.7.sqlite')
48
        shutil.copyfile(db_path, self.db_tmp_path)
49
        db_url = 'sqlite:///' + self.db_tmp_path
50
        self.session, metadata = init_db(db_url)
51
        self.op = get_upgrade_op(self.session)
52
53
    def tearDown(self):
54
        """
55
        Clean up
56
        """
57
        self.session.close()
58
        self.session = None
59
        gc.collect()
60
        retries = 0
61
        while retries < 5:
62
            try:
63
                if os.path.exists(self.tmp_folder):
64
                    shutil.rmtree(self.tmp_folder)
65
                break
2837.1.3 by Raoul Snyman
Fix linting issues
66
            except Exception:
2631.1.2 by Tim Bentley
fix tests
67
                time.sleep(1)
68
                retries += 1
69
2672.1.1 by Raoul Snyman
Initial attempt to move to nose2
70
    def test_delete_column(self):
2631.1.2 by Tim Bentley
fix tests
71
        """
72
        Test deleting a single column in a table
73
        """
74
        # GIVEN: A temporary song db
75
76
        # WHEN: Deleting a columns in a table
77
        drop_column(self.op, 'songs', 'song_book_id')
78
79
        # THEN: The column should have been deleted
80
        meta = sqlalchemy.MetaData(bind=self.op.get_bind())
81
        meta.reflect()
82
        columns = meta.tables['songs'].columns
83
84
        for column in columns:
85
            if column.name == 'song_book_id':
86
                self.fail("The column 'song_book_id' should have been deleted.")
87
2672.1.1 by Raoul Snyman
Initial attempt to move to nose2
88
    def test_delete_columns(self):
2631.1.2 by Tim Bentley
fix tests
89
        """
90
        Test deleting multiple columns in a table
91
        """
92
        # GIVEN: A temporary song db
93
94
        # WHEN: Deleting a columns in a table
95
        drop_columns(self.op, 'songs', ['song_book_id', 'song_number'])
96
97
        # THEN: The columns should have been deleted
98
        meta = sqlalchemy.MetaData(bind=self.op.get_bind())
99
        meta.reflect()
100
        columns = meta.tables['songs'].columns
101
102
        for column in columns:
103
            if column.name == 'song_book_id' or column.name == 'song_number':
104
                self.fail("The column '%s' should have been deleted." % column.name)