Mercurial > p > roundup > code
annotate test/test_instance.py @ 6808:375d40a9e730
Add tests to BasicDatabase and merge implementations
test/session_common.py:
add new tests:
* test set with bad __timestamps
* test get on missing item with no default
* test clear() method
* test new lifetime() method
everything below here was needed to get the tests to work across all
db's.
roundup/backends/sessions_dbm.py:
make set() validate __timestamp as float. If invalid and item is new,
set it to time.time(). If invalid and item exists keep original
timestamp.
add lifetime(key_lifetime) method. Given key_lifetime in seconds,
generate a __timestamp that will be deleted after that many seconds.
roundup/backends/sessions_rdbms.py:
make set() behave the same as session_dbm.
add lifetime method as above.
roundup/backends/sessions_sqlite.py:
import session_rdbms::BasicDatabase and override __init__.
rather than cloning all the code. Kept a few logging and sql methods.
roundup/test/memorydb.py:
make get() on a missing key return KeyError
make set() conform to dbm/rdbms implementations.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Mon, 25 Jul 2022 21:21:26 -0400 |
| parents | 778a9f455067 |
| children | 9c3ec0a5c7fc |
| rev | line source |
|---|---|
|
6300
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
1 # |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
2 # Copyright (C) 2020 John Rouillard |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
3 # All rights reserved. |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
4 # For license terms see the file COPYING.txt. |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
5 # |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
6 |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
7 from __future__ import print_function |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
8 import unittest, os, shutil, errno, sys, difflib |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
9 |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
10 from roundup import instance |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
11 from roundup.instance import TrackerError |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
12 |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
13 try: |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
14 # python2 |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
15 import pathlib2 as pathlib |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
16 except ImportError: |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
17 # python3 |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
18 import pathlib |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
19 |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
20 from . import db_test_base |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
21 |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
22 class InstanceTest(unittest.TestCase): |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
23 |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
24 backend = 'anydbm' |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
25 |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
26 def setUp(self): |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
27 self.dirname = '_test_instance' |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
28 # set up and open a tracker |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
29 self.instance = db_test_base.setupTracker(self.dirname, self.backend) |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
30 |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
31 # open the database |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
32 self.db = self.instance.open('admin') |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
33 |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
34 self.db.commit() |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
35 self.db.close() |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
36 |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
37 def tearDown(self): |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
38 if self.db: |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
39 self.db.close() |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
40 try: |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
41 shutil.rmtree(self.dirname) |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
42 except OSError as error: |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
43 if error.errno not in (errno.ENOENT, errno.ESRCH): raise |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
44 |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
45 |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
46 def testOpenOldStyle(self): |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
47 pathlib.Path(os.path.join(self.dirname, "dbinit.py")).touch() |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
48 # no longer support old style tracker configs |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
49 self.assertRaises(TrackerError, instance.open, self.dirname) |
|
778a9f455067
Remove old code import imp, old style trackers db/backend_name
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
50 |
