<div dir="ltr"><div><div><div>TL;DR: Python mock library is very useful. I share with you an example. Could it be added on sugar-build?<br><br><br>While I was working on bringing 3G database support to sugar platform, I needed to test persistent storage on GConf and locale environment variables.<br>
<br></div>At the end I didn't include the test for this functionality in the pull request [1] but I would like to share with the list my approach.<br><br>Sorry because it is a long message. I will appreciate to get some feedback about including mock library to sugar testing.<br>
<br><br></div>- First of all, I had to install mock [2]. I don't know how to do it properly using osbuild. I do think it would be very useful for testing. BTW, this library is on Python 3.3 standard library as unittest.mock. <br>
<br><br></div><div></div>- Then I imported patch function: <br><div><div><br>```<br>from mock import patch<br>```<br><br><br></div><div>- Next, I imported GConf keys an create a fake GConf client. It just a key data store with initializable values:<br>
<br>```<br>from cpsection.modemconfiguration.model import (<br> GCONF_SP_COUNTRY, GCONF_SP_PROVIDER, GCONF_SP_PLAN<br>)<br><br>class FakeGConfClient(object):<br> store = {<br> GCONF_SP_COUNTRY: '',<br>
GCONF_SP_PROVIDER: '',<br> GCONF_SP_PLAN: '',<br> }<br><br> def __init__(self, **kwargs):<br> self.store.update(kwargs)<br><br> def get_string(self, key):<br> return self.store[key]<br>
<br> def set_string(self, key, value):<br> self.store[key] = value<br> return<br><br> def get_int(self, key):<br> return self.store[key]<br><br> def set_int(self, key, value):<br> self.store[key] = value<br>
return<br>```<br></div><div><br></div><div>- The test suite has 3 parts.<br><br>1. On setUp() I patch GConf.Client.get_default to use my false client:<br></div><div>2. On test_guess_country() I patch locale.getdefaultlocale() to return an arbitrary value.<br>
</div><div>3. Finally I instantiate the main class and assert default country is the expected one<br></div><div><br>```<br>class ServiceProvidersGuessCountry(unittest.TestCase):<br> def setUp(self): # (1)<br> gconf_patcher = patch('gi.repository.GConf.Client.get_default')<br>
gconf_mock = gconf_patcher.start()<br> gconf_mock.return_value = FakeGConfClient(GCONF_SP_COUNTRY=None)<br> self.addCleanup(gconf_patcher.stop)<br><br> def test_guess_country(self):<br> LOCALE = ('hi_IN', 'UTF-8')<br>
default_country_code = LOCALE[0][3:5].lower()<br><br> with patch('locale.getdefaultlocale') as locale_mock: # (2)<br> locale_mock.return_value = LOCALE<br><br> db = ServiceProvidersDatabase()<br>
country = db.get_country()<br> self.assertEqual(country.code, default_country_code) # (3)<br>```<br><br></div><div><div>What do you think?<br><br></div><div><br>[1] <a href="https://github.com/sugarlabs/sugar/pull/58">https://github.com/sugarlabs/sugar/pull/58</a><br clear="all">
<div><div><div><div><div><br>[2] <a href="http://www.voidspace.org.uk/python/mock/">http://www.voidspace.org.uk/python/mock/</a><br>-- <br><div dir="ltr">Miguel González<br>Activity Central: <a href="http://www.activitycentral.com" target="_blank">http://www.activitycentral.com</a><br>
</div>
</div></div></div></div></div></div></div></div></div>