<div dir="ltr"><div><div><div>Hey Manuel!<br><br></div>Your example looks pretty cool and I think the fact that it's in python3 makes it a no brainer. I think we can just add a dep in sugar-build, no need to build it.<br>
<br></div><div>This is what it's going to look like on the chroot branch.<br></div><div><br></div>diff --git a/build/root.json b/build/root.json<br>index 45967f6..e5d0fd5 100644<br>--- a/build/root.json<br>+++ b/build/root.json<br>
@@ -71,7 +71,8 @@<br> "telepathy-mission-control",<br> "zip",<br> "unzip",<br>- "libwebkit2gtk"<br>+ "libwebkit2gtk",<br>
+ "python-mock"<br> ],<br> "sugar-artwork": [<br> "gnome-common",<br><br></div>We should probably wait to get the first patch using it before landing this.<br>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On 10 July 2013 16:15, Miguel González <span dir="ltr"><<a href="mailto:migonzalvar@activitycentral.com" target="_blank">migonzalvar@activitycentral.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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" target="_blank">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/" target="_blank">http://www.voidspace.org.uk/python/mock/</a><span class="HOEnZb"><font color="#888888"><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>
</font></span></div></div></div></div></div></div></div></div></div>
<br>_______________________________________________<br>
Sugar-devel mailing list<br>
<a href="mailto:Sugar-devel@lists.sugarlabs.org">Sugar-devel@lists.sugarlabs.org</a><br>
<a href="http://lists.sugarlabs.org/listinfo/sugar-devel" target="_blank">http://lists.sugarlabs.org/listinfo/sugar-devel</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br>Daniel Narvaez<br>
</div>