source: main/waeup.ikoba/trunk/src/waeup/ikoba/tests/test_catalog.py @ 17497

Last change on this file since 17497 was 11997, checked in by Henrik Bettermann, 10 years ago

propset svn:keywords "Id"

  • Property svn:keywords set to Id
File size: 6.3 KB
Line 
1## $Id: test_catalog.py 11997 2014-11-19 17:02:48Z henrik $
2##
3## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""Tests for basic catalog components.
19"""
20import grok
21import unittest
22from grok import index
23from zope.catalog.interfaces import ICatalog
24from zope.component import getUtility
25from zope.component.hooks import setSite
26from zope.interface import Interface, Attribute
27from zope.interface.verify import verifyClass, verifyObject
28from waeup.ikoba.testing import FunctionalLayer, FunctionalTestCase
29from waeup.ikoba.catalog import FilteredQueryBase, FilteredCatalogQueryBase
30from waeup.ikoba.interfaces import IFilteredQuery, IFilteredCatalogQuery
31
32class FilteredQueryBaseTests(unittest.TestCase):
33
34    def test_iface(self):
35        # make sure, we correctly implement interface requirements
36        obj = FilteredQueryBase()
37        verifyClass(IFilteredQuery, FilteredQueryBase)
38        verifyObject(IFilteredQuery, obj)
39        return
40
41    def test_kw(self):
42        # keywords are set correctly on construction
43        q1 = FilteredQueryBase()
44        self.assertEqual(q1._kw, dict())
45        q2 = FilteredQueryBase(name='Bob')
46        self.assertEqual(q2._kw, dict(name='Bob'))
47        return
48
49    def test_kw_defaults(self):
50        # defaults are set initially but can be overridden.
51        class MyFilter(FilteredQueryBase):
52            defaults=dict(name='Bob')
53        q1 = MyFilter()
54        self.assertEqual(q1._kw, dict(name='Bob'))
55        q2 = MyFilter(name='Alice', age=29)
56        self.assertEqual(q2._kw, dict(name='Alice', age=29))
57        return
58
59    def test_query_not_implemented(self):
60        # we are warned when using `query()`
61        q = FilteredQueryBase()
62        self.assertRaises(NotImplementedError, q.query)
63        return
64
65
66# A sample catalog environment. Has to be grokked to work
67class Herd(grok.Container, grok.Application):
68    pass
69
70class IMammoth(Interface):
71    age = Attribute('Age')
72    name = Attribute('Name')
73
74    def message():
75        """Message for the world.
76        """
77
78class MammothCatalog(grok.Indexes):
79    grok.site(Herd)
80    grok.context(IMammoth)
81    grok.name('mammoths')
82
83    name = index.Field(attribute='name')
84    age = index.Field(attribute='age')
85
86class Mammoth(grok.Model):
87    grok.implements(IMammoth)
88
89    def __init__(self, name, age, message):
90        self.age = age
91        self.name = name
92        self._message = message
93
94    def message(self):
95        return self._message
96
97setup_done = False
98class FilteredCatalogQueryBaseTests(FunctionalTestCase):
99
100    layer = FunctionalLayer
101
102    def setUp(self):
103        global setup_done
104        super(FilteredCatalogQueryBaseTests, self).setUp()
105        if not setup_done:
106            # grok this module to register indexes etc. defined above
107            # ...and do this only once
108            # XXX workaround because functional test cases do not support
109            #     classmethod setUp
110            grok.testing.grok(self.__module__)
111            setup_done = True
112        self.root = self.getRootFolder()
113        self.getRootFolder()['herd'] = Herd()
114        self.herd = self.root['herd']
115        self.populate_catalog()
116        self.catalog = getUtility(
117            ICatalog, context=self.herd, name='mammoths')
118        return
119
120    def populate_catalog(self):
121        setSite(self.herd)
122        self.herd['bob'] = Mammoth('Bob', 21, 'Hi there!')
123        self.herd['alice'] = Mammoth('Alice', 22, 'Hello!')
124        return
125
126    def test_iface(self):
127        # make sure, we correctly implement interface requirements
128        obj = FilteredCatalogQueryBase()
129        verifyClass(IFilteredCatalogQuery, FilteredCatalogQueryBase)
130        verifyObject(IFilteredCatalogQuery, obj)
131        return
132
133    def test_cat_name_respected(self):
134        # a set cat_name is respected when doing the query
135        q = FilteredCatalogQueryBase(name='Bob')
136        q.cat_name = 'mammoths' # set a registered catalog name
137        result = q.query()
138        self.assertEqual([x.name for x in result], ['Bob'])
139        return
140
141    def test_query_catalog(self):
142        # we can pass in a catalog instance
143        q = FilteredCatalogQueryBase(name='Bob')
144        result = q.query_catalog(catalog=self.catalog)
145        self.assertEqual([x.name for x in result], ['Bob'])
146        return
147
148    def test_query_catalog_value_ranges(self):
149        # we can ask for value ranges
150        q = FilteredCatalogQueryBase(name=('Alice', 'Bob'))
151        result = q.query_catalog(catalog=self.catalog)
152        self.assertEqual([x.name for x in result], ['Bob', 'Alice'])
153        return
154
155    def test_query_value_ranges(self):
156        # we can ask for value ranges instead of single values
157        q = FilteredCatalogQueryBase(name=('Alice', 'Bob'))
158        q.cat_name = 'mammoths'
159        result = q.query()
160        self.assertEqual([x.name for x in result], ['Bob', 'Alice'])
161        return
162
163    def test_query_mixed_values(self):
164        # we can ask for different values at the same time
165        q = FilteredCatalogQueryBase(name=('Alice', 'Bob'), age=22)
166        q.cat_name = 'mammoths'
167        result = q.query()
168        self.assertEqual([x.name for x in result], ['Alice'])
169        return
170
171    def test_query_invalid_cat_name(self):
172        # with an invalid catalog name we get empty results
173        q = FilteredCatalogQueryBase(name='Bob')
174        q.cat_name = 'NOT-EXISTING'
175        result = q.query()
176        self.assertEqual([x.name for x in result], [])
177        return
178
179    def test_query_catalog_no_site(self):
180        # without a site with catalog we get empty results
181        setSite(None) # unset current site
182        q = FilteredCatalogQueryBase(name='Bob')
183        q.cat_name = 'mammoths'
184        result = q.query()
185        self.assertEqual([x.name for x in result], [])
186        return
Note: See TracBrowser for help on using the repository browser.