2022-02-15 20:36:58 +00:00
|
|
|
# Copyright 2009-2022 Joshua Bronson. All rights reserved.
|
2017-11-20 03:24:08 +00:00
|
|
|
#
|
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2022-02-02 19:58:05 +00:00
|
|
|
Test for consistency in ordered bidicts after handling duplicate keys/values:
|
2017-11-18 03:35:40 +00:00
|
|
|
|
2022-01-31 21:30:18 +00:00
|
|
|
>>> from bidict import OrderedBidict, RAISE, DROP_OLD, OnDup
|
2017-11-18 03:35:40 +00:00
|
|
|
>>> b = OrderedBidict([(0, 1)])
|
2022-01-31 21:30:18 +00:00
|
|
|
|
2021-09-05 14:56:56 +00:00
|
|
|
>>> b.update([(0, 2), (3, 4), (5, 4)])
|
|
|
|
Traceback (most recent call last):
|
2022-01-31 21:30:18 +00:00
|
|
|
...
|
|
|
|
bidict.ValueDuplicationError: 4
|
|
|
|
|
|
|
|
>>> b
|
|
|
|
OrderedBidict([(0, 1)])
|
|
|
|
>>> b.inv
|
|
|
|
OrderedBidict([(1, 0)])
|
2018-02-27 13:09:57 +00:00
|
|
|
|
2021-09-05 14:56:56 +00:00
|
|
|
>>> b.putall([(2, 1), (2, 3)], OnDup(key=RAISE, val=DROP_OLD))
|
|
|
|
Traceback (most recent call last):
|
2022-01-31 21:30:18 +00:00
|
|
|
...
|
|
|
|
bidict.KeyDuplicationError: 2
|
|
|
|
|
|
|
|
>>> b
|
|
|
|
OrderedBidict([(0, 1)])
|
2022-02-07 16:42:09 +00:00
|
|
|
>>> list(b._node_by_korv)
|
2022-01-31 21:30:18 +00:00
|
|
|
[0]
|
|
|
|
>>> b.inv
|
|
|
|
OrderedBidict([(1, 0)])
|
2018-02-27 13:09:57 +00:00
|
|
|
|
2017-11-18 03:35:40 +00:00
|
|
|
>>> b.forceupdate([(0, 1), (2, 3), (0, 3)])
|
2018-02-19 07:53:03 +00:00
|
|
|
>>> b
|
|
|
|
OrderedBidict([(0, 3)])
|
2022-02-07 16:42:09 +00:00
|
|
|
>>> list(b._node_by_korv)
|
2022-01-31 21:30:18 +00:00
|
|
|
[0]
|
|
|
|
>>> b.inv
|
|
|
|
OrderedBidict([(3, 0)])
|
2018-04-28 01:50:29 +00:00
|
|
|
|
|
|
|
Test for consistency updating an ordered bidict's inverse:
|
|
|
|
|
2022-01-31 21:30:18 +00:00
|
|
|
>>> b.inv[3] = 'UPDATED-KEY'
|
2018-04-28 01:50:29 +00:00
|
|
|
>>> b
|
2022-01-31 21:30:18 +00:00
|
|
|
OrderedBidict([('UPDATED-KEY', 3)])
|
2022-02-07 16:42:09 +00:00
|
|
|
>>> list(b._node_by_korv)
|
2022-01-31 21:30:18 +00:00
|
|
|
['UPDATED-KEY']
|
2018-04-28 01:50:29 +00:00
|
|
|
>>> b.inv
|
2022-01-31 21:30:18 +00:00
|
|
|
OrderedBidict([(3, 'UPDATED-KEY')])
|
2018-04-28 01:50:29 +00:00
|
|
|
|
2022-01-31 21:30:18 +00:00
|
|
|
>>> b.inv.forceput('UPDATED-VAL', 'UPDATED-KEY')
|
2018-04-28 01:50:29 +00:00
|
|
|
>>> b
|
2022-01-31 21:30:18 +00:00
|
|
|
OrderedBidict([('UPDATED-KEY', 'UPDATED-VAL')])
|
2022-02-07 16:42:09 +00:00
|
|
|
>>> list(b._node_by_korv)
|
2022-01-31 21:30:18 +00:00
|
|
|
['UPDATED-KEY']
|
2018-04-28 01:50:29 +00:00
|
|
|
>>> b.inv
|
2022-01-31 21:30:18 +00:00
|
|
|
OrderedBidict([('UPDATED-VAL', 'UPDATED-KEY')])
|
2018-04-28 01:50:29 +00:00
|
|
|
|
2022-01-31 21:30:18 +00:00
|
|
|
>>> b.inv['NEW-VAL'] = 'NEW-KEY'
|
2018-04-28 01:50:29 +00:00
|
|
|
>>> b
|
2022-01-31 21:30:18 +00:00
|
|
|
OrderedBidict([('UPDATED-KEY', 'UPDATED-VAL'), ('NEW-KEY', 'NEW-VAL')])
|
2022-02-07 16:42:09 +00:00
|
|
|
>>> list(b._node_by_korv)
|
2022-01-31 21:30:18 +00:00
|
|
|
['UPDATED-KEY', 'NEW-KEY']
|
2018-04-28 01:50:29 +00:00
|
|
|
>>> b.inv
|
2022-01-31 21:30:18 +00:00
|
|
|
OrderedBidict([('UPDATED-VAL', 'UPDATED-KEY'), ('NEW-VAL', 'NEW-KEY')])
|
2018-04-28 01:50:29 +00:00
|
|
|
|
2022-01-31 21:30:18 +00:00
|
|
|
>>> b.inv.forceput('NEW-VAL', 'UPDATED-KEY')
|
2018-04-28 01:50:29 +00:00
|
|
|
>>> b
|
2022-01-31 21:30:18 +00:00
|
|
|
OrderedBidict([('UPDATED-KEY', 'NEW-VAL')])
|
2022-02-07 16:42:09 +00:00
|
|
|
>>> list(b._node_by_korv)
|
2022-01-31 21:30:18 +00:00
|
|
|
['UPDATED-KEY']
|
2018-04-28 01:50:29 +00:00
|
|
|
>>> b.inv
|
2022-01-31 21:30:18 +00:00
|
|
|
OrderedBidict([('NEW-VAL', 'UPDATED-KEY')])
|
2018-04-28 01:50:29 +00:00
|
|
|
|
2022-01-31 21:30:18 +00:00
|
|
|
>>> b.inv.update([('NEWER-VAL', 'NEWER-KEY'), ('NEW-VAL', 'NEW-KEY'), ('FAIL!', 'NEW-KEY')])
|
2018-04-28 01:50:29 +00:00
|
|
|
Traceback (most recent call last):
|
2022-01-31 21:30:18 +00:00
|
|
|
...
|
|
|
|
bidict.ValueDuplicationError: NEW-KEY
|
2018-04-28 01:50:29 +00:00
|
|
|
>>> b
|
2022-01-31 21:30:18 +00:00
|
|
|
OrderedBidict([('UPDATED-KEY', 'NEW-VAL')])
|
2018-04-28 01:50:29 +00:00
|
|
|
>>> b.inv
|
2022-01-31 21:30:18 +00:00
|
|
|
OrderedBidict([('NEW-VAL', 'UPDATED-KEY')])
|
2018-04-28 01:50:29 +00:00
|
|
|
|
2022-01-31 21:30:18 +00:00
|
|
|
>>> b.inv.forceupdate([('NEWER-VAL', 'NEWER-KEY'), ('NEW-VAL', 'NEW-KEY'), ('SUCCESS!', 'NEW-KEY')])
|
2018-04-28 01:50:29 +00:00
|
|
|
>>> b
|
2022-01-31 21:30:18 +00:00
|
|
|
OrderedBidict([('NEW-KEY', 'SUCCESS!'), ('NEWER-KEY', 'NEWER-VAL')])
|
2018-04-28 01:50:29 +00:00
|
|
|
>>> b.inv
|
2022-01-31 21:30:18 +00:00
|
|
|
OrderedBidict([('SUCCESS!', 'NEW-KEY'), ('NEWER-VAL', 'NEWER-KEY')])
|
2018-11-06 21:10:03 +00:00
|
|
|
|
2022-02-02 19:58:05 +00:00
|
|
|
Test the following here so it shows up in pytest's coverage report
|
2022-01-31 21:30:18 +00:00
|
|
|
(the hypothesis tests may not always hit all code paths,
|
2022-02-02 19:58:05 +00:00
|
|
|
and the doctests in the Sphinx docs don't get counted in the coverage report):
|
2018-11-06 21:10:03 +00:00
|
|
|
|
2022-01-31 21:30:18 +00:00
|
|
|
>>> b.move_to_end('NEW-KEY')
|
2018-11-06 21:10:03 +00:00
|
|
|
>>> b
|
2022-01-31 21:30:18 +00:00
|
|
|
OrderedBidict([('NEWER-KEY', 'NEWER-VAL'), ('NEW-KEY', 'SUCCESS!')])
|
2022-02-02 19:58:05 +00:00
|
|
|
>>> b.inverse.move_to_end('SUCCESS!', last=False)
|
2018-11-06 21:10:03 +00:00
|
|
|
>>> b
|
2022-01-31 21:30:18 +00:00
|
|
|
OrderedBidict([('NEW-KEY', 'SUCCESS!'), ('NEWER-KEY', 'NEWER-VAL')])
|
|
|
|
>>> b.move_to_end('NOT-FOUND')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
KeyError: 'NOT-FOUND'
|
2022-02-02 19:58:05 +00:00
|
|
|
>>> b.inverse.move_to_end('NOT-FOUND')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
KeyError: 'NOT-FOUND'
|
2022-01-31 21:30:18 +00:00
|
|
|
|
|
|
|
>>> b.popitem(last=False)
|
|
|
|
('NEW-KEY', 'SUCCESS!')
|
|
|
|
>>> b.inverse.popitem(last=True)
|
|
|
|
('NEWER-VAL', 'NEWER-KEY')
|
|
|
|
>>> b.popitem()
|
2018-11-06 21:10:03 +00:00
|
|
|
Traceback (most recent call last):
|
2022-01-31 21:30:18 +00:00
|
|
|
...
|
2022-02-11 21:59:06 +00:00
|
|
|
KeyError: 'OrderedBidict is empty'
|
2022-02-02 19:58:05 +00:00
|
|
|
>>> b.inverse.popitem()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2022-02-11 21:59:06 +00:00
|
|
|
KeyError: 'OrderedBidict is empty'
|
2022-02-02 19:58:05 +00:00
|
|
|
|
2022-02-13 16:49:12 +00:00
|
|
|
>>> b._update(arg=())
|
2022-02-02 19:58:05 +00:00
|
|
|
>>> b
|
|
|
|
OrderedBidict()
|
|
|
|
|
|
|
|
>>> OrderedBidict({0: 1}).pop(0)
|
|
|
|
1
|
|
|
|
>>> OrderedBidict({0: 1}).inverse.pop(1)
|
|
|
|
0
|
|
|
|
>>> OrderedBidict().pop(0)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
KeyError: 0
|
|
|
|
>>> OrderedBidict().inverse.pop(1)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
KeyError: 1
|
2022-01-06 02:14:02 +00:00
|
|
|
|
2022-01-31 21:30:18 +00:00
|
|
|
The views returned by .keys(), values(), and items() are reversible:
|
2022-01-06 02:14:02 +00:00
|
|
|
|
|
|
|
>>> b = OrderedBidict([(0, 1), (2, 3)])
|
2022-01-31 21:30:18 +00:00
|
|
|
>>> list(reversed(b.keys()))
|
|
|
|
[2, 0]
|
|
|
|
>>> list(reversed(b.values()))
|
|
|
|
[3, 1]
|
2022-01-06 02:14:02 +00:00
|
|
|
>>> list(reversed(b.items()))
|
|
|
|
[(2, 3), (0, 1)]
|
2022-02-02 19:58:05 +00:00
|
|
|
|
|
|
|
And they compare as expected:
|
|
|
|
|
|
|
|
>>> b1 = OrderedBidict([(0, 1)])
|
|
|
|
>>> b2 = OrderedBidict([(0, 1)])
|
|
|
|
>>> b1.items() == b2.items()
|
|
|
|
True
|
|
|
|
|
|
|
|
>>> b1 = OrderedBidict([(False, None)]).inverse
|
|
|
|
>>> b2 = b1.copy()
|
|
|
|
>>> list(b1.items()) == list(b2.items())
|
|
|
|
True
|