1 #
2 # Copyright (c) 2003 Richard Jones, richard@commonground.com.au
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # This module is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 #
11 # $Id: test_hyperdbvals.py,v 1.3 2006-08-18 01:26:19 richard Exp $
13 import unittest, os, shutil, errno, sys, difflib, cgi, re
14 from roundup.anypy.hashlib_ import sha1
16 from roundup import init, instance, password, hyperdb, date
18 class TestClass:
19 def getprops(self):
20 return {
21 'string': hyperdb.String(),
22 'number': hyperdb.Number(),
23 'boolean': hyperdb.Boolean(),
24 'password': hyperdb.Password(),
25 'date': hyperdb.Date(),
26 'interval': hyperdb.Interval(),
27 'link': hyperdb.Link('test'),
28 'link2': hyperdb.Link('test2'),
29 'multilink': hyperdb.Multilink('test'),
30 'multilink2': hyperdb.Multilink('test2'),
31 }
32 def getkey(self):
33 return 'string'
34 def lookup(self, value):
35 if value == 'valid':
36 return '1'
37 raise KeyError
38 def get(self, nodeid, propname):
39 assert propname.startswith('multilink')
40 assert nodeid is not None
41 return ['2', '3']
43 class TestClass2:
44 def properties(self):
45 return {
46 'string': hyperdb.String(),
47 }
48 def getkey(self):
49 return None
50 def labelprop(self, default_to_id=1):
51 return 'id'
53 class TestDatabase:
54 classes = {'test': TestClass(), 'test2': TestClass2()}
55 def getUserTimezone(self):
56 return 0
58 class RawToHyperdbTest(unittest.TestCase):
59 def _test(self, propname, value, itemid=None):
60 return hyperdb.rawToHyperdb(TestDatabase(), TestClass(), itemid,
61 propname, value)
62 def testString(self):
63 self.assertEqual(self._test('password', ''), None)
64 self.assertEqual(self._test('string', ' a string '), 'a string')
65 def testNumber(self):
66 self.assertEqual(self._test('password', ''), None)
67 self.assertEqual(self._test('number', ' 10 '), 10)
68 self.assertEqual(self._test('number', ' 1.5 '), 1.5)
69 def testBoolean(self):
70 self.assertEqual(self._test('password', ''), None)
71 for true in 'yes true on 1'.split():
72 self.assertEqual(self._test('boolean', ' %s '%true), 1)
73 for false in 'no false off 0'.split():
74 self.assertEqual(self._test('boolean', ' %s '%false), 0)
75 def testPassword(self):
76 self.assertEqual(self._test('password', ''), None)
77 self.assertEqual(self._test('password', ' a string '), 'a string')
78 val = self._test('password', ' a string ')
79 self.assert_(isinstance(val, password.Password))
80 val = self._test('password', '{plaintext}a string')
81 self.assert_(isinstance(val, password.Password))
82 val = self._test('password', '{crypt}a string')
83 self.assert_(isinstance(val, password.Password))
84 s = sha1('a string').hexdigest()
85 val = self._test('password', '{SHA}'+s)
86 self.assert_(isinstance(val, password.Password))
87 self.assertEqual(val, 'a string')
88 self.assertRaises(hyperdb.HyperdbValueError, self._test,
89 'password', '{fubar}a string')
90 def testDate(self):
91 self.assertEqual(self._test('password', ''), None)
92 val = self._test('date', ' 2003-01-01 ')
93 self.assert_(isinstance(val, date.Date))
94 val = self._test('date', ' 2003/01/01 ')
95 self.assert_(isinstance(val, date.Date))
96 val = self._test('date', ' 2003/1/1 ')
97 self.assert_(isinstance(val, date.Date))
98 val = self._test('date', ' 2003-1-1 ')
99 self.assert_(isinstance(val, date.Date))
100 self.assertRaises(hyperdb.HyperdbValueError, self._test, 'date',
101 'fubar')
102 def testInterval(self):
103 self.assertEqual(self._test('password', ''), None)
104 val = self._test('interval', ' +1d ')
105 self.assert_(isinstance(val, date.Interval))
106 self.assertRaises(hyperdb.HyperdbValueError, self._test, 'interval',
107 'fubar')
108 def testLink(self):
109 self.assertEqual(self._test('password', ''), None)
110 self.assertEqual(self._test('link', '1'), '1')
111 self.assertEqual(self._test('link', 'valid'), '1')
112 self.assertRaises(hyperdb.HyperdbValueError, self._test, 'link',
113 'invalid')
114 def testMultilink(self):
115 self.assertEqual(self._test('multilink', '', '1'), [])
116 self.assertEqual(self._test('multilink', '1', '1'), ['1'])
117 self.assertEqual(self._test('multilink', 'valid', '1'), ['1'])
118 self.assertRaises(hyperdb.HyperdbValueError, self._test, 'multilink',
119 'invalid', '1')
120 self.assertEqual(self._test('multilink', '+1', '1'), ['1', '2', '3'])
121 self.assertEqual(self._test('multilink', '+valid', '1'), ['1', '2',
122 '3'])
123 self.assertEqual(self._test('multilink', '+1,-2', '1'), ['1', '3'])
124 self.assertEqual(self._test('multilink', '+valid,-3', '1'), ['1', '2'])
125 self.assertEqual(self._test('multilink', '+1', None), ['1'])
126 self.assertEqual(self._test('multilink', '+valid', None), ['1'])
127 self.assertEqual(self._test('multilink', '', None), [])
129 def test_suite():
130 suite = unittest.TestSuite()
131 suite.addTest(unittest.makeSuite(RawToHyperdbTest))
132 return suite
134 if __name__ == '__main__':
135 runner = unittest.TextTestRunner()
136 unittest.main(testRunner=runner)
137 # vim: set filetype=python ts=4 sw=4 et si