1. Matt Bowen
  2. nimrud

Source

nimrud / conex / src / conex / tests / test_zcml.py

import os
import unittest
from cStringIO import StringIO
from zope.configuration.xmlconfig import xmlconfig
from zope.component.tests import clearZCML
from zope.component import getGlobalSiteManager
from conex.types.factory import ConcaveTypesFactory
from conex.types.interfaces import IConcaveTypesFactory
from zope.component import getUtility

def runSnippet(snippet):
    template = """\
    <configure xmlns='http://namespaces.zope.org/zope'
               xmlns:types="http://namespaces.nimrud.org/types"
          i18n_domain="zope">
          <include package="zope.component" file="meta.zcml" />
          <include package="zope.app.component" file="meta.zcml" />
          <include package="plone.supermodel" />
          <include package="conex.types" file="meta.zcml" />

        %s
    </configure>"""
    xmlconfig(StringIO(template % snippet))


class ZCMLTestCase(unittest.TestCase):
    def setUp(self):
        pass

    def test_without_implementation(self):
        clearZCML()
        #Load enough configuration to get the test to run at all
        xmlconfig(open(os.path.join(os.path.dirname(__file__), 
                                    "test.zcml")))
        #create a factory utility
        gsm = getGlobalSiteManager()
        gsm.registerUtility(ConcaveTypesFactory, IConcaveTypesFactory, "conex.TypesFactory")
        factory = getUtility(IConcaveTypesFactory, "conex.TypesFactory")

        registration = '''<types:type
                          schema="conex.tests.fixtures.ISampleXMLType"
                          />'''
        runSnippet(registration)
        assert u"ISampleXMLType" in factory.known_classes.keys()
        assert u"SampleXMLType" in factory.known_classes.keys()

    def test_with_implementation(self):
        clearZCML()
        #Load enough configuration to get the test to run at all
        xmlconfig(open(os.path.join(os.path.dirname(__file__), 
                                    "test.zcml")))
        #create a factory utility
        gsm = getGlobalSiteManager()
        gsm.registerUtility(ConcaveTypesFactory, IConcaveTypesFactory, "conex.TypesFactory")
        factory = getUtility(IConcaveTypesFactory, "conex.TypesFactory")

        registration = '''<types:type
                          schema="conex.tests.fixtures.sample_python_type.ISamplePythonType"
                          implementation="conex.tests.fixtures.sample_python_type.SamplePythonType"
                          />'''
        runSnippet(registration)
        assert u"ISamplePythonType" in factory.known_classes.keys()
        assert u"SamplePythonType" in factory.known_classes.keys()
        
def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(ZCMLTestCase))
    return suite