bidict/tests/test_orderedbidict.txt

84 lines
2.7 KiB
Plaintext
Raw Normal View History

2021-01-01 17:14:24 +00:00
# Copyright 2009-2021 Joshua Bronson. All Rights Reserved.
#
# 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/.
Test for consistency in ordered bidicts after handling duplicate keys/values::
Various API changes and other improvements. * Deprecate ``bidict.OVERWRITE`` and ``bidict.IGNORE``. A :class:`UserWarning` will now be emitted if these are used. :attr:`bidict.DROP_OLD` and :attr:`bidict.DROP_NEW` should be used instead. * Rename ``DuplicationPolicy`` to :class:`~bidict.OnDupAction` (and implement it via an :class:`~enum.Enum`). A :class:`~bidict.OnDupAction` may be one of :attr:`~bidict.RAISE`, :attr:`~bidict.DROP_OLD`, or :attr:`~bidict.DROP_NEW`. * Expose the new :class:`~bidict.OnDup` class, a named (*key*, *val*, *kv*) tuple of :class:`~bidict.OnDupAction`\s that should be taken upon encountering the 3 kinds of duplication that can occur. * Provide the :attr:`~bidict.ON_DUP_DEFAULT`, :attr:`~bidict.ON_DUP_RAISE`, and :attr:`~bidict.ON_DUP_DROP_OLD` :class:`~bidict.OnDup` convenience instances. * Deprecate the ``on_dup_key``, ``on_dup_val``, and ``on_dup_kv`` arguments of :meth:`~bidict.bidict.put` and :meth:`~bidict.bidict.putall`. A :class:`UserWarning` will now be emitted if these are used. They have been subsumed by the new *on_dup* argument, which takes an :class:`~bidict.OnDup` instance. Use it like this: ``bi.put(1, 2, OnDup(key=DROP_NEW))``. Or better yet, pass one of the ``ON_DUP_*`` convenience instances instead if possible. See the updated :ref:`basic-usage:Values Must Be Unique` docs for more info. * Deprecate the ``on_dup_key``, ``on_dup_val``, and ``on_dup_kv`` bidict class attributes. A :class:`UserWarning` will now be emitted if these are used. They have been subsumed by the new :attr:`~bidict.bidict.on_dup` class attribute, which takes an :class:`~bidict.OnDup` instance. See the updated :doc:`extending` docs for example usage. * Move :meth:`bidict.BidictBase.values` to :meth:`bidict.BidirectionalMapping.values`, since the implementation is generic. * No longer use ``__all__`` in ``bidict/__init__.py``. * Cap max_size rather than disabling health checks and deadline as a less heavyhanded way to improve hypothesis test reliability on Travis.
2020-01-07 22:20:20 +00:00
>>> from bidict import OrderedBidict, DuplicationError, RAISE, DROP_OLD, OnDup
>>> b = OrderedBidict([(0, 1)])
>>> b.update([(0, 2), (3, 4), (5, 4)])
Traceback (most recent call last):
...
ValueDuplicationError: 4
>>> len(b.inv)
1
>>> b.putall([(2, 1), (2, 3)], OnDup(key=RAISE, val=DROP_OLD))
Traceback (most recent call last):
...
KeyDuplicationError: 2
>>> len(b)
1
>>> b.forceupdate([(0, 1), (2, 3), (0, 3)])
2018-02-19 07:53:03 +00:00
>>> b
OrderedBidict([(0, 3)])
2018-04-28 01:50:29 +00:00
Test for consistency updating an ordered bidict's inverse:
>>> b.inv[3] = 'UPDATED KEY'
>>> b
OrderedBidict([('UPDATED KEY', 3)])
>>> b.inv
OrderedBidict([(3, 'UPDATED KEY')])
>>> b.inv.forceput('UPDATED VAL', 'UPDATED KEY')
>>> b
OrderedBidict([('UPDATED KEY', 'UPDATED VAL')])
>>> b.inv
OrderedBidict([('UPDATED VAL', 'UPDATED KEY')])
>>> b.inv['NEW VAL'] = 'NEW KEY'
>>> b
OrderedBidict([('UPDATED KEY', 'UPDATED VAL'), ('NEW KEY', 'NEW VAL')])
>>> b.inv
OrderedBidict([('UPDATED VAL', 'UPDATED KEY'), ('NEW VAL', 'NEW KEY')])
>>> b.inv.forceput('NEW VAL', 'UPDATED KEY')
>>> b
OrderedBidict([('UPDATED KEY', 'NEW VAL')])
>>> b.inv
OrderedBidict([('NEW VAL', 'UPDATED KEY')])
>>> b.inv.update([('NEWER VAL', 'NEWER KEY'), ('NEW VAL', 'NEW KEY'), ('FAIL!', 'NEW KEY')])
Traceback (most recent call last):
...
ValueDuplicationError: NEW KEY
>>> b
OrderedBidict([('UPDATED KEY', 'NEW VAL')])
>>> b.inv
OrderedBidict([('NEW VAL', 'UPDATED KEY')])
>>> b.inv.forceupdate([('NEWER VAL', 'NEWER KEY'), ('NEW VAL', 'NEW KEY'), ('SUCCESS!', 'NEW KEY')])
>>> b
OrderedBidict([('NEW KEY', 'SUCCESS!'), ('NEWER KEY', 'NEWER VAL')])
>>> b.inv
OrderedBidict([('SUCCESS!', 'NEW KEY'), ('NEWER VAL', 'NEWER KEY')])
Test move_to_end here so it shows up in pytest's coverage report
(its hypothesis tests may not always hit all code paths,
and the doctests in the Sphinx docs don't get counted in the coverage report)::
>>> b.move_to_end('NEW KEY')
>>> b
OrderedBidict([('NEWER KEY', 'NEWER VAL'), ('NEW KEY', 'SUCCESS!')])
>>> b.move_to_end('NEW KEY', last=False)
>>> b
OrderedBidict([('NEW KEY', 'SUCCESS!'), ('NEWER KEY', 'NEWER VAL')])
>>> b.move_to_end('NOT FOUND')
Traceback (most recent call last):
...
KeyError: 'NOT FOUND'