~compiz-team/compiz/compiz.fix_1039843

« back to all changes in this revision

Viewing changes to tests/migration-scripts/test_migration.py

  • Committer: Tarmac
  • Author(s): Łukasz 'sil2100' Zemczak
  • Date: 2012-07-30 07:09:04 UTC
  • mfrom: (3294.3.1 compiz_migrationtest)
  • Revision ID: tarmac-20120730070904-xx607oeoj3tj0zxg
Added test module for the migration script created by Francis Ginther (big thanks!). Added a check for non-existence of the MUT.. Fixes: . Approved by Daniel van Vugt.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
 
3
# Copyright (C) 2012 Canonical
 
4
#
 
5
# Authors:
 
6
#  Francis 'fginther' Ginther <francis.ginther@canonical.com>
 
7
#  Łukasz 'sil2100' Zemczak <lukasz.zemczak@canonical.com>
 
8
#
 
9
# This program is free software; you can redistribute it and/or modify it under
 
10
# the terms of the GNU General Public License as published by the Free Software
 
11
# Foundation; version 3.
 
12
#
 
13
# This program is distributed in the hope that it will be useful, but WITHOUTa
 
14
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
15
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
16
# details.
 
17
#
 
18
# You should have received a copy of the GNU General Public License along with
 
19
# this program; if not, write to the Free Software Foundation, Inc.,
 
20
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
21
 
 
22
 
 
23
import sys
 
24
import unittest
 
25
from mock import MagicMock, patch
 
26
from StringIO import StringIO
 
27
 
 
28
import gconf
 
29
import glib
 
30
import os.path
 
31
import subprocess
 
32
 
 
33
# Module Under Test (mut)
 
34
try:
 
35
    mut = __import__("02_migrate_to_gsettings")
 
36
except (ImportError):
 
37
    sys.exit("Error! 02_migrate_to_gsettings module-under-test not found - perhaps you missed including it in the PYTHONPATH environment variable? Add the migration module directory and re-run the script again.")
 
38
 
 
39
class MigrateGconfToGsettingsTests(unittest.TestCase):
 
40
    """Test suite for method migrate_gconf_to_gsettings"""
 
41
 
 
42
    def setUp(self):
 
43
        """Redirects stdout so that tests can assert print statements"""
 
44
        self.stdout = sys.stdout
 
45
        self.out = StringIO()
 
46
        sys.stdout = self.out
 
47
 
 
48
    def tearDown(self):
 
49
        """Restores stdout"""
 
50
        sys.stdout = self.stdout
 
51
        # Dump the content that was sent to stdout
 
52
        # print(self.out.getvalue())
 
53
 
 
54
    def testClientGetDefaultFalse(self):
 
55
        """Test missing gconf client"""
 
56
        gconf.client_get_default = MagicMock(return_value=False)
 
57
        mut.migrate_gconf_to_gsettings()
 
58
        self.assertEqual(self.out.getvalue().strip(), "WARNING: no gconf client found. No transitionning will be done")
 
59
 
 
60
    def testGetSchemaException(self):
 
61
        """Test exception handling for get_schema"""
 
62
        gconf_mock = MagicMock(name="gconf-Mock")
 
63
        gconf.client_get_default = MagicMock(return_value=gconf_mock)
 
64
        gconf_schema_mock = MagicMock(name="gconf.Schema-Mock")
 
65
        gconf_mock.get_schema = MagicMock(return_value=gconf_schema_mock,
 
66
                                 side_effect=glib.GError)
 
67
        self.assertRaises(glib.GError, mut.migrate_gconf_to_gsettings())
 
68
 
 
69
    def testGetSchemaNone(self):
 
70
        """Test missing schema"""
 
71
        gconf_mock = MagicMock(name="gconf-Mock")
 
72
        gconf.client_get_default = MagicMock(return_value=gconf_mock)
 
73
        #gconf_schema_mock = MagicMock(name="gconf.Schema-Mock")
 
74
        gconf_mock.get_schema = MagicMock(return_value=False)
 
75
        mut.migrate_gconf_to_gsettings()
 
76
        self.assertEqual(self.out.getvalue().strip(), "No current profile set, no migration needed")
 
77
 
 
78
    def setupGetSchema(self, profile):
 
79
        """Set up mock objects for testing a valid schema"""
 
80
        # gconf.client_get_default
 
81
        gconf_mock = MagicMock(name="gconf-Mock")
 
82
        gconf.client_get_default = MagicMock(return_value=gconf_mock)
 
83
        # client.get_schema
 
84
        gconf_schema_mock = MagicMock(name="gconf.Schema-Mock")
 
85
        gconf_mock.get_schema = MagicMock(return_value=gconf_schema_mock)
 
86
        # current_profile_schema.get_default_value
 
87
        gconf_gconfvalue_mock = MagicMock(name="gconf.Schema.gconfvalue-Mock")
 
88
        gconf_schema_mock.get_default_value = MagicMock(return_value=gconf_gconfvalue_mock)
 
89
        # current_profile_gconfvalue.get_string
 
90
        gconf_gconfvalue_mock.get_string = MagicMock(return_value=profile)
 
91
 
 
92
        # Popen
 
93
        subprocess_mock = MagicMock(name="subprocess-Mock")
 
94
        popen_mock = MagicMock(name="popen-Mock", return_value=subprocess_mock)
 
95
        subprocess.Popen = popen_mock
 
96
        return popen_mock
 
97
 
 
98
    def testGetSchemaUnity(self):
 
99
        """Test the 'unity' schema"""
 
100
        popen_mock = self.setupGetSchema("unity")
 
101
        mut.migrate_gconf_to_gsettings()
 
102
        # 2 files should be converted
 
103
        self.assertEqual(len(popen_mock.call_args_list), 2)
 
104
        self.assertEqual(popen_mock.call_args_list[0][0][0][1],
 
105
                         '--file=/usr/lib/compiz/migration/compiz-profile-active-unity.convert')
 
106
        self.assertEqual(popen_mock.call_args_list[1][0][0][1],
 
107
                         '--file=/usr/lib/compiz/migration/compiz-profile-Default.convert')
 
108
 
 
109
    def testGetSchemaDefaultConvertPathInvalid(self):
 
110
        """Test the default schema"""
 
111
        popen_mock = self.setupGetSchema("Default")
 
112
        os.path.exists = MagicMock(return_value=False)
 
113
        mut.migrate_gconf_to_gsettings()
 
114
        # 1 file should be converted
 
115
        self.assertEqual(len(popen_mock.call_args_list), 1)
 
116
        self.assertEqual(popen_mock.call_args_list[0][0][0][1],
 
117
                         '--file=/usr/lib/compiz/migration/compiz-profile-active-Default.convert')
 
118
 
 
119
    def testGetSchemaDefaultConvertPathValid(self):
 
120
        """Test the default schema with a valid unity convert file"""
 
121
        popen_mock = self.setupGetSchema("Default")
 
122
        exists_mock = MagicMock(return_value=True)
 
123
        os.path.exists = exists_mock
 
124
        mut.migrate_gconf_to_gsettings()
 
125
        self.assertEqual(exists_mock.call_args_list[0][0][0], 
 
126
                         mut.CONVERT_PATH + 'compiz-profile-unity.convert')
 
127
        # 2 files should be converted
 
128
        self.assertEqual(len(popen_mock.call_args_list), 2)
 
129
        self.assertEqual(popen_mock.call_args_list[0][0][0][1],
 
130
                         '--file=/usr/lib/compiz/migration/compiz-profile-active-Default.convert')
 
131
        self.assertEqual(popen_mock.call_args_list[1][0][0][1],
 
132
                         '--file=/usr/lib/compiz/migration/compiz-profile-unity.convert')
 
133
 
 
134
 
 
135
if __name__ == '__main__':
 
136
    unittest.main()
 
137