class_docstr: Call to methods wrapped with `mongodb_exception_wrapper()`.
class_docstr_tok: ["Call","to","methods","wrapped","with","`","mongodb_exception_wrapper","(",")","`","."]
docstr: Should raise generic DuplicateKeyError.
docstr_tok: ["Should","raise","generic","DuplicateKeyError","."]
code: def test_duplicate_key_error(
      self, monkeypatch, orion_db, test_collection
  ):
      """Should raise generic DuplicateKeyError."""
      # Add unique indexes to force trigger of DuplicateKeyError on write()
      orion_db.ensure_index(
          "test_collection",
          [
              ("field0", Database.ASCENDING),
              ("datetime", Database.ASCENDING),
          ],
          unique=True,
      )
  
      config_to_add = test_collection[0]
      config_to_add.pop("_id")
  
      query = {"_id": test_collection[1]["_id"]}
  
      # Make sure it raises pymongo.errors.DuplicateKeyError when there is no
      # wrapper
      monkeypatch.setattr(
          orion_db,
          "read_and_write",
          functools.partial(
              orion_db.read_and_write.__wrapped__, orion_db
          ),
      )
      with pytest.raises(pymongo.errors.DuplicateKeyError) as exc_info:
          orion_db.read_and_write(
              "test_collection", query, config_to_add
          )
  
      monkeypatch.undo()
  
      # Verify that the wrapper converts it properly to DuplicateKeyError
      with pytest.raises(DuplicateKeyError) as exc_info:
          orion_db.read_and_write(
              "test_collection", query, config_to_add
          )
      assert "duplicate key error" in str(exc_info.value)
  
imports: import functools
  from timeit import timeit
  import pymongo
  import pytest
  from pymongo import MongoClient
  from orion.core.io.database import Database, DatabaseError, DuplicateKeyError
  from orion.core.io.database.mongodb import AUTH_FAILED_MESSAGES, MongoDB
  from .conftest import insert_test_collection
  from .test_database import get_db
label_desc: Use image node_id 1 for calling the TestExceptionWrapper obj's underlying member method code with example usage: obj.test_duplicate_key_error(monkeypatch, orion_db, test_collection) without return types