2

I have a test where I'm trying to identify if I've successfully gotten an instance of a database using pymongo, and would like to use isinstance(obj, class) in an assertion. However I cant figure out how to get a class (not instance) of the database

I've tried several approaches. This seems closest but still no cigar:

import pymongo
client = pymongo.MongoClient('localhost')
database = client['test']
assert isinstance(database, pymongo.MongoClient.db_name)

which doesn't work since db_name isn't defined. Pymongo docs say a new database (reference?) is made using either

pymongo.MongoClient.db_name

or

pymongo.MongoClient[db_name]

Of course these create an instance if I give it a db_name string, yet I want only the class, not an instance.

Thanks!

labroid
  • 452
  • 4
  • 14

1 Answers1

2

All you need to do is check if it is instance of pymongo.database.Database

>>> import pymongo
>>> client = pymongo.MongoClient()
>>> db = client.test
>>> isinstance(db, pymongo.database.Database)
True
styvane
  • 59,869
  • 19
  • 150
  • 156
  • 1
    That worked perfectly, thanks! And thanks for the link at the top of your answer - I never saw that as I was going down the wrong rabbit hole... – labroid Feb 07 '15 at 14:17