1 | import tempfile |
---|
2 | import shutil |
---|
3 | import unittest |
---|
4 | from waeup.ikoba import tests |
---|
5 | from waeup.ikoba.interfaces import IDataCenterConfig, IPayPalConfig |
---|
6 | from zope.component import getUtility, queryUtility, getGlobalSiteManager |
---|
7 | from zope.configuration.exceptions import ConfigurationError |
---|
8 | from zope.configuration.xmlconfig import file as zcfile |
---|
9 | from zope.configuration.xmlconfig import string as zcstring |
---|
10 | |
---|
11 | |
---|
12 | def get_zcml_conf(xml_code): |
---|
13 | """Get a working ZCML configuration file with `xml_code` embedded. |
---|
14 | |
---|
15 | The returned ZCML declares an ikoba namespace for local ZCML |
---|
16 | extensions and includes the `waeup.ikoba.meta` declarations before |
---|
17 | embedding `xml_code`. |
---|
18 | |
---|
19 | The returned string can be 'executed' with `string` or `file` from |
---|
20 | `zope.configuration.xmlconfig. |
---|
21 | """ |
---|
22 | config = ( |
---|
23 | '<configure ' |
---|
24 | ' xmlns="http://namespaces.zope.org/zope"' |
---|
25 | ' xmlns:ikoba="http://namespaces.waeup.org/ikoba"' |
---|
26 | ' i18n_domain="waeup.ikoba"' |
---|
27 | ' package="waeup.ikoba.tests"' |
---|
28 | '>' |
---|
29 | '<include package="waeup.ikoba" file="meta.zcml" />' |
---|
30 | '' |
---|
31 | '%s' |
---|
32 | '' |
---|
33 | '</configure>' |
---|
34 | ) % xml_code |
---|
35 | return config |
---|
36 | |
---|
37 | |
---|
38 | class ZCMLTests(unittest.TestCase): |
---|
39 | |
---|
40 | def setUp(self): |
---|
41 | self.workdir = tempfile.mkdtemp() |
---|
42 | |
---|
43 | def tearDown(self): |
---|
44 | shutil.rmtree(self.workdir) |
---|
45 | # unregister any remaining utils registered during tests |
---|
46 | gsm = getGlobalSiteManager() |
---|
47 | for iface in (IDataCenterConfig, IPayPalConfig): |
---|
48 | conf = queryUtility(iface) |
---|
49 | if conf is None: |
---|
50 | continue |
---|
51 | gsm.unregisterUtility(conf, iface) |
---|
52 | |
---|
53 | def test_datacenter_config_directive(self): |
---|
54 | # the 'datacenter' directive can be used in ZCML |
---|
55 | assert queryUtility(IDataCenterConfig) is None |
---|
56 | zcfile("sample-datacenter.zcml", tests) # 'execute' the ZCML |
---|
57 | result = getUtility(IDataCenterConfig) |
---|
58 | assert result == {'path': u'/some/path'} |
---|
59 | |
---|
60 | def test_datacenter_config_singleton(self): |
---|
61 | # we can define only one datacenter config |
---|
62 | config = get_zcml_conf( |
---|
63 | '<ikoba:datacenter path="/some/path" />' |
---|
64 | '<ikoba:datacenter path="/other/path" />' |
---|
65 | ) |
---|
66 | assert queryUtility(IDataCenterConfig) is None |
---|
67 | self.assertRaises( |
---|
68 | ConfigurationError, zcstring, config) |
---|
69 | |
---|
70 | def test_paypal_config_directive(self): |
---|
71 | # the 'paypalconf' ZCML directive works |
---|
72 | assert queryUtility(IPayPalConfig) is None |
---|
73 | zcfile("sample-paypalconf.zcml", tests) # 'execute' the ZCML file |
---|
74 | result = getUtility(IPayPalConfig) |
---|
75 | assert result == {'path': u'/path/to/some/paypal.conf'} |
---|
76 | |
---|
77 | def test_paypal_config_singleton(self): |
---|
78 | # we can define only one paypal config |
---|
79 | config = get_zcml_conf( |
---|
80 | '<ikoba:paypalconf path="/some/path" />' |
---|
81 | '<ikoba:paypalconf path="/other/path" />' |
---|
82 | ) |
---|
83 | assert queryUtility(IPayPalConfig) is None |
---|
84 | self.assertRaises( |
---|
85 | ConfigurationError, zcstring, config) |
---|