Code Repositories xandikos / 764f6ee
Add tests for apply_text_match. Jelmer Vernooij a month ago
3 changed file(s) with 57 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
290290 def apply_text_match(el, value):
291291 collation = el.get('collation', 'i;ascii-casemap')
292292 negate_condition = el.get('negate-condition', 'no')
293 matches = davcommon.collations[collation](el.text, value)
293 matches = davcommon.get_collation(collation)(el.text, value)
294294
295295 if negate_condition == 'yes':
296296 return (not matches)
8181
8282 # see https://tools.ietf.org/html/rfc4790
8383
84 class UnknownCollation(Exception):
85
86 def __init__(self, collation):
87 super(UnknownCollation, self).__init__(
88 "Collation %r is not supported" % collation)
89 self.collation = collation
90
91
8492 collations = {
8593 'i;ascii-casemap': lambda a, b: (a.decode('ascii').upper() ==
8694 b.decode('ascii').upper()),
8795 'i;octet': lambda a, b: a == b,
8896 }
97
98
99 def get_collation(name):
100 """Get a collation by name.
101
102 :param name: Collation name
103 :raises UnknownCollation: If the collation is not supported
104 """
105 try:
106 return collations[name]
107 except KeyError:
108 raise UnknownCollation(name)
1616 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
1717 # MA 02110-1301, USA.
1818
19 import unittest
20
1921 from wsgiref.util import setup_testing_defaults
2022
21 from xandikos import caldav
23 from xandikos import caldav, davcommon
2224 from xandikos.webdav import Property, WebDAVApp, ET
2325
2426 from xandikos.tests import test_webdav
6971 code, headers, contents = self.mkcalendar(app, '/resource/bla')
7072 self.assertEqual('201 Created', code)
7173 self.assertEqual(b'', contents)
74
75
76 class ApplyTextMatchTest(unittest.TestCase):
77
78 def test_default_collation(self):
79 el = ET.Element('someel')
80 el.text = b"foobar"
81 self.assertTrue(caldav.apply_text_match(el, b"FOOBAR"))
82 self.assertTrue(caldav.apply_text_match(el, b"foobar"))
83 self.assertFalse(caldav.apply_text_match(el, b"fobar"))
84
85 def test_casecmp_collation(self):
86 el = ET.Element('someel')
87 el.set('collation', 'i;ascii-casemap')
88 el.text = b"foobar"
89 self.assertTrue(caldav.apply_text_match(el, b"FOOBAR"))
90 self.assertTrue(caldav.apply_text_match(el, b"foobar"))
91 self.assertFalse(caldav.apply_text_match(el, b"fobar"))
92
93 def test_cmp_collation(self):
94 el = ET.Element('someel')
95 el.text = b"foobar"
96 el.set('collation', 'i;octet')
97 self.assertFalse(caldav.apply_text_match(el, b"FOOBAR"))
98 self.assertTrue(caldav.apply_text_match(el, b"foobar"))
99 self.assertFalse(caldav.apply_text_match(el, b"fobar"))
100
101 def test_unknown_collation(self):
102 el = ET.Element('someel')
103 el.set('collation', 'i;blah')
104 el.text = b"foobar"
105 self.assertRaises(davcommon.UnknownCollation,
106 caldav.apply_text_match, el, b"FOOBAR")