Mercurial > p > roundup > code
comparison test/test_admin.py @ 6176:d25638d1826c
Add roundup-admin filter command; fix bad doc example; add tests
admin_guide.txt had an example using find with username prop. This is
wrong. Find only works with links not string. Fix it to use filter.
Add filter command to roundup-admin.
Add tests for filter, specification and find.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Mon, 18 May 2020 23:28:03 -0400 |
| parents | b76be13e027e |
| children | 41907e1f9c3f |
comparison
equal
deleted
inserted
replaced
| 6175:72a69753f49a | 6176:d25638d1826c |
|---|---|
| 11 | 11 |
| 12 from . import db_test_base | 12 from . import db_test_base |
| 13 from .test_mysql import skip_mysql | 13 from .test_mysql import skip_mysql |
| 14 from .test_postgresql import skip_postgresql | 14 from .test_postgresql import skip_postgresql |
| 15 | 15 |
| 16 # https://stackoverflow.com/questions/4219717/how-to-assert-output-with-nosetest-unittest-in-python | |
| 17 # lightly modified | |
| 18 from contextlib import contextmanager | |
| 19 _py3 = sys.version_info[0] > 2 | |
| 20 if _py3: | |
| 21 from io import StringIO # py3 | |
| 22 else: | |
| 23 from StringIO import StringIO # py2 | |
| 24 | |
| 25 @contextmanager | |
| 26 def captured_output(): | |
| 27 new_out, new_err = StringIO(), StringIO() | |
| 28 old_out, old_err = sys.stdout, sys.stderr | |
| 29 try: | |
| 30 sys.stdout, sys.stderr = new_out, new_err | |
| 31 yield sys.stdout, sys.stderr | |
| 32 finally: | |
| 33 sys.stdout, sys.stderr = old_out, old_err | |
| 16 | 34 |
| 17 class AdminTest(object): | 35 class AdminTest(object): |
| 18 | 36 |
| 19 backend = None | 37 backend = None |
| 20 | 38 |
| 24 def tearDown(self): | 42 def tearDown(self): |
| 25 try: | 43 try: |
| 26 shutil.rmtree(self.dirname) | 44 shutil.rmtree(self.dirname) |
| 27 except OSError as error: | 45 except OSError as error: |
| 28 if error.errno not in (errno.ENOENT, errno.ESRCH): raise | 46 if error.errno not in (errno.ENOENT, errno.ESRCH): raise |
| 47 | |
| 48 def install_init(self, type="classic", | |
| 49 settings="mail_domain=example.com," + | |
| 50 "mail_host=localhost," + "tracker_web=http://test/" ): | |
| 51 ''' install tracker with settings for required config.ini settings. | |
| 52 ''' | |
| 53 | |
| 54 admin=AdminTool() | |
| 55 | |
| 56 # Run under context manager to suppress output of help text. | |
| 57 with captured_output() as (out, err): | |
| 58 sys.argv=['main', '-i', '_test_admin', 'install', | |
| 59 type, self.backend, settings ] | |
| 60 ret = admin.main() | |
| 61 self.assertEqual(ret, 0) | |
| 62 | |
| 63 # initialize tracker with initial_data.py. Put password | |
| 64 # on cli so I don't have to respond to prompting. | |
| 65 sys.argv=['main', '-i', '_test_admin', 'initialise', 'admin'] | |
| 66 ret = admin.main() | |
| 67 self.assertEqual(ret, 0) | |
| 29 | 68 |
| 30 def testInit(self): | 69 def testInit(self): |
| 31 import sys | 70 import sys |
| 32 self.admin=AdminTool() | 71 self.admin=AdminTool() |
| 33 sys.argv=['main', '-i', '_test_admin', 'install', 'classic', self.backend] | 72 sys.argv=['main', '-i', '_test_admin', 'install', 'classic', self.backend] |
| 66 self.assertTrue(ret == 0) | 105 self.assertTrue(ret == 0) |
| 67 self.assertTrue(os.path.isfile(self.dirname + "/config.ini")) | 106 self.assertTrue(os.path.isfile(self.dirname + "/config.ini")) |
| 68 self.assertTrue(os.path.isfile(self.dirname + "/schema.py")) | 107 self.assertTrue(os.path.isfile(self.dirname + "/schema.py")) |
| 69 config=CoreConfig(self.dirname) | 108 config=CoreConfig(self.dirname) |
| 70 self.assertEqual(config['MAIL_DEBUG'], self.dirname + "/SendMail.LOG") | 109 self.assertEqual(config['MAIL_DEBUG'], self.dirname + "/SendMail.LOG") |
| 110 | |
| 111 def testFind(self): | |
| 112 ''' Note the tests will fail if you run this under pdb. | |
| 113 the context managers capture the pdb prompts and this screws | |
| 114 up the stdout strings with (pdb) prefixed to the line. | |
| 115 ''' | |
| 116 import sys, json | |
| 117 | |
| 118 self.admin=AdminTool() | |
| 119 self.install_init() | |
| 120 | |
| 121 with captured_output() as (out, err): | |
| 122 sys.argv=['main', '-i', '_test_admin', 'create', 'issue', | |
| 123 'title="foo bar"', 'assignedto=admin' ] | |
| 124 ret = self.admin.main() | |
| 125 | |
| 126 out = out.getvalue().strip() | |
| 127 print(out) | |
| 128 self.assertEqual(out, '1') | |
| 129 | |
| 130 self.admin=AdminTool() | |
| 131 with captured_output() as (out, err): | |
| 132 sys.argv=['main', '-i', '_test_admin', 'create', 'issue', | |
| 133 'title="bar foo bar"', 'assignedto=anonymous' ] | |
| 134 ret = self.admin.main() | |
| 135 | |
| 136 out = out.getvalue().strip() | |
| 137 print(out) | |
| 138 self.assertEqual(out, '2') | |
| 139 | |
| 140 self.admin=AdminTool() | |
| 141 with captured_output() as (out, err): | |
| 142 sys.argv=['main', '-i', '_test_admin', 'find', 'issue', | |
| 143 'assignedto=1'] | |
| 144 ret = self.admin.main() | |
| 145 | |
| 146 out = out.getvalue().strip() | |
| 147 print(out) | |
| 148 self.assertEqual(out, "['1']") | |
| 149 | |
| 150 # Reopen the db closed by previous filter call | |
| 151 self.admin=AdminTool() | |
| 152 with captured_output() as (out, err): | |
| 153 ''' 1,2 should return all entries that have assignedto | |
| 154 either admin or anonymous | |
| 155 ''' | |
| 156 sys.argv=['main', '-i', '_test_admin', 'find', 'issue', | |
| 157 'assignedto=1,2'] | |
| 158 ret = self.admin.main() | |
| 159 | |
| 160 out = out.getvalue().strip() | |
| 161 print(out) | |
| 162 # out can be "['2', '1']" or "['1', '2']" | |
| 163 # so eval to real list so Equal can do a list compare | |
| 164 self.assertEqual(sorted(eval(out)), ['1', '2']) | |
| 165 | |
| 166 # Reopen the db closed by previous filter call | |
| 167 self.admin=AdminTool() | |
| 168 with captured_output() as (out, err): | |
| 169 ''' 1,2 should return all entries that have assignedto | |
| 170 either admin or anonymous | |
| 171 ''' | |
| 172 sys.argv=['main', '-i', '_test_admin', 'find', 'issue', | |
| 173 'assignedto=admin,anonymous'] | |
| 174 ret = self.admin.main() | |
| 175 | |
| 176 out = out.getvalue().strip() | |
| 177 print(out) | |
| 178 # out can be "['2', '1']" or "['1', '2']" | |
| 179 # so eval to real list so Equal can do a list compare | |
| 180 self.assertEqual(sorted(eval(out)), ['1', '2']) | |
| 181 | |
| 182 def testSpecification(self): | |
| 183 ''' Note the tests will fail if you run this under pdb. | |
| 184 the context managers capture the pdb prompts and this screws | |
| 185 up the stdout strings with (pdb) prefixed to the line. | |
| 186 ''' | |
| 187 import sys | |
| 188 | |
| 189 self.install_init() | |
| 190 self.admin=AdminTool() | |
| 191 | |
| 192 import inspect | |
| 71 | 193 |
| 194 spec='''username: <roundup.hyperdb.String> (key property) | |
| 195 alternate_addresses: <roundup.hyperdb.String> | |
| 196 realname: <roundup.hyperdb.String> | |
| 197 roles: <roundup.hyperdb.String> | |
| 198 organisation: <roundup.hyperdb.String> | |
| 199 queries: <roundup.hyperdb.Multilink to "query"> | |
| 200 phone: <roundup.hyperdb.String> | |
| 201 address: <roundup.hyperdb.String> | |
| 202 timezone: <roundup.hyperdb.String> | |
| 203 password: <roundup.hyperdb.Password>''' | |
| 204 | |
| 205 spec = inspect.cleandoc(spec) | |
| 206 with captured_output() as (out, err): | |
| 207 sys.argv=['main', '-i', '_test_admin', 'specification', 'user'] | |
| 208 ret = self.admin.main() | |
| 209 | |
| 210 out = out.getvalue().strip() | |
| 211 print(out) | |
| 212 self.assertEqual(out, spec) | |
| 72 | 213 |
| 73 | 214 |
| 74 class anydbmAdminTest(AdminTest, unittest.TestCase): | 215 class anydbmAdminTest(AdminTest, unittest.TestCase): |
| 75 backend = 'anydbm' | 216 backend = 'anydbm' |
| 76 | 217 |
