/
test_modemconfiguration.py
189 lines (153 loc) · 7.11 KB
/
test_modemconfiguration.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# -*- encoding: utf-8 -*-
# Copyright (C) 2013, Miguel González
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import unittest
from xml.etree.cElementTree import ElementTree
from mock import patch
from cpsection.modemconfiguration.model import CountryCodeParser, \
ServiceProvidersParser, ServiceProviders, PROVIDERS_PATH
from cpsection.modemconfiguration.model import GCONF_SP_COUNTRY, \
GCONF_SP_PROVIDER, GCONF_SP_PLAN
class CountryCodeParserTest(unittest.TestCase):
def test_get_country(self):
self.assertEqual(CountryCodeParser().get('ad'), 'Andorra')
self.assertEqual(CountryCodeParser().get('es'), 'Spain')
self.assertEqual(CountryCodeParser().get('zw'), 'Zimbabwe')
def test_raise_if_not_found(self):
with self.assertRaises(KeyError):
CountryCodeParser().get('xx')
class ServiceProvidersParserTest(unittest.TestCase):
def setUp(self):
self.tree = ElementTree(file=PROVIDERS_PATH)
self.countries_from_xml = self.tree.findall('country')
self.db = ServiceProvidersParser()
self.countries_from_class = self.db.get_countries()
def test_get_countries(self):
for country in self.countries_from_class:
self.assertEqual(country.tag, 'country')
def test_get_country_idx_by_code(self):
for idx, country in enumerate(self.countries_from_class):
country_code = country.attrib['code']
country_idx = self.db.get_country_idx_by_code(country_code)
self.assertEqual(idx, country_idx)
def test_get_country_name_by_idx(self):
for idx, country in enumerate(self.countries_from_class):
country_code = country.attrib['code']
self.assertEqual(
CountryCodeParser().get(country_code),
self.db.get_country_name_by_idx(idx)
)
def test_get_providers(self):
for country_idx, country in enumerate(self.countries_from_class):
providers = self.db.get_providers(country_idx)
for provider in providers:
self.assertEqual(provider.tag, 'provider')
self.assertIsNotNone(provider.find('.//gsm'))
def test_get_plans(self):
for country_idx, country in enumerate(self.countries_from_class):
providers = self.db.get_providers(country_idx)
for provider_idx, provider in enumerate(providers):
plans = self.db.get_plans(country_idx, provider_idx)
for plan in plans:
self.assertEqual(plan.tag, 'apn')
def get_providers(self, country_xml):
"""Given a country element find all provider with a gsm tag."""
idx = 0
for provider in country_xml.findall('provider'):
if provider.find('.//gsm'):
yield idx, provider
idx = idx + 1
def get_plans(self, provider_xml):
"""Given a provider element find all apn elements."""
for idx, plan in enumerate(provider_xml.findall('.//apn')):
yield idx, plan
def test_get_some_specific_values(self):
for country in self.countries_from_xml:
country_code = country.attrib['code']
country_idx = self.db.get_country_idx_by_code(country_code)
for provider_idx, provider in self.get_providers(country):
plans_from_class = self.db.get_plans(country_idx,
provider_idx)
for plan_idx, plan in self.get_plans(provider):
plan_from_class = plans_from_class[plan_idx]
self.assertEqual(plan.attrib['value'],
plan_from_class.attrib['value'])
class ServiceProvidersTest(unittest.TestCase):
def setUp(self):
self.db = ServiceProviders()
self.countries = self.db.get_countries()
def test_go_trough_all_combo_options(self):
# Traverse countries
for country in self.countries:
# Check if country is stored
self.db.set_country(country.idx)
new_country = self.db.get_country()
self.assertEqual(country.code, new_country.code)
# Traverse providers for country
providers = self.db.get_providers()
for provider in providers:
# Check if provider is stored
self.db.set_provider(provider.idx)
new_provider = self.db.get_provider()
self.assertEqual(provider.name, new_provider.name)
# Traverse plans for provider
plans = self.db.get_plans()
for plan in plans:
# Check if plan is stored
self.db.set_plan(plan.idx)
new_plan = self.db.get_plan()
self.assertEqual(plan.name, new_plan.name)
# Check if selection is permanently stored
db2 = ServiceProviders()
country2 = db2.get_country()
provider2 = db2.get_provider()
plan2 = db2.get_plan()
self.assertEqual(country2.idx, country.idx)
self.assertEqual(provider2.idx, provider.idx)
self.assertEqual(plan2.idx, plan.idx)
class FakeGConfClient(object):
def __init__(self, **kwargs):
self.store = {
GCONF_SP_COUNTRY: None,
GCONF_SP_PROVIDER: None,
GCONF_SP_PLAN: None,
}
self.store.update(kwargs)
def get_string(self, key):
return self.store[key]
def set_string(self, key, value):
self.store[key] = value
return
def get_int(self, key):
return self.store[key]
def set_int(self, key, value):
self.store[key] = value
return
class ServiceProvidersGuessCountryTest(unittest.TestCase):
def setUp(self):
# patch GConf.Client.get_default to use a fake client
gconf_patcher = patch('gi.repository.GConf.Client.get_default')
gconf_mock = gconf_patcher.start()
gconf_mock.return_value = FakeGConfClient(GCONF_SP_COUNTRY=None)
self.addCleanup(gconf_patcher.stop)
def test_guess_country(self):
LOCALE = ('hi_IN', 'UTF-8')
default_country_code = LOCALE[0][3:5].lower()
with patch('locale.getdefaultlocale') as locale_mock:
locale_mock.return_value = LOCALE
db = ServiceProviders()
country = db.get_country()
self.assertEqual(country.code, default_country_code)