当“在处理上述异常期间,发生另一个异常”时,如何编写单元测试

时间:2018-06-28 19:33:57

标签: python unit-testing mongoengine

当我尝试保存具有相同id的对象时,mongoengine中的唯一索引约束会引发此异常:

|2018-06-28 14:23:28.084| [DEBUG] (rec.exchange.base|26): Processing exchange data
Traceback (most recent call last):
  File "/home/nst/virtualenvs/myenv/lib/python3.6/site-packages/mongoengine/document.py", line 372, in save
    object_id = self._save_create(doc, force_insert, write_concern)
  File "/home/nst/virtualenvs/myenv/lib/python3.6/site-packages/mongoengine/document.py", line 428, in _save_create
    object_id = collection.save(doc, **write_concern)
  File "/home/nst/virtualenvs/myenv/lib/python3.6/site-packages/pymongo/collection.py", line 2917, in save
    to_save, True, check_keys, manipulate, write_concern)
  File "/home/nst/virtualenvs/myenv/lib/python3.6/site-packages/pymongo/collection.py", line 599, in _insert
    bypass_doc_val, session)
  File "/home/nst/virtualenvs/myenv/lib/python3.6/site-packages/pymongo/collection.py", line 580, in _insert_one
    _check_write_command_response(result)
  File "/home/nst/virtualenvs/myenv/lib/python3.6/site-packages/pymongo/helpers.py", line 207, in _check_write_command_response
    _raise_last_write_error(write_errors)
  File "/home/nst/virtualenvs/myenv/lib/python3.6/site-packages/pymongo/helpers.py", line 188, in _raise_last_write_error
    raise DuplicateKeyError(error.get("errmsg"), 11000, error)
pymongo.errors.DuplicateKeyError: E11000 duplicate key error collection: TRX.exchanges index: exchange_1_order.id_1 dup key: { : "BFNX", : "1234" }

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "run.py", line 5, in <module>
    application.run()
  File "/home/nst/Dev/rec/rec/trx_app.py", line 118, in run
    self.run_scheduler_app()
  File "/home/nst/Dev/rec/rec/trx_app.py", line 106, in run_scheduler_app
    self.run_jobs_hook_without_scheduler()
  File "/home/nst/Dev/rec/rec/trx_app.py", line 92, in run_jobs_hook_without_scheduler
    get_exchange_job(order)
  File "/home/nst/Dev/rec/rec/scheduler/job_hooks.py", line 39, in get_exchange_job
    ExchangeJob(job_cfg).run()
  File "/home/nst/Dev/rec/rec/exchange/job.py", line 22, in run
    exchange_data = self.data_source.get_exchange_data()
  File "/home/nst/Dev/rec/rec/exchange/base.py", line 15, in get_exchange_data
    return self._map_data(self._read_data())
  File "/home/nst/Dev/rec/rec/exchange/base.py", line 31, in _read_data
    parser = self._build_data_parser()
  File "/home/nst/Dev/rec/rec/exchange/impl/bitfinex.py", line 113, in _build_data_parser
    ed1.save()
  File "/home/nst/virtualenvs/myenv/lib/python3.6/site-packages/mongoengine/document.py", line 395, in save
    raise NotUniqueError(message % six.text_type(err))
mongoengine.errors.NotUniqueError: Tried to save duplicate unique keys (E11000 duplicate key error collection: TRX.exchanges index: exchange_1_order.id_1 dup key: { : "BFNX", : "1234" })

我正在尝试为这种情况编写单元测试:

from mongoengine.errors import NotUniqueError

def test_duplicate_exchange_data(self):
    order1 = ExchangeOrder(id='1234', currency_pair='tBTGUSD',
                           datetime=datetime.datetime.today(),
                           amount=1500, price=57)
    exchange_data1 = ExchangeData(exchange='BFNX', order=order1)
    exchange_data1.save()
    order2 = ExchangeOrder(id='1234', currency_pair='tBTCUSD',
                           datetime=datetime.datetime.today(),
                           amount=500, price=57.5)
    exchange_data2 = ExchangeData(exchange='BFNX', order=order2)
    self.assertRaises(NotUniqueError, exchange_data2.save)

但是assertRaises()由于第二个异常而无法正常工作。在测试中如何处理?

0 个答案:

没有答案