Skip to content

Commit

Permalink
registry records were renamed; added tests for the control panel and …
Browse files Browse the repository at this point in the history
…registry
  • Loading branch information
hvelarde committed Jul 23, 2012
1 parent 4017a42 commit c26ff8a
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 41 deletions.
16 changes: 9 additions & 7 deletions src/collective/weather/browser/controlpanel.py
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

from zope.component import getUtility from zope.component import getUtility


from zope.interface import alsoProvides from zope.interface import alsoProvides
Expand Down Expand Up @@ -35,7 +37,7 @@ class NoaaGroup(group.Group):
class WeatherControlPanelEditForm(controlpanel.RegistryEditForm): class WeatherControlPanelEditForm(controlpanel.RegistryEditForm):
schema = IWeatherSchema schema = IWeatherSchema


label = _("Weather setup") label = _("Weather Setup")
description = _("""Lets you configure several weather locations""") description = _("""Lets you configure several weather locations""")


fields = IGoogleWeatherSchema fields = IGoogleWeatherSchema
Expand All @@ -47,12 +49,12 @@ def getContent(self):


# def updateFields(self): # def updateFields(self):
# super(WeatherControlPanelEditForm, self).updateFields() # super(WeatherControlPanelEditForm, self).updateFields()
# self.fields['g_locations_id'].widgetFactory = EnhancedTextLinesFieldWidget # self.fields['google_location_ids'].widgetFactory = EnhancedTextLinesFieldWidget
# self.groups[0].fields['y_locations_id'].widgetFactory = EnhancedTextLinesFieldWidget # self.groups[0].fields['yahoo_location_ids'].widgetFactory = EnhancedTextLinesFieldWidget
# self.groups[1].fields['n_locations_id'].widgetFactory = EnhancedTextLinesFieldWidget # self.groups[1].fields['noaa_location_ids'].widgetFactory = EnhancedTextLinesFieldWidget


# def updateWidgets(self): # def updateWidgets(self):
# super(NITFSettingsEditForm, self).updateWidgets() # super(WeatherControlPanelEditForm, self).updateWidgets()
# self.widgets['available_sections'].rows = 8 # self.widgets['available_sections'].rows = 8
# self.widgets['available_sections'].style = u'width: 30%;' # self.widgets['available_sections'].style = u'width: 30%;'


Expand Down Expand Up @@ -100,5 +102,5 @@ def __repr__(self):
def _get_proxy(self, interface): def _get_proxy(self, interface):
proxies = self.__proxies__ proxies = self.__proxies__
return proxies.get(interface) or \ return proxies.get(interface) or \
proxies.setdefault(interface, self.__registry__.\ proxies.setdefault(interface,
forInterface(interface)) self.__registry__.forInterface(interface))
48 changes: 28 additions & 20 deletions src/collective/weather/browser/interfaces.py
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

from zope.interface import Interface from zope.interface import Interface


from zope import schema from zope import schema
Expand All @@ -15,66 +17,72 @@




class IGoogleWeatherSchema(form.Schema): class IGoogleWeatherSchema(form.Schema):
""" """
Configurations for getting weather information from Google Configurations for getting weather information from Google
""" """


use_google = schema.Bool(title=_(u"Use Google weather service")) use_google = schema.Bool(title=_(u"Use Google weather service"),
default=False,
)


form.widget(g_locations_id="collective.z3cform.widgets.enhancedtextlines.EnhancedTextLinesFieldWidget") form.widget(google_location_ids="collective.z3cform.widgets.enhancedtextlines.EnhancedTextLinesFieldWidget")
g_locations_id = schema.List(title=_(u"Available options"), google_location_ids = schema.List(title=_(u"Available options"),
description=_(u"Enter here all available locations that will be shown in the locations drop down."), description=_(u"Enter here all available locations that will be shown in the locations drop down."),
value_type= schema.TextLine(), value_type=schema.TextLine(),
default=[], default=[],
required=False) required=False)


g_hl = schema.TextLine(title=_(u"Language"), google_language = schema.TextLine(title=_(u"Language"),
description=_(u"Enter the language code to show the content."), description=_(u"Enter the language code to show the content."),
required=False) required=False)


g_units = schema.Choice(title=_(u'Units'), google_units = schema.Choice(title=_(u'Units'),
description=_(u"Units to show the results."), description=_(u"Units to show the results."),
default='metric', default='metric',
required=False, required=False,
source=units_vocab) source=units_vocab)




class IYahooWeatherSchema(form.Schema): class IYahooWeatherSchema(form.Schema):
""" """
Configurations for getting weather information from Yahoo Configurations for getting weather information from Yahoo
""" """


use_yahoo = schema.Bool(title=_(u"Use Yahoo weather service")) use_yahoo = schema.Bool(title=_(u"Use Yahoo weather service"),
default=False,
)


form.widget(y_locations_id="collective.z3cform.widgets.enhancedtextlines.EnhancedTextLinesFieldWidget") form.widget(yahoo_location_ids="collective.z3cform.widgets.enhancedtextlines.EnhancedTextLinesFieldWidget")
y_locations_id = schema.List(title=_(u"Available options"), yahoo_location_ids = schema.List(title=_(u"Available options"),
description=_(u"Enter here all available locations that " description=_(u"Enter here all available locations that "
"will be shown in the locations drop down." "will be shown in the locations drop down."
"Check http://code.google.com/p/python-weather-api/#Yahoo!_Weather for further information."), "Check http://code.google.com/p/python-weather-api/#Yahoo!_Weather for further information."),
value_type= schema.TextLine(), value_type=schema.TextLine(),
default=[], default=[],
required=False) required=False)


y_units = schema.Choice(title=_(u'Units'), yahoo_units = schema.Choice(title=_(u'Units'),
description=_(u"Units to show the results."), description=_(u"Units to show the results."),
default='metric', default='metric',
required=False, required=False,
source=units_vocab) source=units_vocab)




class INoaaWeatherSchema(form.Schema): class INoaaWeatherSchema(form.Schema):
""" """
Configurations for getting weather information from NOAA Configurations for getting weather information from NOAA
""" """


use_noaa = schema.Bool(title=_(u"Use NOAA weather service")) use_noaa = schema.Bool(title=_(u"Use NOAA weather service"),
default=False,
)


form.widget(n_locations_id="collective.z3cform.widgets.enhancedtextlines.EnhancedTextLinesFieldWidget") form.widget(noaa_location_ids="collective.z3cform.widgets.enhancedtextlines.EnhancedTextLinesFieldWidget")
n_locations_id = schema.List(title=_(u"Available options"), noaa_location_ids = schema.List(title=_(u"Available options"),
description=_(u"Enter here all available locations that " description=_(u"Enter here all available locations that "
"will be shown in the locations drop down. " "will be shown in the locations drop down. "
"Check http://code.google.com/p/python-weather-api/#NOAA for further information."), "Check http://code.google.com/p/python-weather-api/#NOAA for further information."),
value_type= schema.TextLine(), value_type=schema.TextLine(),
default=[], default=[],
required=False) required=False)


Expand All @@ -83,8 +91,8 @@ class IWeatherSchema(IGoogleWeatherSchema, IYahooWeatherSchema, INoaaWeatherSche
""" """
""" """



class IWeatherControlPanelForm(Interface): class IWeatherControlPanelForm(Interface):
""" """
Control panel used to manage weather configurations Control panel used to manage weather configurations
""" """

2 changes: 1 addition & 1 deletion src/collective/weather/profiles/default/registry.xml
Expand Up @@ -3,4 +3,4 @@
<records interface="collective.weather.browser.interfaces.IGoogleWeatherSchema" /> <records interface="collective.weather.browser.interfaces.IGoogleWeatherSchema" />
<records interface="collective.weather.browser.interfaces.IYahooWeatherSchema" /> <records interface="collective.weather.browser.interfaces.IYahooWeatherSchema" />
<records interface="collective.weather.browser.interfaces.INoaaWeatherSchema" /> <records interface="collective.weather.browser.interfaces.INoaaWeatherSchema" />
</registry> </registry>
9 changes: 9 additions & 0 deletions src/collective/weather/profiles/uninstall/registry.xml
@@ -1,3 +1,12 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<registry> <registry>
<record name="collective.weather.browser.interfaces.IGoogleWeatherSchema.use_google" delete="true" />
<record name="collective.weather.browser.interfaces.IGoogleWeatherSchema.google_location_ids" delete="true" />
<record name="collective.weather.browser.interfaces.IGoogleWeatherSchema.google_language" delete="true" />
<record name="collective.weather.browser.interfaces.IGoogleWeatherSchema.google_units" delete="true" />
<record name="collective.weather.browser.interfaces.IGoogleWeatherSchema.use_yahoo" delete="true" />
<record name="collective.weather.browser.interfaces.IGoogleWeatherSchema.yahoo_location_ids" delete="true" />
<record name="collective.weather.browser.interfaces.IGoogleWeatherSchema.yahoo_units" delete="true" />
<record name="collective.weather.browser.interfaces.IGoogleWeatherSchema.use_noaa" delete="true" />
<record name="collective.weather.browser.interfaces.IGoogleWeatherSchema.noaa_location_ids" delete="true" />
</registry> </registry>
168 changes: 168 additions & 0 deletions src/collective/weather/tests/test_controlpanel.py
@@ -0,0 +1,168 @@
# -*- coding: utf-8 -*-

import unittest2 as unittest

from zope.component import getMultiAdapter
from zope.component import getUtility

from plone.app.testing import TEST_USER_ID
from plone.app.testing import logout
from plone.app.testing import setRoles
from plone.registry.interfaces import IRegistry

from collective.weather.config import PROJECTNAME
from collective.weather.browser.interfaces import IGoogleWeatherSchema
from collective.weather.browser.interfaces import INoaaWeatherSchema
from collective.weather.browser.interfaces import IYahooWeatherSchema
from collective.weather.testing import INTEGRATION_TESTING


class ControlPanelTestCase(unittest.TestCase):

layer = INTEGRATION_TESTING

def setUp(self):
self.portal = self.layer['portal']
self.controlpanel = self.portal['portal_controlpanel']
setRoles(self.portal, TEST_USER_ID, ['Manager'])

def test_controlpanel_has_view(self):
view = getMultiAdapter((self.portal, self.portal.REQUEST),
name='weather-controlpanel')
view = view.__of__(self.portal)
self.assertTrue(view())

def test_controlpanel_view_is_protected(self):
from AccessControl import Unauthorized
logout()
self.assertRaises(Unauthorized,
self.portal.restrictedTraverse,
'@@weather-controlpanel')

def test_controlpanel_installed(self):
actions = [a.getAction(self)['id']
for a in self.controlpanel.listActions()]
self.assertTrue('WeatherSettings' in actions,
'control panel was not installed')

def test_controlpanel_removed_on_uninstall(self):
qi = self.portal['portal_quickinstaller']
qi.uninstallProducts(products=[PROJECTNAME])
actions = [a.getAction(self)['id']
for a in self.controlpanel.listActions()]
self.assertTrue('WeatherSettings' not in actions,
'control panel was not removed')


class GoogleRegistryTestCase(unittest.TestCase):

layer = INTEGRATION_TESTING

def setUp(self):
self.portal = self.layer['portal']
self.registry = getUtility(IRegistry)
self.settings = self.registry.forInterface(IGoogleWeatherSchema)
setRoles(self.portal, TEST_USER_ID, ['Manager'])

def test_use_google_record_in_registry(self):
self.assertTrue(hasattr(self.settings, 'use_google'))
self.assertEqual(self.settings.use_google, False)

def test_google_location_ids_record_in_registry(self):
self.assertTrue(hasattr(self.settings, 'google_location_ids'))
self.assertEqual(self.settings.google_location_ids, [])

def test_google_language_record_in_registry(self):
self.assertTrue(hasattr(self.settings, 'google_language'))
self.assertEqual(self.settings.google_language, None)

def test_google_units_record_in_registry(self):
self.assertTrue(hasattr(self.settings, 'google_units'))
self.assertEqual(self.settings.google_units, u'metric')

def get_record(self, record):
""" Helper function; it raises KeyError if the record is not in the
registry.
"""
prefix = 'collective.weather.browser.interfaces.IGoogleWeatherSchema.'
return self.registry[prefix + record]

def test_records_removed_on_uninstall(self):
# XXX: I haven't found a better way to test this; anyone?
qi = self.portal['portal_quickinstaller']
qi.uninstallProducts(products=[PROJECTNAME])
self.assertRaises(KeyError, self.get_record, 'use_google')
self.assertRaises(KeyError, self.get_record, 'google_location_ids')
self.assertRaises(KeyError, self.get_record, 'google_language')
self.assertRaises(KeyError, self.get_record, 'google_units')


class YahooRegistryTestCase(unittest.TestCase):

layer = INTEGRATION_TESTING

def setUp(self):
self.portal = self.layer['portal']
self.registry = getUtility(IRegistry)
self.settings = self.registry.forInterface(IYahooWeatherSchema)
setRoles(self.portal, TEST_USER_ID, ['Manager'])

def test_use_yahoo_record_in_registry(self):
self.assertTrue(hasattr(self.settings, 'use_yahoo'))
self.assertEqual(self.settings.use_yahoo, False)

def test_yahoo_location_ids_record_in_registry(self):
self.assertTrue(hasattr(self.settings, 'yahoo_location_ids'))
self.assertEqual(self.settings.yahoo_location_ids, [])

def test_yahoo_units_record_in_registry(self):
self.assertTrue(hasattr(self.settings, 'yahoo_units'))
self.assertEqual(self.settings.yahoo_units, u'metric')

def get_record(self, record):
""" Helper function; it raises KeyError if the record is not in the
registry.
"""
prefix = 'collective.weather.browser.interfaces.IYahooWeatherSchema.'
return self.registry[prefix + record]

def test_records_removed_on_uninstall(self):
# XXX: I haven't found a better way to test this; anyone?
qi = self.portal['portal_quickinstaller']
qi.uninstallProducts(products=[PROJECTNAME])
self.assertRaises(KeyError, self.get_record, 'use_yahoo')
self.assertRaises(KeyError, self.get_record, 'yahoo_location_ids')
self.assertRaises(KeyError, self.get_record, 'yahoo_units')


class NoaaRegistryTestCase(unittest.TestCase):

layer = INTEGRATION_TESTING

def setUp(self):
self.portal = self.layer['portal']
self.registry = getUtility(IRegistry)
self.settings = self.registry.forInterface(INoaaWeatherSchema)
setRoles(self.portal, TEST_USER_ID, ['Manager'])

def test_use_noaa_record_in_registry(self):
self.assertTrue(hasattr(self.settings, 'use_noaa'))
self.assertEqual(self.settings.use_noaa, False)

def test_noaa_location_ids_record_in_registry(self):
self.assertTrue(hasattr(self.settings, 'noaa_location_ids'))
self.assertEqual(self.settings.noaa_location_ids, [])

def get_record(self, record):
""" Helper function; it raises KeyError if the record is not in the
registry.
"""
prefix = 'collective.weather.browser.interfaces.INoaaWeatherSchema.'
return self.registry[prefix + record]

def test_records_removed_on_uninstall(self):
# XXX: I haven't found a better way to test this; anyone?
qi = self.portal['portal_quickinstaller']
qi.uninstallProducts(products=[PROJECTNAME])
self.assertRaises(KeyError, self.get_record, 'use_noaa')
self.assertRaises(KeyError, self.get_record, 'noaa_location_ids')
22 changes: 9 additions & 13 deletions src/collective/weather/utility.py
@@ -1,8 +1,6 @@


from AccessControl import ClassSecurityInfo from AccessControl import ClassSecurityInfo


from zope.component import getUtility

from zope.interface import classProvides from zope.interface import classProvides
from zope.interface import implements from zope.interface import implements


Expand All @@ -29,14 +27,12 @@ class Weather(SimpleItem):


security = ClassSecurityInfo() security = ClassSecurityInfo()


use_google = FieldProperty(IGoogleWeatherSchema("use_google")) use_google = FieldProperty(IGoogleWeatherSchema('use_google'))
g_locations_id = FieldProperty(IGoogleWeatherSchema("g_locations_id")) google_location_ids = FieldProperty(IGoogleWeatherSchema('google_location_ids'))
g_hl = FieldProperty(IGoogleWeatherSchema("g_hl")) google_language = FieldProperty(IGoogleWeatherSchema('google_language'))
g_units = FieldProperty(IGoogleWeatherSchema("g_units")) google_units = FieldProperty(IGoogleWeatherSchema('google_units'))
use_yahoo = FieldProperty(IYahooWeatherSchema("use_yahoo")) use_yahoo = FieldProperty(IYahooWeatherSchema('use_yahoo'))
y_locations_id = FieldProperty(IYahooWeatherSchema("y_locations_id")) yahoo_location_ids = FieldProperty(IYahooWeatherSchema('yahoo_location_ids'))
y_units = FieldProperty(IYahooWeatherSchema("y_units")) yahoo_units = FieldProperty(IYahooWeatherSchema('yahoo_units'))
use_noaa = FieldProperty(INoaaWeatherSchema("use_noaa")) use_noaa = FieldProperty(INoaaWeatherSchema('use_noaa'))
n_locations_id = FieldProperty(INoaaWeatherSchema("n_locations_id")) noaa_location_ids = FieldProperty(INoaaWeatherSchema('noaa_location_ids'))


0 comments on commit c26ff8a

Please sign in to comment.